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 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 encode_sandesh(sandesh, sandesh_instance=None): 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: if sandesh_instance is not None: sandesh_instance.msg_stats().update_tx_stats( sandesh.__class__.__name__, 0, SandeshTxDropReason.HeaderWriteFailed ) return None # write the sandesh if sandesh.write(protocol) < 0: if sandesh_instance is not None: sandesh_instance.msg_stats().update_tx_stats( sandesh.__class__.__name__, 0, SandeshTxDropReason.WriteFailed ) 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