Guest User

Untitled

a guest
Sep 19th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. #send message to 6777
  4. #receive message from 8888
  5. #default host = local host = 127.0.0.1
  6.  
  7. import threading, socket, Queue , pprint, msgpack
  8.  
  9. pp = pprint.PrettyPrinter(indent=4)
  10.  
  11. class Sender:
  12. def __init__(self, UDP_IP_ADDRESS="127.0.0.1", UDP_PORT_NO=6777):
  13. self.UDP_IP_ADDRESS = UDP_IP_ADDRESS
  14. self.UDP_PORT_NO = UDP_PORT_NO
  15. self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  16. self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  17.  
  18. def sendMessage(self, message):
  19. self._socket.sendto(message, (self.UDP_IP_ADDRESS, self.UDP_PORT_NO))
  20.  
  21. class Receiver(threading.Thread):
  22. def __init__(self, UDP_IP_ADDRESS="127.0.0.1", UDP_PORT_NO=8888):
  23. self.result = None
  24. self.UDP_IP_ADDRESS = UDP_IP_ADDRESS
  25. self.UDP_PORT_NO = UDP_PORT_NO
  26. self.messages = Queue.Queue(64)
  27. threading.Thread.__init__(self)
  28.  
  29. def run(self):
  30. self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  31. self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  32. self._socket.bind((self.UDP_IP_ADDRESS, self.UDP_PORT_NO))
  33. while True:
  34. response = self._socket.recv(2048)
  35. # self.messages.put(msgpack.unpackb(response), True)
  36. pp.pprint(response)
  37. # pp.pprint(self.getNewMessage())
  38.  
  39. def getNewMessage(self):
  40. return self.messages.get_nowait() if not self.messages.empty() else None
  41.  
  42.  
  43. def main():
  44. # sends messages from application to pipeline
  45. def producer():
  46. sender = Sender()
  47. while True:
  48. # does not get the correct data
  49. sender.sendMessage(b'\x01'+b'\x08') # messageHeader=1, messageType=8 for imu batch
  50.  
  51. # receives messages from pipeline to application
  52. def consumer():
  53. thread = Receiver()
  54. thread.start()
  55.  
  56. cons_thread = threading.Thread(target=consumer)
  57. prod_thread = threading.Thread(target=producer)
  58.  
  59. cons_thread.start()
  60. prod_thread.start()
  61.  
  62. cons_thread.join()
  63. prod_thread.join()
  64.  
  65. if __name__ == '__main__':
  66. main()
Add Comment
Please, Sign In to add comment