hydrus/include/ServerController.py

224 lines
7.7 KiB
Python
Raw Normal View History

2015-04-01 20:44:54 +00:00
import httplib
2013-02-19 00:11:43 +00:00
import HydrusConstants as HC
2015-04-01 20:44:54 +00:00
import HydrusController
2015-03-25 22:04:19 +00:00
import HydrusData
import HydrusGlobals
2013-10-02 22:06:06 +00:00
import HydrusServer
2013-03-15 02:38:12 +00:00
import HydrusSessions
2014-05-21 21:37:35 +00:00
import HydrusThreading
2015-03-04 22:44:32 +00:00
import ServerDaemons
2013-02-19 00:11:43 +00:00
import ServerDB
import os
import traceback
import wx
2013-10-02 22:06:06 +00:00
from twisted.internet import reactor
2013-10-23 21:36:47 +00:00
from twisted.internet import defer
2013-02-19 00:11:43 +00:00
2014-11-20 01:48:04 +00:00
ID_MAINTENANCE_EVENT_TIMER = wx.NewId()
MAINTENANCE_PERIOD = 5 * 60
2015-04-01 20:44:54 +00:00
class Controller( HydrusController.HydrusController ):
db_class = ServerDB.DB
2013-02-19 00:11:43 +00:00
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
2014-09-17 21:28:26 +00:00
def ActionService( self, service_key, action ):
2013-02-19 00:11:43 +00:00
2014-09-24 21:50:07 +00:00
if action != 'stop': ( service_type, options ) = self.Read( 'service_info', service_key )
2013-07-10 20:25:57 +00:00
2014-09-17 21:28:26 +00:00
def TWISTEDDoIt():
2013-10-02 22:06:06 +00:00
2013-10-23 21:36:47 +00:00
def StartService( *args, **kwargs ):
2013-10-02 22:06:06 +00:00
try:
2013-10-09 18:13:42 +00:00
if 'port' not in options: return
2013-10-02 22:06:06 +00:00
port = options[ 'port' ]
connection = httplib.HTTPConnection( '127.0.0.1', port, timeout = 10 )
try:
connection.connect()
connection.close()
2015-03-25 22:04:19 +00:00
raise Exception( 'Something was already bound to port ' + HydrusData.ToString( port ) )
2013-10-02 22:06:06 +00:00
except:
message = options[ 'message' ]
2014-08-27 22:15:22 +00:00
if service_type == HC.SERVER_ADMIN: service_object = HydrusServer.HydrusServiceAdmin( service_key, service_type, message )
elif service_type == HC.FILE_REPOSITORY: service_object = HydrusServer.HydrusServiceRepositoryFile( service_key, service_type, message )
elif service_type == HC.TAG_REPOSITORY: service_object = HydrusServer.HydrusServiceRepositoryTag( service_key, service_type, message )
2013-10-02 22:06:06 +00:00
elif service_type == HC.MESSAGE_DEPOT: return
2014-08-27 22:15:22 +00:00
self._services[ service_key ] = reactor.listenTCP( port, service_object )
2013-10-02 22:06:06 +00:00
connection = httplib.HTTPConnection( '127.0.0.1', port, timeout = 10 )
try:
connection.connect()
connection.close()
except:
2015-03-25 22:04:19 +00:00
raise Exception( 'Tried to bind port ' + HydrusData.ToString( port ) + ' but it failed.' )
2013-10-02 22:06:06 +00:00
except Exception as e:
print( traceback.format_exc() )
2013-10-09 18:13:42 +00:00
2013-10-02 22:06:06 +00:00
2014-09-17 21:28:26 +00:00
if action == 'start': StartService()
2013-10-02 22:06:06 +00:00
else:
2014-08-27 22:15:22 +00:00
deferred = defer.maybeDeferred( self._services[ service_key ].stopListening )
2013-10-02 22:06:06 +00:00
2014-09-17 21:28:26 +00:00
if action == 'restart': deferred.addCallback( StartService )
2014-09-24 21:50:07 +00:00
elif action == 'stop': del self._services[ service_key ]
2013-10-02 22:06:06 +00:00
2013-10-09 18:13:42 +00:00
2013-10-02 22:06:06 +00:00
2014-09-17 21:28:26 +00:00
reactor.callFromThread( TWISTEDDoIt )
2015-04-01 20:44:54 +00:00
def EventExit( self, event ):
wx.CallAfter( self._tbicon.Destroy )
self.ShutdownDB()
2014-09-17 21:28:26 +00:00
def GetManager( self, manager_type ): return self._managers[ manager_type ]
2014-10-29 21:39:01 +00:00
def JustWokeFromSleep( self ): return False
2015-04-01 20:44:54 +00:00
def MaintainDB( self ):
pass
2014-09-17 21:28:26 +00:00
def OnInit( self ):
try:
2015-04-01 20:44:54 +00:00
HydrusController.HydrusController.OnInit( self )
2014-09-17 21:28:26 +00:00
2015-04-01 20:44:54 +00:00
self.InitDB()
2014-11-20 01:48:04 +00:00
2014-09-17 21:28:26 +00:00
self.Bind( wx.EVT_MENU, self.EventExit, id=wx.ID_EXIT )
self._managers[ 'restricted_services_sessions' ] = HydrusSessions.HydrusSessionManagerServer()
self._managers[ 'messaging_sessions' ] = HydrusSessions.HydrusMessagingSessionManagerServer()
2015-03-25 22:04:19 +00:00
HydrusGlobals.pubsub.sub( self, 'ActionService', 'action_service' )
2014-09-17 21:28:26 +00:00
self._services = {}
#
( service_type, options ) = self.Read( 'service_info', HC.SERVER_ADMIN_KEY )
port = options[ 'port' ]
connection = httplib.HTTPConnection( '127.0.0.1', port, timeout = 10 )
try:
connection.connect()
connection.close()
2015-03-25 22:04:19 +00:00
message = 'Something was already bound to port ' + HydrusData.ToString( port )
2014-09-17 21:28:26 +00:00
wx.MessageBox( message )
return False
except: pass
#
service_keys = self.Read( 'service_keys' )
for service_key in service_keys: self.ActionService( service_key, 'start' )
self.StartDaemons()
2014-11-12 23:33:13 +00:00
if HC.PLATFORM_WINDOWS: self._tbicon = TaskBarIcon()
else:
stay_open_frame = wx.Frame( None, title = 'Hydrus Server' )
stay_open_frame.SetBackgroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_BTNFACE ) )
stay_open_frame.SetIcon( wx.Icon( HC.STATIC_DIR + os.path.sep + 'hydrus.ico', wx.BITMAP_TYPE_ICO ) )
wx.StaticText( stay_open_frame, label = 'The hydrus server is now running.' + os.linesep * 2 + 'Close this window to stop it.' )
( x, y ) = stay_open_frame.GetEffectiveMinSize()
stay_open_frame.SetInitialSize( ( x, y ) )
stay_open_frame.Show()
2014-09-17 21:28:26 +00:00
return True
except Exception as e:
print( traceback.format_exc() )
return False
2013-10-09 18:13:42 +00:00
def StartDaemons( self ):
2015-03-04 22:44:32 +00:00
HydrusThreading.DAEMONQueue( 'FlushRequestsMade', ServerDaemons.DAEMONFlushRequestsMade, 'request_made', period = 60 )
2013-10-09 18:13:42 +00:00
2015-03-04 22:44:32 +00:00
HydrusThreading.DAEMONWorker( 'CheckMonthlyData', ServerDaemons.DAEMONCheckMonthlyData, period = 3600 )
HydrusThreading.DAEMONWorker( 'ClearBans', ServerDaemons.DAEMONClearBans, period = 3600 )
HydrusThreading.DAEMONWorker( 'DeleteOrphans', ServerDaemons.DAEMONDeleteOrphans, period = 86400 )
HydrusThreading.DAEMONWorker( 'GenerateUpdates', ServerDaemons.DAEMONGenerateUpdates, period = 1200 )
HydrusThreading.DAEMONWorker( 'CheckDataUsage', ServerDaemons.DAEMONCheckDataUsage, period = 86400 )
HydrusThreading.DAEMONWorker( 'UPnP', ServerDaemons.DAEMONUPnP, ( 'notify_new_options', ), period = 43200 )
2013-10-09 18:13:42 +00:00
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