def receive_data_from_federate(self, timeout): """called whenever data is available from any federate and immediately proceeds to send data through process pipe """ dataarray = [] if self.federates: events = self.sel.select(timeout) for key, mask in events: conn = key.fileobj try: header = conn.recv(4) except Exception as e: continue if header: try: buffer = self._get_header_length(header) raw_protobuf_message = conn.recv(buffer) print(raw_protobuf_message) protobuf_object = FederatedEvent() protobuf_object.ParseFromString(raw_protobuf_message) dataarray.append(protobuf_object) except Exception as e: conn.recv(10000) continue else: self.disconnect_client(key.data.uid) return dataarray else: return None
def send_disconnection_data(self, CoT: SendDisconnect): if self.federates: proto_obj = FederatedEvent() proto_obj.contact.uid = str(CoT.modelObject.detail.link.uid) proto_obj.contact.callsign = str(CoT.modelObject.detail.link.type) proto_obj.contact.operation = 4 proto_str = proto_obj.SerializeToString() header = self._generate_header(len(proto_str)) for fed in self.federates.values(): fed.conn.send(header + proto_str) else: return None
def test_from_protobuf_serialization(self): from FreeTAKServer.model.FTSModel.Event import Event from FreeTAKServer.model.protobufModel.fig_pb2 import FederatedEvent import lxml protobufString = b'\n\x83\x04\x08\xbe\xb9\xfb\x85\xeb.\x10\xbe\xb9\xfb\x85\xeb.\x18\x96\xab\x92\x86\xeb.!\xaf\xf6b\x88\\d<@) {Mcr\x1aG\xc01\x00\x00\x00\xe0\xcf\x12cA9\x00\x00\x00\xe0\xcf\x12cAA\x00\x00\x00\xe0\xcf\x12cAJ.S-1-5-21-2720623347-3037847324-4167270909-1002R\x0ba-f-G-U-C-IZ\x03h-eb\xfa\x02<detail><takv version="4.0.0.117" platform="WinTAK-CIV" os="Microsoft Windows 10 Home" device="Micro-Star International Co., Ltd MS-7C02"/><contact callsign="FEATHER" endpoint="*:-1:stcp"/><uid Droid="FEATHER"/><__group name="Cyan" role="Team Member"/><_flow-tags_ TAK-Server-c0581fed97ff4cb89eb8666a8794670cc9f77ddb-badf-48da-abe7-84545ecda69d="2020-12-29T23:04:27Z"/></detail>hd' # protobufString2 = b'"\x00\x00\x00="\x12;\x08\x01\x12.S-1-5-21-2720623347-3037847324-4167270909-1002\x1a\x07FEATHER' protobufObj = FederatedEvent() protobufObj.ParseFromString(protobufString) # protobufObj.ParseFromString(protobufString2) #y = [name.name for name in protobufObj.event.DESCRIPTOR.fields] fts_obj = ProtobufSerializer().from_format_to_fts_object(protobufObj, Event.Connection()) z = lxml.etree.tostring(XmlSerializer().from_fts_object_to_format(fts_obj)) 1 == 1
def _send_connected_clients(self, connection): clients = self.db.query_user() for client in clients: try: proto_obj = FederatedEvent() proto_obj.contact.uid = str(client.uid) proto_obj.contact.callsign = str(client.CoT.detail.contact.callsign) proto_obj.contact.operation = 1 proto_str = proto_obj.SerializeToString() header = self._generate_header(len(proto_str)) connection.send(header + proto_str) except Exception as e: logger.warning("error thrown sending federate data to newly connected federate " + str(e)) continue
def send_connection_data(self, CoT: ClientInformation) -> None: try: if self.federates: logger.debug("connection data received in send_connection_data") proto_obj = FederatedEvent() proto_obj.contact.uid = str(CoT.modelObject.uid) proto_obj.contact.callsign = str(CoT.modelObject.detail.contact.callsign) proto_obj.contact.operation = 1 proto_str = proto_obj.SerializeToString() header = self._generate_header(len(proto_str)) for fed in self.federates.values(): fed.conn.send(header + proto_str) return None else: return None except Exception as e: logger.warning("exception throw sending new connection data to federates " + str(e)) return None
def from_fts_object_to_format(self, FTSObject: Event) -> type: try: obj = FederatedEvent() if hasattr(FTSObject.detail, 'marti'): for dest in FTSObject.detail.marti.dest: callsign = dest.getcallsign() if callsign: obj.event.ptpCallsigns.append(callsign) else: pass for descriptor in obj.event.DESCRIPTOR.fields: attribute_name = descriptor.name if attribute_name in self.attribute_name_mapping: attribute_name = self.attribute_name_mapping[ attribute_name] if attribute_name != 'other' and attribute_name: getters = self._get_fts_object_var_getter( FTSObject, attribute_name) getter = self._get_method_in_method_list(getters, 'event') if attribute_name == 'time' or attribute_name == 'start' or attribute_name == 'stale': import datetime attribute = getter() setattr( obj.event, descriptor.name, int( datetime.datetime.strptime( attribute, "%Y-%m-%dT%H:%M:%S.%fZ").timestamp() * 1000)) elif attribute_name in [ 'lat', 'lon', 'hae', 'ce', 'le', 'speed', 'course' ]: setattr(obj.event, descriptor.name, float(getter())) elif attribute_name == 'battery': setattr(obj.event, descriptor.name, int(getter())) else: setattr(obj.event, descriptor.name, getter()) else: from lxml import etree xmldetail = etree.tostring( XmlSerializer().from_fts_object_to_format( FTSObject.detail)).decode() setattr(obj.event, 'other', xmldetail) return obj except Exception as e: raise e
def _receive_new_data(self, key): try: conn = key.fileobj header = conn.recv(4) if header: try: buffer = self._get_header_length(header) raw_protobuf_message = conn.recv(buffer) print(raw_protobuf_message) protobuf_object = FederatedEvent() protobuf_object.ParseFromString(raw_protobuf_message) return protobuf_object except Exception as e: conn.recv(10000) return None else: self.disconnect_client(key.data.uid) except OSError: return None except Exception as e: logger.warning("exception in receiving data from federate "+str(e)) self.disconnect_client(key.data.uid)
def send_data_to_federates(self, data): try: # sends supplied data to supplied socket upon being called federatedEvent = FederatedEvent() ProtoObj = FederatedCoTController().serialize_from_FTS_modelv1( federatedevent=federatedEvent, ftsobject=data) protostring = ProtoObj.SerializeToString() header = self.generate_header(len(protostring)) protostring = header + protostring print(b'sent ' + protostring + b' to federate') self.connection.send(protostring) return 1 except Exception as e: pass
def receive_data_from_federates(self): # returns data received from federate # the following logic receives data from the federate and processes the protobuf # up to 100 CoT's dataCount = 0 dataArray = [] # 100 is the limit of data which can be received from a federate in one iteration while dataCount < 100: dataCount += 1 try: try: self.connection.settimeout(0.01) data = self.connection.recv(self.buffer) self.connection.settimeout(0) except TimeoutError: break except Exception as e: self.disconnect() self.killSwitch = True return 0 if data != [b'']: header = data[0] content = self.connection.recv( self.get_header_length(header)) EmptyFTSObject = Event.FederatedCoT() protoObject = FederatedEvent().FromString(content) print(protoObject) FTSObject = FederatedCoTController( ).serialize_main_contentv1(protoObject, EmptyFTSObject) print('received data from Federate') print(content) else: self.killSwitch = True dataArray.append(FTSObject) except Exception as e: pass
pass def send_data_to_federates(self, data): try: # sends supplied data to supplied socket upon being called federatedEvent = FederatedEvent() ProtoObj = FederatedCoTController().serialize_from_FTS_modelv1( federatedevent=federatedEvent, ftsobject=data) protostring = ProtoObj.SerializeToString() header = self.generate_header(len(protostring)) protostring = header + protostring print(b'sent ' + protostring + b' to federate') self.connection.send(protostring) return 1 except Exception as e: pass def recv_in_data_pipe(self, pipe): pass def send_in_data_pipe(self, pipe, data): pass if __name__ == '__main__': FederationServiceController content = b'\n\xb1\x06\x08\xd8\x81\xa6\xa1\xec.\x10\x84\xf0\xa5\xa1\xec.\x18\x84\xa8\xbf\xca\xec.1\x00\x00\x00\xe0\xcf\x12cA9\x00\x00\x00\xe0\xcf\x12cAA\x00\x00\x00\xe0\xcf\x12cAJjGeoChat.S-1-5-21-2720623347-3037847324-4167270909-1002.All Chat Rooms.673a0aa4-c8eb-4bb5-aa7e-abab7d8d89a0R\x05b-t-fZ\th-g-i-g-ob\x80\x05<detail><__chat id="All Chat Rooms" chatroom="All Chat Rooms" senderCallsign="FEATHER" groupOwner="false"><chatgrp id="All Chat Rooms" uid0="S-1-5-21-2720623347-3037847324-4167270909-1002" uid1="All Chat Rooms"/></__chat><link uid="S-1-5-21-2720623347-3037847324-4167270909-1002" type="a-f-G-U-C-I" relation="p-p"/><remarks source="BAO.F.WinTAK.S-1-5-21-2720623347-3037847324-4167270909-1002" sourceID="S-1-5-21-2720623347-3037847324-4167270909-1002" to="All Chat Rooms" time="2021-01-02T17:33:40.74Z">aa</remarks><_flow-tags_ TAK-Server-c0581fed97ff4cb89eb8666a8794670cc9f77ddb-badf-48da-abe7-84545ecda69d="2021-01-02T17:33:43Z"/></detail>' y = FederatedEvent().FromString(content) 1 == 1