/
pyro_client.py
316 lines (242 loc) · 10.7 KB
/
pyro_client.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-
import time, random
import Pyro.core
import Pyro.naming
from Pyro.errors import *
from threading import Thread
from socket import gethostname
import subprocess
import sys, signal, os
global serverURI, abort
motelist= {}
abort=0
class Listener(Pyro.core.ObjBase):
def __init__(self):
Pyro.core.ObjBase.__init__(self)
self.hostname = gethostname()
self.isProgrammingNode = None
self.isDownloadingFiles = None
self.isDataCorrect = None
self.mote_list = {}
self.rxBinaryFile = "isa_receiver.ihex"
self.txBinaryFile = "isa_sender.ihex"
self.ascii_extend = ['€', 'ƒ', '„', '†', '‡', '‰', 'Š', '‹', 'Œ', 'Ž', '‘', '’', '“', '”', '•', '–',
'—', '˜', '™', 'š', '›', 'œ', 'ž', 'Ÿ', '¡', '¢', '£', '¤', '¥', '¦', '§', '©',
'ª', '«', '¬', '®', '¯', '°', '±', '²', '³', 'µ', '¶', '¹', 'º', '»', '¼', '½',
'¾', '¿', 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ë', 'Ì', 'Í', 'Î',
'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', '×', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'Þ',
'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î',
'ï', 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', '÷', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'þ',
'ÿ']
def callback(self, message):
print 'GOT CALLBACK: ',message
def get_hostname(self):
return gethostname()
def upload_transmitter_binary(self, bindata):
print 'uploading binary data <', self.txBinaryFile,'>'
fileobj = open(self.txBinaryFile, "wb")
fileobj.write(bindata)
fileobj.close()
def upload_receiver_binary(self, bindata):
try:
print 'uploading binary data <', self.rxBinaryFile,'>'
fileobj = open(self.rxBinaryFile, "wb")
fileobj.write(bindata)
fileobj.close()
except IOError, ex:
print ex
def program_rcv_mote(self, moteid):
self.isProgrammingNode=True
cmd ='make TARGET=sky upload-ihex FILE=isa_receiver.ihex MOTE='+str(moteid)
print 'cmd: ', cmd
myOutput=subprocess.Popen(cmd.split(), stdout=subprocess.PIPE).communicate()[0]
self.isProgrammingNode=False
#return myOutput
def program_all_motes_receivers(self):
self.isProgrammingNode=True
cmd ='make TARGET=sky upload-ihex FILE=isa_receiver.ihex'
print 'cmd: ', cmd
myOutput=subprocess.Popen(cmd.split(), stdout=subprocess.PIPE).communicate()[0]
self.isProgrammingNode=False
#print 'out>> ', myOutput
def program_tx_mote(self, moteid):
self.isProgrammingNode=True
cmd ='make TARGET=sky upload-ihex FILE=isa_sender.ihex MOTE='+str(moteid)
print 'cmd: ', cmd
myOutput=subprocess.Popen(cmd.split(), stdout=subprocess.PIPE).communicate()[0]
self.isProgrammingNode=False
#return myOutput
def notify_to_exec(self, cmdstr):
print 'server calling to execute ', cmdstr
myOutput=subprocess.Popen(cmdstr.split(), stdout=subprocess.PIPE).communicate()[0]
print myOutput
def check_data_integrity(self, data):
print 'checking data integrity'
got_filename = None
node_is_tx = None
filename = ''
n = 0
next_n = 0
for substr in data.split('\n'):
if got_filename == None:
idx = substr.find('r:')
if idx != -1:
filename = substr[(idx + 2):]
got_filename = True
else:
if substr.find('E') != -1 or substr.find('END') != -1:
return True
if substr.find('-') != -1 and substr.find('link') == -1:
c = substr.split('-')[0]
#get rid of all unwanted characters.......
for unwanted in self.ascii_extend:
if c.find(unwanted) != -1:
c.replace(unwanted, '')
if c.find('E') != -1 or c.find('END') != -1:
return True
n = int(c, 16)
if n != next_n:
return False
else:
next_n = (n + 16)
#end of IF
if got_filename == True:
return True
else:
return False
def download_mote_data(self, moteid):
self.isDownloadingFiles=True
cmd = './sconnect '+str((int(moteid)-1))
print 'downloading data at ', cmd
myoutput=subprocess.Popen(cmd.split(), stdout=subprocess.PIPE).communicate()[0]
if myoutput.find('node_tx') != -1:
#node is a tx, skeep
self.isDownloadingFiles=False
return
else:
if self.check_data_integrity(myoutput) == True:
obj=serverURI.getProxy()
obj.save_data_to_file(myoutput)
self.isDownloadingFiles=False
else:
self.isDownloadingFiles=True
#print myoutput
#get rid of the process...
#os.kill(proc.pid, signal.SIGUSR1)
#obj=serverURI.getProxy()
#if obj.check_data_integrity(myoutput):
# obj.save_data_to_file(myoutput)
def start_mote_tx(self, moteid):
cmd = './transmit '+str(moteid)
print 'mote transmitting '
proc=subprocess.Popen(cmd.split(), stdout=subprocess.PIPE).communicate()[0]
#get rid of the process...
#os.kill(proc.pid, signal.SIGUSR1)
def is_programming_mote(self):
return self.isProgrammingNode
def programming_mote_reset(self):
self.isProgrammingNode = None
def is_downloading_files(self):
return self.isDownloadingFiles
def download_files_reset(self):
self.isDownloadingFiles = None
def set_motelist(self):
print 'setting motes list'
self.mote_list = []
cmd = 'make sky-motes'
myoutput = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE).communicate()[0]
for mote in myoutput.split():
if mote:
self.mote_list.append(mote)
def get_motelist(self):
copymotelist = []
cmd ='make sky-motes'
myoutput = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE).communicate()[0]
print 'motes: ',myoutput
#for mote in myoutput.split():
for mote in myoutput.split(): #use the copy of the list instead
if mote and mote.find('/dev/ttyUSB') != -1:
copymotelist.append(mote)
return copymotelist #myoutput #
def get_node_id(self, usb_idx):
usb_devs=[]
cmd = './node_id '+str(int(usb_idx))
node_id=''
myoutput =subprocess.Popen(cmd.split(), stdout=subprocess.PIPE).communicate()[0]
for myid in myoutput.split():
try:
if myid.find('id:') != -1:
id_idx = myid[(myid.find('id:')+3):]
node_id = str(id_idx)
break
except Exception:
print 'error getting node_ids'
return node_id
def add2cache(self, cached):
global motelist
motelist={}
cache2=cached.split(',')
for a in cache2:
idx= a.find(':')
key = a[:(idx)]
val = a[(idx+1):]
motelist[key] = val
def get_cache(self):
motes = self.get_motelist()
mydic=''
for mote in motes:
cache_idx = mote[(mote.find('USB')+3):]
node_id = self.get_node_id(cache_idx)
if int(node_id):
if mydic == '':
mydic += str(cache_idx)+str(':'+node_id)
else:
mydic += ','+ str(cache_idx)+str(':'+node_id)
#
self.add2cache(mydic)
return motelist
def exit_experiment(self):
global abort
abort = 1
cmd ='kill -9 $(pidof python)'
myoutput = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE).communicate()[0]
sys.exit(0)
def shouter(objectURI):
global abort
object=objectURI.getProxy() # we get our own proxy object because we're running in our own thread.
print 'Shouter thread is running.'
n = int(1)
while not abort:
print 'Shouting something'
#object.shout('Hello out there!')
time.sleep(random.random()*3)
if n == 2:
break
n = int(n+1)
print'Shouter thread is exiting.'
def main():
global abort, serverURI
Pyro.core.initServer()
Pyro.core.initClient()
daemon = Pyro.core.Daemon()
locator = Pyro.naming.NameServerLocator()
NS = locator.getNS()
daemon.useNameServer(NS)
listener=Listener()
daemon.connect(listener)
serverURI=NS.resolve(':test.callback')
server = serverURI.getProxy()
server.register(listener.getProxy())
thread=Thread(target=shouter, args=(serverURI,))
thread.start()
while not abort:
#print 'Waiting for notification...'
try:
daemon.handleRequests()
except KeyboardInterrupt:
abort=1
thread.join()
print 'Exiting.'
if __name__=='__main__':
main()