def deserialize(base, buf, protocol_factory=TBinaryProtocol.TBinaryProtocolFactory()): transport = TTransport.TMemoryBuffer(buf) protocol = protocol_factory.getProtocol(transport) base.read(protocol) return base
def encode_sandesh(sandesh): transport = TTransport.TMemoryBuffer() protocol_factory = TXMLProtocol.TXMLProtocolFactory() protocol = protocol_factory.getProtocol(transport) from gen_py.sandesh.ttypes import SandeshHeader sandesh_hdr = SandeshHeader(sandesh.scope(), sandesh.timestamp(), sandesh.module(), sandesh.source_id(), sandesh.context(), sandesh.seqnum(), sandesh.versionsig(), sandesh.type(), sandesh.hints(), sandesh.level(), sandesh.category(), sandesh.node_type(), sandesh.instance_id()) # write the sandesh header if sandesh_hdr.write(protocol) < 0: print 'Error in encoding sandesh header' return None # write the sandesh if sandesh.write(protocol) < 0: print 'Error in encoding sandesh' return None # get the message msg = transport.getvalue() # calculate the message length msg_len = len(_XML_SANDESH_OPEN) + len(msg) + len(_XML_SANDESH_CLOSE) len_width = len(_XML_SANDESH_OPEN) - \ (len(_XML_SANDESH_OPEN_ATTR_LEN) + len(_XML_SANDESH_OPEN_END)) # pad the length with leading 0s len_str = (str(msg_len)).zfill(len_width) encoded_buf = _XML_SANDESH_OPEN_ATTR_LEN + len_str + \ _XML_SANDESH_OPEN_END + msg + _XML_SANDESH_CLOSE return encoded_buf
def create_http_response(sandesh_resp, sandesh_init): universal_xsl_str = '<?xml-stylesheet type="text/xsl" href="/universal_parse.xsl"?>' transport = TTransport.TMemoryBuffer() protocol_factory = TXMLProtocol.TXMLProtocolFactory() protocol = protocol_factory.getProtocol(transport) if sandesh_resp.write(protocol) < 0: sandesh_init.logger().error( 'Http Response: Failed to encode sandesh (%s)', sandesh_resp.__class__.__name__) return sandesh_resp_xml = transport.getvalue() if not SandeshHttp._http_response: SandeshHttp._state = 'HXMLNew' SandeshHttp._http_response = cStringIO.StringIO() SandeshHttp._http_response.write(universal_xsl_str) if sandesh_resp._more: sandesh_name_end = sandesh_resp_xml.find(' ') if sandesh_name_end == -1: sandesh_init.logger().error( 'Http Response: Failed to get Sandesh name (%s)', sandesh_resp.__class__.__name__) return SandeshHttp._http_response_context = sandesh_resp_xml[ 1:sandesh_name_end] SandeshHttp._http_response.write( '<__%s_list type="slist">' % (SandeshHttp._http_response_context)) SandeshHttp._state = 'HXMLIncomplete' SandeshHttp._http_response.write(sandesh_resp_xml) if not sandesh_resp._more and SandeshHttp._state != 'HXMLNew': SandeshHttp._http_response.write( '</__%s_list>' % (SandeshHttp._http_response_context))
def _receive_sandesh_msg(self, session, msg): (hdr, hdr_len, sandesh_name) = SandeshReader.extract_sandesh_header(msg) if sandesh_name is None: self._logger.error('Failed to decode sandesh header for "%s"' % (msg)) return # update the sandesh rx stats self._sandesh_instance.stats().update_stats(sandesh_name, len(msg), False) if hdr.Hints & SANDESH_CONTROL_HINT: self._logger.debug('Received sandesh control message [%s]' % (sandesh_name)) if sandesh_name != 'SandeshCtrlServerToClient': self._logger.error('Invalid sandesh control message [%s]' % (sandesh_name)) return transport = TTransport.TMemoryBuffer(msg[hdr_len:]) protocol_factory = TXMLProtocol.TXMLProtocolFactory() protocol = protocol_factory.getProtocol(transport) from gen_py.sandesh_ctrl.ttypes import SandeshCtrlServerToClient sandesh_ctrl_msg = SandeshCtrlServerToClient() if sandesh_ctrl_msg.read(protocol) == -1: self._logger.error( 'Failed to decode sandesh control message "%s"' % (msg)) else: self._state_machine.on_sandesh_ctrl_msg_receive( session, sandesh_ctrl_msg, hdr.Source) else: self._logger.debug('Received sandesh message [%s]' % (sandesh_name)) self._client.handle_sandesh_msg(sandesh_name, msg[hdr_len:])
def handle_sandesh_msg(self, sandesh_name, sandesh_xml): transport = TTransport.TMemoryBuffer(sandesh_xml) protocol_factory = TXMLProtocol.TXMLProtocolFactory() protocol = protocol_factory.getProtocol(transport) sandesh_req = self._sandesh_instance.get_sandesh_request_object( sandesh_name) if sandesh_req: if sandesh_req.read(protocol) == -1: self._logger.error('Failed to decode sandesh request "%s"' \ % (sandesh_name)) else: self._sandesh_instance.enqueue_sandesh_request(sandesh_req)
def extract_sandesh_header(sandesh_xml): transport = TTransport.TMemoryBuffer(sandesh_xml) protocol_factory = TXMLProtocol.TXMLProtocolFactory() protocol = protocol_factory.getProtocol(transport) from gen_py.sandesh.ttypes import SandeshHeader hdr = SandeshHeader() hdr_len = hdr.read(protocol) if hdr_len == -1: return (None, 0, None) # Extract the sandesh name (length, sandesh_name) = protocol.readSandeshBegin() if length == -1: return (hdr, hdr_len, None) return (hdr, hdr_len, sandesh_name)
def send_msg(self, sandesh): transport = TTransport.TMemoryBuffer() protocol_factory = TJSONProtocol.TJSONProtocolFactory() protocol = protocol_factory.getProtocol(transport) # write the sandesh if sandesh.write(protocol) < 0: self._logger.error('Write Json failed') return -1 # get the message msg = transport.getvalue() if not msg: self._logger.error('Write Json failed') return -1 # send the message self.send_buf(msg) return 0
def handle_sandesh_msg(self, sandesh_name, sandesh_xml, msg_len): transport = TTransport.TMemoryBuffer(sandesh_xml) protocol_factory = TXMLProtocol.TXMLProtocolFactory() protocol = protocol_factory.getProtocol(transport) sandesh_req = self._sandesh_instance.get_sandesh_request_object(sandesh_name) if sandesh_req: if sandesh_req.read(protocol) == -1: self._sandesh_instance.update_rx_stats(sandesh_name, msg_len, SandeshRxDropReason.DecodingFailed) self._logger.error('Failed to decode sandesh request "%s"' \ % (sandesh_name)) else: self._sandesh_instance.update_rx_stats(sandesh_name, msg_len) self._sandesh_instance.enqueue_sandesh_request(sandesh_req) else: self._sandesh_instance.update_rx_stats(sandesh_name, msg_len, SandeshRxDropReason.CreateFailed)
def serialize(thrift_object, protocol_factory=TBinaryProtocol.TBinaryProtocolFactory()): transport = TTransport.TMemoryBuffer() protocol = protocol_factory.getProtocol(transport) thrift_object.write(protocol) return transport.getvalue()