def lineReceived(self, line): """ Called when a line is recieved from the client. Must end with '\n'. :param line: The data recieved. """ self.debug("Data: %s" % line.rstrip("\n")) try: data = json.loads(line) except ValueError: self.warn("Unable to parse JSON: %s" % line.rstrip("\n")) data = json.dumps({"error": "No JSON object could be decoded", "from": "core"}) self.send(data) except Exception as e: util.output_exception(self) # Never do this, by the way. Hackish formatting = baaaaaaaad. data = json.dumps({"error": str(e), "from": "core"}) self.sendLine(data) else: self.debug("Parsed data: %s" % data) if "pong" in data: pong = data["pong"] self.debug("Received pong: %s" % pong) self.pings.remove(pong) event = pongReceivedEvent(self, pong) self.events.runCallback("pingSent", event) return event = dataReceivedEvent(self, data) self.events.runCallback("dataReceived", event)
def __init__(self): self.servers = {} self.clients = [] self.logger = logging.getLogger("Factory") self.plugman = PluginManagerSingleton.get() self.events = manager.manager() self.plugman.setPluginPlaces(["plugins"]) self.plugman.setPluginInfoExtension("plug") self.plugman.collectPlugins() self.logger.info("Loading plugins..") for pluginInfo in self.plugman.getAllPlugins(): try: self.plugman.activatePluginByName(pluginInfo.name) pluginInfo.plugin_object._add_variables(pluginInfo, self) pluginInfo.plugin_object.setup() except Exception: self.logger.warn("Unable to load plugin: %s v%s" % (pluginInfo.name, pluginInfo.version)) util.output_exception(self.logger, logging.WARN) self.plugman.deactivatePluginByName(pluginInfo.name) else: self.logger.info("Loaded plugin: %s v%s" % (pluginInfo.name, pluginInfo.version)) event = pluginLoadedEvent(self, pluginInfo) self.events.runCallback("pluginLoaded", event) self.logger.info("Finished loading plugins.") event = pluginsLoadedEvent(self) self.events.runCallback("pluginsLoaded", event)
def _load_file(self, filename): try: fh = open(filename, "r") data = fh.read() fh.close() return yaml.load(data) except Exception: util.output_exception(self.logger) return None
def _save_file(self, filename, dictionary): try: fh = open(filename, "w") data = yaml.dump(dictionary, default_flow_style=False) fh.write(data) fh.flush() fh.close() return True except Exception: util.output_exception(self.logger) return False
def runCallback(self, callback, event): if self.hasCallback(callback): for cb in self.getCallbacks(callback): try: self.logger.debug("Running callback: %s" % cb) if event.cancelled: if cb["cancelled"]: cb["function"](event) else: self.logger.debug("Not running, event is cancelled and handler doesn't accept cancelled " + "events") else: cb["function"](event) except Exception as e: self.logger.warn("Error running callback '%s': %s" % (callback, e)) util.output_exception(self.logger, logging.WARN)
def cleanup(self): """ Cleans up plugins and the like after the program has been asked to terminate. """ self.logger.info("Shutting down..") self.logger.info("Dropping clients..") for client in self.clients: client.send(json.dumps({"error": "Server is shutting down", "from": "core"})) client.transport.loseConnection() del client self.logger.info("Disabling plugins..") for pluginInfo in self.plugman.getAllPlugins(): try: self.plugman.deactivatePluginByName(pluginInfo.name) except Exception: self.logger.warn("Error disabling plugin: %s v%s" % (pluginInfo.name, pluginInfo.version)) util.output_exception(self.logger, logging.WARN) else: self.logger.info("Disabled plugin: %s v%s" % (pluginInfo.name, pluginInfo.version)) self.logger.info("Finished shutting down.")
logging.basicConfig(format="%(asctime)s | %(name)8s | %(levelname)8s | %(message)s", datefmt="%d %b %Y - %H:%M:%S", level=(logging.DEBUG if "--debug" in sys.argv else logging.INFO)) logger = logging.getLogger("Init") logger.info("Starting up...") conf = config.conf() networking = conf.get("networking") if not networking: logger.critical("Networking configuration is unavailable. The program will now terminate.") exit(1) factory = CoreFactory() try: reactor.listenTCP(networking["port"], factory) logger.info("Now listening on port %s." % networking["port"]) reactor.run() except Exception as e: logger.error("Error starting up: %s" % e) util.output_exception(logger) finally: try: logger.info("Shutting down..") factory.cleanup() except: pass