def _setup_proxy_handler(self): from mpx.service.network.http.handlers.proxy import PROXYHandler self.proxy_handler = PROXYHandler() self.proxy_handler.configure({'name':'PROXY Handler','parent':self}) return self.proxy_handler
def _setup_proxy_handler(self): from mpx.service.network.http.handlers.proxy import PROXYHandler self.proxy_handler = PROXYHandler() self.proxy_handler.configure({'name': 'PROXY Handler', 'parent': self}) return self.proxy_handler
class Server(ServiceNode): _thread = None _servers = {} servers = [] # "Overridable" references, use self to lookup these references so # derived classes can override them. server_class = _http.HTTPServer server_type = 'HTTP' def __new__(klass, *args, **kw): server = super(Server, klass).__new__(klass, *args, **kw) klass.servers.append(server) return server def __init__(self): ServiceNode.__init__(self) self._added_thread_count = 0 self._file_request_handler = None self.psp_handler = None self.authenticator = None self.request_responder = None self.request_manager = _request.RequestSingleton().request_manager def configure(self, config): set_attribute(self, 'debug', 0, config, int) set_attribute(self, 'ip', '', config) default = '${mpx.properties.%s_PORT}' % self.server_type set_attribute(self,'port',default,config,int) set_attribute(self,'user_manager', as_node('/services/User Manager'),config,as_node) set_attribute(self,'authentication','form',config) if (self.authentication is not None and self.authentication not in ('digest','basic','form')): raise EInvalidValue('authentication',self.authentication, 'Authentication scheme not recognized.') set_attribute(self,'port',default,config,int) set_attribute(self, 'maintenance_interval', 25, config, int) set_attribute(self, 'zombie_timeout', 600, config, int) set_attribute(self, 'thread_count', 3, config, int) if not self.has_child("Request Responder"): self._setup_request_responder() if not self.has_child("Authenticator"): self._setup_authentication_handler() if not self.has_child("PSP Handler"): self._setup_psp_handler() if not self.has_child("JSON-RPC Handler"): self._setup_json_handler() ServiceNode.configure(self, config) def configuration(self): config = ServiceNode.configuration(self) get_attribute(self, 'ip', config) get_attribute(self, 'port', config, str) get_attribute(self, 'user_manager', config, as_node_url) get_attribute(self, 'authentication', config, str) get_attribute(self, 'debug', config, str) get_attribute(self, 'maintenance_interval', config, str) get_attribute(self, 'zombie_timeout', config, str) get_attribute(self, 'thread_count', config, str) return config def _add_child(self, child): if self.has_child(child.name): existing = self.get_child(child.name) if isinstance(existing, HandlerAlias): msglog.log("broadway", msglog.types.WARN, "Removing %s before adding %s" % (existing, child)) existing.prune() result = super(Server, self)._add_child(child) if self.is_running(): self._setup_handler(child) return result def _setup_handler(self, handler): if hasattr(handler, "handle_request"): self._setup_request_handler(handler) elif hasattr(handler, "handle_response"): self._setup_response_handler(handler) else: msglog.log("broadway", msglog.types.WARN, "Handler type uknown: %s" % handler) def _setup_request_handler(self, handler): if not isinstance(handler, HandlerAlias): for server in self.servers: if server is not self: if (not server.has_child(handler.name) and not server.handles_path(getpath(handler))): message = "%s aliasing %r handler from %s." msglog.log("broadway", msglog.types.INFO, message % (server, getpath(handler), self)) alias = server.create_alias(handler) if server.is_running(): server.server.install_request_handler(handler) if isinstance(handler, FileRequestHandler): self._file_request_handler = handler handler.setup_filesystem() back = True else: back = False self.server.install_request_handler(handler, back) def _setup_response_handler(self, handler): self.server.install_response_handler(handler) def request_handlers(self): return [child for child in self.children_nodes() if hasattr(child, "handle_request")] def response_handlers(self): return [child for child in self.children_nodes() if hasattr(child, "handle_response")] def handles_path(self, path): handlers = self.request_handlers() return any(getpath(handler) == path for handler in handlers) def _setup_server(self): self._added_thread_count = self.thread_count self.request_manager.add_threads(self._added_thread_count) self.server = self.server_class(self.ip, self.port, self.user_manager,self.name, self.authentication, self.maintenance_interval, self.zombie_timeout, self.debug) for handler in self.children_nodes(): self._setup_handler(handler) def _setup_psp_handler(self): from mpx.service.network.http.psp_handler import factory self.psp_handler = factory() self.psp_handler.configure({'name':'PSP Handler','parent':self}) return self.psp_handler def _setup_authentication_handler(self): self.authenticator = Authenticator() self.authenticator.configure({'name':'Authenticator','parent':self}) return self.authenticator def _setup_json_handler(self): from mpx.service.network.http.handlers.jsonrpc import JSONRPCHandler self.json_handler = JSONRPCHandler() self.json_handler.configure({'name':'JSON-RPC Handler','parent':self}) return self.json_handler def _setup_proxy_handler(self): from mpx.service.network.http.handlers.proxy import PROXYHandler self.proxy_handler = PROXYHandler() self.proxy_handler.configure({'name':'PROXY Handler','parent':self}) return self.proxy_handler def _setup_request_responder(self): self.request_responder = RequestResponder() self.request_responder.configure( {"name": "Request Responder", "parent": self}) return self.request_responder def _teardown_server(self): self.request_manager.remove_threads(self._added_thread_count) self._file_request_handler = None self._added_thread_count = 0 self.server.close() def send_to_file_handler(self,request): deprecated('Use request.send_to_handler(skip=self) instead') return self._file_request_handler.handle_request(request) def start(self): if not Server._servers.has_key(self.port): Server._servers[self.port] = self self._setup_server() if Server._thread is None: Server._thread = _RedusaThread() Server._thread.start() ServiceNode.start(self) def create_alias(self, handler): if self.debug: msglog.log("broadway", msglog.types.DB, "Aliasing %s within %s." % (handler, self)) alias = HandlerAlias() config = {"parent": self, "name": handler.name, "node": handler} alias.configure(config) return alias def stop(self): if Server._servers.has_key(self.port): del Server._servers[self.port] self._teardown_server() return ServiceNode.stop(self) def open_resource(self, path, mode="r"): """ Returns open file-handle based on URL path. Allows callers to get file handles based on URLs instead of file-system paths. """ return self._file_request_handler._filesystem.open(path, mode) def read_resource(self, path): resource = self.open_resource(path, "r") content = resource.read() resource.close() return content def write_resource(self, path, data): resource = self.open_resource(path, "w") response = resource.write(data) resource.close() return response def append_resource(self, path, data): resource = self.open_resource(path, "a") response = resource.write(data) resource.close() return response
class Server(ServiceNode): _thread = None _servers = {} servers = [] # "Overridable" references, use self to lookup these references so # derived classes can override them. server_class = _http.HTTPServer server_type = 'HTTP' def __new__(klass, *args, **kw): server = super(Server, klass).__new__(klass, *args, **kw) klass.servers.append(server) return server def __init__(self): ServiceNode.__init__(self) self._added_thread_count = 0 self._file_request_handler = None self.psp_handler = None self.authenticator = None self.request_responder = None self.request_manager = _request.RequestSingleton().request_manager def configure(self, config): set_attribute(self, 'debug', 0, config, int) set_attribute(self, 'ip', '', config) default = '${mpx.properties.%s_PORT}' % self.server_type set_attribute(self, 'port', default, config, int) set_attribute(self, 'user_manager', as_node('/services/User Manager'), config, as_node) set_attribute(self, 'authentication', 'form', config) if (self.authentication is not None and self.authentication not in ('digest', 'basic', 'form')): raise EInvalidValue('authentication', self.authentication, 'Authentication scheme not recognized.') set_attribute(self, 'port', default, config, int) set_attribute(self, 'maintenance_interval', 25, config, int) set_attribute(self, 'zombie_timeout', 600, config, int) set_attribute(self, 'thread_count', 3, config, int) if not self.has_child("Request Responder"): self._setup_request_responder() if not self.has_child("Authenticator"): self._setup_authentication_handler() if not self.has_child("PSP Handler"): self._setup_psp_handler() if not self.has_child("JSON-RPC Handler"): self._setup_json_handler() ServiceNode.configure(self, config) def configuration(self): config = ServiceNode.configuration(self) get_attribute(self, 'ip', config) get_attribute(self, 'port', config, str) get_attribute(self, 'user_manager', config, as_node_url) get_attribute(self, 'authentication', config, str) get_attribute(self, 'debug', config, str) get_attribute(self, 'maintenance_interval', config, str) get_attribute(self, 'zombie_timeout', config, str) get_attribute(self, 'thread_count', config, str) return config def _add_child(self, child): if self.has_child(child.name): existing = self.get_child(child.name) if isinstance(existing, HandlerAlias): msglog.log("broadway", msglog.types.WARN, "Removing %s before adding %s" % (existing, child)) existing.prune() result = super(Server, self)._add_child(child) if self.is_running(): self._setup_handler(child) return result def _setup_handler(self, handler): if hasattr(handler, "handle_request"): self._setup_request_handler(handler) elif hasattr(handler, "handle_response"): self._setup_response_handler(handler) else: msglog.log("broadway", msglog.types.WARN, "Handler type uknown: %s" % handler) def _setup_request_handler(self, handler): if not isinstance(handler, HandlerAlias): for server in self.servers: if server is not self: if (not server.has_child(handler.name) and not server.handles_path(getpath(handler))): message = "%s aliasing %r handler from %s." msglog.log("broadway", msglog.types.INFO, message % (server, getpath(handler), self)) alias = server.create_alias(handler) if server.is_running(): server.server.install_request_handler(handler) if isinstance(handler, FileRequestHandler): self._file_request_handler = handler handler.setup_filesystem() back = True else: back = False self.server.install_request_handler(handler, back) def _setup_response_handler(self, handler): self.server.install_response_handler(handler) def request_handlers(self): return [ child for child in self.children_nodes() if hasattr(child, "handle_request") ] def response_handlers(self): return [ child for child in self.children_nodes() if hasattr(child, "handle_response") ] def handles_path(self, path): handlers = self.request_handlers() return any(getpath(handler) == path for handler in handlers) def _setup_server(self): self._added_thread_count = self.thread_count self.request_manager.add_threads(self._added_thread_count) self.server = self.server_class(self.ip, self.port, self.user_manager, self.name, self.authentication, self.maintenance_interval, self.zombie_timeout, self.debug) for handler in self.children_nodes(): self._setup_handler(handler) def _setup_psp_handler(self): from mpx.service.network.http.psp_handler import factory self.psp_handler = factory() self.psp_handler.configure({'name': 'PSP Handler', 'parent': self}) return self.psp_handler def _setup_authentication_handler(self): self.authenticator = Authenticator() self.authenticator.configure({'name': 'Authenticator', 'parent': self}) return self.authenticator def _setup_json_handler(self): from mpx.service.network.http.handlers.jsonrpc import JSONRPCHandler self.json_handler = JSONRPCHandler() self.json_handler.configure({ 'name': 'JSON-RPC Handler', 'parent': self }) return self.json_handler def _setup_proxy_handler(self): from mpx.service.network.http.handlers.proxy import PROXYHandler self.proxy_handler = PROXYHandler() self.proxy_handler.configure({'name': 'PROXY Handler', 'parent': self}) return self.proxy_handler def _setup_request_responder(self): self.request_responder = RequestResponder() self.request_responder.configure({ "name": "Request Responder", "parent": self }) return self.request_responder def _teardown_server(self): self.request_manager.remove_threads(self._added_thread_count) self._file_request_handler = None self._added_thread_count = 0 self.server.close() def send_to_file_handler(self, request): deprecated('Use request.send_to_handler(skip=self) instead') return self._file_request_handler.handle_request(request) def start(self): if not Server._servers.has_key(self.port): Server._servers[self.port] = self self._setup_server() if Server._thread is None: Server._thread = _RedusaThread() Server._thread.start() ServiceNode.start(self) def create_alias(self, handler): if self.debug: msglog.log("broadway", msglog.types.DB, "Aliasing %s within %s." % (handler, self)) alias = HandlerAlias() config = {"parent": self, "name": handler.name, "node": handler} alias.configure(config) return alias def stop(self): if Server._servers.has_key(self.port): del Server._servers[self.port] self._teardown_server() return ServiceNode.stop(self) def open_resource(self, path, mode="r"): """ Returns open file-handle based on URL path. Allows callers to get file handles based on URLs instead of file-system paths. """ return self._file_request_handler._filesystem.open(path, mode) def read_resource(self, path): resource = self.open_resource(path, "r") content = resource.read() resource.close() return content def write_resource(self, path, data): resource = self.open_resource(path, "w") response = resource.write(data) resource.close() return response def append_resource(self, path, data): resource = self.open_resource(path, "a") response = resource.write(data) resource.close() return response