Guest User

heaven.py

a guest
Mar 22nd, 2013
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. # Program to see if there is a live connection with earth
  2. from datetime import datetime, timedelta
  3. import sys
  4. import zmq
  5. context = zmq.Context()
  6.  
  7. import infoPack_pb2
  8.  
  9. # Set up subscriber connection to receive messages from the receiveBroker
  10. subscriber = context.socket(zmq.SUB)
  11. subscriber.connect('tcp://localhost:12345')
  12. subscriber.setsockopt(zmq.SUBSCRIBE, '')
  13.  
  14. # Set up publisher connection to send messages to the sendBroker
  15. publisher = context.socket(zmq.PUB)
  16. publisher.bind('tcp://*:54321')
  17.  
  18. # Initialise poll set
  19. poller = zmq.Poller()
  20. poller.register(subscriber, zmq.POLLIN)
  21.  
  22. handshake = False
  23. print('\nWait for handshake from earth')
  24.  
  25. while True:
  26. events = dict(poller.poll(2000))
  27. if subscriber in events and events[subscriber] == zmq.POLLIN:
  28. message = subscriber.recv()
  29. beat = infoPack_pb2.heartBeat()
  30. beat.ParseFromString(message)
  31. if beat.heartBeat == 1:
  32. publisher.send(message)
  33. if handshake == False:
  34. handshake = True
  35. print('Handshake received from earth')
  36. sys.stdout.write('.')
  37. sys.stdout.flush()
  38. if not events:
  39. if handshake != False:
  40. print('The Earth is silent')
Advertisement
Add Comment
Please, Sign In to add comment