11 from collections
import namedtuple
13 log = logging.getLogger(__name__)
14 PeerInfo = namedtuple(
"PeerInfo",
"uuid nick headers")
18 Peer at the network to discover others and advertise self. 20 This is equivalent to C++ zio::Peer and is a wrapper around Pyre, 26 Create a peer with a nickname. 28 Extra headers may be given as a dictionary. 30 self.
zyre = pyre.Pyre(nickname)
33 for k,v
in headers.items():
35 self.
zyre.set_header(k,v)
38 self.
poller.register(self.
zyre.socket(), zmq.POLLIN)
47 This MUST be called or the application will hang. 49 if hasattr(self,
"zyre"):
56 Poll the network for an update. 58 Return True if an event was received. True is returned on 59 reception of any type of Zyre event. Use timeout in msec to 63 if not self.
zyre.socket()
in which:
65 msg = self.
zyre.recv()
66 if log.isEnabledFor(logging.DEBUG):
67 log.debug(
"[peer %s]: zyre message:" %(self.
zyre.
name(),))
68 for ind, part
in enumerate(msg):
70 log.debug(
" part %d: %s" %(ind, part))
72 log.debug(
" part %d: (long %d bytes)" %(ind, len(part)))
73 if msg[0] == b
'ENTER':
74 uid = uuid.UUID(bytes=msg[1])
75 nick = msg[2].decode(
'utf-8')
76 headers = json.loads(msg[3].decode(
'utf-8'))
79 uid = uuid.UUID(bytes=msg[1])
80 if self.
peers.get(uid,
None):
88 Continually poll until all queued Zyre events are processed. 95 Return UUIDs of all peers with matching nicks 98 for uid,pi
in self.
peers.items():
106 Wait for at least one peer with the nickname to be. 108 Return a list of UUIDs of peers discovered to have this 111 t0ms = 1000*time.time()
117 nowms = 1000*time.time()
119 timeout = timeout - (nowms-t0ms)
122 log.debug(
"[peer %s]: wait for %s (timeout=%d)" % \
123 (self.
zyre.
name(), nickname, timeout))
130 Wait as set of nicks have been seen or until timeout. 132 Return a peer dictionary holding last seen info about peers 137 until = time.time() + 1e-3*timeout
143 for pi
in self.
peers.values():
146 know = set([pi.nick
for pi
in seen.values()])
150 timeout = int(1000*(until - time.time()))
153 print (
"seen:",
' '.join(know))
def waitfor(self, nickname, timeout=-1)
def matchnick(self, nick)
def __init__(self, nickname, headers)
def party(self, nicks, timeout=-1)
def poll(self, timeout=0)