forked from MattMills/radiocapture-rf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrontend_connector.py
More file actions
222 lines (184 loc) · 7.05 KB
/
frontend_connector.py
File metadata and controls
222 lines (184 loc) · 7.05 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
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
#!/usr/bin/env python
# Copyright 2019,2020 Radiocapture LLC - Radiocapture.com
import zmq
import random
import threading
import time
import logging
class frontend_connector():
def __init__(self, dest='127.0.0.1', host='127.0.0.1', port=50000):
#temp hack until I have auto-frontend figured out
self.log = logging.getLogger('overseer.frontend_connector')
self.thread_lock = threading.Lock()
self.send_lock = threading.Lock()
self.dest = dest
#self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#self.s.connect((host,port))
self.log.debug('Initializing Frontend Connector')
self.continue_running = True
self.host = host
self.port = port
self.channel_port = 0
self.connection_init()
self.connect()
connection_handler = threading.Thread(target=self.connection_handler, name='connection_handler')
connection_handler.daemon = True
connection_handler.start()
def connection_init(self):
self.context = zmq.Context()
self.socket = self.context.socket(zmq.REQ)
self.socket.setsockopt(zmq.RCVTIMEO, 1000)
self.socket.setsockopt(zmq.LINGER, 0)
self.log.info('Attempting ZMQ socket connection to tcp://%s:%s' % (self.host, self.port))
self.socket.connect("tcp://%s:%s" % (self.host, self.port))
self.log.info('Successful ZMQ socket connection to tcp://%s:%s' % (self.host, self.port))
self.my_client_id = None
self.channel_id = None
self.channel_port = 0
def connection_teardown(self):
try:
self.socket.close()
except:
pass
try:
self.context.term()
except:
pass
try:
self.context.destroy()
except:
pass
def send(self, data):
tries = 0
while tries < 5:
try:
with self.send_lock:
self.socket.send(data)
response = self.socket.recv()
response = response.split(',')
return response
except Exception as e:
self.log.error('Exception in frontend_connector.send(): %s %s' % (type(e), e))
tries += 1
return None
def connect(self):
self.log.debug('Sending connect command')
data = self.send('connect')
if data == None:
return None
self.my_client_id = int(data[1])
self.log.debug('Received client ID %s' % self.my_client_id)
def __exit__(self):
try:
aself.send('quit,%s' % self.my_client_id)
except:
pass
def set_port(self, port):
self.log.debug('Setting port to %s' % port)
self.current_port = port
def scan_mode_set_freq(self, freq):
self.log.debug('scan_mode_set_freq(freq = %s)' % freq)
with self.thread_lock:
data = self.send('scan_mode_set_freq,%s' % (freq))
if data == 'success':
return True
else:
return False
def create_channel(self, channel_rate, freq):
self.thread_lock.acquire()
self.log.debug('create_channel(channel_rate = %s, freq = %s)' % (channel_rate, freq))
data = self.send('create,%s,%s,%s' % (self.my_client_id, channel_rate, freq))
if data == None or data[0] == 'na': #failed
self.thread_lock.release()
self.log.error('Failed to create channel')
return False, False
elif data[0] == 'create': #succeeded
channel_id = data[1]
port = data[2]
self.channel_port = port
self.channel_id = channel_id
self.thread_lock.release()
return channel_id, port
else:
self.thread_lock.release()
return False, False
def release_channel(self):
self.thread_lock.acquire()
if self.channel_id == None:
self.thread_lock.release()
return False
#if we dont have a port set, we can't have a channel.
self.log.debug('release_channel()')
data = self.send('release,%s,%s' % (self.my_client_id, self.channel_id))
if data == None or data[0] == 'na': #failed
self.log.error('Failed to release channel, probably leaking channels')
self.thread_lock.release()
return False
elif data[0] == 'release': #succeeded
channel_id = data[1]
self.channel_id = None
self.thread_lock.release()
return channel_id
else:
self.thread_lock.release()
return False
def report_offset(self, offset):
self.thread_lock.acquire()
if self.channel_id == None:
self.thread_lock.release()
return False #We're not running, cant change offset
self.log.debug('report_offset(%s)' % offset)
data = self.send('offset,%s,%s,%s' % (self.my_client_id, self.channel_id, offset))
if data == None or data[0] == 'na': #failed
self.log.error('Failed to set offset')
self.thread_lock.release()
return False
elif data[0] == 'offset': #success
self.thread_lock.release()
return True
def exit(self):
self.log.debug('exit() called')
self.continue_running = False
def connection_handler(self):
self.log.debug('connection_handler() init')
time.sleep(0.1)
while self.continue_running == True:
self.thread_lock.acquire()
self.log.debug('Sending heartbeat')
try:
data = self.send('hb,%s' % self.my_client_id)
if data == None or data[0] == 'fail':
self.log.error('Failed to heartbeat')
self.connection_teardown()
self.connection_init()
self.connect()
except Exception as e:
self.log.error('Failed to heartbeat: %s' % e)
self.connection_teardown()
self.connection_init()
self.connect()
self.thread_lock.release()
time.sleep(0.25)
self.thread_lock.acquire()
self.log.debug('Sending quit command to ZMQ socket')
self.send('quit,%s' % self.my_client_id)
self.socket.close()
self.context.destroy()
self.thread_lock.release()
self.log.debug('connection_handler() exit')
if __name__ == '__main__':
test = frontend_connector()
channel_id = test.create_channel(25000, 855000000)
if channel_id == False:
raise Exception('test failed: create')
if test.release_channel(channel_id) == False:
raise Exception('test failed: release')
print 'function test pass'
channels = []
start = time.time()
for i in range(0,100):
channels.append(test.create_channel(25000, 855000000))
for i in channels:
test.release_channel(i)
end = time.time()
print 'speed test %s' % (end-start)