def run_loop(self): # Basically set up a client connection here, complete with normal # sockets. self.socket_client = self.context.socket(zmq.DEALER) self.socket_client.setsockopt(zmq.IDENTITY, self.identity) # Hang around for a tiny bit before closing, just to clear out self.socket_client.setsockopt(zmq.LINGER, 100) self.socket_client.connect("inproc://ws-queue") # TODO We should be able to do this in a poll loop, but I can't figure # out how to get a pollable fileno from gevent-websocket. The internal # socket object just blocks the zmq poller even if set non-block. So, # for the moment, we're stuck with using 2 more greenlets. Sadness. sl = greenlet.spawn_gevent_func("websocket send %s" % self.identity, "websockets", WebSocketClient.send_loop, self) rl = greenlet.spawn_gevent_func("websocket receive %s" % self.identity, "websockets", WebSocketClient.recv_loop, self) try: while self.alive: gevent.sleep(1) except: self.alive = False self.socket_client.send(msgpack.packb(["s", "BPClose"])) sl.join() rl.join() logging.info("Shutting down Websocket %s", self.identity)
def scan_for_plugins(): """Look through plugin directory for any directory with a file named "bpplugin.json". Fill in an plugin object, and pass to _run_count_plugin to handle lifetime. """ for i in os.listdir(config.get_dir("plugin")): plugin_file = os.path.join(config.get_dir("plugin"), i, Plugin.PLUGIN_INFO_FILE) if not os.path.exists(plugin_file): continue info = None try: with open(plugin_file) as pfile: info = json.load(pfile) except ValueError: logging.warning("JSON configuration not valid for plugin %s!", i) continue if not set(Plugin.PLUGIN_REQUIRED_KEYS).issubset(set(info.keys())): raise PluginException("Invalid Plugin") if info["name"] in _plugins.keys(): raise PluginException("Plugin Collision! Two plugins named " + info["name"]) greenlet.spawn_gevent_func("run_count_plugin: %s" % info["name"], "plugin", _run_count_plugin, Plugin(info, i))
def scan_for_plugins(): """Look through plugin directory for any directory with a file named "bpplugin.json". Fill in an plugin object, and pass to _run_count_plugin to handle lifetime. """ for i in os.listdir(config.get_dir("plugin")): plugin_file = os.path.join(config.get_dir("plugin"), i, Plugin.PLUGIN_INFO_FILE) if not os.path.exists(plugin_file): continue info = None try: with open(plugin_file) as pfile: info = json.load(pfile) except ValueError: logging.warning("JSON configuration not valid for plugin %s!", i) continue if not set(Plugin.PLUGIN_REQUIRED_KEYS).issubset(set(info.keys())): raise PluginException("Invalid Plugin") if info["name"] in _plugins.keys(): raise PluginException("Plugin Collision! Two plugins named " + info["name"]) greenlet.spawn_gevent_func("run_count_plugin: %s" % info["name"], "plugin", _run_count_plugin, Plugin(info, i))
def _handle_close(identity, msg): """Handle a request to close a connection. """ greenlet.spawn_gevent_func("close and block: %s" % identity, "main", _close_internal, identity, msg)
def _handle_client(identity, msg): """Handle a request to begin a new client connection """ greenlet.spawn_gevent_func("client: %s" % identity, "client", client.handle_client, identity, msg)
def _handle_claim_device(identity, msg): """Handle a request to claim a device from a plugin. """ greenlet.spawn_gevent_func("claim_device: %s" % msg[2], "device", plugin.run_device_plugin, identity, msg)
def spawn_heartbeat(identity, g): """Start a new heartbeat process.""" return greenlet.spawn_gevent_func("heartbeat-%s" % identity, "heartbeat", _heartbeat, identity, g)
def _handle_close(identity, msg): """Handle a request to close a connection. """ greenlet.spawn_gevent_func("close and block: %s" % identity, "main", _close_internal, identity, msg)
def _handle_client(identity, msg): """Handle a request to begin a new client connection """ greenlet.spawn_gevent_func("client: %s" % identity, "client", client.handle_client, identity, msg)
def _handle_claim_device(identity, msg): """Handle a request to claim a device from a plugin. """ greenlet.spawn_gevent_func("claim_device: %s" % msg[2], "device", plugin.run_device_plugin, identity, msg)