def sendLine(self, line): """ Override sendLine to add a timeout to response. """ if not self._current: self.setTimeout(self.persistentTimeOut) LineReceiver.sendLine(self, line)
class RTL433ProcessProtocol(ProcessProtocol): def __init__(self, target): self.__target = target self.__line_receiver = LineReceiver() self.__line_receiver.delimiter = '\n' self.__line_receiver.lineReceived = self.__lineReceived def outReceived(self, data): """Implements ProcessProtocol.""" # split lines self.__line_receiver.dataReceived(data) def errReceived(self, data): """Implements ProcessProtocol.""" # we should inherit stderr, not pipe it raise Exception('shouldn\'t happen') def __lineReceived(self, line): # rtl_433's JSON encoder is not perfect (e.g. it will emit unescaped newlines), so protect against parse failures try: message = json.loads(line) except ValueError: log.msg('bad JSON from rtl_433: %s' % line) return log.msg('rtl_433 message: %r' % (message,)) # rtl_433 provides a time field, but when in file-input mode it assumes the input is not real-time and generates start-of-file-relative timestamps, so we can't use them. wrapper = RTL433MessageWrapper(message, time.time()) self.__target(wrapper)
def dataReceived(self, *args, **kwargs): # receives the data then checks if the request is complete. # if it is, it calls full_Request_received LineReceiver.dataReceived(self, *args, **kwargs) if self._request_obj.complete: self.full_request_received()
def sendLine(self, line): log.info('Responding: {}'.format(line)) line = line.replace('\r\n', '') if sys.version_info[0] == 3: LineReceiver.sendLine(self, line.encode('utf-8')) else: LineReceiver.sendLine(self, line)
def sendLine(self, line): if self.onXMLReceived: raise FoneworxException, "onXMLReceived already initialized before sending" self.onXMLReceived = Deferred() log.msg("Sending line: %s" % line, logLevel=logging.DEBUG) LineReceiver.sendLine(self, line) return self.onXMLReceived
class APRSProcessProtocol(ProcessProtocol): def __init__(self, target): self.__target = target self.__line_receiver = LineReceiver() self.__line_receiver.delimiter = '\n' self.__line_receiver.lineReceived = self.__lineReceived self.__last_line = None def outReceived(self, data): # split lines self.__line_receiver.dataReceived(data) def errReceived(self, data): # we should inherit stderr, not pipe it raise Exception('shouldn\'t happen') def __lineReceived(self, line): if line == '': # observed glitch in output pass elif line.startswith('Enabled demodulators:'): pass elif line.startswith( '$ULTW' ) and self.__last_line is not None: # observed glitch in output; need to glue to previous line, I think? ll = self.__last_line self.__last_line = None self.__target(ll + line) elif line.startswith('APRS: '): line = line[len('APRS: '):] self.__last_line = line self.__target(line) else: # TODO: Log these properly print 'Not APRS line: %r' % line
def connectionMade(self): LineReceiver.connectionMade(self) remote_ip = self.transport.getPeer() host_ip = self.transport.getHost() self.remote_ip = remote_ip.host + ":" + str(remote_ip.port) self.host_ip = host_ip.host + ":" + str(LISTENING_PORT) print("Connection from", self.transport.getPeer())
def connectionMade(self): LineReceiver.connectionMade(self) # print('connectionMade') self.intent = 'stream' message = Message(intent=self.intent) message['sessionId'] = self.factory.service.getSessionId() self.sendLine(message.getJsonContent().encode('utf-8'))
def connectionLost(self, reason): """ Cause any outstanding commands to fail. """ self._disconnected = True self._cancelCommands(reason) LineReceiver.connectionLost(self, reason)
class RTL433ProcessProtocol(ProcessProtocol): def __init__(self, target): self.__target = target self.__line_receiver = LineReceiver() self.__line_receiver.delimiter = '\n' self.__line_receiver.lineReceived = self.__lineReceived def outReceived(self, data): """Implements ProcessProtocol.""" # split lines self.__line_receiver.dataReceived(data) def errReceived(self, data): """Implements ProcessProtocol.""" # we should inherit stderr, not pipe it raise Exception('shouldn\'t happen') def __lineReceived(self, line): # rtl_433's JSON encoder is not perfect (e.g. it will emit unescaped newlines), so protect against parse failures try: message = json.loads(line) except ValueError: log.msg('bad JSON from rtl_433: %s' % line) return log.msg('rtl_433 message: %r' % (message, )) # rtl_433 provides a time field, but when in file-input mode it assumes the input is not real-time and generates start-of-file-relative timestamps, so we can't use them. wrapper = RTL433MessageWrapper(message, time.time()) self.__target(wrapper)
def connectionLost(self, why): self.connected = 0 self.script_hashes.clear() self.factory.delConnection(self) LineReceiver.connectionLost(self, why) while self.replyQueue.waiting: self.replyReceived(ConnectionError("Lost connection"))
def __init__(self, svc=None): self.lineReceiver = LineReceiver() self.lineReceiver.delimiter = '\n' self.lineReceiver.lineReceived = self.lineReceived self.svc = svc self.isReady = False self.completionDeferred = Deferred()
class APRSProcessProtocol(ProcessProtocol): def __init__(self, target): self.__target = target self.__line_receiver = LineReceiver() self.__line_receiver.delimiter = '\n' self.__line_receiver.lineReceived = self.__lineReceived self.__last_line = None def outReceived(self, data): # split lines self.__line_receiver.dataReceived(data) def errReceived(self, data): # we should inherit stderr, not pipe it raise Exception('shouldn\'t happen') def __lineReceived(self, line): if line == '': # observed glitch in output pass elif line.startswith('Enabled demodulators:'): pass elif line.startswith('$ULTW') and self.__last_line is not None: # observed glitch in output; need to glue to previous line, I think? ll = self.__last_line self.__last_line = None self.__target(ll + line) elif line.startswith('APRS: '): line = line[len('APRS: '):] self.__last_line = line self.__target(line) else: # TODO: Log these properly print 'Not APRS line: %r' % line
def dataReceived(self, data): if self.factory.stream_response and self.stream_response: self.factory.return_transport.write(data) LineReceiver.dataReceived(self, data) if not self.completed: if self._response_obj.complete: self.completed = True self.handle_response_end()
def __init__(self): self.state = self.State.ANNOUNCE self.session = None self.nonce = None self.seq = 0 LineReceiver.setRawMode(self) global video_server_connection video_server_connection = self
def dataReceived(self, data): if self.factory.stream_response: self.factory.return_transport.write(data) LineReceiver.dataReceived(self, data) if not self.completed: if self._response_obj.complete: self.completed = True self.handle_response_end()
def connectionMade(self): ProcessProtocol.connectionMade(self) LineReceiver.connectionMade(self) self.transport.disconnecting = False #erm. Needs this to fix a bug in Twisted? self.send_to_client("hello", [self.communicator.mod.name]) if not self.messages_not_acknowledged: self.client_started_processing_at = time.time() self.messages_not_acknowledged += 1
def connectionLost(self, reason): """ ups... stop reactor """ LineReceiver.connectionLost(self, reason) print "Connection lost: ", reason.getErrorMessage() try: reactor.stop() except ReactorNotRunning: print " - reactor not running, so we are not stopping it"
def connectionMade(self): """ Notify our factory that we're ready to accept connections. """ LineReceiver.connectionMade(self) if self.factory.deferred is not None: self.factory.deferred.callback(self) self.factory.deferred = None
def __init__(self, server_name, connected_deferred): self.__proxy_obj = None self.__server_name = server_name self.__connected_deferred = connected_deferred self.__line_receiver = LineReceiver() self.__line_receiver.delimiter = '\n' self.__line_receiver.lineReceived = self.__lineReceived self.__waiting_for_responses = [] self.__receive_cmd = None self.__receive_arg = None
def dataReceived(self, *args, **kwargs): # receives the data then checks if the request is complete. # if it is, it calls full_Request_received LineReceiver.dataReceived(self, *args, **kwargs) if self._request_obj.complete: try: self.full_request_received() except PappyException as e: print str(e)
def __init__(self, factory ): LineReceiver.__init__(self) self.__factory=factory self.__parts=factory.parts self.setLineMode() self.__current_command_id: int = 3 self.__issued_command_storage = {} self.__commandRE = re.compile('(\[(\S+)\])?([\S]+)\.(\S+)\((.*)\)') self.delimiter = b'\r' self.__parts['sys'] = FunctionCaller('sys')
def create_request(self): channel = LineReceiver() channel.site = PixelatedSite(mock()) request = PixelatedSite.requestFactory(channel=channel, queued=True) request.method = "GET" request.uri = "localhost" request.clientproto = 'HTTP/1.1' request.prepath = [] request.postpath = request.uri.split('/')[1:] request.path = "/" return request
def __init__(self, factory): LineReceiver.__init__(self) self.delimiter = b'\r' self.__factory = factory self.__parts = factory.parts #Observable.__init__(self, 'tcp') #self.setLineMode() #self.decoding_talker = compose(self.say_array, self.codec) #self.add_listener("cmd", self.process_result) self.__current_command_id: int = 3 self.__issued_command_storage = {} self.__commandRE = re.compile('(\[(\S+)\])?([\S]+)\.(\S+)\((.*)\)')
def sendLine(self, line): if isinstance(line, str): if six.PY3: super(StringLineReceiver, self).sendLine(line.encode()) else: LineReceiver.sendLine(self, line.encode()) elif isinstance(line, bytes): if six.PY3: super(StringLineReceiver, self).sendLine(line) else: LineReceiver.sendLine(self, line) else: raise RuntimeError("Only str and bytes are allowed.")
def __init__(self, transport, delimiter="\r\n"): """ @param transport: The transport from which to read bytes and to which to write them! @type transport: L{GreenletTransport} @param delimiter: The line delimiter to split lines on. @type delimiter: C{str} """ self.delimiter = delimiter self.transport = transport self.receiver = LineReceiver() self.receiver.delimiter = delimiter self.lines = [] self.receiver.lineReceived = self.lines.append
def __init__(self, reactor): self.__reactor = reactor self.__line_receiver = LineReceiver() self.__line_receiver.delimiter = b';' self.__line_receiver.lineReceived = self.__lineReceived self.__communication_error = u'not_responding' self.__explicit_waits = defaultdict(list) # set up dummy initial state self.__scheduled_poll = reactor.callLater(0, lambda: None) self.__scheduled_poll.cancel() # Set up proxy. # Do this last because the proxy fetches metadata from us so we should be otherwise fully initialized. self.__proxy_obj = _ElecraftRadio(self)
def sendLine(self, line): self.log.info('SEND: ' + line) for regex, callback in self.send_line_hooks: if regex.match(line) is not None: callback(self, line) return LineReceiver.sendLine(self, line)
class CommandReceiver(channel.ChannelReceiver): def __init__(self): self.lineReceiver = LineReceiver() self.lineReceiver.lineReceived = self.lineReceived def lineReceived(self, line): cmd, data = line.split(':', 1) self.commandReceived(cmd.strip(), data.strip()) def commandReceived(self, cmd, data): raise NotImplementedError def sendCommand(self, cmd, data): return self.send(self.cmd_channel, '%s: %s\r\n' % (cmd, data)) def channeReceived(self, channel, type, data): if channel == self.cmd_channel: self.lineReceiver.dataReceived(data)
class LineBuffer(object): """ A line-buffering wrapper for L{GreenletTransport}s (or any other object with C{read} and C{write} methods). Call L{readLine} to get the next line, call L{writeLine} to write a line. """ def __init__(self, transport, delimiter="\r\n"): """ @param transport: The transport from which to read bytes and to which to write them! @type transport: L{GreenletTransport} @param delimiter: The line delimiter to split lines on. @type delimiter: C{str} """ self.delimiter = delimiter self.transport = transport self.receiver = LineReceiver() self.receiver.delimiter = delimiter self.lines = [] self.receiver.lineReceived = self.lines.append def writeLine(self, data): """ Write some data to the transport followed by the delimiter. """ self.transport.write(data + self.delimiter) def readLine(self): """ Return a line of data from the transport. """ while not self.lines: self.receiver.dataReceived(self.transport.read()) return self.lines.pop(0) def __iter__(self): """ Yield the result of L{readLine} forever. """ while True: yield self.readLine()
class _ControllerProtocol(Protocol): def __init__(self): self.__line_receiver = LineReceiver() self.__line_receiver.delimiter = b';' self.__line_receiver.lineReceived = self.__lineReceived def connectionMade(self): """overrides Protocol""" # TODO: Report success def connectionLost(self, reason=None): """overrides Protocol""" # TODO: Report loss to user def dataReceived(self, data): """overrides Protocol""" self.__line_receiver.dataReceived(data) def __lineReceived(self, line): print line def send(self, cmd): self.transport.write(cmd)
def sendLine(self, data, filter=True, await_ack=True): """ Send a line to the Gyrid server. When not connected, store the data in the cache. @param data The line to send. @param await_ack Whether the line should be added to the await_ack buffer. """ data = str(data).strip() r = self.factory.filter(data) if filter else data if self.factory.config['enable_cache'] and not self.factory.connected \ and not self.factory.cache.closed and not self.factory.cache_full \ and not data.startswith('MSG') and not data.startswith('STATE'): if r != None: self.factory.cache.write(r + '\n') else: if r != None and self.transport != None: LineReceiver.sendLine(self, r) if await_ack and not r.startswith('MSG') \ and not r.startswith('STATE') \ and self.factory.config['enable_cache']: self.factory.ackmap.addItem(AckItem(r))
def connectionLost(self, reason): LineReceiver.connectionLost(self, reason) try: self.lc_ping.stop() except: pass print('pair: ', self.factory.pair) print('conn_list: ', self.factory.connection_list) print('peers: ', self.factory.peers) print('host_list:', self.factory.host_list) print('miner_list:', self.factory.miner_nodes) if self.miner_id in self.factory.miner_nodes: self.factory.miner_nodes.pop(self.miner_id) print('miner :', self.miner_id, ' disconnected') if self.remote_nodeid in self.factory.peers: host = self.factory.pair[self.remote_nodeid] self.factory.pair.pop(self.remote_nodeid) self.factory.peers.pop(self.remote_nodeid) self.factory.host_list.remove(host) self.factory.connection_list.remove(host) print(self.remote_nodeid, "disconnected") else: print(self.nodeid, 'disconnected')
class _ControllerProtocol(Protocol): def __init__(self): self.__line_receiver = LineReceiver() self.__line_receiver.delimiter = ';' self.__line_receiver.lineReceived = self.__lineReceived def connectionMade(self): """overrides Protocol""" # TODO: Report success def connectionLost(self, reason=None): """overrides Protocol""" # TODO: Report loss to user def dataReceived(self, data): """overrides Protocol""" self.__line_receiver.dataReceived(data) def __lineReceived(self, line): print line def send(self, cmd): self.transport.write(cmd)
class PostgresMonitor(ProcessProtocol): """ A monitoring protocol which watches the postgres subprocess. """ log = Logger() def __init__(self, svc=None): self.lineReceiver = LineReceiver() self.lineReceiver.delimiter = '\n' self.lineReceiver.lineReceived = self.lineReceived self.svc = svc self.isReady = False self.completionDeferred = Deferred() def lineReceived(self, line): if self.svc is None: return if not self.isReady: if _MAGIC_READY_COOKIE in line: self.svc.ready() disconnecting = False def connectionMade(self): self.lineReceiver.makeConnection(self) def outReceived(self, out): for line in out.split("\n"): if line: self.log.info("{message}", message=line) # self.lineReceiver.dataReceived(out) def errReceived(self, err): for line in err.split("\n"): if line: self.log.error("{message}", message=line) self.lineReceiver.dataReceived(err) def processEnded(self, reason): self.log.info( "pg_ctl process ended with status={status}", status=reason.value.status ) # If pg_ctl exited with zero, we were successful in starting postgres # If pg_ctl exited with nonzero, we need to give up. self.lineReceiver.connectionLost(reason) if reason.value.status == 0: self.completionDeferred.callback(None) else: self.log.error("Could not start postgres; see postgres.log") self.completionDeferred.errback(reason)
class _PostgresMonitor(ProcessProtocol): """ A monitoring protocol which watches the postgres subprocess. """ def __init__(self, svc=None): self.lineReceiver = LineReceiver() self.lineReceiver.delimiter = '\n' self.lineReceiver.lineReceived = self.lineReceived self.svc = svc self.isReady = False self.completionDeferred = Deferred() def lineReceived(self, line): if self.svc is None: return if not self.isReady: if _MAGIC_READY_COOKIE in line: self.svc.ready() disconnecting = False def connectionMade(self): self.lineReceiver.makeConnection(self) def outReceived(self, out): log.warn("received postgres stdout {out!r}", out=out) # self.lineReceiver.dataReceived(out) def errReceived(self, err): log.warn("received postgres stderr {err}", err=err) self.lineReceiver.dataReceived(err) def processEnded(self, reason): log.warn( "postgres process ended with status {status}", status=reason.value.status ) # If pg_ctl exited with zero, we were successful in starting postgres # If pg_ctl exited with nonzero, we need to give up. self.lineReceiver.connectionLost(reason) if reason.value.status == 0: self.completionDeferred.callback(None) else: log.warn("Could not start postgres; see postgres.log") self.completionDeferred.errback(reason)
class _PostgresMonitor(ProcessProtocol): """ A monitoring protocol which watches the postgres subprocess. """ def __init__(self, svc=None): self.lineReceiver = LineReceiver() self.lineReceiver.delimiter = '\n' self.lineReceiver.lineReceived = self.lineReceived self.svc = svc self.isReady = False self.completionDeferred = Deferred() def lineReceived(self, line): if self.svc is None: return if not self.isReady: if _MAGIC_READY_COOKIE in line: self.svc.ready() disconnecting = False def connectionMade(self): self.lineReceiver.makeConnection(self) def outReceived(self, out): log.msg("received postgres stdout %r" % (out,)) # self.lineReceiver.dataReceived(out) def errReceived(self, err): log.msg("received postgres stderr %r" % (err,)) self.lineReceiver.dataReceived(err) def processEnded(self, reason): log.msg("postgres process ended %r" % (reason,)) result = (reason.value.status == 0) self.lineReceiver.connectionLost(reason) self.completionDeferred.callback(result)
class _ElecraftClientProtocol(Protocol): __log = Logger() def __init__(self, reactor): self.__reactor = reactor self.__line_receiver = LineReceiver() self.__line_receiver.delimiter = b';' self.__line_receiver.lineReceived = self.__lineReceived self.__communication_error = u'not_responding' self.__explicit_waits = defaultdict(list) # set up dummy initial state self.__scheduled_poll = reactor.callLater(0, lambda: None) self.__scheduled_poll.cancel() # Set up proxy. # Do this last because the proxy fetches metadata from us so we should be otherwise fully initialized. self.__proxy_obj = _ElecraftRadio(self) def connectionMade(self): """overrides Protocol""" self.__reinitialize() def connectionLost(self, reason): # pylint: disable=signature-differs """overrides Protocol""" if self.__scheduled_poll.active(): self.__scheduled_poll.cancel() self.__communication_error = u'serial_gone' def get_communication_error(self): return self.__communication_error def dataReceived(self, data): """overrides Protocol""" self.__line_receiver.dataReceived(data) def send_command(self, cmd_text): """Send a raw command. The text must include its trailing semicolon. If wait=True, return a Deferred. """ if isinstance(cmd_text, six.text_type): cmd_text = cmd_text.encode( 'us-ascii') # TODO: correct choice of encoding assert isinstance(cmd_text, bytes) assert cmd_text == b'' or cmd_text.endswith(b';') self.transport.write(cmd_text) def get(self, name): """TODO explain Note that this may wait too little time, if the same command is already in flight. """ d = defer.Deferred() self.__explicit_waits[name].append(d) self.send_command(name + ';') return d def __reinitialize(self): # TODO: Use ID, K3, and OM commands to confirm identity and customize self.transport.write( b'AI2;' # Auto-Info Mode 2; notification of any change (delayed) b'K31;') # enable extended response, important for FW command self.request_all() # also triggers polling cycle def __schedule_timeout(self): if self.__scheduled_poll.active(): self.__scheduled_poll.cancel() self.__scheduled_poll = self.__reactor.callLater( 1, self.__poll_doubtful) def __schedule_got_response(self): if self.__scheduled_poll.active(): self.__scheduled_poll.cancel() self.__scheduled_poll = self.__reactor.callLater( 0.04, self.__poll_fast_reactive) def request_all(self): self.transport.write( b'IF;' b'AG;AG$;AN;AP;BN;BN$;BW;BW$;CP;CW;DV;ES;FA;FB;FI;FR;FT;GT;IS;KS;' b'LK;LK$;LN;MC;MD;MD$;MG;ML;NB;NB$;PA;PA$;PC;RA;RA$;RG;RG$;SB;SQ;' b'SQ$;VX;XF;XF$;') # If we don't get a response, this fires self.__schedule_timeout() def __poll_doubtful(self): """If this method is called then we didn't get a prompt response.""" self.__communication_error = 'not_responding' self.transport.write(b'FA;') self.__schedule_timeout() def __poll_fast_reactive(self): """Normal polling activity.""" # Get FA (VFO A frequency) so we respond fast. # Get BN (band) because if we find out we changed bands we need to update band-dependent things. # Get MD (mode) because on KX3, A/B button does not report changes self.transport.write(b'FA;BN;MD;MD$;') self.__schedule_timeout() def __lineReceived(self, line): line = line.lstrip(b'\x00') # nulls are sometimes sent on power-on self.__log.debug('Elecraft client: received {line!r}', line=line) if b'\x00' in line: # Bad data that may be received during radio power-on return elif line == b'?': # busy indication; nothing to do about it as yet pass else: try: cmd = six.text_type(line[:2], 'ascii') sub = len(line) > 2 and line[2] == b'$' cmd_sub = cmd + ('$' if sub else '') data = line[(3 if sub else 2):] handler = _st.dispatch(cmd) if handler is not None: handler(data, sub, self._update) else: self.__log.warn( 'Elecraft client: unrecognized message {cmd!r} in {line!r}', cmd=cmd, line=line) if cmd_sub in self.__explicit_waits: waits = self.__explicit_waits[cmd_sub] del self.__explicit_waits[cmd_sub] for d in waits: self.__reactor.callLater(0, d.callback, data) except ValueError: # bad digits or whatever self.__log.failure( 'Elecraft client: error while parsing message {line!r}', line=line) self.__communication_error = 'bad_data' return # don't consider as OK, but don't reinit either if not self.__communication_error: # communication is still OK self.__schedule_got_response() else: self.__communication_error = None # If there was a communication error, we might be out of sync, so resync and also start up normal polling. self.__reinitialize() def _update(self, key, value, sub=False): """Handle a received value update.""" # TODO less poking at internals, more explicit cell = self.__proxy_obj.state().get(key) if not cell: cell = (self.__proxy_obj.get_rx_sub() if sub else self.__proxy_obj.get_rx_main()).state().get(key) if cell: old_value = cell.get() cell.set_internal(value) else: # just don't crash self.__log.warn('Elecraft client: missing cell for state {key!r}', key=key) if key == 'band' and value != old_value: # Band change! Check the things we don't get prompt or any notifications for. self.request_all() def _proxy(self): """for use by connect_to_rig""" return self.__proxy_obj
def sendCommand(self, cmd): print ">", cmd LineReceiver.sendLine(self, cjson.encode(cmd))
def sendLine(self, line): print "Relay -> iPhone",line LineReceiver.sendLine(self,line)
def sendLine(self, line): print "Relay -> Server",line LineReceiver.sendLine(self,line)
def __init__(self, target): self.__target = target self.__line_receiver = LineReceiver() self.__line_receiver.delimiter = '\n' self.__line_receiver.lineReceived = self.__lineReceived self.__last_line = None
def connectionMade(self): LineReceiver.connectionMade(self) self.transport_connected = True self.log("Transport connected")
class _HamlibClientProtocol(Protocol): def __init__(self, server_name, connected_deferred, log): self.__server_name = server_name self.__connected_deferred = connected_deferred self.__log = log self.__proxy_obj = None self.__line_receiver = LineReceiver() self.__line_receiver.delimiter = b'\n' self.__line_receiver.lineReceived = self.__lineReceived self.__waiting_for_responses = [] self.__receive_cmd = None self.__receive_arg = None def connectionMade(self): self.__connected_deferred.callback(self) def connectionLost(self, reason): # pylint: disable=signature-differs if self.__proxy_obj is not None: self.__proxy_obj._clientConnectionLost(reason) def dataReceived(self, data): self.__line_receiver.dataReceived(data) def __lineReceived(self, line): line = six.text_type(line, 'us-ascii') # TODO verify best choice of encoding if self.__receive_cmd is None: match = re.match(r'^(\w+):\s*(.*)$', line) if match is not None: # command response starting line self.__receive_cmd = match.group(1) self.__receive_arg = match.group(2) return self.__log.error( '%s client: Unrecognized line (no command active): %r' % (self.__server_name, line)) # TODO use logger formatting else: match = re.match(r'^RPRT (-?\d+)$', line) if match is not None: # command response ending line return_code = int(match.group(1)) waiting = self.__waiting_for_responses i = 0 for i, (wait_cmd, wait_deferred) in enumerate(waiting): if self.__receive_cmd != wait_cmd: self.__log.error( "%s client: Didn't get a response for command %r before receiving one for command %r" % (self.__server_name, wait_cmd, self.__receive_cmd)) # TODO use logger formatting else: # TODO: Consider 'parsing' return code more here. if return_code != 0: self.__proxy_obj._clientError( self.__receive_cmd, return_code) wait_deferred.callback(return_code) break self.__waiting_for_responses = waiting[i + 1:] self.__receive_cmd = None self.__receive_arg = None return if self.__receive_cmd == 'get_level': # Should be a level value match = re.match(r'^-?\d+\.?\d*$', line) if match: self.__proxy_obj._clientReceivedLevel( self.__receive_arg, line) return match = re.match(r'^([\w ,/-]+):\s*(.*)$', line) if match is not None: # Command response if self.__proxy_obj is not None: self.__proxy_obj._clientReceived(self.__receive_cmd, match.group(1), match.group(2)) return match = re.match(r'^\t', line) if match is not None and self.__receive_cmd == 'dump_caps': # Sub-info from dump_caps, not currently used return match = re.match(r'^Warning--', line) if match is not None: # Warning from dump_caps, not currently used return match = re.match(r'^$', line) if match is not None: return self.__log.error('%s client: Unrecognized line during %s: %r' % (self.__server_name, self.__receive_cmd, line)) # TODO use logger formatting def _set_proxy(self, proxy): self.__proxy_obj = proxy def rc_send(self, cmd, argstr=''): if not re.match( r'^\w+$', cmd ): # no spaces (stuffing args in), no newlines (breaking the command) raise ValueError('Syntactically invalid command name %r' % (cmd, )) if not re.match(r'^[^\r\n]*$', argstr): # no newlines raise ValueError('Syntactically invalid arguments string %r' % (cmd, )) self.transport.write( ('+\\' + cmd + ' ' + argstr + '\n').encode('us-ascii')) d = defer.Deferred() self.__waiting_for_responses.append((cmd, d)) return d
def connectionLost(self, reason=connectionDone): LineReceiver.connectionLost(self) self.transport_connected = False self.log("Transport disconnected")
def write(self, line): LineReceiver.sendLine(self, line)
def __init__(self, target): self.__target = target self.__line_receiver = LineReceiver() self.__line_receiver.delimiter = '\n' self.__line_receiver.lineReceived = self.__lineReceived