-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from papr/master
Problem: Pyre does not implement zyre_event
- Loading branch information
Showing
3 changed files
with
64 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |