-
Notifications
You must be signed in to change notification settings - Fork 0
/
contact.py
47 lines (35 loc) · 1.34 KB
/
contact.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python
from keyspace import *
class NetworkError(StandardError):
pass
class Contact(object):
def __init__(self, ring_id=1, id=None, ip=None, port=None, network_protocol=None):
self.ring_id = ring_id
self.id = id if id else random_key()
self.ip = ip
self.port = port
self.network_protocol = network_protocol
def __hash__(self):
return hash((self.ring_id, self.id))
def __eq__(self, other):
if not isinstance(other, Contact):
return False
return (self.ring_id == other.ring_id and\
self.id == other.id and\
self.ip == other.ip and\
self.port == other.port)
def __str__(self):
return '%s:%s' % (self.ip, self.port)
def to_tuple(self):
return (self.ring_id, self.id, self.ip, self.port)
@classmethod
def from_tuple(cls, the_tuple, network_protocol=None):
return cls(ring_id=the_tuple[0], id=the_tuple[1], ip=the_tuple[2], port=the_tuple[3], network_protocol=network_protocol)
def send(self, obj):
if not self.network_protocol:
raise NetworkError('no bound network protocol object')
# embed local (id, ring_id) in every message
obj.id = self.network_protocol.id
obj.ring_id = self.network_protocol.ring_id
# send message to remote (ip, port)
self.network_protocol.send_obj(self.ip, self.port, obj)