-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfield_device_server.py
More file actions
executable file
·125 lines (99 loc) · 4.55 KB
/
Copy pathinfield_device_server.py
File metadata and controls
executable file
·125 lines (99 loc) · 4.55 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
"""
A TCP/IP interface to any Infield Devices.
Philip Blackwell 2009-09-25
"""
import logging
import re
import socket
from Almada.experiment.ground_truth import GroundTruthAction
from Almada.experiment.sound import action_sounds
from Almada.config import DEFAULT_IFD_PORT
class InfieldDeviceServer(object):
"""
A TCP/IP interface to any Infield Devices.
"""
def __init__(self, experiment, anchors, tag_ids, reference_points, sequence=None, port=DEFAULT_IFD_PORT):
"""
"""
super(InfieldDeviceServer, self).__init__()
self.experiment = experiment
self.anchors = anchors
self.tag_ids = tag_ids
self.reference_points = reference_points
self.sequence = sequence
self.port = port
self.clients = []
print self.reference_points
print self.sequence
def connect(self):
"Bind the TCP socket. Start listening for Infield Devices"
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind(('', self.port))
self.socket.listen(10) # 10 queued connections
def service_client_request(self, request, client):
"""
Service a request from an Infield Device.
"""
logging.info("IFD: Request: %s" % request)
if request.lower() == "tags":
tag_ids = "Tag IDs:"
for tag_id in self.tag_ids:
tag_ids += " %d" % tag_id
logging.info("Sending tags response: %s" % tag_ids)
client.send("%s\r\n" % tag_ids)
if request.lower() == "reference":
if self.sequence:
reference_point_labels = self.sequence
else:
reference_point_labels = self.reference_points.keys()
reference_point_labels.sort()
response = "Reference Points: " + " ".join(reference_point_labels)
logging.info("Sending reference points response: %s" % response)
client.send("%s\r\n" % response)
arrived_re = re.compile("Tag ([0-9]+) Arrived at Reference (.*)")
passed_re = re.compile("Tag ([0-9]+) Passed Reference (.*)")
heading_re = re.compile("Tag ([0-9]+) Left Reference (.*)")
abandoned_re = re.compile("Tag ([0-9]+) Abandoned Reference (.*)")
regular_expressions = {GroundTruthAction.arrived_code: arrived_re,
GroundTruthAction.passed_code: passed_re,
GroundTruthAction.heading_code: heading_re,
GroundTruthAction.abandoned_code: abandoned_re}
def service_client_command(self, command, client):
"""
Service a command. That is a line ending in a full stop.
"""
logging.info("IFD: Command: %s" % command)
# Try this command against regexps for all actions, it should match exactly one.
matches = [(regexp.match(command), action) for action, regexp in self.regular_expressions.iteritems()]
matches = filter(lambda x: x[0], matches) # Remove any non-matches
if len(matches) != 1:
logging.error("IFD: Unexpected command from client (matched %d): %s" % (len(matches), command))
logging.error("IDF: Matches: %s" % str(matches))
return
match, action = matches[0]
action_sounds[action].play()
tag_id, reference_name = match.groups()
tag_id = int(tag_id)
location = self.reference_points[reference_name]
print tag_id, location, action, reference_name
self.experiment.apply_ground_truth_info(tag_id, location, action, reference_name)
def service_client(self, client):
"""Service some activity for this client."""
message = client.recv(1024)
if message:
meat = message.strip()
if meat.endswith("?"):
self.service_client_request(meat[:-1], client)
elif meat.endswith("."):
self.service_client_command(meat[:-1], client)
else:
logging.error("IFD: Unexpected message: %s" % message.strip())
else:
logging.info("IFD: Dropping client connection")
self.clients.remove(client)
def accept_client(self):
"""Accept a new client on the socket."""
conn, addr = self.socket.accept()
self.clients.append(conn)
logging.info("IFD: New client connection: %s" % (str(addr)))