-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommunicationMediator.py
109 lines (86 loc) · 3.92 KB
/
CommunicationMediator.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
# coding=utf-8
import json
import logging
import time
from collections import namedtuple
from Utility.Communication import Communication
class CommunicationMediator:
sm = None # type: ServerMediator
idData = namedtuple("idData", "data set isException")
idList = {}
MtxIdList = None
logger = logging.getLogger('CommunicationMediator')
def __init__(self, ism):
self.sm = ism
def runCommand(self, name, data, cid='', session=''):
self.sm.send(Communication.make_command(node=name, _id=cid, _from=self.sm.owner.getServiceName(), data=data,
session=session))
def runCommandSync(self, name, data, cid='', session=''):
self.sm.send(Communication.make_command(node=name, _id=cid, _from=self.sm.owner.getServiceName(), data=data,
session=session))
x = self.idData(cid, False, False)
cid = str(cid)
self.idList[cid] = x
while self.idList[cid].set is False:
# todo: put a timeout for commands
time.sleep(0.030)
dt = self.idList[cid].data
if self.idList[cid].isException:
raise Exception("Error received from Core: " + dt)
del self.idList[cid]
return dt
def runCallback(self, name, data, id, success=True):
self.sm.send(Communication.make_callback(node=name, _id=id, _from=self.sm.owner.getServiceName(), data=data,
success=success))
def runEvent(self, name, data):
self.sm.send(Communication.make_event(name, self.sm.owner.getServiceName(), data))
def registerEvent(self, name):
self.sm.send(
Communication.make_command(node="_core.registerEvent", _id="", _from=self.sm.owner.getServiceName(),
data='{"names" : [' + name + "]}"))
def removeEvent(self, name):
self.sm.send(Communication.make_command(node="_core.removeEvent", _id="", _from=self.sm.owner.getServiceName(),
data='{"names" : [' + name + "]}"))
def registerCommand(self, name):
self.sm.send(
Communication.make_command(node="_core.registerCommand", _id="", _from=self.sm.owner.getServiceName(),
data='{"names" : [' + name + "]}"))
def removeCommand(self, name):
self.sm.send(
Communication.make_command(node="_core.removeCommand", _id="", _from=self.sm.owner.getServiceName(),
data='{"names" : [' + name + "]}"))
def registerHook(self, name, session=""):
self.sm.send(Communication.make_command(node="_core.registerHook", _id="", _from=self.sm.owner.getServiceName(),
data='{"names" : [' + name + "]}"))
def removeHook(self, name):
self.sm.send(Communication.make_command(node="_core.registerHook", _id="", _from=self.sm.owner.getServiceName(),
data='{"names" : [' + name + "]}"))
def errorReport(self, node, id, desc):
self.sm.send(Communication.make_error(node, id, desc))
def dataReceive(self, data):
js = json.loads(data)
try:
id = str(js["id"])
dt = js["data"]
except:
return False
if id not in self.idList:
return False
x = self.idData(dt, True, False)
self.idList[id] = x
return True
def errorReceive(self, data):
js = json.loads(data)
try:
id = str(js["data"]["id"])
dt = js["data"]["description"]
except:
return False
if id not in self.idList:
return False
x = self.idData(dt, True, True)
self.idList[id] = x
return True
@staticmethod
def getNameAndType():
return "CommunicationMediator"