comparison src/server/server.py @ 470:34ce41e014c4

server side: options managing improvments: - cleaned a bit the code / comments - i18n - added Libervia.OPT_PARAMETERS_CFG and moved Libervia.OPT_PARAMETERS to Libervia.OPT_PARAMETERS_BOTH, to allow options to be only in sat.conf - use of new constant ASCII_APP_NAME for tap name
author Goffi <goffi@goffi.org>
date Tue, 10 Jun 2014 15:38:47 +0200
parents 33ec27ef4b6a
children de039f008333
comparison
equal deleted inserted replaced
469:305044acb6f0 470:34ce41e014c4
109 def __init__(self): 109 def __init__(self):
110 self.waiting_ids = {} 110 self.waiting_ids = {}
111 111
112 def waitForId(self, callback, action_id, profile, *args, **kwargs): 112 def waitForId(self, callback, action_id, profile, *args, **kwargs):
113 """Wait for an action result 113 """Wait for an action result
114
114 @param callback: method to call when action gave a result back 115 @param callback: method to call when action gave a result back
115 @param action_id: action_id to wait for 116 @param action_id: action_id to wait for
116 @param profile: %(doc_profile)s 117 @param profile: %(doc_profile)s
117 @param *args: additional argument to pass to callback 118 @param *args: additional argument to pass to callback
118 @param **kwargs: idem""" 119 @param **kwargs: idem
120 """
119 action_tuple = (action_id, profile) 121 action_tuple = (action_id, profile)
120 self.waiting_ids[action_tuple] = (callback, args, kwargs) 122 self.waiting_ids[action_tuple] = (callback, args, kwargs)
121 reactor.callLater(self.ID_LIFETIME, self.purgeID, action_tuple) 123 reactor.callLater(self.ID_LIFETIME, self.purgeID, action_tuple)
122 124
123 def purgeID(self, action_tuple): 125 def purgeID(self, action_tuple):
1001 1003
1002 1004
1003 class Libervia(service.Service): 1005 class Libervia(service.Service):
1004 1006
1005 DATA_DIR_DEFAULT = '' 1007 DATA_DIR_DEFAULT = ''
1006 OPT_PARAMETERS = [['connection_type', 't', 'https', "'http', 'https' or 'both' (to launch both servers).", coerceConnectionType], 1008 OPT_PARAMETERS_BOTH = [['connection_type', 't', 'https', _(u"'http', 'https' or 'both' (to launch both servers)."), coerceConnectionType],
1007 ['port', 'p', 8080, 'The port number to listen HTTP on.', int], 1009 ['port', 'p', 8080, _(u'The port number to listen HTTP on.'), int],
1008 ['port_https', 's', 8443, 'The port number to listen HTTPS on.', int], 1010 ['port_https', 's', 8443, _(u'The port number to listen HTTPS on.'), int],
1009 ['port_https_ext', 'e', 0, 'The external port number used for HTTPS (0 means port_https value).', int], 1011 ['port_https_ext', 'e', 0, _(u'The external port number used for HTTPS (0 means port_https value).'), int],
1010 ['ssl_certificate', 'c', 'libervia.pem', 'PEM certificate with both private and public parts.', str], 1012 ['ssl_certificate', 'c', 'libervia.pem', _(u'PEM certificate with both private and public parts.'), str],
1011 ['redirect_to_https', 'r', 1, 'Automatically redirect from HTTP to HTTPS.', int], 1013 ['redirect_to_https', 'r', 1, _(u'Automatically redirect from HTTP to HTTPS.'), int],
1012 ['security_warning', 'w', 1, 'Warn user that he is about to connect on HTTP.', int], 1014 ['security_warning', 'w', 1, _(u'Warn user that he is about to connect on HTTP.'), int],
1013 # FIXME: twistd bugs when printing 'à' on "Unknown command" error (works on normal command listing) 1015 ['passphrase', 'k', '', _(u"Passphrase for the SàT profile named '%s'") % C.SERVICE_PROFILE, str],
1014 ['passphrase', 'k', '', u"Passphrase for the SaT profile named '%s'" % C.SERVICE_PROFILE, str], 1016 ['data_dir', 'd', DATA_DIR_DEFAULT, _(u'Data directory for Libervia'), coerceDataDir],
1015 ['data_dir', 'd', DATA_DIR_DEFAULT, u'Data directory for Libervia', coerceDataDir], 1017 ] # options which are in sat.conf and on command line, see https://twistedmatrix.com/documents/current/api/twisted.python.usage.Options.html
1016 ] 1018 OPT_PARAMETERS_CFG = [] # Options which are in sat.conf only
1017 1019
1018 def __init__(self, *args, **kwargs): 1020 def __init__(self, *args, **kwargs):
1019 if not kwargs:
1020 # During the loading of the twisted plugins, we just need the default values.
1021 # This part is not executed when the plugin is actually started.
1022 for name, value in [(option[0], option[2]) for option in self.OPT_PARAMETERS]:
1023 kwargs[name] = value
1024 self.initialised = defer.Deferred() 1021 self.initialised = defer.Deferred()
1025 self.connection_type = kwargs['connection_type'] 1022
1026 self.port = kwargs['port'] 1023 # options managing
1027 self.port_https = kwargs['port_https'] 1024 for opt in self.OPT_PARAMETERS_BOTH + self.OPT_PARAMETERS_CFG:
1028 self.port_https_ext = kwargs['port_https_ext'] 1025 opt_name = opt[0]
1026 setattr(self, opt_name, kwargs.get(opt_name, opt[2]))
1029 if not self.port_https_ext: 1027 if not self.port_https_ext:
1030 self.port_https_ext = self.port_https 1028 self.port_https_ext = self.port_https
1031 self.ssl_certificate = kwargs['ssl_certificate']
1032 self.redirect_to_https = kwargs['redirect_to_https']
1033 self.security_warning = kwargs['security_warning']
1034 self.passphrase = kwargs['passphrase']
1035 self.data_dir = kwargs['data_dir']
1036 if self.data_dir == Libervia.DATA_DIR_DEFAULT: 1029 if self.data_dir == Libervia.DATA_DIR_DEFAULT:
1037 coerceDataDir(self.data_dir) # this is not done when using the default value 1030 coerceDataDir(self.data_dir) # this is not done when using the default value
1031
1038 self.html_dir = os.path.join(self.data_dir, C.HTML_DIR) 1032 self.html_dir = os.path.join(self.data_dir, C.HTML_DIR)
1039 self.server_css_dir = os.path.join(self.data_dir, C.SERVER_CSS_DIR) 1033 self.server_css_dir = os.path.join(self.data_dir, C.SERVER_CSS_DIR)
1034
1040 self._cleanup = [] 1035 self._cleanup = []
1036
1041 root = ProtectedFile(self.html_dir) 1037 root = ProtectedFile(self.html_dir)
1038
1042 self.signal_handler = SignalHandler(self) 1039 self.signal_handler = SignalHandler(self)
1043 _register = Register(self) 1040 _register = Register(self)
1044 _upload_radiocol = UploadManagerRadioCol(self) 1041 _upload_radiocol = UploadManagerRadioCol(self)
1045 _upload_avatar = UploadManagerAvatar(self) 1042 _upload_avatar = UploadManagerAvatar(self)
1046 self.signal_handler.plugRegister(_register) 1043 self.signal_handler.plugRegister(_register)
1047 self.sessions = {} # key = session value = user 1044 self.sessions = {} # key = session value = user
1048 self.prof_connected = set() # Profiles connected 1045 self.prof_connected = set() # Profiles connected
1049 self.action_handler = SATActionIDHandler() 1046 self.action_handler = SATActionIDHandler()
1047
1050 ## bridge ## 1048 ## bridge ##
1051 try: 1049 try:
1052 self.bridge = DBusBridgeFrontend() 1050 self.bridge = DBusBridgeFrontend()
1053 except BridgeExceptionNoService: 1051 except BridgeExceptionNoService:
1054 print(u"Can't connect to SàT backend, are you sure it's launched ?") 1052 print(u"Can't connect to SàT backend, are you sure it's launched ?")