Skip to content

Commit

Permalink
Merge pull request #1 from RoadRunnr/master
Browse files Browse the repository at this point in the history
update packet endoding to ZRE 2
  • Loading branch information
sphaero committed Oct 24, 2014
2 parents 0361a05 + db0c86a commit dba5278
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
2 changes: 1 addition & 1 deletion pyre.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ def recv_api(self):
msg.set_group(grpname)
self.status += 1
msg.set_status(self.status)
for peer in self.peers:
for peer in self.peers.values():
peer.send(msg)
self.own_groups.pop(grpname)
print("Node is leaving group %s" % grpname)
Expand Down
51 changes: 30 additions & 21 deletions zre_msg.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ def recv(self, input_socket):
self.id = self._get_number1();
#print("ZreMsg id: %i" % self.id)

version = self._get_number1()
if version != 2:
logger.debug("Invalid version {0}".format(version))
return None

if self.id == ZreMsg.HELLO:
self.unpack_hello()
elif self.id == ZreMsg.WHISPER:
Expand Down Expand Up @@ -125,6 +130,9 @@ def send(self, output_socket):
#print("ZreMsg: ", self.id)
self._put_number1(self.id)
#print(self.struct_data)
# add version
self._put_number1(2)

if self.id == ZreMsg.HELLO:
self.pack_hello()
elif self.id == ZreMsg.WHISPER:
Expand Down Expand Up @@ -339,10 +347,6 @@ def _put_long_string(self, s):
d = struct.pack('%is' %len(s), s.encode('UTF-8'))
self.struct_data += d

def _zre_dictstring_to_dict(self, s):
l = s.split("=")
return { l[0]: l[1] }

def unpack_hello(self):
"""unpack a zre hello packet
Expand Down Expand Up @@ -370,11 +374,12 @@ def unpack_hello(self):
#print("post_group: needle is at: %i"% self._needle )
self.status = self._get_number1()
self.name = self._get_string()
headers_len = self._get_number1()
headers_len = self._get_number4()
self.headers = {}
for x in range(headers_len):
hdr_item = self._get_string()
self.headers.update(self._zre_dictstring_to_dict(hdr_item))
key = self._get_string()
val = self._get_long_string()
self.headers.update({key: val})
#import ast
#for hdr in hdrlist:
# # TODO: safer to use ast.literal_eval
Expand Down Expand Up @@ -405,22 +410,27 @@ def pack_hello(self):
self._put_string(self.name)
self._put_number4(len(self.headers))
for key, val in self.headers.items():
self._put_long_string("%s=%s" %(key, val))
self._put_string(key)
self._put_long_string(val)

if __name__ == '__main__':
testdata = struct.pack('Hb3sHbb2sb2sb2sbbb3sb3s',
11, # sequence
3, # str length
testdata = struct.pack('>Hb9sII2sI2sI2sbb4sIb1sI1sb1sI1s',
11, # sequence
9, # str length
b"192:20123", # endpoint
3, # groups len
2,b"g1", # length + groupname
2,b"g2", # length + groupname
2,b"g3", # length + groupname
4, # status
2, # header len
3,b"a=z", # length + dict
3,b"b=x" # length + dict
)
3, # groups len
2, b"g1", # length + groupname
2, b"g2", # length + groupname
2, b"g3", # length + groupname
4, # status
4, b"NAME", # name
2, # header len
1, b"a", # length + dict
1, b"z", # length + dict
1, b"b", # length + dict
1, b"b" # length + dict
)

print("New ZRE HELLO message")
m = ZreMsg(ZreMsg.HELLO, data=testdata)
print("Unpack a HELLO message")
Expand All @@ -429,4 +439,3 @@ def pack_hello(self):
m.pack_hello()
print("Unpack the packed HELLO message")
m.unpack_hello()

0 comments on commit dba5278

Please sign in to comment.