Skip to content

Commit

Permalink
PeerConnection is able to time out.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasnick committed May 6, 2014
1 parent a9b892c commit 0975249
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions node/p2p.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@
# Connection to one peer
class PeerConnection(object):
def __init__(self, address):
# timeout in seconds
self._timeout = 10
self._address = address

def create_socket(self):
self._ctx = zmq.Context()
self._socket = self._ctx.socket(zmq.REQ)
self._socket.setsockopt(zmq.LINGER, 0)
self._socket.connect(self._address)

def cleanup_socket(self):
Expand All @@ -50,8 +53,14 @@ def _send_raw(self, serialized):
self.create_socket()

self._socket.send(serialized)
msg = self._socket.recv()
self.on_message(msg)

poller = zmq.Poller()
poller.register(self._socket, zmq.POLLIN)
if poller.poll(self._timeout * 1000):
msg = self._socket.recv()
self.on_message(msg)
else:
print "Peer " + self._address + " timed out."

self.cleanup_socket()

Expand Down

0 comments on commit 0975249

Please sign in to comment.