Show More
@@ -1,56 +1,57 b'' | |||||
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 os |
|
15 | import os | |
16 | import socket |
|
16 | import socket | |
17 | import sys |
|
17 | import sys | |
18 | from threading import Thread |
|
18 | from threading import Thread | |
19 |
|
19 | |||
20 | import zmq |
|
20 | import zmq | |
21 |
|
21 | |||
22 | from IPython.utils.localinterfaces import LOCALHOST |
|
22 | from IPython.utils.localinterfaces import LOCALHOST | |
23 |
|
23 | |||
24 | #----------------------------------------------------------------------------- |
|
24 | #----------------------------------------------------------------------------- | |
25 | # Code |
|
25 | # Code | |
26 | #----------------------------------------------------------------------------- |
|
26 | #----------------------------------------------------------------------------- | |
27 |
|
27 | |||
28 |
|
28 | |||
29 | class Heartbeat(Thread): |
|
29 | class Heartbeat(Thread): | |
30 | "A simple ping-pong style heartbeat that runs in a thread." |
|
30 | "A simple ping-pong style heartbeat that runs in a thread." | |
31 |
|
31 | |||
32 | def __init__(self, context, addr=('tcp', LOCALHOST, 0)): |
|
32 | def __init__(self, context, addr=('tcp', LOCALHOST, 0)): | |
33 | Thread.__init__(self) |
|
33 | Thread.__init__(self) | |
34 | self.context = context |
|
34 | self.context = context | |
35 | self.transport, self.ip, self.port = addr |
|
35 | self.transport, self.ip, self.port = addr | |
36 | if self.port == 0: |
|
36 | if self.port == 0: | |
37 | if addr[0] == 'tcp': |
|
37 | if addr[0] == 'tcp': | |
38 | s = socket.socket() |
|
38 | s = socket.socket() | |
39 | # '*' means all interfaces to 0MQ, which is '' to socket.socket |
|
39 | # '*' means all interfaces to 0MQ, which is '' to socket.socket | |
40 | s.bind(('' if self.ip == '*' else self.ip, 0)) |
|
40 | s.bind(('' if self.ip == '*' else self.ip, 0)) | |
41 | self.port = s.getsockname()[1] |
|
41 | self.port = s.getsockname()[1] | |
42 | s.close() |
|
42 | s.close() | |
43 | elif addr[0] == 'ipc': |
|
43 | elif addr[0] == 'ipc': | |
44 | while os.path.exists(self.ip + '-' + self.port): |
|
44 | self.port = 1 | |
|
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 | zmq.device(zmq.FORWARDER, self.socket, self.socket) | |
56 |
|
57 |
General Comments 0
You need to be logged in to leave comments.
Login now