コード例 #1
0
ファイル: client.py プロジェクト: trb116/pythonanalyzer
 def sendRequest(self, command, **params):
     self.sent_request = DaqServerRequest(command, params)
     request_string = self.sent_request.serialize()
     log.debug('sending request: {}'.format(request_string))
     self.transport.write(''.join([request_string, '\r\n']))
     self.timeoutCallback = reactor.callLater(self.timeout,
                                              self.requestTimedOut)
     self.waiting_for_response = True
コード例 #2
0
 def sendRequest(self, command, **params):
     self.sent_request = DaqServerRequest(command, params)
     request_string = self.sent_request.serialize()
     log.debug('sending request: {}'.format(request_string))
     self.transport.write(''.join([request_string, '\r\n']))
     self.timeoutCallback = reactor.callLater(self.timeout, self.requestTimedOut)
     self.waiting_for_response = True
コード例 #3
0
 def lineReceived(self, line):
     line = line.strip()
     log.info('Received: {}'.format(line))
     try:
         request = DaqServerRequest.deserialize(line)
     except Exception, e:  # pylint: disable=W0703
         # PyDAQmx exceptions use "mess" rather than the standard "message"
         # to pass errors...
         message = getattr(e, 'mess', e.message)
         self.sendError('Received bad request ({}: {})'.format(e.__class__.__name__, message))
コード例 #4
0
ファイル: client.py プロジェクト: trb116/pythonanalyzer
class CommandExecutorProtocol(Protocol):
    def __init__(self, command, timeout=10, retries=1):
        self.command = command
        self.sent_request = None
        self.waiting_for_response = False
        self.keep_going = None
        self.ports_to_pull = None
        self.factory = None
        self.timeoutCallback = None
        self.timeout = timeout
        self.retries = retries
        self.retry_count = 0

    def connectionMade(self):
        if self.command.name == 'get_data':
            self.sendRequest('list_port_files')
        else:
            self.sendRequest(self.command.name, **self.command.params)

    def connectionLost(self, reason=ConnectionDone):
        if isinstance(reason, ConnectionLost):
            self.errorOut('connection lost: {}'.format(reason))
        elif self.waiting_for_response:
            self.errorOut(
                'Server closed connection without sending a response.')
        else:
            log.debug('connection terminated.')

    def sendRequest(self, command, **params):
        self.sent_request = DaqServerRequest(command, params)
        request_string = self.sent_request.serialize()
        log.debug('sending request: {}'.format(request_string))
        self.transport.write(''.join([request_string, '\r\n']))
        self.timeoutCallback = reactor.callLater(self.timeout,
                                                 self.requestTimedOut)
        self.waiting_for_response = True

    def dataReceived(self, data):
        self.keep_going = False
        if self.waiting_for_response:
            self.waiting_for_response = False
            self.timeoutCallback.cancel()
            try:
                response = DaqServerResponse.deserialize(data)
            except Exception, e:  # pylint: disable=W0703
                self.errorOut('Invalid response: {} ({})'.format(data, e))
            else:
                if response.status != Status.ERROR:
                    self.processResponse(response)  # may set self.keep_going
                    if not self.keep_going:
                        self.commandCompleted(response.status,
                                              response.message, response.data)
                else:
                    self.errorOut(response.message)
        else:
コード例 #5
0
 def lineReceived(self, line):
     line = line.strip()
     log.info('Received: {}'.format(line))
     try:
         request = DaqServerRequest.deserialize(line)
     except Exception, e:  # pylint: disable=W0703
         # PyDAQmx exceptions use "mess" rather than the standard "message"
         # to pass errors...
         message = getattr(e, 'mess', e.message)
         self.sendError('Received bad request ({}: {})'.format(
             e.__class__.__name__, message))
コード例 #6
0
class CommandExecutorProtocol(Protocol):

    def __init__(self, command, timeout=10, retries=1):
        self.command = command
        self.sent_request = None
        self.waiting_for_response = False
        self.keep_going = None
        self.ports_to_pull = None
        self.factory = None
        self.timeoutCallback = None
        self.timeout = timeout
        self.retries = retries
        self.retry_count = 0

    def connectionMade(self):
        if self.command.name == 'get_data':
            self.sendRequest('list_port_files')
        else:
            self.sendRequest(self.command.name, **self.command.params)

    def connectionLost(self, reason=ConnectionDone):
        if isinstance(reason, ConnectionLost):
            self.errorOut('connection lost: {}'.format(reason))
        elif self.waiting_for_response:
            self.errorOut('Server closed connection without sending a response.')
        else:
            log.debug('connection terminated.')

    def sendRequest(self, command, **params):
        self.sent_request = DaqServerRequest(command, params)
        request_string = self.sent_request.serialize()
        log.debug('sending request: {}'.format(request_string))
        self.transport.write(''.join([request_string, '\r\n']))
        self.timeoutCallback = reactor.callLater(self.timeout, self.requestTimedOut)
        self.waiting_for_response = True

    def dataReceived(self, data):
        self.keep_going = False
        if self.waiting_for_response:
            self.waiting_for_response = False
            self.timeoutCallback.cancel()
            try:
                response = DaqServerResponse.deserialize(data)
            except Exception, e:  # pylint: disable=W0703
                self.errorOut('Invalid response: {} ({})'.format(data, e))
            else:
                if response.status != Status.ERROR:
                    self.processResponse(response)  # may set self.keep_going
                    if not self.keep_going:
                        self.commandCompleted(response.status, response.message, response.data)
                else:
                    self.errorOut(response.message)
        else:
コード例 #7
0
 def lineReceived(self, line):
     line = line.strip()
     if sys.version_info[0] == 3:
         line = line.decode('utf-8')
     log.info('Received: {}'.format(line))
     try:
         request = DaqServerRequest.deserialize(line)
     except Exception as e:  # pylint: disable=W0703
         # PyDAQmx exceptions use "mess" rather than the standard "message"
         # to pass errors...
         message = getattr(e, 'mess', e.args[0] if e.args else str(e))
         self.sendError('Received bad request ({}: {})'.format(
             e.__class__.__name__, message))
     else:
         self.processRequest(request)
コード例 #8
0
class CommandExecutorProtocol(Protocol):

    def __init__(self, command, timeout=10, retries=1):
        self.command = command
        self.sent_request = None
        self.waiting_for_response = False
        self.keep_going = None
        self.ports_to_pull = None
        self.factory = None
        self.timeoutCallback = None
        self.timeout = timeout
        self.retries = retries
        self.retry_count = 0

    def connectionMade(self):
        if self.command.name == 'get_data':
            self.sendRequest('list_port_files')
        else:
            self.sendRequest(self.command.name, **self.command.params)

    def connectionLost(self, reason=ConnectionDone):
        if isinstance(reason, ConnectionLost):
            self.errorOut('connection lost: {}'.format(reason))
        elif self.waiting_for_response:
            self.errorOut('Server closed connection without sending a response.')
        else:
            log.debug('connection terminated.')

    def sendRequest(self, command, **params):
        self.sent_request = DaqServerRequest(command, params)
        request_string = self.sent_request.serialize()
        log.debug('sending request: {}'.format(request_string))
        request_string = ''.join([request_string, '\r\n'])
        if sys.version_info[0] == 3:
            self.transport.write(request_string.encode('utf-8'))
        else:
            self.transport.write(request_string)
        self.timeoutCallback = reactor.callLater(self.timeout, self.requestTimedOut)
        self.waiting_for_response = True

    def dataReceived(self, data):
        self.keep_going = False
        if self.waiting_for_response:
            self.waiting_for_response = False
            self.timeoutCallback.cancel()
            if sys.version_info[0] == 3:
                data = data.decode('utf-8')
            try:
                response = DaqServerResponse.deserialize(data)
            except Exception as e:  # pylint: disable=W0703
                self.errorOut('Invalid response: {} ({})'.format(data, e))
            else:
                if response.status != Status.ERROR:
                    self.processResponse(response)  # may set self.keep_going
                    if not self.keep_going:
                        self.commandCompleted(response.status, response.message, response.data)
                else:
                    self.errorOut(response.message)
        else:
            self.errorOut('unexpected data received: {}\n'.format(data))

    def processResponse(self, response):
        if self.sent_request.command in ['list_ports', 'list_port_files']:
            self.processPortsResponse(response)
        elif self.sent_request.command == 'list_devices':
            self.processDevicesResponse(response)
        elif self.sent_request.command == 'pull':
            self.processPullResponse(response)

    def processPortsResponse(self, response):
        if 'ports' not in response.data:
            self.errorOut('Response did not containt ports data: {} ({}).'.format(response, response.data))
        ports = response.data['ports']
        response.data = ports
        if self.command.name == 'get_data':
            if ports:
                self.ports_to_pull = ports
                self.sendPullRequest(self.ports_to_pull.pop())
            else:
                response.status = Status.OKISH
                response.message = 'No ports were returned.'

    def processDevicesResponse(self, response):
        if response.status == Status.OK:
            if 'devices' not in response.data:
                self.errorOut('Response did not containt devices data: {} ({}).'.format(response, response.data))
            devices = response.data['devices']
            response.data = devices

    def sendPullRequest(self, port_id):
        self.sendRequest('pull', port_id=port_id)
        self.keep_going = True

    def processPullResponse(self, response):
        if 'port_number' not in response.data:
            self.errorOut('Response does not contain port number: {} ({}).'.format(response, response.data))
        port_number = response.data.pop('port_number')
        filename = self.sent_request.params['port_id'] + '.csv'
        self.factory.initiateFileTransfer(filename, port_number)
        if self.ports_to_pull:
            self.sendPullRequest(self.ports_to_pull.pop())

    def commandCompleted(self, status, message=None, data=None):
        self.factory.result.status = status
        self.factory.result.message = message
        self.factory.result.data = data
        self.transport.loseConnection()

    def requestTimedOut(self):
        self.retry_count += 1
        if self.retry_count > self.retries:
            self.errorOut("Request timed out; server failed to respond.")
        else:
            log.debug('Retrying...')
            self.connectionMade()

    def errorOut(self, message):
        self.factory.errorOut(message)