2013-02-19 00:11:43 +00:00
|
|
|
import httplib
|
|
|
|
import HydrusConstants as HC
|
2013-10-02 22:06:06 +00:00
|
|
|
import HydrusServer
|
2013-03-15 02:38:12 +00:00
|
|
|
import HydrusSessions
|
2013-10-02 22:06:06 +00:00
|
|
|
import ServerConstants as SC
|
2013-02-19 00:11:43 +00:00
|
|
|
import ServerDB
|
|
|
|
import os
|
|
|
|
import random
|
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
import traceback
|
|
|
|
import wx
|
|
|
|
import yaml
|
2013-10-02 22:06:06 +00:00
|
|
|
from twisted.internet import reactor
|
2013-02-19 00:11:43 +00:00
|
|
|
|
|
|
|
class Controller( wx.App ):
|
|
|
|
|
|
|
|
def _AlreadyRunning( self, port ):
|
|
|
|
|
2013-09-25 20:20:10 +00:00
|
|
|
connection = httplib.HTTPConnection( '127.0.0.1', port, timeout = 20 )
|
2013-02-19 00:11:43 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
connection.connect()
|
|
|
|
connection.close()
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
except: return False
|
|
|
|
|
|
|
|
|
2013-10-02 22:06:06 +00:00
|
|
|
def _Read( self, action, *args, **kwargs ): return self._db.Read( action, HC.HIGH_PRIORITY, *args, **kwargs )
|
2013-03-15 02:38:12 +00:00
|
|
|
|
2013-10-02 22:06:06 +00:00
|
|
|
def _Write( self, action, priority, *args, **kwargs ): return self._db.Write( action, priority, *args, **kwargs )
|
|
|
|
|
|
|
|
def AddSession( self, service_identifier, account ): return self._session_manager.AddSession( service_identifier, account )
|
2013-02-19 00:11:43 +00:00
|
|
|
|
2013-10-02 22:06:06 +00:00
|
|
|
def GetAccount( self, session_key, service_identifier ): return self._session_manager.GetAccount( session_key, service_identifier )
|
2013-03-15 02:38:12 +00:00
|
|
|
|
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 OnInit( self ):
|
|
|
|
|
2013-07-10 20:25:57 +00:00
|
|
|
HC.app = self
|
|
|
|
|
2013-10-02 22:06:06 +00:00
|
|
|
try:
|
|
|
|
|
|
|
|
self._db = ServerDB.DB()
|
|
|
|
|
|
|
|
self.Bind( wx.EVT_MENU, self.EventExit, id=wx.ID_EXIT )
|
|
|
|
|
|
|
|
self.Bind( HC.EVT_PUBSUB, self.EventPubSub )
|
|
|
|
|
|
|
|
self._tbicon = TaskBarIcon()
|
|
|
|
|
|
|
|
self._session_manager = HydrusSessions.HydrusSessionManagerServer()
|
|
|
|
|
|
|
|
HC.pubsub.sub( self, 'RestartService', 'restart_service' )
|
|
|
|
|
|
|
|
self._services = {}
|
|
|
|
|
|
|
|
services_info = self.Read( 'services' )
|
|
|
|
|
|
|
|
for ( service_identifier, options ) in services_info: self.RestartService( service_identifier, options )
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2013-02-19 00:11:43 +00:00
|
|
|
except Exception as e:
|
|
|
|
|
2013-10-02 22:06:06 +00:00
|
|
|
print( traceback.format_exc() )
|
2013-02-19 00:11:43 +00:00
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2013-10-02 22:06:06 +00:00
|
|
|
|
|
|
|
def Read( self, action, *args, **kwargs ):
|
2013-03-15 02:38:12 +00:00
|
|
|
|
2013-10-02 22:06:06 +00:00
|
|
|
return self._Read( action, *args, **kwargs )
|
2013-02-19 00:11:43 +00:00
|
|
|
|
2013-10-02 22:06:06 +00:00
|
|
|
|
|
|
|
def RestartService( self, service_identifier, options ):
|
2013-02-19 00:11:43 +00:00
|
|
|
|
2013-10-02 22:06:06 +00:00
|
|
|
def TWISTEDRestartService():
|
|
|
|
|
|
|
|
def StartService():
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
port = options[ 'port' ]
|
|
|
|
|
|
|
|
connection = httplib.HTTPConnection( '127.0.0.1', port, timeout = 10 )
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
connection.connect()
|
|
|
|
connection.close()
|
|
|
|
|
|
|
|
raise Exception( 'Something was already bound to port ' + HC.u( port ) )
|
|
|
|
|
|
|
|
except:
|
|
|
|
|
|
|
|
message = options[ 'message' ]
|
|
|
|
|
|
|
|
service_type = service_identifier.GetType()
|
|
|
|
|
|
|
|
if service_type == HC.SERVER_ADMIN: service_object = HydrusServer.HydrusServiceAdmin( service_identifier, message )
|
|
|
|
elif service_type == HC.FILE_REPOSITORY: service_object = HydrusServer.HydrusServiceRepositoryFile( service_identifier, message )
|
|
|
|
elif service_type == HC.TAG_REPOSITORY: service_object = HydrusServer.HydrusServiceRepositoryTag( service_identifier, message )
|
|
|
|
elif service_type == HC.MESSAGE_DEPOT: return
|
|
|
|
|
|
|
|
self._services[ service_identifier ] = reactor.listenTCP( port, service_object )
|
|
|
|
|
|
|
|
connection = httplib.HTTPConnection( '127.0.0.1', port, timeout = 10 )
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
connection.connect()
|
|
|
|
connection.close()
|
|
|
|
|
|
|
|
except:
|
|
|
|
|
|
|
|
raise Exception( 'Tried to bind port ' + HC.u( port ) + ' but it failed.' )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
|
|
print( traceback.format_exc() )
|
|
|
|
|
|
|
|
|
|
|
|
if service_identifier not in self._services: StartService()
|
|
|
|
else:
|
|
|
|
|
|
|
|
deferred = self._services[ service_identifier ].stopListening()
|
|
|
|
|
|
|
|
deferred.AddCallback( StartService )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
reactor.callFromThread( TWISTEDRestartService )
|
|
|
|
|
|
|
|
|
|
|
|
def Write( self, action, *args, **kwargs ):
|
2013-02-19 00:11:43 +00:00
|
|
|
|
2013-10-02 22:06:06 +00:00
|
|
|
return self._Write( action, HC.HIGH_PRIORITY, *args, **kwargs )
|
2013-02-19 00:11:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|