-
Notifications
You must be signed in to change notification settings - Fork 19
/
rpc.py
67 lines (56 loc) · 2.5 KB
/
rpc.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
import logging
import socket
import errno
import xmlrpclib
import logging
__author__ = 'dushyant'
logger = logging.getLogger('syncIt')
def safe_rpc(fn):
"""decorator to add try/catch to rpc function calls"""
def safe_fn(*args):
try:
result = fn(*args)
if result is None:
result = "success"
return result
except socket.error as e:
if e.errno == errno.ECONNREFUSED or e.errno == errno.EHOSTUNREACH:
logger.critical("Problem connecting to rpc - no rpc server running. function: %s", fn.func_name)
return None #rpc request failed
else:
raise
return safe_fn
@safe_rpc
def pull_file(dest_ip, dest_port, filename, source_uname, source_ip):
rpc_connect = xmlrpclib.ServerProxy("http://%s:%s/"% (dest_ip, dest_port), allow_none = True)
rpc_connect.pull_file(filename, source_uname, source_ip)
@safe_rpc
def req_push_file(dest_ip, dest_port, filedata, source_uname, source_ip, source_port):
rpc_connect = xmlrpclib.ServerProxy("http://%s:%s/"% (dest_ip, dest_port), allow_none = True)
return rpc_connect.req_push_file(filedata, source_uname, source_ip, source_port)
@safe_rpc
def ack_push_file(dest_ip, dest_port, server_filename, source_uname, source_ip, source_port):
rpc_connect = xmlrpclib.ServerProxy("http://%s:%s/"% (dest_ip, dest_port), allow_none = True)
return rpc_connect.ack_push_file(server_filename, source_uname, source_ip, source_port)
@safe_rpc
def mark_presence(dest_ip, dest_port, source_ip, source_port):
"""rpc call to marks client as available"""
rpc_connect = xmlrpclib.ServerProxy("http://%s:%s/"% (dest_ip, dest_port), allow_none = True)
logger.debug("rpc call to mark available")
logger.debug("available methods on rpc server %s", rpc_connect.system.listMethods())
rpc_connect.mark_presence(source_ip, source_port)
@safe_rpc
def get_client_public_key(dest_ip, dest_port):
rpc_connect = xmlrpclib.ServerProxy("http://%s:%s/"% (dest_ip, dest_port), allow_none = True)
return rpc_connect.get_public_key()
def find_available(dest_ip, dest_port):
"""rpc call to find client's rpc availability"""
rpc_connect = xmlrpclib.ServerProxy("http://%s:%s/"% (dest_ip, dest_port), allow_none = True)
try:
rpc_connect.system.listMethods()
return True
except socket.error as e:
if e.errno == errno.ECONNREFUSED or e.errno == errno.EHOSTUNREACH:
return False
else:
raise