aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorpzread <netfirewall@gmail.com>2013-06-15 03:27:23 +0800
committerpzread <netfirewall@gmail.com>2013-06-15 03:27:23 +0800
commite8ab9406d9c1e4c0091eea35bf78638e92ed014b (patch)
tree1a16d0a5db115fd41cb6d4b54feece006e765ce7 /src
parentf7823d5630515a6aee93183dcaa6c8d0e5797f10 (diff)
downloadtaiwan-online-judge-e8ab9406d9c1e4c0091eea35bf78638e92ed014b.tar
taiwan-online-judge-e8ab9406d9c1e4c0091eea35bf78638e92ed014b.tar.gz
taiwan-online-judge-e8ab9406d9c1e4c0091eea35bf78638e92ed014b.tar.bz2
taiwan-online-judge-e8ab9406d9c1e4c0091eea35bf78638e92ed014b.tar.lz
taiwan-online-judge-e8ab9406d9c1e4c0091eea35bf78638e92ed014b.tar.xz
taiwan-online-judge-e8ab9406d9c1e4c0091eea35bf78638e92ed014b.tar.zst
taiwan-online-judge-e8ab9406d9c1e4c0091eea35bf78638e92ed014b.zip
Add IMC filter support
Diffstat (limited to 'src')
-rwxr-xr-xsrc/py/backend_server.py18
-rw-r--r--src/py/imc/auth.py12
-rwxr-xr-xsrc/py/imc/proxy.py77
3 files changed, 87 insertions, 20 deletions
diff --git a/src/py/backend_server.py b/src/py/backend_server.py
index a27240c..e965bd3 100755
--- a/src/py/backend_server.py
+++ b/src/py/backend_server.py
@@ -109,13 +109,14 @@ class BackendWorker(tornado.tcpserver.TCPServer):
Proxy.instance.add_conn(self.center_conn)
Proxy.instance.register_call('test/','get_client_list',self._test_get_client_list)
- imc_register_call('','test_dst',self._test_dst)
+ imc_register_call('test/','test_dst',self._test_dst)
+ Proxy.instance.register_filter('test/',self._test_filter)
#imc_register_call('','test_dsta',self._test_dsta)
#$time.sleep(1)
- #if self._link == '/backend/2/':
- #self._test_call(None)
+ if self._link == '/backend/2/':
+ self._test_call(None)
sock_ip,sock_port = self.sock_addr
netio.send_pack(stream,bytes(json.dumps({
@@ -243,15 +244,20 @@ class BackendWorker(tornado.tcpserver.TCPServer):
return list(self._client_linkmap.items())
@imc.async.caller
+ def _test_filter(self,dpart,func_name):
+ print(dpart)
+ print(func_name)
+
+ @imc.async.caller
def _test_call(self,param):
with TOJAuth.change_current_iden(self._idendesc):
for i in range(0,1024):
- dst = '/backend/' + str((i % 8) + 2) + '/'
+ dst = '/backend/' + str((i % 2) + 2) + '/'
if dst == self._link:
continue
fileres = Proxy.instance.sendfile(dst,'test.py')
- ret = imc_call(dst,'test_dst',fileres.filekey)
+ ret = imc_call(dst + 'test/','test_dst',fileres.filekey)
print(fileres.wait())
print(self._link)
@@ -282,7 +288,7 @@ class BackendWorker(tornado.tcpserver.TCPServer):
@imc.async.caller
def _test_dst(self,filekey):
- #print(filekey)
+ print(filekey)
fileres = Proxy.instance.recvfile(filekey,'data')
#print('recv ' + fileres.wait())
diff --git a/src/py/imc/auth.py b/src/py/imc/auth.py
index 02dd2dc..84197a8 100644
--- a/src/py/imc/auth.py
+++ b/src/py/imc/auth.py
@@ -34,7 +34,7 @@ class Auth:
return idendesc
@staticmethod
- def change_current_iden(idendesc,auth = None):
+ def change_current_iden(idendesc = None,auth = None):
@contextlib.contextmanager
def context():
global current_idendata
@@ -44,9 +44,13 @@ class Auth:
if auth == None:
auth = Auth.instance
- iden = auth.get_iden(idendesc)
- if iden == None:
- raise ValueError('Illegal idendesc')
+ if idendesc == None:
+ iden = None
+
+ else:
+ iden = auth.get_iden(idendesc)
+ if iden == None:
+ raise ValueError('Illegal idendesc')
old_idendata = current_idendata
current_idendata = (iden,idendesc)
diff --git a/src/py/imc/proxy.py b/src/py/imc/proxy.py
index 45841ab..e21b171 100755
--- a/src/py/imc/proxy.py
+++ b/src/py/imc/proxy.py
@@ -3,6 +3,7 @@ import uuid
import os
import datetime
import ssl
+from collections import deque
from Crypto.Hash import SHA512
import tornado.ioloop
@@ -89,7 +90,7 @@ class Proxy:
self._conn_linkmap = {}
self._conn_retidmap = {self._link:{}}
self._conn_filekeymap = {self._link:{}}
- self._call_pathmap = {}
+ self._callpath_root = ({},{},[])
self._info_filekeymap = {}
@@ -145,7 +146,48 @@ class Proxy:
return None
def register_call(self,path,func_name,func):
- self._call_pathmap[''.join([path,func_name])] = func
+ parts = path.split('/')[:-1]
+ child,name,filt = self._callpath_root
+ i = 0
+ size = len(parts)
+ while i < size:
+ try:
+ child,name,filt = child[parts[i]]
+ i += 1
+
+ except KeyError:
+ while i < size:
+ part = parts[i]
+ node = ({},{},[])
+ child[part] = node
+ child,name,filt = node
+ i += 1
+
+ break
+
+ name[func_name] = func
+
+ def register_filter(self,path,func):
+ parts = path.split('/')[:-1]
+ child,name,filt = self._callpath_root
+ i = 0
+ size = len(parts)
+ while i < size:
+ try:
+ child,name,filt = child[parts[i]]
+ i += 1
+
+ except KeyError:
+ while i < size:
+ part = parts[i]
+ node = ({},{},[])
+ child[part] = node
+ child,name,filt = node
+ i += 1
+
+ break
+
+ filt.append(func)
def call(self,dst,func_name,timeout,*args):
return self._route_call(None,self._link,async.get_retid(),Auth.get_current_idendesc(),dst,func_name,timeout,list(args))
@@ -255,9 +297,9 @@ class Proxy:
return __ret((False,'Eilliden'))
try:
- dst_part = dst.split('/',3)
+ dst_part = dst.split('/')
dst_link = ''.join(['/',dst_part[1],'/',dst_part[2],'/'])
- dst_path = dst_part[3]
+ dst_path = dst_part[3:-1]
except Exception:
return __ret((False,'Enoexist'))
@@ -266,15 +308,30 @@ class Proxy:
__add_wait_caller(self._link)
try:
- if Auth.get_current_idendesc() == idendesc:
- result = self._call_pathmap[''.join([dst_path,func_name])](*param)
+ with Auth.change_current_iden(None,self._auth):
+ dpart = deque(dst_path)
+ child,name,filt = self._callpath_root
+ for func in filt:
+ func(dpart,func_name)
- else:
- with Auth.change_current_iden(idendesc,self._auth):
- result = self._call_pathmap[''.join([dst_path,func_name])](*param)
+ for part in dst_path:
+ child,name,filt = child[part]
+
+ dpart.popleft()
+ for func in filt:
+ func(dpart,func_name)
+
+ func = name[func_name]
except KeyError:
- result = (False,'Enoexist')
+ return __ret((False,'Enoexist'))
+
+ if Auth.get_current_idendesc() == idendesc:
+ result = func(*param)
+
+ else:
+ with Auth.change_current_iden(idendesc,self._auth):
+ result = func(*param)
__del_wait_caller(self._link)