def Start(db, port=0, is_master=False, server_cls=ThreadedHTTPServer, reqhandler_cls=DataServerHandler): """Start the data server.""" # This is the service that will handle requests to the data store. global SERVICE SERVICE = store.DataStoreService(db) # Create the command table for faster execution of remote calls. # Along with a method, each command has the required permissions. global CMDTABLE cmd = rdfvalue.DataStoreCommand.Command CMDTABLE = { cmd.DELETE_ATTRIBUTES: (SERVICE.DeleteAttributes, "w"), cmd.DELETE_SUBJECT: (SERVICE.DeleteSubject, "w"), cmd.DELETE_ATTRIBUTES_REGEX: (SERVICE.DeleteAttributesRegex, "w"), cmd.MULTI_SET: (SERVICE.MultiSet, "w"), cmd.MULTI_RESOLVE_REGEX: (SERVICE.MultiResolveRegex, "r"), cmd.RESOLVE_MULTI: (SERVICE.ResolveMulti, "r"), cmd.LOCK_SUBJECT: (SERVICE.LockSubject, "w"), cmd.EXTEND_SUBJECT: (SERVICE.ExtendSubject, "w"), cmd.UNLOCK_SUBJECT: (SERVICE.UnlockSubject, "w") } # Initialize nonce store for authentication. global NONCE_STORE NONCE_STORE = auth.NonceStore() server_port = port or config_lib.CONFIG["Dataserver.port"] if is_master: InitMasterServer(server_port) else: InitDataServer(server_port) try: server = server_cls(("", server_port), reqhandler_cls) server.serve_forever() except KeyboardInterrupt: print("Caught keyboard interrupt, stopping server at port %s" % server_port) except socket.error: print "Service already running at port %s" % server_port finally: if MASTER: MASTER.Stop() else: DATA_SERVER.Stop()
def Start(db, port=0, address_family=socket.AF_INET, is_master=False, server_cls=ThreadedHTTPServer, reqhandler_cls=DataServerHandler): """Start the data server.""" # This is the service that will handle requests to the data store. if reqhandler_cls.MASTER or reqhandler_cls.DATA_SERVER: logging.fatal("Attempt to start server with duplicate request handler.") if not reqhandler_cls.SERVICE: reqhandler_cls.SERVICE = store.DataStoreService(db) # Create the command table for faster execution of remote calls. # Along with a method, each command has the required permissions. cmd = rdf_data_server.DataStoreCommand.Command reqhandler_cls.CMDTABLE = { cmd.DELETE_ATTRIBUTES: (reqhandler_cls.SERVICE.DeleteAttributes, "w"), cmd.DELETE_SUBJECT: (reqhandler_cls.SERVICE.DeleteSubject, "w"), cmd.MULTI_SET: (reqhandler_cls.SERVICE.MultiSet, "w"), cmd.MULTI_RESOLVE_PREFIX: (reqhandler_cls.SERVICE.MultiResolvePrefix, "r"), cmd.RESOLVE_MULTI: (reqhandler_cls.SERVICE.ResolveMulti, "r"), cmd.LOCK_SUBJECT: (reqhandler_cls.SERVICE.LockSubject, "w"), cmd.EXTEND_SUBJECT: (reqhandler_cls.SERVICE.ExtendSubject, "w"), cmd.UNLOCK_SUBJECT: (reqhandler_cls.SERVICE.UnlockSubject, "w"), cmd.SCAN_ATTRIBUTES: (reqhandler_cls.SERVICE.ScanAttributes, "r") } # Initialize nonce store for authentication. if not reqhandler_cls.NONCE_STORE: reqhandler_cls.NONCE_STORE = auth.NonceStore() if port == 0 or port is None: logging.debug("No port was specified as a parameter. Expecting to find " "port in configuration file.") else: logging.debug("Port specified was '%i'. Ignoring configuration directive " "Dataserver.port.", port) server_port = port or config_lib.CONFIG["Dataserver.port"] if is_master: logging.debug("Master server running on port '%i'", server_port) reqhandler_cls.InitMasterServer(server_port) else: logging.debug("Non-master data server running on port '%i'", server_port) reqhandler_cls.InitDataServer(server_port) reqhandler_cls.InitHandlerTables() logging.info("Starting! master: " + str(is_master) + " with handler " + reqhandler_cls.__name__) try: server_cls.address_family = address_family server = server_cls(("", server_port), reqhandler_cls) server.serve_forever() except KeyboardInterrupt: print "Caught keyboard interrupt, stopping server at port %s" % server_port except socket.error: print "Service already running at port %s" % server_port finally: if reqhandler_cls.MASTER: reqhandler_cls.MASTER.Stop() else: reqhandler_cls.DATA_SERVER.Stop()