Esempio n. 1
0
 def readpackettime(self, client, basetime):
     ret_msg = None
     # print("Going for timing")
     while ret_msg is None and util.gettimestamp() < (basetime + 60000):
         ret_msg = client.readpacket()
     endtime = util.gettimestamp()
     delaytime = int((endtime / 10 - basetime / 10))
     #print("Response time is " + str(delaytime) + "ms")
     self.assertTrue(delaytime < 600, "Response time is too long")
     return ret_msg
Esempio n. 2
0
    def test_timestamp(self):
        tmstmp = util.gettimestamp()

        # assert that it makes sense
        self.assertTrue(tmstmp > 0, "timestamp less than 0")

        # check accuracy - 1ms accuracy required
        # do many times and check accuracy is less than 2.0ms
        for i in range(0, 100):
            tsOne = util.gettimestamp()
            time.sleep(0.001)
            tsTwo = util.gettimestamp()
            self.assertTrue((tsTwo - tsOne) < 20, "Time accuracy wrong")
Esempio n. 3
0
    def pinginterfaces(self):
        """Run a server ping on all ifaces. Also functions as a status/heartbeat
        checker. Returns initial list of all clients, then the ping responses"""
        retping = []
        alldevices = []
        if self.pingbasetime.value == 0:
            self.pingbasetime.value = util.gettimestamp()
        else:
            print("Ping already in process")

        # wait 500ms for ping returns
        for i in range(0, 500):
            time.sleep(0.001)
            if not self.ping_q.empty():
                resp = self.ping_q.get()
                if len(resp.split(',')) == 2:
                    alldevices.append(resp)
                else:
                    retping.append(resp)

        #figure out if any devices did not respond and put them as -1 in the ping list
        for ret in alldevices:
            i_d, iface = ret.split(',')
            found = False
            for pret in retping:
                if i_d + ',' + iface in pret:
                    found = True
            #not found
            if found == False:
                retping.append(i_d + ',' + iface + ',-1')

        # print("End ping")
        self.pingbasetime.value = 0
        self.pinginprogress.value = 0
        return retping
Esempio n. 4
0
    def test_twoclientssinglepacket(self):
        # encode
        network_id = os.urandom(32)
        device_idUAS = random.randint(1, 31)
        device_idGCS = random.randint(32, 63)
        msgUAS = self.makePacketrnd(network_id, device_idUAS)
        msgGCS = self.makePacketrnd(network_id, device_idGCS)

        # send initial packets
        self.clientUAS.writepacket(
            self.makePacketrnd(network_id, -device_idUAS, b"CL_BEGIN"))
        self.clientGCS.writepacket(
            self.makePacketrnd(network_id, -device_idGCS, b"CL_BEGIN"))

        # send data packets
        basetime = util.gettimestamp()
        self.clientUAS.writepacket(msgUAS)
        self.clientGCS.writepacket(msgGCS)
        time.sleep(0.001)

        # rx packets, with performance counters
        retmsgGCS = self.readpackettime(self.clientGCS, basetime)
        retmsgUAS = self.readpackettime(self.clientUAS, basetime)

        # send end packets
        self.clientUAS.writepacket(
            self.makePacketrnd(network_id, -device_idUAS, b"CL_END"))
        self.clientGCS.writepacket(
            self.makePacketrnd(network_id, -device_idGCS, b"CL_END"))
        time.sleep(0.001)

        # and assert
        self.assertEqual(msgUAS, retmsgGCS, "Message UAS -> GCS incorrect")
        self.assertEqual(msgGCS, retmsgUAS, "Message GCS -> UAS incorrect")
Esempio n. 5
0
    def test_serverping(self):
        # encode
        network_id = os.urandom(32)
        device_idUAS = random.randint(1, 31)

        # send initial packets
        self.clientUAS.writepacket(
            self.makePacketrnd(network_id, -device_idUAS, b'CL_BEGIN'))

        # send ping packet
        pingpkt = self.makePacketrnd(network_id, -device_idUAS, b'CL_SVRPING')
        starttime = util.gettimestamp()
        self.clientUAS.writepacket(pingpkt)
        time.sleep(0.001)
        retping = self.readpackettime(self.clientUAS, starttime)

        # send end packets
        self.clientUAS.writepacket(
            self.makePacketrnd(network_id, -device_idUAS, b'CL_END'))
        time.sleep(0.001)
        self.assertEqual(retping, pingpkt, "MessageP GCS -> GCS incorr")
Esempio n. 6
0
    def run(self):
        serversocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        serversocket.setblocking(False)
        serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        #serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 10)
        #serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 10)
        serversocket.bind(self.localaddport)
        udpclients = []
        udpclientdevid = []
        lastrxseq = -1
        seq_no = 0

        # print("Started")
        while self.endloop.value == 0:
            # sync with any changes to the client interfaces
            (udpclients,
             udpclientdevid) = self.sync_ifaces(udpclients, udpclientdevid,
                                                seq_no)
            # send out a server ping if required (and return list of all current clients)
            if self.pingbasetime.value != 0 and self.pinginprogress.value == 0:
                seq_no = self.sendallClients(udpclients, udpclientdevid, -1,
                                             seq_no, b'CL_SVRPING')
                self.pinginprogress.value = 1
                # print("Sending ping " + str(seq_no))
                for i in range(0, len(udpclients)):
                    self.ping_q.put(
                        str(udpclientdevid[i]) + "," +
                        str(udpclients[i].getiface()))

            ready = select.select([serversocket], [], [], 0.0001)
            # if there is data from the local client, send it to the remote
            # only read up to the max payload size though
            if ready[0]:
                data, address = serversocket.recvfrom(
                    self.pkt.maxPayloadSize())

                # send to the all the clients...
                # print("Sending data")
                seq_no = self.sendallClients(udpclients, udpclientdevid, 1,
                                             seq_no, data)

            # poll the clients for any new data
            for i in range(0, len(udpclients)):
                # if client has data, compare to latest timestamp
                datarx = udpclients[i].readpacket()
                if datarx:
                    dec_msg = self.pkt.recoverpacket(datarx)
                    # check if it's a returned ping packet
                    if dec_msg.Payload == b'CL_SVRPING' and dec_msg.DeviceID < 0 and self.pinginprogress.value == 1:
                        deltatime = int(
                            (util.gettimestamp() - self.pingbasetime.value) /
                            10)
                        self.ping_q.put(
                            str(udpclientdevid[i]) + "," +
                            str(udpclients[i].getiface()) + "," +
                            str(deltatime))

                    # print("Got packet from serverhub["+ str(i) +"][" +
                    #      str(dec_msg.DeviceID) + "] " +
                    #      str(dec_msg.Timestamp) + ", " + str(lastrxtime))
                    # if this data is newer, send to local client and
                    # update the timestamp
                    # need to ensure it's a new packet and we have a
                    # connected local client
                    if dec_msg.Sequence > lastrxseq and (
                            'address' in locals() or 'address' in globals()):
                        # print("rx packet")
                        lastrxseq = dec_msg.Sequence
                        serversocket.sendto(dec_msg.Payload, address)
        # close the socket on end
        if serversocket is not None:
            # send ending packets
            seq_no = self.sendallClients(udpclients, udpclientdevid, -1,
                                         seq_no, b'CL_END')
            # close the sockets - just remove the first item n times
            # (array reindexes each time)
            for i in range(0, len(udpclients)):
                udpclients[0].close()
                time.sleep(0.001)
                udpclients.remove(udpclients[0])
                udpclientdevid.remove(udpclientdevid[0])
            serversocket.close()