def sender(self, socket): logger.debug('Waiting for events') # subscribe self.eventbus.subscribe("core") self.eventbus.subscribe("plugin") # listen for events for event in self.eventbus.listen(): logger.debug("Event received: " + event.to_json()) if(event.name == "device.push"): packet = Packet() packet.dst = 25 packet.src = 1 packet.cmd = event.name packet.data = event.data #logger.debug("packet to send: " + packet.to_json()) addr = self.cache.get(packet.dst) if(addr == None): logger.error("Address not found for device: " + str(packet.dst)) continue address = addr.split(":") #address = ["192.168.1.1", 4444] # send packet socket.sendto(packet.to_json(), (address[0], int(address[1]))) logger.debug("Packet sent: " + packet.to_json() + " to: " + str(address)) continue
def main(): # create udp socket sock = socket(AF_INET,SOCK_DGRAM) #sock.bind(("0.0.0.0", SERVER_PORT)) # send messages to server for i in range(0, 10): packet = Packet() packet.src = 25 packet.dst = 1 packet.cmd = "device.update" packet.seq = i packet.data = {"sys.banner":"hello world", "io.counter": i} sock.sendto(packet.to_json(), (SERVER_IP, SERVER_PORT)) print "Packet sent: " + packet.to_json() #time.sleep(0.001) print("Listening for packets...") counter = 0 while True: # blocking read datagram, address = sock.recvfrom(1024) # buffer size is 1024 bytes counter = counter + 1 print("Packet received: " + str(datagram) + " ip: " + str(address) + " c: " + str(counter))