def __init__(self, config): super(AntelopePortAgent, self).__init__(config) self.inst_addr = config['instaddr'] self.inst_port = config['instport'] self.keep_going = False self.orb = Orb('%s:%d' % (self.inst_addr, self.inst_port)) self.orb.connect() log.msg('Opened orb: %s' % self.orb) self.orb_thread = None self._pause = False self.router.registerProducer(self) reactor.addSystemEventTrigger('before', 'shutdown', self._orb_stop)
class AntelopePortAgent(PortAgent): def __init__(self, config): super(AntelopePortAgent, self).__init__(config) self.inst_addr = config['instaddr'] self.inst_port = config['instport'] self.keep_going = False self.orb = Orb('%s:%d' % (self.inst_addr, self.inst_port)) self.orb.connect() log.msg('Opened orb: %s' % self.orb) self.orb_thread = None self._pause = False self.router.registerProducer(self) reactor.addSystemEventTrigger('before', 'shutdown', self._orb_stop) def _register_loggers(self): """ Overridden, no logging on antelope, antelope keeps track of its own data... """ def client_disconnected(self, connection): """ Overridden to stop the running orb thread (if running) if all clients disconnect """ super(AntelopePortAgent, self).client_disconnected(connection) if len(self.clients) == 0: log.msg('All clients disconnected, stopping orb thread if running') self._orb_stop() def register_commands(self, command_protocol): super(AntelopePortAgent, self).register_commands(command_protocol) log.msg('PortAgent register commands for protocol: %s' % command_protocol) command_protocol.register_command('orblist', self._list_channels) command_protocol.register_command('orbselect', self._set_select) command_protocol.register_command('orbseek', self._set_seek) command_protocol.register_command('orbstart', self._orb_start) command_protocol.register_command('orbstop', self._orb_stop) command_protocol.register_command('orbget', self._orb_get) def _list_channels(self, *args): sources = self.orb.sources() return Packet.create(json.dumps(sources, indent=1) + NEWLINE, PacketType.PA_STATUS) def _set_select(self, command, *args): if len(args) == 0: num_sources = self.orb.select('') else: num_sources = self.orb.select(args[0]) msg = 'Orb select(%s) yielded num_sources: %d' % (args[:1], num_sources) return Packet.create(msg + NEWLINE, PacketType.PA_STATUS) def _set_seek(self, command, *args): if len(args) == 0: seek = ORBOLDEST else: seek = int(args[0]) self.orb.seek(seek) msg = 'Orb seek set to %s' % seek return Packet.create(msg + NEWLINE, PacketType.PA_STATUS) def _orb_start(self, *args): self._pause = False if self.orb_thread is None: self.keep_going = True self.orb_thread = OrbThread(self.orb, self) self.orb_thread.start() msg = 'Started orb thread' else: msg = 'Orb already running!' return Packet.create(msg + NEWLINE, PacketType.PA_STATUS) def _orb_stop(self, *args): self.keep_going = False if self.orb_thread is not None: self.orb_thread.join() self.orb_thread = None msg = 'Stopped orb thread' else: msg = 'Orb thread not running!' return Packet.create(msg + NEWLINE, PacketType.PA_STATUS) def _orb_get(self, *args): self.router.got_data(get_one(self.orb)) def get_state(self, *args): msg = 'CONNECTED' return Packet.create(msg, PacketType.PA_STATUS) def stopProducing(self): self._orb_stop() def pauseProducing(self): self._pause = True def resumeProducing(self): self._pause = False
class AntelopePortAgent(PortAgent): def __init__(self, config): super(AntelopePortAgent, self).__init__(config) self.inst_addr = config['instaddr'] self.inst_port = config['instport'] self.keep_going = False self.orb = Orb('%s:%d' % (self.inst_addr, self.inst_port)) self.orb.connect() log.msg('Opened orb: %s' % self.orb) self.orb_thread = None self._pause = False self.router.registerProducer(self) reactor.addSystemEventTrigger('before', 'shutdown', self._orb_stop) def _register_loggers(self): """ Overridden, no logging on antelope, antelope keeps track of its own data... """ def client_disconnected(self, connection): """ Overridden to stop the running orb thread (if running) if all clients disconnect """ super(AntelopePortAgent, self).client_disconnected(connection) if len(self.clients) == 0: log.msg('All clients disconnected, stopping orb thread if running') self._orb_stop() def register_commands(self, command_protocol): super(AntelopePortAgent, self).register_commands(command_protocol) log.msg('PortAgent register commands for protocol: %s' % command_protocol) command_protocol.register_command('orblist', self._list_channels) command_protocol.register_command('orbselect', self._set_select) command_protocol.register_command('orbseek', self._set_seek) command_protocol.register_command('orbstart', self._orb_start) command_protocol.register_command('orbstop', self._orb_stop) command_protocol.register_command('orbget', self._orb_get) def _list_channels(self, *args): sources = self.orb.sources() return Packet.create( json.dumps(sources, indent=1) + NEWLINE, PacketType.PA_STATUS) def _set_select(self, command, *args): if len(args) == 0: num_sources = self.orb.select('') else: num_sources = self.orb.select(args[0]) msg = 'Orb select(%s) yielded num_sources: %d' % (args[:1], num_sources) return Packet.create(msg + NEWLINE, PacketType.PA_STATUS) def _set_seek(self, command, *args): if len(args) == 0: seek = ORBOLDEST else: seek = int(args[0]) self.orb.seek(seek) msg = 'Orb seek set to %s' % seek return Packet.create(msg + NEWLINE, PacketType.PA_STATUS) def _orb_start(self, *args): self._pause = False if self.orb_thread is None: self.keep_going = True self.orb_thread = OrbThread(self.orb, self) self.orb_thread.start() msg = 'Started orb thread' else: msg = 'Orb already running!' return Packet.create(msg + NEWLINE, PacketType.PA_STATUS) def _orb_stop(self, *args): self.keep_going = False if self.orb_thread is not None: self.orb_thread.join() self.orb_thread = None msg = 'Stopped orb thread' else: msg = 'Orb thread not running!' return Packet.create(msg + NEWLINE, PacketType.PA_STATUS) def _orb_get(self, *args): self.router.got_data(get_one(self.orb)) def get_state(self, *args): msg = 'CONNECTED' return Packet.create(msg, PacketType.PA_STATUS) def stopProducing(self): self._orb_stop() def pauseProducing(self): self._pause = True def resumeProducing(self): self._pause = False