-
Notifications
You must be signed in to change notification settings - Fork 2
/
comm.py
143 lines (116 loc) · 4.59 KB
/
comm.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
'''
This module provide communication interface with the Medusa in kernel.
There are at least two possibilities to communicate:
1. through '/dev/medusa' virtual character device
2. network
For now, there is implemented only the first possibility,
by the class CommFile.
'''
import os
from importlib import import_module
from queue import Queue
from threading import Thread, Lock, Event
from constants import MED_OK, MED_NO
from framework import exec
from mcp import doMedusaCommAuthanswer
def getSupportedComms():
comms = {}
dirComms = '.'
for dirName, subdirList, fileList in os.walk(dirComms):
if dirName != dirComms:
continue
for fname in fileList:
if not fname.startswith('comm'):
continue
if not fname.endswith('.py'):
continue
if fname[:-3] == __name__: #skip this module - comm.py
continue
try:
fnameModule = import_module(fname[:-3], package=None)
except ImportError as err:
for arg in err.args:
print('module %s error: %s' % (fname, arg))
else:
comms.update(fnameModule.getCommType()) #add type of imported communication type
return comms
class Comm(object):
def __init__(self, host):
self.host_name = host['host_name']
self.host_confdir = host['host_confdir']
self.host_commtype = host['host_commtype']
self.host_commdev = host['host_commdev']
self.hook_module = host['hook_module']
self.hook_list = self.hook_module.register.hooks
if self.hook_list is None:
self.hook_list = {}
self.requestsQueue = None
self.requestsThread = None
# requests (fetch/update) from auth server to medusa
self.requestsAuth2Med = dict()
self.requestsAuth2Med_lock = Lock()
# event if 'init' from user module is done
self.init_executed = Event()
self.init_executed.clear()
self.init_done = False
def __enter__(self):
# thread for auth requests handling
self.requestsQueue = Queue()
self.requestsThread = Thread(name="requestThread", target=Comm.decideQueue, args=(self,))
self.requestsThread.start()
def __exit__(self, *args):
raise NotImplementedError
def read(self, size):
raise NotImplementedError
def write(self, what):
raise NotImplementedError
def decideQueue(self):
self.init_executed.wait()
while True:
request_id, evtype, subj, obj = self.requestsQueue.get()
evtype._request_id = request_id
print('before method decide')
res = self.decide(evtype, subj, obj)
print("Comm.decideQueue: evtype='%s', request_id=%x, res=%x" % (evtype._name, request_id, res))
doMedusaCommAuthanswer(self, request_id, res)
self.requestsQueue.task_done()
def decide(self, event, subj, obj):
def _doCheck(check, kobject):
if check is None:
print('docheck return True')
return True
print('docheck exec()')
return exec(check, kobject)
print('method decide')
for hook in self.hook_list.get(event._name, []):
print('for')
try:
if not _doCheck(hook['event'], event): continue
if not _doCheck(hook['object'], obj): continue
if not _doCheck(hook['subject'], subj): continue
if obj is None:
print('obj None')
res = hook['exec'](event, subj)
else:
print('obj not None')
res = hook['exec'](event, subj, obj)
if res == MED_NO:
print('MED_NO')
return res
except Exception as err:
import traceback, sys
traceback.print_exc(file=sys.stdout)
for arg in err.args:
print("error in hook: %s" % arg)
pass #todo error msg
print('MED_OK')
return MED_OK
def init(self):
for hook in self.hook_list.get('init', []):
try:
hook['exec']()
except Exception as e:
print(e)
self.init_executed.set()
def __str__(self):
return '%s = {\n host_name = %s\n host_confdir = %s\n host_commtype = %s\n host_commdev = %s\n}' % (type(self), self.host_name, self.host_confdir, self.host_commtype, self.host_commdev)