Ejemplo n.º 1
0
def test(json):
    modelObject = Event.dropPoint()
    out = XMLCoTController().serialize_CoT_to_model(modelObject,
                                                    etree.fromstring(json))
    xml = XMLCoTController().serialize_model_to_CoT(out, 'event')
    from FreeTAKServer.controllers.SpecificCoTControllers.SendDropPointController import SendDropPointController
    rawcot = RawCoT()
    rawcot.xmlString = xml
    rawcot.clientInformation = None
    object = SendDropPointController(rawcot)
    print(
        etree.tostring(object.sendDropPoint.xmlString,
                       pretty_print=True).decode())
    '''EventObject = json
Ejemplo n.º 2
0
 def test_xml_serialization(self):
     """
     test new serialization in contrast with legacy serializer to ensure compatibility
     """
     from FreeTAKServer.model.FTSModel.Event import Event
     xmlstring = '<event version="2.0" uid="GeoChat.ANDROID-359975090666199.FEATHER.27d8ef23-8578-4cb4-8f53-02f5dc150cd2" type="b-t-f" how="h-g-i-g-o" start="2021-01-03T19:01:35.472Z" time="2021-01-03T19:01:35.472Z" stale="2021-01-04T19:01:35.472Z"><detail><__chat id="S-1-5-21-2720623347-3037847324-4167270909-1002" parent="RootContactGroup" chatroom="FEATHER" groupOwner="false"><chatgrp uid0="ANDROID-359975090666199" uid1="S-1-5-21-2720623347-3037847324-4167270909-1002" id="S-1-5-21-2720623347-3037847324-4167270909-1002"/></__chat><link uid="ANDROID-359975090666199" relation="p-p" type="a-f-G-E-V-A"/><remarks time="2021-01-03T19:01:35.472Z" source="BAO.F.ATAK.ANDROID-359975090666199" to="S-1-5-21-2720623347-3037847324-4167270909-1002">at VDO</remarks><__serverdestination destinations="192.168.2.66:4242:tcp:ANDROID-359975090666199"/><marti><dest callsign = "WOLF"/><dest callsign="GALLOP"/><dest callsign="FEATHER"/></marti></detail><point le="9999999.0" ce="3.2" hae="22.958679722315807" lon="-66.10803" lat="43.855711"/></event>'
     fts_obj = XmlSerializer().from_format_to_fts_object(
         xmlstring, Event.GeoChat())
     obj = XmlSerializer().from_fts_object_to_format(fts_obj)
     print(etree.tostring(obj).decode())
     legacyftsobj = XMLCoTController().serialize_CoT_to_model(
         Event.GeoChat(), etree.fromstring(xmlstring))
     legacy_string = XMLCoTController().serialize_model_to_CoT(legacyftsobj)
     self.assertEqual(legacy_string.decode(), etree.tostring(obj).decode())
Ejemplo n.º 3
0
 def test_xml_serialization(self):
     """
     test new serialization in contrast with legacy serializer to ensure compatibility
     """
     from FreeTAKServer.model.FTSModel.Event import Event
     # xmlstring = '<event version="2.0" uid="GeoChat.ANDROID-359975090666199.FEATHER.27d8ef23-8578-4cb4-8f53-02f5dc150cd2" type="b-t-f" how="h-g-i-g-o" start="2021-01-03T19:01:35.472Z" time="2021-01-03T19:01:35.472Z" stale="2021-01-04T19:01:35.472Z"><detail><__chat id="S-1-5-21-2720623347-3037847324-4167270909-1002" parent="RootContactGroup" chatroom="FEATHER" groupOwner="false"><chatgrp uid0="ANDROID-359975090666199" uid1="S-1-5-21-2720623347-3037847324-4167270909-1002" id="S-1-5-21-2720623347-3037847324-4167270909-1002"/></__chat><link uid="ANDROID-359975090666199" relation="p-p" type="a-f-G-E-V-A"/><remarks time="2021-01-03T19:01:35.472Z" source="BAO.F.ATAK.ANDROID-359975090666199" to="S-1-5-21-2720623347-3037847324-4167270909-1002">at VDO</remarks><__serverdestination destinations="192.168.2.66:4242:tcp:ANDROID-359975090666199"/><marti><dest callsign = "WOLF"/><dest callsign="GALLOP"/><dest callsign="FEATHER"/></marti></detail><point le="9999999.0" ce="3.2" hae="22.958679722315807" lon="-66.10803" lat="43.855711"/></event>'
     xmlstring = '<event version="2.0" uid="115e1a1a-9947-4531-801c-40215fa4888c" type="b-m-r" time="2021-03-17T21:53:08.07Z" start="2021-03-17T21:53:08.07Z" stale="2021-03-24T21:53:08.07Z" how="h-e"><point lat="0" lon="0" hae="0" ce="9999999" le="9999999" /><detail><contact callsign="Eliopoli/BOP" /><link uid="4eaf4026-8db6-48ef-83c2-063dd0120339" type="b-m-p-w" point="44.1644630, -65.7995800,85.84967474" callsign="Eliopoli/BOP SP" relation="c" remarks="" /><link uid="42882562-9dc6-481b-9c89-9a5ba96fbf57" type="b-m-p-w" point="43.9676310, -66.1266740,16.80836837" callsign="Eliopoli" relation="c" remarks="" /><link_attr color="-1" type="b-m-r" method="Driving" direction="Infil" routetype="Primary" order="Ascending Check Points" /></detail></event>'
     # fts_obj = XmlSerializer().from_format_to_fts_object(xmlstring, Event.GeoChat())
     fts_obj = XmlSerializer().from_format_to_fts_object(xmlstring, Event.Route())
     obj = XmlSerializer().from_fts_object_to_format(fts_obj)
     print(etree.tostring(obj).decode())
     # legacyftsobj = XMLCoTController().serialize_CoT_to_model(Event.GeoChat(), etree.fromstring(xmlstring))
     legacy_string = XMLCoTController().serialize_model_to_CoT(fts_obj)
     self.assertEqual(legacy_string.decode(), etree.tostring(obj).decode())
Ejemplo n.º 4
0
 def __init__(self, json):
     tempObject = event.Presence()
     object = SendPresence()
     object.setModelObject(tempObject)
     object.modelObject = self._serializeJsonToModel(object.modelObject, json)
     object.setXmlString(XMLCoTController().serialize_model_to_CoT(object.modelObject))
     self.setCoTObject(object)
Ejemplo n.º 5
0
    def send_active_emergencys(self, client):
        """
        this function needs to be cleaned up however it's functionality is as follows
        it query's the DB for active emergency's at which point it iterates over all
        emergency objects, transforms them into model objects and then xmlStrings
        finally the object is sent to the client.
        """
        try:

            from FreeTAKServer.model.SpecificCoT.SendEmergency import SendEmergency
            emergencys = self.dbController.query_ActiveEmergency()
            for emergency in emergencys:
                emergencyobj = SendEmergency()
                modelObject = Event.emergecyOn()

                filledModelObject = SqlAlchemyObjectController(
                ).convert_sqlalchemy_to_modelobject(emergency.event,
                                                    modelObject)
                emergencyobj.setXmlString(
                    XMLCoTController().serialize_model_to_CoT(
                        filledModelObject))
                emergencyobj.setModelObject(filledModelObject)
                SendDataController().sendDataInQueue(None, emergencyobj,
                                                     [client])

        except Exception as e:
            self.logger.error(
                'an exception has been thrown in sending active emergencies ' +
                str(e))
Ejemplo n.º 6
0
    def test_xml_serialization(self):
        """
        test new serialization in contrast with legacy serializer to ensure compatibility
        """
        from FreeTAKServer.model.FTSModel.Event import Event
        # xmlstring = '<event version="2.0" uid="GeoChat.ANDROID-359975090666199.FEATHER.27d8ef23-8578-4cb4-8f53-02f5dc150cd2" type="b-t-f" how="h-g-i-g-o" start="2021-01-03T19:01:35.472Z" time="2021-01-03T19:01:35.472Z" stale="2021-01-04T19:01:35.472Z"><detail><__chat id="S-1-5-21-2720623347-3037847324-4167270909-1002" parent="RootContactGroup" chatroom="FEATHER" groupOwner="false"><chatgrp uid0="ANDROID-359975090666199" uid1="S-1-5-21-2720623347-3037847324-4167270909-1002" id="S-1-5-21-2720623347-3037847324-4167270909-1002"/></__chat><link uid="ANDROID-359975090666199" relation="p-p" type="a-f-G-E-V-A"/><remarks time="2021-01-03T19:01:35.472Z" source="BAO.F.ATAK.ANDROID-359975090666199" to="S-1-5-21-2720623347-3037847324-4167270909-1002">at VDO</remarks><__serverdestination destinations="192.168.2.66:4242:tcp:ANDROID-359975090666199"/><marti><dest callsign = "WOLF"/><dest callsign="GALLOP"/><dest callsign="FEATHER"/></marti></detail><point le="9999999.0" ce="3.2" hae="22.958679722315807" lon="-66.10803" lat="43.855711"/></event>'
        xmlstring = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><event version="2.0" uid="03Z0170972" type="a-f-A-M-H-Q" time="2021-06-17T19:08:19.699Z" start="2021-06-17T19:08:19.699Z" stale="2021-06-18T19:08:23.199Z" how="m-g"><point lat="43.85566678637877" lon="-66.10800161079035" hae="0.11165844181401496" ce="0.0" le="0.0" /><detail> <contact callsign="UAS-NOVA" /><sensor elevation="0.0" vfov="66.0" north="226.70000076293945" roll="0.0" range="300" azimuth="46.0" model="Phantom 3 Advanced Camera" fov="81.0" type="r-e" version="0.6" /></detail></event>'

        # fts_obj = XmlSerializer().from_format_to_fts_object(xmlstring, Event.GeoChat())
        fts_obj = XmlSerializer().from_format_to_fts_object(
            xmlstring, Event.DroneSensor())
        obj = XmlSerializer().from_fts_object_to_format(fts_obj)
        print(etree.tostring(obj).decode())
        # legacyftsobj = XMLCoTController().serialize_CoT_to_model(Event.GeoChat(), etree.fromstring(xmlstring))
        legacy_string = XMLCoTController().serialize_model_to_CoT(fts_obj)
        self.assertEqual(legacy_string.decode(), etree.tostring(obj).decode())
Ejemplo n.º 7
0
 def __init__(self, json):
     tempObject = event.SimpleCoT()
     object = SendSimpleCoT()
     object.setModelObject(tempObject)
     object.modelObject = self._serializeJsonToModel(object.modelObject, json)
     DatabaseController().create_CoT(object.modelObject)
     object.setXmlString(XMLCoTController().serialize_model_to_CoT(object.modelObject))
     self.setCoTObject(object)
Ejemplo n.º 8
0
 def create_xml_string(self, modelObject):
     """
     this function calls the model to xml serializer within XMLCoTController
     with an instantiated model object supplied in the function argument
     """
     from FreeTAKServer.controllers.XMLCoTController import XMLCoTController
     try:
         xml = XMLCoTController().serialize_model_to_CoT(modelObject, 'event')
         return xml
     except Exception as e:
         logger.error('there has been an exception in the creation of the xml string ' +str(e))
         return -1
Ejemplo n.º 9
0
 def __init__(self):
     log_format = logging.Formatter(loggingConstants.LOGFORMAT)
     self.logger = logging.getLogger(loggingConstants.LOGNAME)
     self.logger.setLevel(logging.DEBUG)
     self.logger.addHandler(
         self.newHandler(loggingConstants.DEBUGLOG, logging.DEBUG,
                         log_format))
     self.logger.addHandler(
         self.newHandler(loggingConstants.ERRORLOG, logging.ERROR,
                         log_format))
     self.logger.addHandler(
         self.newHandler(loggingConstants.INFOLOG, logging.INFO,
                         log_format))
     # create necessary queues
     self.clientInformationQueue = []
     # this contains a list of all pipes which are transmitting CoT from clients
     self.pipeList = []
     # Internal Pipe used for CoT generated by the server itself
     self.internalCoTArray = []
     self.ClientReceptionHandlerEventPipe = ''
     # health check events
     self.healthCheckEventArray = []
     # instantiate controllers
     self.ActiveThreadsController = ActiveThreadsController()
     self.ClientInformationController = ClientInformationController()
     self.ClientInformationQueueController = ClientInformationQueueController(
     )
     self.ClientSendHandler = ClientSendHandler()
     self.DataQueueController = DataQueueController()
     self.ReceiveConnections = ReceiveConnections()
     self.ReceiveConnectionsProcessController = ReceiveConnectionsProcessController(
     )
     self.MainSocketController = MainSocketController()
     self.XMLCoTController = XMLCoTController()
     self.SendClientData = SendClientData()
     self.KillSwitch = 0
     self.openSockets = 0
     self.openSocketsArray = []
Ejemplo n.º 10
0
    def serialize_main_contentv1(self, protoobject, ftsobject):
        print('beginning serialization to FT')
        try:
            event = protoobject.event


            # the following try and except statements format milliseconds to CoT compatible DT
            try:
                datetime.datetime.strftime(datetime.datetime.strptime(str(datetime.datetime.fromtimestamp(float(event.sendTime) / 1000.0)), "%Y-%m-%d %H:%M:%S"), "%Y-%m-%dT %H:%M:%S.%fZ")
            except:
                datetime.datetime.strftime(datetime.datetime.strptime(str(datetime.datetime.fromtimestamp(float(event.sendTime) / 1000.0)), "%Y-%m-%d %H:%M:%S.%f"), "%Y-%m-%dT %H:%M:%S.%fZ")

            try:
                formatedDatatime = str(datetime.datetime.fromtimestamp(float(event.startTime) / 1000.0))
                datetime.datetime.strftime(datetime.datetime.strptime(formatedDatatime, "%Y-%m-%d %H:%M:%S"), "%Y-%m-%dT %H:%M:%S.%fZ")
            except Exception as e:
                formatedDatatime = str(datetime.datetime.fromtimestamp(float(event.startTime) / 1000.0))
                datetime.datetime.strftime(datetime.datetime.strptime(formatedDatatime, "%Y-%m-%d %H:%M:%S.%f"),"%Y-%m-%dT %H:%M:%S.%fZ")

            try:
                datetime.datetime.strftime(datetime.datetime.strptime(str(datetime.datetime.fromtimestamp(float(event.staleTime) / 1000.0)), "%Y-%m-%d %H:%M:%S"), "%Y-%m-%dT %H:%M:%S.%fZ")
            except:
                datetime.datetime.strftime(datetime.datetime.strptime(str(datetime.datetime.fromtimestamp(float(event.staleTime) / 1000.0)), "%Y-%m-%d %H:%M:%S.%f"), "%Y-%m-%dT %H:%M:%S.%fZ")

            ftsobject.setuid(event.uid)
            ftsobject.settype(event.type)
            ftsobject.sethow(event.coordSource)

            point = ftsobject.getpoint()
            point.setce(event.ce)
            point.setle(event.le)
            point.setlon(event.lon)
            point.setlat(event.lat)
            point.sethae(event.hae)

            ftsobject.setpoint(point)

            detail = event.other

            obj = SendFederatedCoT()
            obj.modelObject = ftsobject
            tempxmlstring = etree.fromstring(XMLCoTController().serialize_model_to_CoT(ftsobject))
            eventString = tempxmlstring
            eventString.append(etree.fromstring(detail.encode()))
            obj.xmlString = etree.tostring(eventString)
            print('serialized protobuf to ' + str(obj.xmlString))
            return obj
        except Exception as e:
            print(e)
            return -1
Ejemplo n.º 11
0
 def monitorRawCoT(self, data):
     # this needs to be the most robust function as it is the keystone of the program
     # this will attempt to define the type of CoT along with the designated controller
     try:
         if isinstance(data, int):
             return None
         else:
             CoT = XMLCoTController().determineCoTGeneral(data)
             function = getattr(self, CoT[0])
             output = function(CoT[1])
             return output
     except Exception as e:
         self.logger.error(loggingConstants.MONITORRAWCOTERRORB + str(e))
         return -1
Ejemplo n.º 12
0
def SendGeoChat():
    try:
        json = request.json
        modelObject = Event.GeoChat()
        out = ApplyFullJsonController().serializeJsonToModel(modelObject, json)
        xml = XMLCoTController().serialize_model_to_CoT(out, 'event')
        from FreeTAKServer.controllers.SpecificCoTControllers.SendGeoChatController import SendGeoChatController
        rawcot = RawCoT()
        rawcot.xmlString = xml
        rawcot.clientInformation = None
        object = SendGeoChatController(rawcot)
        APIPipe.send(object.getObject())
        return '200', 200
    except Exception as e:
        print(e)
Ejemplo n.º 13
0
 def __init__(self, json):
     if isinstance(json, EmergencyPost):
         tempObject = event.emergecyOn()
     elif isinstance(json, EmergencyDelete):
         tempObject = event.emergecyOff()
     else:
         raise Exception(
             'unsupported object type passed under json argument')
     object = SendEmergency()
     object.setModelObject(tempObject)
     object.modelObject = self._serializeJsonToModel(
         object.modelObject, json)
     object.setXmlString(XMLCoTController().serialize_model_to_CoT(
         object.modelObject))
     self.setCoTObject(object)
 def intstantiateClientInformationModelFromConnection(
         self, rawClientInformation, queue):
     try:
         tempObject = Event.Connection()
         self.clientInformation = ClientInformation()
         argument = "initialConnection"
         self.clientInformation.dataQueue = queue
         self.clientInformation.socket = rawClientInformation.socket
         self.clientInformation.IP = rawClientInformation.ip
         self.clientInformation.idData = rawClientInformation.xmlString
         self.clientInformation.alive = 1
         self.clientInformation.ID = uuid.uuid1().int
         self.clientInformation.modelObject = XMLCoTController(
         ).serialize_CoT_to_model(
             tempObject,
             etree.fromstring(rawClientInformation.xmlString.encode()))
         return self.clientInformation
     except Exception as e:
         logger.debug('error in client information controller ' + str(e))
         return -1
Ejemplo n.º 15
0
class Orchestrator:
    #TODO: fix repeat attempts to add user
    # default constructor  def __init__(self):
    def __init__(self):
        log_format = logging.Formatter(loggingConstants.LOGFORMAT)
        self.logger = logging.getLogger(loggingConstants.LOGNAME)
        self.logger.setLevel(logging.DEBUG)
        self.logger.addHandler(
            self.newHandler(loggingConstants.DEBUGLOG, logging.DEBUG,
                            log_format))
        self.logger.addHandler(
            self.newHandler(loggingConstants.ERRORLOG, logging.ERROR,
                            log_format))
        self.logger.addHandler(
            self.newHandler(loggingConstants.INFOLOG, logging.INFO,
                            log_format))
        # create necessary queues
        self.clientInformationQueue = []
        # this contains a list of all pipes which are transmitting CoT from clients
        self.pipeList = []
        # Internal Pipe used for CoT generated by the server itself
        self.internalCoTArray = []
        self.ClientReceptionHandlerEventPipe = ''
        # health check events
        self.healthCheckEventArray = []
        # instantiate controllers
        self.ActiveThreadsController = ActiveThreadsController()
        self.ClientInformationController = ClientInformationController()
        self.ClientInformationQueueController = ClientInformationQueueController(
        )
        self.ClientSendHandler = ClientSendHandler()
        self.DataQueueController = DataQueueController()
        self.ReceiveConnections = ReceiveConnections()
        self.ReceiveConnectionsProcessController = ReceiveConnectionsProcessController(
        )
        self.MainSocketController = MainSocketController()
        self.XMLCoTController = XMLCoTController()
        self.SendClientData = SendClientData()
        self.KillSwitch = 0
        self.openSockets = 0
        self.openSocketsArray = []

    def clear_user_table(self):
        self.dbController.remove_user()
        print('user table cleared')

    def testing(self):
        """
        function which creates variables for testing
        """
        from multiprocessing import Pipe
        from FreeTAKServer.controllers.DatabaseControllers.DatabaseController import DatabaseController
        self.dbController = DatabaseController()
        self.CoTSharePipe, other = Pipe()
        return None

    def newHandler(self, filename, log_level, log_format):
        handler = RotatingFileHandler(filename,
                                      maxBytes=loggingConstants.MAXFILESIZE,
                                      backupCount=loggingConstants.BACKUPCOUNT)
        handler.setFormatter(log_format)
        handler.setLevel(log_level)
        return handler

    def sendUserConnectionGeoChat(self, clientInformation):
        # TODO: refactor as it has a proper implementation of a PM to a user generated by the server
        '''
        function to create and send pm to connecting user
        :param clientInformation:
        :return:
        '''
        from FreeTAKServer.controllers.SpecificCoTControllers.SendGeoChatController import SendGeoChatController
        from FreeTAKServer.model.RawCoT import RawCoT
        from FreeTAKServer.model.FTSModel.Dest import Dest
        import uuid
        if OrchestratorConstants().DEFAULTCONNECTIONGEOCHATOBJ != None:
            ChatObj = RawCoT()
            ChatObj.xmlString = f'<event><point/><detail><remarks>{OrchestratorConstants().DEFAULTCONNECTIONGEOCHATOBJ}</remarks><marti><dest/></marti></detail></event>'

            classobj = SendGeoChatController(ChatObj, AddToDB=False)
            instobj = classobj.getObject()
            instobj.modelObject.detail._chat.chatgrp.setuid1(
                clientInformation.modelObject.uid)
            dest = Dest()
            dest.setcallsign(
                clientInformation.modelObject.detail.contact.callsign)
            instobj.modelObject.detail.marti.setdest(dest)
            instobj.modelObject.detail._chat.setchatroom(
                clientInformation.modelObject.detail.contact.callsign)
            instobj.modelObject.detail._chat.setparent("RootContactGroup")
            instobj.modelObject.detail._chat.setid(
                clientInformation.modelObject.uid)
            instobj.modelObject.detail._chat.setgroupOwner("True")
            instobj.modelObject.detail.remarks.setto(
                clientInformation.modelObject.uid)
            instobj.modelObject.setuid(
                'GeoChat.' + 'SERVER-UID.' +
                clientInformation.modelObject.detail.contact.callsign + '.' +
                str(uuid.uuid1()))
            instobj.modelObject.detail._chat.chatgrp.setid(
                clientInformation.modelObject.uid)
            classobj.reloadXmlString()
            SendDataController().sendDataInQueue(None, instobj,
                                                 self.clientInformationQueue)
            return 1
        else:
            return 1

    def clientConnected(self, rawConnectionInformation):
        try:
            # temporarily broken
            # self.check_for_dead_sockets()
            from FreeTAKServer.controllers.DatabaseControllers.EventTableController import EventTableController
            clientPipe = None
            self.logger.info(loggingConstants.CLIENTCONNECTED)
            clientInformation = self.ClientInformationController.intstantiateClientInformationModelFromConnection(
                rawConnectionInformation, clientPipe)
            if self.checkOutput(clientInformation):
                pass
            else:
                raise Exception('error in the creation of client information')
            self.openSockets += 1
            #breaks ssl
            #self.ClientInformationQueueController.addClientToQueue(clientInformation)
            self.clientInformationQueue.append(clientInformation)
            try:
                if hasattr(clientInformation.socket, 'getpeercert'):
                    cn = "placeholder"
                else:
                    cn = None
                CoTRow = EventTableController().convert_model_to_row(
                    clientInformation.modelObject)
                self.dbController.create_user(
                    uid=clientInformation.modelObject.uid,
                    callsign=clientInformation.modelObject.detail.contact.
                    callsign,
                    IP=clientInformation.IP,
                    CoT=CoTRow,
                    CN=cn)
            except Exception as e:
                print(e)
                self.logger.error(
                    'there has been an error in a clients connection while adding information to the database '
                    + str(e))
            #self.logger.info(loggingConstants.CLIENTCONNECTEDFINISHED + str(clientInformation.modelObject.detail.contact.callsign))
            sock = clientInformation.socket
            clientInformation.socket = None
            self.clientDataPipe.put(
                ['add', clientInformation, self.openSockets])
            clientInformation.socket = sock
            self.sendUserConnectionGeoChat(clientInformation)
            return clientInformation
        except Exception as e:
            self.logger.warning(loggingConstants.CLIENTCONNECTEDERROR + str(e))
            return -1

    def check_for_dead_sockets(self):
        # fix function
        try:
            for sock in self.clientInformationQueue:
                if sock.is_alive():
                    pass
                else:
                    self.clientDisconnected(sock)
            return 1
        except Exception as e:
            self.logger.error(
                "there has been an exception in checking for dead sockets " +
                str(e))
            return -1

    def emergencyReceived(self, processedCoT):
        try:
            if processedCoT.status == loggingConstants.ON:
                self.internalCoTArray.append(processedCoT)
                self.logger.debug(loggingConstants.EMERGENCYCREATED)
            elif processedCoT.status == loggingConstants.OFF:
                for CoT in self.internalCoTArray:
                    if CoT.type == "Emergency" and CoT.modelObject.uid == processedCoT.modelObject.uid:
                        self.internalCoTArray.remove(CoT)
                        self.logger.debug(loggingConstants.EMERGENCYREMOVED)
        except Exception as e:
            self.logger.error(loggingConstants.EMERGENCYRECEIVEDERROR + str(e))

    def dataReceived(self, RawCoT):
        # this will be executed in the event that the use case for the CoT isnt specified in the orchestrator
        try:
            # this will check if the CoT is applicable to any specific controllers
            RawCoT = self.XMLCoTController.determineCoTType(RawCoT)
            # the following calls whatever controller was specified by the above function
            module = importlib.import_module(
                'FreeTAKServer.controllers.SpecificCoTControllers.' +
                RawCoT.CoTType)
            CoTSerializer = getattr(module, RawCoT.CoTType)
            #TODO: improve way in which the dbController is passed to CoTSerializer
            RawCoT.dbController = self.dbController
            processedCoT = CoTSerializer(RawCoT).getObject()
            sender = processedCoT.clientInformation
            # this will send the processed object to a function which will send it to connected clients
            '''try:
                # TODO: method of determining if CoT should be added to the internal array should
                #  be improved
                if processedCoT.type == "Emergency":
                    self.emergencyReceived(processedCoT)
                else:
                    pass
            except Exception as e:
                return -1'''
            return processedCoT
        except Exception as e:
            self.logger.error(loggingConstants.DATARECEIVEDERROR + str(e))
            return -1

    def sendInternalCoT(self, client):
        try:
            if len(self.internalCoTArray) > 0:
                for processedCoT in self.internalCoTArray:
                    SendDataController().sendDataInQueue(
                        processedCoT.clientInformation, processedCoT, [client])
            else:
                pass
            self.send_active_emergencys(client)
            return 1
        except Exception as e:
            self.logger.error(
                loggingConstants.MONITORRAWCOTERRORINTERNALSCANERROR + str(e))
            return -1

    def send_active_emergencys(self, client):
        """
        this function needs to be cleaned up however it's functionality is as follows
        it query's the DB for active emergency's at which point it iterates over all
        emergency objects, transforms them into model objects and then xmlStrings
        finally the object is sent to the client.
        """
        try:

            from FreeTAKServer.model.SpecificCoT.SendEmergency import SendEmergency
            from lxml import etree
            emergencys = self.dbController.query_ActiveEmergency()
            for emergency in emergencys:
                emergencyobj = SendEmergency()
                modelObject = Event.emergecyOn()

                filledModelObject = SqlAlchemyObjectController(
                ).convert_sqlalchemy_to_modelobject(emergency.event,
                                                    modelObject)
                # emergencyobj.setXmlString(XMLCoTController().serialize_model_to_CoT(filledModelObject))
                emergencyobj.setXmlString(
                    etree.tostring((XmlSerializer().from_fts_object_to_format(
                        filledModelObject))))
                print(emergencyobj.xmlString)
                emergencyobj.setModelObject(filledModelObject)
                SendDataController().sendDataInQueue(None, emergencyobj,
                                                     [client])

        except Exception as e:
            import traceback
            self.logger.error(traceback.format_exc())
            self.logger.error(
                'an exception has been thrown in sending active emergencies ' +
                str(e))

    def clientDisconnected(self, clientInformation):
        if hasattr(clientInformation, "clientInformation"):
            clientInformation = clientInformation.clientInformation
        try:
            for client in self.clientInformationQueue:
                if client.ID == clientInformation.ID:
                    self.clientInformationQueue.remove(client)
                else:
                    pass
        except AttributeError:
            for client in self.clientInformationQueue:
                if client.ID == clientInformation.clientInformation.ID:
                    self.clientInformationQueue.remove(client)
                else:
                    pass
        except Exception as e:
            self.logger.critical("client removal failed " + str(e))
        try:
            self.ActiveThreadsController.removeClientThread(clientInformation)
            self.dbController.remove_user(
                query=f'uid = "{clientInformation.modelObject.uid}"')
        except Exception as e:
            self.logger.critical(
                'there has been an error in a clients disconnection while adding information to the database '
                + str(e))
            pass
        if hasattr(clientInformation, 'clientInformation'):
            clientInformation = clientInformation.clientInformation
        else:
            pass
        try:
            self.openSockets -= 1
            socketa = clientInformation.socket
            clientInformation.socket = None
            self.clientDataPipe.put(
                ['remove', clientInformation, self.openSockets])
            clientInformation.socket = socketa
            try:
                clientInformation.socket.shutdown(socket.SHUT_RDWR)
            except Exception as e:
                self.logger.error(
                    'error shutting socket down in client disconnection')
                pass
            try:
                clientInformation.socket.close()
            except Exception as e:
                self.logger.error(
                    'error closing socket in client disconnection')
                pass

            self.logger.info(loggingConstants.CLIENTDISCONNECTSTART)
            # TODO: remove string
            tempXml = RawCoT()
            tempXml.xmlString = '<event><detail><link uid="{0}"/></detail></event>'.format(
                clientInformation.modelObject.uid).encode()
            disconnect = SendDisconnectController(tempXml)
            SendDataController().sendDataInQueue(
                disconnect.getObject().clientInformation,
                disconnect.getObject(), self.clientInformationQueue,
                self.CoTSharePipe)
            self.logger.info(
                loggingConstants.CLIENTDISCONNECTEND +
                str(clientInformation.modelObject.detail.contact.callsign))
            return 1
        except Exception as e:
            self.logger.error(loggingConstants.CLIENTCONNECTEDERROR + " " +
                              str(e))
            pass

    def monitorRawCoT(self, data):
        # this needs to be the most robust function as it is the keystone of the program
        # this will attempt to define the type of CoT along with the designated controller
        try:
            if isinstance(data, int):
                return None
            else:
                CoT = XMLCoTController().determineCoTGeneral(data)
                function = getattr(self, CoT[0])
                output = function(CoT[1])
                return output
        except Exception as e:
            self.logger.error(loggingConstants.MONITORRAWCOTERRORB + str(e))
            return -1

    def checkOutput(self, output):
        if output != -1 and output != None:
            return True
        else:
            return False

    def loadAscii(self):
        ascii()

    def mainRunFunction(self,
                        clientData,
                        receiveConnection,
                        sock,
                        pool,
                        event,
                        clientDataPipe,
                        ReceiveConnectionKillSwitch,
                        CoTSharePipe,
                        ssl=False):
        print('server started')
        import datetime
        receiveconntimeoutcount = datetime.datetime.now()
        lastprint = datetime.datetime.now()
        while event.is_set():
            self.CoTSharePipe = CoTSharePipe
            try:
                if ssl == True:
                    pass
                self.clientDataPipe = clientDataPipe
                if event.is_set():
                    try:
                        if ReceiveConnectionKillSwitch.is_set():
                            try:
                                receiveConnection.successful()
                            except:
                                pass
                            ReceiveConnectionKillSwitch.clear()
                            receiveConnection = pool.apply_async(
                                ReceiveConnections().listen, (sock, ))
                        else:
                            receiveConnectionOutput = receiveConnection.get(
                                timeout=0.01)
                            receiveConnection = pool.apply_async(
                                ReceiveConnections().listen, (
                                    sock,
                                    ssl,
                                ))
                            receiveconntimeoutcount = datetime.datetime.now()
                            lastprint = datetime.datetime.now()
                            CoTOutput = self.handel_connection_data(
                                receiveConnectionOutput)

                    except multiprocessing.TimeoutError:

                        if (datetime.datetime.now() - receiveconntimeoutcount
                            ) > datetime.timedelta(seconds=60) and ssl == True:
                            from multiprocessing.pool import ThreadPool
                            try:
                                pass
                                print('\n\nresetting\n\n')
                                pool.terminate()
                                pool = ThreadPool(processes=2)
                                self.pool = pool
                                receiveconntimeoutcount = datetime.datetime.now(
                                )
                                lastprint = datetime.datetime.now()
                                clientData = pool.apply_async(
                                    ClientReceptionHandler().startup,
                                    (self.clientInformationQueue, ))
                                receiveConnection = pool.apply_async(
                                    ReceiveConnections().listen, (
                                        sock,
                                        ssl,
                                    ))
                            except Exception as e:
                                print(str(e))
                        elif ssl == True and (datetime.datetime.now() -
                                              lastprint) > datetime.timedelta(
                                                  seconds=30):
                            print('time since last reset ' +
                                  str(datetime.datetime.now() -
                                      receiveconntimeoutcount))
                            lastprint = datetime.datetime.now()
                        else:
                            pass
                    except Exception as e:
                        self.logger.error(
                            'exception in receive connection within main run function '
                            + str(e))

                    try:
                        clientDataOutput = clientData.get(timeout=0.01)
                        if self.checkOutput(clientDataOutput) and isinstance(
                                clientDataOutput, list):
                            CoTOutput = self.handel_regular_data(
                                clientDataOutput)
                        else:
                            raise Exception(
                                'client reception handler has returned data which is not of type list data is '
                                + str(clientDataOutput))
                        clientData = pool.apply_async(
                            ClientReceptionHandler().startup,
                            (self.clientInformationQueue, ))
                    except multiprocessing.TimeoutError:
                        pass
                    except Exception as e:
                        #self.logger.info('exception in receive client data within main run function ' + str(e))
                        pass
                    try:
                        if not CoTSharePipe.empty():
                            # print('getting share pipe data')
                            data = CoTSharePipe.get()
                            CoTOutput = self.handel_shared_data(data)
                        else:
                            pass
                    except Exception as e:
                        self.logger.error(
                            'there has been an excepion in the handling of data supplied by the rest API '
                            + str(e))
                        pass
                else:
                    self.stop()
                    break
            except Exception as e:
                self.logger.info(
                    'there has been an uncaught error thrown in mainRunFunction'
                    + str(e))
                pass
        self.stop()

    def handel_shared_data(self, modelData):
        try:
            #print('\n \n handling shared data \n \n')
            # print('data received within orchestrator '+str(modelData.xmlString))
            if hasattr(modelData, 'clientInformation'):
                output = SendDataController().sendDataInQueue(
                    modelData.clientInformation, modelData,
                    self.clientInformationQueue)
            #

            elif modelData.type == "connmessage":
                self.internalCoTArray.append(modelData)

            # this runs in the event of a new connection
            else:
                print(modelData)
                output = SendDataController().sendDataInQueue(
                    None, modelData, self.clientInformationQueue)
        except Exception as e:
            self.logger.error("data base connection error " + str(e))
            print(e)

    def handel_regular_data(self, clientDataOutput):
        try:
            for clientDataOutputSingle in clientDataOutput:
                try:
                    print('handling reg data')
                    if clientDataOutputSingle == -1:
                        continue
                    CoTOutput = self.monitorRawCoT(clientDataOutputSingle)
                    if CoTOutput == 1:
                        continue
                    elif self.checkOutput(CoTOutput):
                        output = SendDataController().sendDataInQueue(
                            CoTOutput.clientInformation, CoTOutput,
                            self.clientInformationQueue, self.CoTSharePipe)
                        if self.checkOutput(output) and isinstance(
                                output, tuple) == False:
                            pass
                        elif isinstance(output, tuple):
                            self.logger.error(
                                'issue sending data to client now disconnecting'
                            )
                            self.clientDisconnected(output[1])

                        else:
                            self.logger.error(
                                'send data failed in main run function with data '
                                + str(CoTOutput.xmlString) + ' from client ' +
                                CoTOutput.clientInformation.modelObject.detail.
                                contact.callsign)

                    else:
                        raise Exception('error in general data processing')
                except Exception as e:
                    self.logger.info(
                        'exception in client data, data processing within main run function '
                        + str(e) + ' data is ' + str(CoTOutput))
                    pass
                except Exception as e:
                    self.logger.info(
                        'exception in client data, data processing within main run function '
                        + str(e) + ' data is ' + str(clientDataOutput))
        except Exception as e:
            self.logger.info(
                "there has been an error iterating client data output " +
                str(e))
            return -1
        return 1

    def handel_connection_data(self, receiveConnectionOutput):
        try:
            print('handling conn data')
            if receiveConnectionOutput == -1:
                return None

            CoTOutput = self.monitorRawCoT(receiveConnectionOutput)
            if CoTOutput != -1 and CoTOutput != None:
                output = SendDataController().sendDataInQueue(
                    CoTOutput, CoTOutput, self.clientInformationQueue,
                    self.CoTSharePipe)
                if self.checkOutput(output):
                    self.logger.debug(
                        'connection data from client ' +
                        str(CoTOutput.modelObject.detail.contact.callsign) +
                        ' successfully processed')
                else:
                    raise Exception('error in sending data')
            else:
                pass
        except Exception as e:
            self.logger.error(
                'exception in receive connection data processing within main run function '
                + str(e) + ' data is ' + str(CoTOutput))
            return -1
        self.sendInternalCoT(CoTOutput)
        return 1

    def start(self, IP, CoTPort, Event, clientDataPipe,
              ReceiveConnectionKillSwitch, RestAPIPipe):
        try:
            self.db = sqlite3.connect(DPConst().DATABASE)
            os.chdir('../../../')
            # create socket controller
            self.MainSocketController.changeIP(IP)
            self.MainSocketController.changePort(CoTPort)
            sock = self.MainSocketController.createSocket()
            #changed
            from multiprocessing.pool import ThreadPool
            pool = ThreadPool(processes=2)
            self.pool = pool
            clientData = pool.apply_async(ClientReceptionHandler().startup,
                                          (self.clientInformationQueue, ))
            receiveConnection = pool.apply_async(ReceiveConnections().listen,
                                                 (sock, ))
            # instantiate domain model and save process as object
            self.mainRunFunction(clientData, receiveConnection, sock, pool,
                                 Event, clientDataPipe,
                                 ReceiveConnectionKillSwitch, RestAPIPipe)

        except Exception as e:
            self.logger.critical(
                'there has been a critical error in the startup of FTS' +
                str(e))
            return -1

    def stop(self):
        self.clientDataPipe.close()
        self.pool.terminate()
        self.pool.close()
        self.pool.join()
Ejemplo n.º 16
0
def updatetemplate(checklistid, taskid):
    from flask import request
    from defusedxml import ElementTree as etree
    from FreeTAKServer.controllers.SpecificCoTControllers.SendExcheckUpdateController import SendExcheckUpdateController
    from FreeTAKServer.controllers.XMLCoTController import XMLCoTController
    from FreeTAKServer.model.FTSModel.Event import Event
    from FreeTAKServer.model.RawCoT import RawCoT
    import uuid
    import hashlib

    data = request.data

    xml = etree.parse(
        str(
            PurePath(Path(MainConfig.ExCheckChecklistFilePath),
                     Path(checklistid + '.xml')))).getroot()
    updatedTask = etree.fromstring(data)
    tasks = xml.find('checklistTasks')
    for task in tasks:
        uid = task.find('uid')
        if uid.text == taskid:
            tasks.replace(task, updatedTask)
        else:
            pass
    with open(
            str(
                PurePath(Path(MainConfig.ExCheckChecklistFilePath),
                         Path(checklistid + '.xml'))), 'w+') as file:
        file.write(etree.tostring(xml).decode())
        file.close()

    # Create Object to send to client
    object = Event.ExcheckUpdate()
    object.setuid(str(uuid.uuid4()))
    object.setversion('2.0')
    object.detail.mission.settype("CHANGE")
    object.detail.mission.settool("ExCheck")
    object.detail.mission.setname(checklistid)
    object.detail.mission.setauthorUid(request.args.get("clientUid"))
    object.detail.mission.MissionChanges.MissionChange.creatorUid.setINTAG(
        request.args.get("clientUid"))
    object.detail.mission.MissionChanges.MissionChange.missionName.setINTAG(
        checklistid)
    object.detail.mission.MissionChanges.MissionChange.type.setINTAG(
        "ADD_CONTENT")
    object.detail.mission.MissionChanges.MissionChange.contentResource.filename.setINTAG(
        taskid + '.xml')
    object.detail.mission.MissionChanges.MissionChange.contentResource.hash.setINTAG(
        str(
            hashlib.sha256(
                str(
                    open(
                        MainConfig.ExCheckChecklistFilePath + '/' +
                        checklistid + '.xml', 'r')).encode()).hexdigest()))
    object.detail.mission.MissionChanges.MissionChange.contentResource.keywords.setINTAG(
        'Task')
    object.detail.mission.MissionChanges.MissionChange.contentResource.name.setINTAG(
        taskid)
    object.detail.mission.MissionChanges.MissionChange.contentResource.size.setINTAG(
        str(len(data)))
    #TODO: change this value
    object.detail.mission.MissionChanges.MissionChange.contentResource.submitter.setINTAG(
        'atak')
    object.detail.mission.MissionChanges.MissionChange.contentResource.uid.setINTAG(
        taskid)
    '''object = etree.fromstring(templateex)
    object.uid = uuid.uuid4()
    object.find('detail').find('mission').type= "CHANGE"
    object.find('detail').find('mission').name = taskid
    object.find('detail').find('mission').Uid = request.args.get("clientUid")
    object.find('detail').find('mission').find('MissionChanges').find('MissionChange').find('creatorUid').text = request.args.get("clientUid")
    object.find('detail').find('mission').find('MissionChanges').find('MissionChange').find('missionName').text = taskid
    object.find('detail').find('mission').find('MissionChanges').find('MissionChange').find('filename').text = checklistid+'.xml'
    object.detail.mission.MissionChanges.MissionChange.contentResource.hash.setINTAG(str(hashlib.sha256(str(data).encode()).hexdigest()))
    object.detail.mission.MissionChanges.MissionChange.contentResource.keywords.setINTAG('Task')
    object.detail.mission.MissionChanges.MissionChange.contentResource.name.setINTAG(checklistid)
    object.detail.mission.MissionChanges.MissionChange.contentResource.size.setINTAG(str(len(data)))
    #TODO: change this value
    object.detail.mission.MissionChanges.MissionChange.contentResource.submitter.setINTAG('test')
    object.detail.mission.MissionChanges.MissionChange.contentResource.uid.setINTAG(checklistid)'''
    rawcot = RawCoT()
    xml = XMLCoTController().serialize_model_to_CoT(object)
    rawcot.xmlString = xml

    PIPE.put(rawcot)
    #PIPE.send()

    return '', 200
Ejemplo n.º 17
0
                    self.setdest()
                    returnvalue = self.dest[self.__index]
                    return returnvalue
                else:
                    self.__index = 0
                    return self.dest[self.__index]
            except IndexError:
                self.__index = 0
                return self.dest[self.__index]

    def setdest(self, Dest=None):
        if not Dest:
            Dest = DestObject(self.__index)
        self.dest.append(Dest)
        self.__index += 1


if __name__ == "__main__":
    from FreeTAKServer.controllers.XMLCoTController import XMLCoTController
    from lxml import etree
    a = Marti.other()
    b = etree.fromstring(b'<marti><dest callsign = "bbbb"/></marti>')
    x = XMLCoTController().serialize_CoT_to_model(a, b)
    y = x.getdest().callsign
    print(a.__dict__)
    M = DestObject()
    M.setcallsign('13243432w')
    a.setdest(M)
    for x in a.dest:
        f = a.getdest()
    print('done')