-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.py
80 lines (68 loc) · 2.27 KB
/
router.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
'''
Created on 7 Mar, 2014
@author: qiren
'''
import log as logging
import threading
import settings
from multiprocessing import Queue
logger = logging.getLogger('engine')
def singleton(cls, *args, **kwargs):
instances = {}
def _singleton():
if cls not in instances:
instances[cls] = cls(*args, **kwargs)
return instances[cls]
return _singleton
@singleton
class Router:
"""
store the messages from vinzor server and forward it to right image
"""
ROUTE_TABLE = {}
lock = threading.Lock()
def __init__(self):
pass
def _check(self, data):
for i in settings.VINZOR_HEADER:
if i not in data:
return False
if 'template_id' in data['param'] and 'packages' in data['param'] and \
'template_name' in data['param'] and 'template_is_public' in data['param'] and \
'template_type' in data['param']:
for i in settings.ENGINE_RECV_PACKAGES_KEY:
for j in data['param']['packages']:
if i not in j:
return False
return True
return False
def tmpl_forward(self, template_id):
logger.debug(template_id)
logger.debug(self.ROUTE_TABLE)
if template_id in self.ROUTE_TABLE:
resp = self.ROUTE_TABLE[template_id].get()
self.lock.acquire()
if self.ROUTE_TABLE[template_id].empty():
logger.info('delete record %s' % template_id)
del self.ROUTE_TABLE[template_id]
self.lock.release()
return resp
return None
def tmpl_store(self, data):
print(data)
if self._check(data):
ss = data['param']['template_id']
self.lock.acquire()
del data['param']['template_id']
del data['param']['template_name']
del data['param']['template_type']
del data['param']['template_is_public']
data['param'] = data['param']['packages']
if ss not in self.ROUTE_TABLE:
self.ROUTE_TABLE[ss] = Queue()
self.ROUTE_TABLE[ss].put(data)
self.lock.release()
return True
return False
# sys.modules[__name__] = Router()
router = Router()