-
Notifications
You must be signed in to change notification settings - Fork 22
/
master.py
91 lines (69 loc) · 2.76 KB
/
master.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
import re
import uuid
import json
from os import path
import asyncio
import subprocess
from threading import Thread
from tornado.ioloop import IOLoop
from tornado.gen import coroutine
from tornado.websocket import WebSocketHandler
from ..lib import getEnv, getMaster
class Master(WebSocketHandler):
status = False
thread = None
proc = None
clients = {}
bridge_master_changes = None
launch_master_changes = None
def open(self):
print("[MASTER]: open")
cls = self.__class__
self.id = str(uuid.uuid4())
cls.clients[self.id] = (IOLoop.current(), self.write_message)
self.write_message( json.dumps({ 'status': cls.status }) )
def on_message(self, message):
#print("[MASTER]: message, ", message)
cls = self.__class__
msg = json.loads(message)
if msg['cmd'] == "start" and cls.proc == None :
print("[MASTER]: starting")
cls.thread = Thread(target=cls.run, args=([path.join(getEnv(), 'roslaunch'), getMaster()],))
cls.thread.daemon = True
cls.thread.start()
elif msg['cmd'] == "stop" and cls.proc != None:
print("[MASTER]: stopping")
if cls.proc.poll() == None :
cls.proc.terminate()
def on_close(self):
print("[MASTER]: close")
cls = self.__class__
cls.clients.pop(self.id)
def check_origin(self, origin):
print("[MASTER]: check origin")
return True
@classmethod
def run(cls, command):
asyncio.set_event_loop(asyncio.new_event_loop())
cls.proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
cls.status = True
for loop, write in cls.clients.values() :
loop.add_callback(write, json.dumps({ 'status': cls.status }) )
cls.bridge_master_changes(cls.status)
cls.launch_master_changes(cls.status)
print("[MASTER]: running")
out, err = cls.proc.communicate()
cls.proc = None
print("[MASTER]: stopped")
msg = ''.join( re.split(r'\x1b]2;.*?\x07', (err if err else out)) )
cls.status = False
if err :
for loop, write in cls.clients.values() :
loop.add_callback(write, json.dumps({ 'status': cls.status, 'error': msg }) )
cls.bridge_master_changes(cls.status)
cls.launch_master_changes(cls.status)
else :
for loop, write in cls.clients.values() :
loop.add_callback(write, json.dumps({ 'status': cls.status, 'output': msg }) )
cls.bridge_master_changes(cls.status)
cls.launch_master_changes(cls.status)