-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayClient.py
More file actions
71 lines (56 loc) · 2 KB
/
ArrayClient.py
File metadata and controls
71 lines (56 loc) · 2 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Camera data client, request CCD shots and get data via ZMQ
# Based on lazy pirate pattern
import zmq
import numpy
from pylab import plot, show
#TODO handle keyboard interrupt correctly
def recv_array(socket, flags=0, copy=False, track=False):
"""recv a numpy array"""
md = socket.recv_json(flags=flags)
msg = socket.recv(flags=flags, copy=copy, track=track)
buf = buffer(msg)
A = numpy.frombuffer(buf, dtype=md['dtype'])
return A.reshape(md['shape'])
REQUEST_TIMEOUT = 2500
REQUEST_RETRIES = 3
SERVER_ENDPOINT = "tcp://localhost:5555"
context = zmq.Context()
# Socket to talk to server
print "Connecting to hello world server..."
client = context.socket(zmq.REQ)
client.connect(SERVER_ENDPOINT)
poll = zmq.Poller()
poll.register(client, zmq.POLLIN)
retries_left = REQUEST_RETRIES
while retries_left:
shots_requested = 1
request = str(shots_requested) # ask for one shot of data
print "I: Sending (%s)" % request
client.send(request)
expect_reply = True
while expect_reply:
socks = dict(poll.poll(REQUEST_TIMEOUT))
if socks.get(client) == zmq.POLLIN:
data_array = recv_array(client)
print "I: Server replied OK (%s)" % len(data_array)
retries_left = REQUEST_RETRIES
expect_reply = False
plot(data_array[200,:,0])
show()
else:
print "W: No response from server, retrying..."
# Socket is confused. Close and remove it.
client.setsockopt(zmq.LINGER, 0)
client.close()
poll.unregister(client)
retries_left -= 1
if retries_left == 0:
print "E: Server seems to be offline, abandoning"
break
print "I: Reconnecting and resending (%s)" % request
# Create new connection
client = context.socket(zmq.REQ)
client.connect(SERVER_ENDPOINT)
poll.register(client, zmq.POLLIN)
client.send(request)
context.term()