Skip to content

Commit

Permalink
Problem: Pyre does not implement zyre_event
Browse files Browse the repository at this point in the history
Solution: Implement PyreEvent
  • Loading branch information
papr committed Jun 29, 2016
1 parent 4db359a commit 169a30d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
3 changes: 2 additions & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ Contents:
:members:
.. autoclass:: Pyre
:members:

.. autoclass:: PyreEvent
:members:

Indices and tables
==================
Expand Down
1 change: 1 addition & 0 deletions pyre/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
__all__ = ['pyre', 'zbeacon', 'zhelper']

from .pyre import Pyre
from .pyre_event import PyreEvent
61 changes: 61 additions & 0 deletions pyre/pyre_event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import zmq, uuid, json

class PyreEvent(object):
"""Parsing Pyre messages
This class provides a higher-level API to the Pyre.recv() call, by doing
work that you will want to do in many cases, such as unpacking the peer
headers for each ENTER event received.
"""
def __init__(self, node):
"""Constructor, creates a new Pyre event. Receive an event from the Pyre node, wraps Pyre.recv.
Args:
node (Pyre): Pyre node
"""
super(PyreEvent, self).__init__()
try:
incoming = node.recv()
except zmq.ZMQError:
# recv failed.
return None

self.type = incoming.pop(0).decode('utf-8')
self.peer_uuid_bytes = incoming.pop(0)
self.peer_name = incoming.pop(0).decode('utf-8')
self.headers = None
self.peer_addr = None
self.group = None
self.msg = None
if self.type == "ENTER":
self.headers = json.loads(incoming.pop(0).decode('utf-8'))
self.peer_addr = incoming.pop(0).decode('utf-8')
elif self.type == "JOIN" or self.type == "LEAVE":
self.group = incoming.pop(0).decode('utf-8')
elif self.type == "WHISPER":
self.msg = incoming
elif self.type == "SHOUT":
self.group = incoming.pop(0).decode('utf-8')
self.msg = incoming

def header(self,name):
"""Getter for single header values
Args:
name (str): Header name
Returns:
str: Header value
"""
if self.headers and name in self.headers:
return self.headers[name]
return None

@property
def peer_uuid(self):
"""Creates uuid.UUID object
Returns:
TYPE: uuid.UUID
"""
return uuid.UUID(bytes=self.peer_uuid_bytes)

0 comments on commit 169a30d

Please sign in to comment.