def accepted(self, client): """ Process the request on the accepted socket. :param client: A client socket. :type client: socket.socket """ try: client.setsockopt(IPPROTO_TCP, TCP_NODELAY, 1) client.setsockopt(SOL_SOCKET, SO_LINGER, struct.pack('ii', 1, 1)) message = client.recv(4096) call = Document() call.load(message) reply = self.dispatch(call) client.send(reply) except Exception, e: log.error(utf8(e))
def call(self, *args, **kwargs): """ Remote call. """ socket = Socket(AF_INET, SOCK_STREAM) socket.connect(self.address) try: method = Document() method.name = self.name method.args = args method.kwargs = kwargs socket.send(method.dump()) reply = socket.recv(4096) result = Document() result.load(reply) return result finally: socket.close()
def _read(path): """ Read a request. :param path: The path to the journal file. :type path: str :return: The read request. :rtype: Document """ with open(path) as fp: try: request = Document() body = fp.read() request.load(body) log.debug('read [%s]: %s', path, body) return request except ValueError: log.error('%s corrupt (discarded)', path) unlink(path)
def _rejected(self, code, description, document, details): """ Called to process the received (invalid) document. This method intended to be overridden by subclasses. :param code: The rejection code. :type code: str :param description: rejection description :type description: str :param document: The received (json) document. :type document: str :param details: The explanation. :type details: str """ details = dict( code=code, description=description, details=details) request = Document() request.load(document) self._send(request, 'rejected', **details)
def _read(path): """ Read a request. :param path: The path to the journal file. :type path: str :return: The read request. :rtype: Document """ fp = open(path) try: try: request = Document() body = fp.read() request.load(body) log.debug('read [%s]: %s', path, body) return request except ValueError: log.error('%s corrupt (discarded)', path) os.unlink(path) finally: fp.close()