Example #1
0
    def POISocketSender(self):
        try:
            print "\tin POISocketSender"
            messageNum = 1

            while self.close == False:
                while self.sendingData != True and self.close == False:
                    time.sleep(0.1)

                while self.sendingData == True and self.close == False:
                    # TODO only sent requested types
                    syncTime = time.time()
                    POIToSend = [(uid, point.toDict()) for uid, point in POIelements.items() if point.getTimestamp() >= self.lastSync] # and point.type in self.overlays]
                    jsonPOI = json.dumps({"POI":dict(POIToSend)})

                    print "\tSS message #{0} : {1} ({2} points, connection {3}, lastSync {4})".format(
                        messageNum, "not shown", len(POIToSend), self.connectionNumber, self.lastSync)

                    self.lastSync = syncTime
                    self.clientsocket.sendall(jsonPOI + "\n")

                    messageNum += 1
                    time.sleep(3.875 + (random.random() / 4.0)) # 4 second average pause


        except KeyboardInterrupt:
            print "\tPSS broke by KeyboardInterrupt (%d)" %(self.connectionNumber)
        except Exception as e:
            print "\tPSS broke by some random exception or interrupt (%d)" %(self.connectionNumber)
            print e
Example #2
0
    def POISocketListener(self):
        try:
            print "\tin POISocketListener"
            while True:
                data = self.clientsocket.recv(BUFFERSIZE)
                if not data:
                    print "\n\tPSL Data empty - closing %d" %(self.connectionNumber)
                    break

                print "PSL %d received data: \"%s\"" %(self.connectionNumber, data)
                for tempData in data.split("\n"):
                    if tempData == "":
                        continue

                    if tempData == "close":
                        self.close = True
                        print "\tPSL exiting from stopPOI (%d)" %(self.connectionNumber)
                        return

                    if tempData == "sendPOI":
                        self.sendingData = True
                        continue

                    if tempData == "stopPOI":
                        self.sendingData = False
                        continue

                    if tempData.startswith("addOverlay:") or tempData.startswith("removeOverlay:"):
                        # partition returns 3-tuple (before, seperator, after)
                        action, sep, overlayName = tempData.partition(":")

                        if action.startswith("add"):
                            self.overlays.add(overlayName)
                        else:
                            # remove throws error if not present, discard does not
                            self.overlays.discard(overlayName)    

                        # resync all points (to force new overlay points sync)
                        self.lastSync = -1.0
                        continue

                    if tempData.startswith("addPoint:") or tempData.startswith("removePoint:"):
                        # partition returns 3-tuple (before, seperator, after)
                        action, sep, point = tempData.partition(":")
                        try:
                            tempPOI = poi.POI(point)

                            if action.startswith("add"):
                                tempPOI.setUID(nextUIDgenerator.next())
                                POIelements[tempPOI.getUID()] = tempPOI
                                print "\tadded:", tempPOI, "as UID:", tempPOI.getUID()
                            else:
                                removed = POIelements.pop(tempPOI.getUID(), "not present")
                                if removed != "not present":
                                    print "\tremoved POI with UID:", tempPOI.getUID()
                                else:
                                    print "\tfailed to remove POI:", tempPOI, POIelements.keys()

                        except Exception as e:
                            print "\tPSL failed to parse data: ", e
                        continue

        except KeyboardInterrupt:
            print "\tPSL broke by KeyboardInterrupt (%d)" %(self.connectionNumber)
        except Exception as e:
            print "\tPSL broke by some random exception or interrupt (%d)" %(self.connectionNumber)
            print e