hydrus/include/ServerController.py

116 lines
3.0 KiB
Python
Raw Normal View History

2013-02-19 00:11:43 +00:00
import httplib
import HydrusConstants as HC
2013-03-15 02:38:12 +00:00
import HydrusSessions
2013-02-19 00:11:43 +00:00
import ServerDB
import os
import random
import threading
import time
import traceback
import wx
import yaml
class Controller( wx.App ):
def _AlreadyRunning( self, port ):
2013-07-31 21:26:38 +00:00
connection = httplib.HTTPConnection( 'localhost:' + HC.u( port ) )
2013-02-19 00:11:43 +00:00
try:
connection.connect()
connection.close()
return True
except: return False
2013-03-15 02:38:12 +00:00
def AddSession( self, session_key, service_identifier, account_identifier, expiry ): self._session_manager.AddSession( session_key, service_identifier, account_identifier, expiry )
2013-02-19 00:11:43 +00:00
def ChangePort( self, port ):
new_server = self._server_callable( port )
server_daemon = threading.Thread( target=new_server.serve_forever )
server_daemon.setDaemon( True )
server_daemon.start()
2013-07-31 21:26:38 +00:00
connection = httplib.HTTPConnection( 'localhost:' + HC.u( port ) )
2013-02-19 00:11:43 +00:00
connection.connect()
connection.request( 'GET', '/' )
response = connection.getresponse()
data = response.read()
if response.status != 200: raise Exception( yaml.safe_load( data ) )
connection.close()
self._server.shutdown()
self._server = new_server
2013-03-15 02:38:12 +00:00
def GetAccountIdentifier( self, session_key, service_identifier ): return self._session_manager.GetAccountIdentifier( session_key, service_identifier )
2013-02-19 00:11:43 +00:00
def EventExit( self, event ): self._tbicon.Destroy()
def EventPubSub( self, event ):
pubsubs_queue = HC.pubsub.GetQueue()
( callable, args, kwargs ) = pubsubs_queue.get()
try: callable( *args, **kwargs )
except TypeError: pass
2013-08-14 20:21:49 +00:00
except Exception as e: HC.ShowException( e )
2013-02-19 00:11:43 +00:00
pubsubs_queue.task_done()
def GetDB( self ): return self._db
def OnInit( self ):
2013-07-10 20:25:57 +00:00
HC.app = self
2013-02-19 00:11:43 +00:00
try: self._db = ServerDB.DB()
except Exception as e:
2013-08-14 20:21:49 +00:00
HC.ShowException( e )
2013-02-19 00:11:43 +00:00
return False
2013-03-15 02:38:12 +00:00
self._session_manager = HydrusSessions.HydrusSessionManagerServer()
2013-02-19 00:11:43 +00:00
self.Bind( wx.EVT_MENU, self.EventExit, id=wx.ID_EXIT )
self.Bind( HC.EVT_PUBSUB, self.EventPubSub )
self._tbicon = TaskBarIcon()
return True
class TaskBarIcon( wx.TaskBarIcon ):
def __init__( self ):
wx.TaskBarIcon.__init__( self )
icon = wx.Icon( HC.STATIC_DIR + os.path.sep + 'hydrus.ico', wx.BITMAP_TYPE_ICO )
self.SetIcon( icon, 'hydrus server' )
self._tbmenu = wx.Menu()
self._tbmenu.Append( wx.ID_EXIT, 'exit' )
2013-09-04 16:48:44 +00:00
self.Bind( wx.EVT_TASKBAR_RIGHT_DOWN, lambda event: self.PopupMenu( self._tbmenu ) )
2013-02-19 00:11:43 +00:00