-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContactReceiver.py
282 lines (216 loc) · 9.09 KB
/
ContactReceiver.py
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/env python
import rediscontactadder
import argparse
import socket
from threading import Thread, Event
import Queue
from sociopatterns import Sighting, Contact
from sociopatterns import xxtea
import spparser
import struct
import time
import os
class UDPLoader(object):
def __init__(self, address, port=2342, processor=None):
"""
UDP Loader class constructor.
Arguments:
address -- the address of the computer that is receiving packets
(where ContactReceiver.py is executed).
port -- the port to which the UDP packets are sent (default 2342).
processor -- the processor object that is used to
process the packets received.
"""
self.address = address
self.port = port
self.processor = processor
def open(self):
self.sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
self.sock.bind((self.address, self.port))
def close(self):
self.sock.close()
def __iter__(self):
"""
Returns an iterator that runs over a stream of objects
"""
while 1:
pktlen = 32
packet = self.sock.recvfrom(pktlen)
data, addr = packet
station_id = struct.unpack('>L', socket.inet_aton(addr[0]))[0]
payload = data[16:]
obj = self.processor.process(station_id, payload)
if obj is not None:
yield obj
class SPProcessor(object):
def __init__(self, decode=False, xxtea_crypto_key=None,
load_sightings=0, unique_sightings=0, sighting_time_delta=10,
load_contacts=1, unique_contacts=1, contact_time_delta=10,
packet_parser=spparser.PacketParser()):
"""
SocioPatterns packet processor
keyword arguments:
decode -- Boolean flag. If set, the Loader decodes packets
before processing them, using the crypto key specified
with the xxtea_crypto keyword argument.
Otherwise,the packets are assumed to be unencrypted.
xxtea_crypto_key -- XXTEA 128-bit key, represented
as a sequence of four unsigned 32-bit integers.
load_sightings -- Boolean flag. If set, process sighting packets,
otherwise ignore them.
load_contacts -- Boolean flag. If set, process contact packets,
otherwise ignore them.
unique_sightings -- Boolean flag. If set, multiple copies
of the sighting packets received by different readers
over a sliding window of fixed duration (specified by
the keyword argument sighting_time_delta)
are detected and only distinct sightings are processed.
unique_contacts -- Boolean flag. If set, multiple copies
of the contact packets received by different readers
over a sliding window of fixed duration (specified by
the keyword argument contact_time_delta)
are detected and only distinct contacts are processed.
sighting_time_delta -- integers representing the length,
in seconds, of the sliding window over which duplicate
sightings are detected and dropped.
Unless the data collection infrastructure implements
store and forward techniques, or otherwise introduces
large delays (several seconds), any value above
a few seconds will work well.
contact_time_delta -- integers representing the length,
in seconds, of the sliding window over which duplicate
sightings are detected and dropped.
Unless the data collection infrastructure implements
store and forward techniques, or otherwise introduces
large delays (several seconds), any value above
a few seconds will work well.
packet_parser -- parser for a specific experiment.
This can be used by subclasses to activate experiment-specific
packet processing.
"""
self.load_sightings = load_sightings
self.unique_sightings = unique_sightings
self.load_contacts = load_contacts
self.unique_contacts = unique_contacts
self.decode = decode
if self.decode:
xxtea.set_key(*xxtea_crypto_key)
self.sighting_hash_dict = {}
self.sighting_time_delta = sighting_time_delta
self.contact_hash_dict = {}
self.contact_time_delta = contact_time_delta
self.time_delta = max(sighting_time_delta, contact_time_delta)
self.tcleanup = -1
self.parser = packet_parser
def hash_cleanup(self):
self.contact_hash_dict = dict(filter(lambda (h, t): t > self.tcleanup
- self.contact_time_delta, self.contact_hash_dict.items()))
self.sighting_hash_dict = dict(filter(lambda (h, t): t > self.tcleanup
- self.sighting_time_delta, self.sighting_hash_dict.items()))
def process(self, station_id, payload):
if self.decode:
payload = xxtea.decode(payload)
timestamp = int(time.time())
obj = self.parser.parse_packet(timestamp, station_id, payload)
if obj is None:
return
if obj.t >= self.tcleanup:
self.hash_cleanup()
self.tcleanup = obj.t + self.time_delta
if (obj.__class__ == Sighting) and self.load_sightings:
if self.unique_sightings:
h = obj.get_hash()
if h in self.sighting_hash_dict:
if obj.t - self.sighting_hash_dict[h] > self.sighting_time_delta:
self.sighting_hash_dict[h] = obj.t
return obj
else:
self.sighting_hash_dict[h] = obj.t
return obj
else:
return obj
elif (obj.__class__ == Contact) and self.load_contacts:
if self.unique_contacts:
h = obj.get_hash()
if h in self.contact_hash_dict:
if obj.t - self.contact_hash_dict[h] > self.contact_time_delta:
self.contact_hash_dict[h] = obj.t
return obj
else:
self.contact_hash_dict[h] = obj.t
return obj
else:
return obj
class ProducerThread(Thread):
def __init__(self, loader, queue, run_event):
self.run_event = run_event
self.loader = loader
self.queue = queue
super(ProducerThread, self).__init__()
def run(self):
try:
print "ProducerThread created."
self.loader.open()
for contact in self.loader:
self.queue.put(contact)
if not self.run_event.is_set():
break
except ValueError:
print "Producer: Error %s" % ValueError
self.loader.close()
raise
def main():
parser = argparse.ArgumentParser(description='Start a contact capture into a REDIS database.')
parser.add_argument('name', metavar='<run name>',
help='name to identify the RUN inside the REDIS database')
parser.add_argument('tstart', metavar='<start time>', nargs='?',
default=time.time(), type=int,
help='start time for the capture data')
parser.add_argument('delta', metavar='<frame duration>', nargs='?',
default='20', type=int,
help='duration in seconds of time frames')
parser.add_argument('url', metavar='<Redis server URL>', nargs='?',
default="localhost", help='URL of Redis server')
parser.add_argument('port', metavar='<port>', nargs='?',
default=6379, type=int, help='port of Redis server')
parser.add_argument('password', metavar='<password>', nargs='?',
default=None, help='password to access the database')
args = parser.parse_args()
RUN_NAME = args.name
DELTAT = args.delta
REDIS_URL = args.url
PORT = args.port
PASSWD = args.password
UDP_IP = "10.254.0.1"
UDP_PORT = 2342
adder = rediscontactadder.RedisContactAdder(RUN_NAME, '', DELTAT, REDIS_URL, PORT, PASSWD)
queue = Queue.Queue()
loader = UDPLoader(UDP_IP, UDP_PORT,
SPProcessor(packet_parser=spparser.PacketParserOBG()))
run_event = Event()
run_event.set()
prod = ProducerThread(loader, queue, run_event)
prod.daemon = True
prod.start()
try:
while 1:
try:
contact = queue.get(True, 1)
if contact is not None:
adder.store_contact(contact)
print "Contact stored", contact
queue.task_done()
except KeyboardInterrupt:
break
except Queue.Empty:
pass
finally:
print "Attempting to close threads"
run_event.clear()
prod.join(2)
print "Producer closed."
loader.close()
print "Socket closed."
if __name__ == '__main__':
main()