Show More
@@ -1,56 +1,65 | |||||
1 | """The client and server for a basic ping-pong style heartbeat. |
|
1 | """The client and server for a basic ping-pong style heartbeat. | |
2 | """ |
|
2 | """ | |
3 |
|
3 | |||
4 | #----------------------------------------------------------------------------- |
|
4 | #----------------------------------------------------------------------------- | |
5 | # Copyright (C) 2008-2011 The IPython Development Team |
|
5 | # Copyright (C) 2008-2011 The IPython Development Team | |
6 | # |
|
6 | # | |
7 | # Distributed under the terms of the BSD License. The full license is in |
|
7 | # Distributed under the terms of the BSD License. The full license is in | |
8 | # the file COPYING, distributed as part of this software. |
|
8 | # the file COPYING, distributed as part of this software. | |
9 | #----------------------------------------------------------------------------- |
|
9 | #----------------------------------------------------------------------------- | |
10 |
|
10 | |||
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 | # Imports |
|
12 | # Imports | |
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 |
|
14 | |||
|
15 | import errno | |||
15 | import os |
|
16 | import os | |
16 | import socket |
|
17 | import socket | |
17 | from threading import Thread |
|
18 | from threading import Thread | |
18 |
|
19 | |||
19 | import zmq |
|
20 | import zmq | |
20 |
|
21 | |||
21 | from IPython.utils.localinterfaces import LOCALHOST |
|
22 | from IPython.utils.localinterfaces import LOCALHOST | |
22 |
|
23 | |||
23 | #----------------------------------------------------------------------------- |
|
24 | #----------------------------------------------------------------------------- | |
24 | # Code |
|
25 | # Code | |
25 | #----------------------------------------------------------------------------- |
|
26 | #----------------------------------------------------------------------------- | |
26 |
|
27 | |||
27 |
|
28 | |||
28 | class Heartbeat(Thread): |
|
29 | class Heartbeat(Thread): | |
29 | "A simple ping-pong style heartbeat that runs in a thread." |
|
30 | "A simple ping-pong style heartbeat that runs in a thread." | |
30 |
|
31 | |||
31 | def __init__(self, context, addr=('tcp', LOCALHOST, 0)): |
|
32 | def __init__(self, context, addr=('tcp', LOCALHOST, 0)): | |
32 | Thread.__init__(self) |
|
33 | Thread.__init__(self) | |
33 | self.context = context |
|
34 | self.context = context | |
34 | self.transport, self.ip, self.port = addr |
|
35 | self.transport, self.ip, self.port = addr | |
35 | if self.port == 0: |
|
36 | if self.port == 0: | |
36 | if addr[0] == 'tcp': |
|
37 | if addr[0] == 'tcp': | |
37 | s = socket.socket() |
|
38 | s = socket.socket() | |
38 | # '*' means all interfaces to 0MQ, which is '' to socket.socket |
|
39 | # '*' means all interfaces to 0MQ, which is '' to socket.socket | |
39 | s.bind(('' if self.ip == '*' else self.ip, 0)) |
|
40 | s.bind(('' if self.ip == '*' else self.ip, 0)) | |
40 | self.port = s.getsockname()[1] |
|
41 | self.port = s.getsockname()[1] | |
41 | s.close() |
|
42 | s.close() | |
42 | elif addr[0] == 'ipc': |
|
43 | elif addr[0] == 'ipc': | |
43 | self.port = 1 |
|
44 | self.port = 1 | |
44 | while os.path.exists("%s-%s" % (self.ip, self.port)): |
|
45 | while os.path.exists("%s-%s" % (self.ip, self.port)): | |
45 | self.port = self.port + 1 |
|
46 | self.port = self.port + 1 | |
46 | else: |
|
47 | else: | |
47 | raise ValueError("Unrecognized zmq transport: %s" % addr[0]) |
|
48 | raise ValueError("Unrecognized zmq transport: %s" % addr[0]) | |
48 | self.addr = (self.ip, self.port) |
|
49 | self.addr = (self.ip, self.port) | |
49 | self.daemon = True |
|
50 | self.daemon = True | |
50 |
|
51 | |||
51 | def run(self): |
|
52 | def run(self): | |
52 | self.socket = self.context.socket(zmq.REP) |
|
53 | self.socket = self.context.socket(zmq.REP) | |
53 | c = ':' if self.transport == 'tcp' else '-' |
|
54 | c = ':' if self.transport == 'tcp' else '-' | |
54 | self.socket.bind('%s://%s' % (self.transport, self.ip) + c + str(self.port)) |
|
55 | self.socket.bind('%s://%s' % (self.transport, self.ip) + c + str(self.port)) | |
55 | zmq.device(zmq.FORWARDER, self.socket, self.socket) |
|
56 | while True: | |
56 |
|
57 | try: | ||
|
58 | zmq.device(zmq.FORWARDER, self.socket, self.socket) | |||
|
59 | except zmq.ZMQError as e: | |||
|
60 | if e.errno == errno.EINTR: | |||
|
61 | continue | |||
|
62 | else: | |||
|
63 | raise | |||
|
64 | else: | |||
|
65 | break |
General Comments 0
You need to be logged in to leave comments.
Login now