mirror of
https://github.com/haiwen/ccnet-server.git
synced 2025-09-04 22:30:36 +00:00
support python3 (#35)
* use searpc-named-pipe-transport * delete unused files * remove not threaded rpc * use NamedPipeClient * remove not threaded rpc * remove unused code * remove unused rpc function * remove unused code and source files * Add name of rpc named pipe to ccent.h.
This commit is contained in:
committed by
Jiaqiang Xu
parent
f014fc5263
commit
9a1858aea5
@@ -1,179 +1,9 @@
|
||||
from pysearpc import SearpcClient, searpc_func, SearpcError
|
||||
from pysearpc import searpc_func, NamedPipeClient
|
||||
|
||||
from ccnet.status_code import SC_CLIENT_CALL, SS_CLIENT_CALL, \
|
||||
SC_CLIENT_MORE, SS_CLIENT_MORE, SC_SERVER_RET, \
|
||||
SC_SERVER_MORE, SC_PROC_DEAD
|
||||
class CcnetThreadedRpcClient(NamedPipeClient):
|
||||
|
||||
from ccnet.errors import NetworkError
|
||||
|
||||
class DeadProcError(Exception):
|
||||
def __str__(self):
|
||||
return "Processor is dead"
|
||||
|
||||
|
||||
class RpcClientBase(SearpcClient):
|
||||
|
||||
def __init__(self, ccnet_client_pool, service_name, retry_num=1,
|
||||
is_remote=False, remote_peer_id='', req_pool=False):
|
||||
SearpcClient.__init__(self)
|
||||
self.pool = ccnet_client_pool
|
||||
self.service_name = service_name
|
||||
self.retry_num = retry_num
|
||||
self.is_remote = is_remote
|
||||
self.remote_peer_id = remote_peer_id
|
||||
self.req_pool = req_pool
|
||||
if self.is_remote and len(self.remote_peer_id) != 40:
|
||||
raise ValueError("Invalid remote peer id")
|
||||
|
||||
def _start_service(self, client):
|
||||
req_id = client.get_request_id()
|
||||
req_str = self.service_name
|
||||
if self.is_remote:
|
||||
req_str = "remote " + self.remote_peer_id + " " + self.service_name
|
||||
client.send_request(req_id, req_str)
|
||||
rsp = client.read_response()
|
||||
if rsp.code != "200":
|
||||
raise SearpcError("Error received: %s %s (In _start_service)" % (rsp.code, rsp.code_msg))
|
||||
return req_id
|
||||
|
||||
def _real_call(self, client, req_id, fcall_str):
|
||||
client.send_update(req_id, SC_CLIENT_CALL, SS_CLIENT_CALL, fcall_str)
|
||||
|
||||
rsp = client.read_response()
|
||||
if rsp.code == SC_SERVER_RET:
|
||||
return rsp.content
|
||||
elif rsp.code == SC_SERVER_MORE:
|
||||
buf = rsp.content
|
||||
while True:
|
||||
client.send_update(req_id, SC_CLIENT_MORE,
|
||||
SS_CLIENT_MORE, '')
|
||||
rsp = client.read_response()
|
||||
if rsp.code == SC_SERVER_MORE:
|
||||
buf += rsp.content
|
||||
elif rsp.code == SC_SERVER_RET:
|
||||
buf += rsp.content
|
||||
break
|
||||
else:
|
||||
raise SearpcError("Error received: %s %s (In Read More)" % (rsp.code, rsp.code_msg))
|
||||
|
||||
return buf
|
||||
elif rsp.code == SC_PROC_DEAD:
|
||||
raise DeadProcError()
|
||||
else:
|
||||
raise SearpcError("Error received: %s %s" % (rsp.code, rsp.code_msg))
|
||||
|
||||
def call_remote_func_sync(self, fcall_str):
|
||||
"""Call remote function `fcall_str` and wait response."""
|
||||
|
||||
retried = 0
|
||||
while True:
|
||||
try:
|
||||
client = self.pool.get_client()
|
||||
if self.req_pool:
|
||||
req_id = client.req_ids.get(self.service_name, -1)
|
||||
if req_id == -1:
|
||||
req_id = self._start_service(client)
|
||||
client.req_ids[self.service_name] = req_id
|
||||
try:
|
||||
ret = self._real_call(client, req_id, fcall_str)
|
||||
except DeadProcError:
|
||||
client.req_ids[self.service_name] = -1
|
||||
self.pool.return_client(client)
|
||||
if retried < self.retry_num:
|
||||
retried = retried + 1
|
||||
continue
|
||||
else:
|
||||
raise
|
||||
|
||||
self.pool.return_client(client)
|
||||
return ret
|
||||
else:
|
||||
# no req pool
|
||||
req_id = self._start_service(client)
|
||||
ret = self._real_call(client, req_id, fcall_str)
|
||||
client.send_update(req_id, "103", "service is done", "")
|
||||
self.pool.return_client(client)
|
||||
return ret
|
||||
except (NetworkError, SearpcError):
|
||||
# the client is not returned to the pool and is freed automatically
|
||||
if retried < self.retry_num:
|
||||
retried = retried + 1
|
||||
continue
|
||||
else:
|
||||
raise
|
||||
|
||||
class CcnetRpcClient(RpcClientBase):
|
||||
|
||||
def __init__(self, ccnet_client_pool, retry_num=1, *args, **kwargs):
|
||||
RpcClientBase.__init__(self, ccnet_client_pool, "ccnet-rpcserver",
|
||||
*args, **kwargs)
|
||||
|
||||
@searpc_func("string", [])
|
||||
def list_peers(self):
|
||||
pass
|
||||
|
||||
@searpc_func("objlist", [])
|
||||
def list_resolving_peers(self):
|
||||
pass
|
||||
|
||||
@searpc_func("objlist", ["string"])
|
||||
def get_peers_by_role(self):
|
||||
pass
|
||||
|
||||
@searpc_func("object", ["string"])
|
||||
def get_peer(self):
|
||||
pass
|
||||
|
||||
@searpc_func("object", [])
|
||||
def get_session_info(self):
|
||||
pass
|
||||
|
||||
@searpc_func("int", ["string"])
|
||||
def add_client(self):
|
||||
pass
|
||||
|
||||
@searpc_func("int", ["string", "string"])
|
||||
def add_role(self, peer_id, role):
|
||||
pass
|
||||
|
||||
@searpc_func("int", ["string", "string"])
|
||||
def remove_role(self, peer_id, role):
|
||||
pass
|
||||
|
||||
@searpc_func("objlist", ["int", "int"])
|
||||
def get_procs_alive(self, offset, limit):
|
||||
pass
|
||||
|
||||
@searpc_func("int", [])
|
||||
def count_procs_alive(self):
|
||||
pass
|
||||
|
||||
@searpc_func("objlist", ["int", "int"])
|
||||
def get_procs_dead(self, offset, limit):
|
||||
pass
|
||||
|
||||
@searpc_func("int", [])
|
||||
def count_procs_dead(self):
|
||||
pass
|
||||
|
||||
@searpc_func("string", ["string"])
|
||||
def get_config(self, key):
|
||||
pass
|
||||
|
||||
@searpc_func("int", ["string", "string"])
|
||||
def set_config(self, key, value):
|
||||
pass
|
||||
|
||||
@searpc_func("objlist", [])
|
||||
def list_peer_stat(self, key, value):
|
||||
pass
|
||||
|
||||
|
||||
class CcnetThreadedRpcClient(RpcClientBase):
|
||||
|
||||
def __init__(self, ccnet_client_pool, retry_num=1, *args, **kwargs):
|
||||
RpcClientBase.__init__(self, ccnet_client_pool, "ccnet-threaded-rpcserver",
|
||||
*args, **kwargs)
|
||||
def __init__(self, socket_path):
|
||||
NamedPipeClient.__init__(self, socket_path, "ccnet-threaded-rpcserver")
|
||||
|
||||
@searpc_func("int", ["string", "string", "int", "int"])
|
||||
def add_emailuser(self, email, passwd, is_staff, is_active):
|
||||
|
Reference in New Issue
Block a user