-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtmlaunch
executable file
·236 lines (209 loc) · 8.52 KB
/
rtmlaunch
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
#!/usr/bin/env python
import sys,os,subprocess,signal,time,socket, commands
import rtmext
from rtshell import rtls,rtcon,rtact,rtdeact,rtconf,rtfind,rtdel
if __name__ == '__main__':
args=sys.argv
xmlfile=""
path=""
if len(args)==3:
path=rtmext.rtmpack(["find",args[1]])
if path == "":
print >>sys.stderr, "rtmlaunch : package not found: " + args[1]
#exit(1)
xmlfile=path+"/"+args[2]
elif len(args)==2:
xmlfile=args[1]
else:
print >>sys.stderr, "usage: rtmlaunch [package] [launcher_xml]"
print >>sys.stderr, " or: rtmlaunch [launcher_xml]"
sys.exit(1)
rtml=rtmext.read_launch_xml(xmlfile, launchpath=path)
if rtml:
#rtml.debug_print()
# delete all zombies
rtdel.main(['--zombies'], None)
# run
execcomps=[]
processes=[]
rtcdcommands=[]
for comp in rtml.components:
cxtname="/" + rtml.nameserver + "/" + comp.context_name()
hascomp=rtmext.alive_component(cxtname,[rtml.nameserver])
if comp.execute and not hascomp:
execcomps.append(comp)
elif hascomp:
print comp.context_name() + ": already exist."
else:
print comp.context_name() + ": execute = false."
for proc in rtml.processes:
execcomps.append(proc)
ecomps=[]
terminations=[]
if rtml.nameserver != None:
colonp = rtml.nameserver.find(":")
if colonp != -1:
exechost=rtml.nameserver[:colonp]
execport=rtml.nameserver[colonp+1:]
else:
exechost=rtml.nameserver
execport=2809
else:
exechost='localhost'
execport=2809
if rtml.invoketype == "tabs":
epacks=[]
delays=[]
ecxts=[]
erates=[]
execxtpacks=[]
for ecomp in execcomps:
epacks.append(ecomp.package)
ecomps.append(ecomp.comp)
ecxts.append(ecomp.context)
erates.append(ecomp.execrate)
execxtpacks.append(ecomp.ecxtpack)
if hasattr(ecomp, "delay"):
delays.append(ecomp.delay)
else:
delays.append("0")
#how to terminate this process
if ecomp.comp.find(".l") >= 0:
terminations.append("kill")
else:
terminations.append("int")
proc, rtcdcommands = rtmext.rtmrun_with_tabs(epacks, ecomps, ecxts, delays, rates=erates, ecxtpacks=execxtpacks, host=exechost, port=execport,terminal_options=rtml.terminal_options)
processes.append(proc)
else:
for ecomp in execcomps:
print ecomp.context_name() + ": starting up."
if hasattr(ecomp, "delay"):
delay=ecomp.delay
else:
delay="0"
#how to terminate this process
if ecomp.comp.find(".l") >= 0:
terminations.append("kill")
else:
terminations.append("int")
proc, rtcdcmd = rtmext.rtmrun(ecomp.package,ecomp.comp, ecomp.context, delay, rate=ecomp.execrate, ecxtpack=ecomp.ecxtpack, host=exechost, port=execport)
processes.append(proc)
rtcdcommands.append(rtcdcmd)
# wait a few second for initialize comps
check_count=50
check_int=0.1
check_comp=True
for i in range(check_count):
check_comp=True
for comp in rtml.components:
cxtname="/" + rtml.nameserver + "/" + comp.context_name()
hascomp=rtmext.alive_component(cxtname,[rtml.nameserver])
check_comp = check_comp and hascomp
if check_comp:
break
else:
time.sleep(check_int)
if not check_comp:
print >>sys.stderr, "component registration failed."
for proc in processes:
proc.send_signal(signal.SIGINT)
for proc in processes:
proc.wait()
sys.exit(1)
# connect
for conn in rtml.connectors:
# dataport connection
if conn.outport:
oport=conn.outport
oportname=""
for comp in rtml.components:
if oport.context==comp.context:
oportname="/" + rtml.nameserver + "/" + comp.context_name() + ":" + oport.portname
break
for iport in conn.inports:
iportname=""
for comp in rtml.components:
if iport.context==comp.context:
iportname="/" + rtml.nameserver + "/" + comp.context_name() + ":" + iport.portname
break
rtcon.main([oportname, iportname], None)
# serviceport connection
elif len(conn.services)==2:
sportnames=[]
for sport in conn.services:
sportname=""
for comp in rtml.components:
if sport.context==comp.context:
sportname="/" + rtml.nameserver + "/" + comp.context_name() + ":" + sport.portname
sportnames.append(sportname)
break
rtcon.main(sportnames, None)
# set configuration variables
for conf in rtml.configurations:
colonp = rtml.nameserver.find(":")
if colonp != -1:
host=rtml.nameserver[:colonp]
else:
host=rtml.nameserver
rtfret , cmp =rtfind.main(['/'+host, '--name='+conf[0]+'.rtc', '--type=c'], None)
if cmp != None:
cmp = cmp[0].encode('utf-8')
else:
continue
vname = conf[1].encode('utf-8')
vval = conf[2].encode('utf-8')
rtconf.main([cmp, "set", vname, vval], None)
# activate
for comp in rtml.components:
compname="/" + rtml.nameserver + "/" + comp.context_name()
if comp.dstate == 'activate' or comp.dstate == None:
if comp.ainterval != None:
time.sleep(comp.ainterval/1000.0)
rtact.main([compname], None)
# run til C-c
try:
while 1:
time.sleep(0.001)
pass
except KeyboardInterrupt:
# for comp in rtml.components:
# compname="/" + rtml.nameserver + "/" + comp.context_name()
# rtdeact.main([compname], None)
# time.sleep(1.0)
if rtml.invoketype == "tabs":
pid = None
prlists= commands.getoutput('ps auxf').split('\n')
for (ecp, term, rtcdcmd) in zip(ecomps, terminations, rtcdcommands):
for p in prlists:
if p.find(ecp)!=-1 or (rtcdcmd != None and p.find(rtcdcmd)!=-1):
pp = p.split(' ')
ppp = pp[1:]
for wr in ppp:
if wr != '':
pid = int(wr)
break
if pid!=None:
if term == 'int':
os.kill(pid, signal.SIGINT)
elif term == 'kill':
os.kill(pid, signal.SIGKILL)
else:
os.kill(pid, signal.SIGINT)
pid = None
for (proc, term) in zip(processes, terminations):
if term == 'int':
proc.send_signal(signal.SIGINT)
elif term == 'kill':
proc.send_signal(signal.SIGKILL)
else:
proc.send_signal(signal.SIGINT)
for proc in processes:
proc.wait()
print "process terminated."
sys.exit(0)
else:
if len(args)==3:
print >>sys.stderr, "rtmlaunch: no such file or package: " + args[1] + " , " + args[2]
elif len(args)==2:
print >>sys.stderr, "rtmlaunch: no such file: " + args[1]
sys.exit(1)