CLI: cleaned up exceptions to make code more readable
This commit is contained in:
parent
30e9496270
commit
2a83b1a61b
130
binding/rpc.py
130
binding/rpc.py
@ -22,6 +22,27 @@ import socket
|
|||||||
import struct
|
import struct
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
class PeerClosedConnError(Exception):
|
||||||
|
def __init__(self, msg):
|
||||||
|
self.msg = msg
|
||||||
|
def __str__(self):
|
||||||
|
return self.msg
|
||||||
|
|
||||||
|
class RpcError(Exception):
|
||||||
|
def __init__(self, msg):
|
||||||
|
self.msg = msg
|
||||||
|
def __str__(self):
|
||||||
|
return self.msg
|
||||||
|
|
||||||
|
class RpcMismatchError(Exception):
|
||||||
|
def __init__(self, msg, received, expected):
|
||||||
|
self.msg = msg
|
||||||
|
self.received = received
|
||||||
|
self.expected = expected
|
||||||
|
def __str__(self):
|
||||||
|
return '%s - Expected method %d, Received method %d' % (
|
||||||
|
self.msg, self.expected, self.received)
|
||||||
|
|
||||||
class OstinatoRpcController(RpcController):
|
class OstinatoRpcController(RpcController):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(OstinatoRpcController, self).__init__()
|
super(OstinatoRpcController, self).__init__()
|
||||||
@ -35,9 +56,10 @@ class OstinatoRpcChannel(RpcChannel):
|
|||||||
try:
|
try:
|
||||||
self.sock.connect((host, port))
|
self.sock.connect((host, port))
|
||||||
except socket.error, e:
|
except socket.error, e:
|
||||||
print 'ERROR: Unable to connect to Drone %s (%s)' % (
|
error = 'ERROR: Unable to connect to Drone %s (%s)' % (
|
||||||
self.peer, str(e))
|
self.peer, str(e))
|
||||||
sys.exit(1)
|
print error
|
||||||
|
raise
|
||||||
|
|
||||||
def disconnect(self):
|
def disconnect(self):
|
||||||
self.sock.close()
|
self.sock.close()
|
||||||
@ -48,83 +70,75 @@ class OstinatoRpcChannel(RpcChannel):
|
|||||||
OST_PB_MSG_TYPE_RESPONSE = 2
|
OST_PB_MSG_TYPE_RESPONSE = 2
|
||||||
OST_PB_MSG_TYPE_BLOB = 3
|
OST_PB_MSG_TYPE_BLOB = 3
|
||||||
|
|
||||||
|
error = ''
|
||||||
try:
|
try:
|
||||||
req = request.SerializeToString()
|
req = request.SerializeToString()
|
||||||
self.sock.sendall(struct.pack('>HHI',
|
self.sock.sendall(struct.pack('>HHI',
|
||||||
OST_PB_MSG_TYPE_REQUEST, method.index, len(req)) + req)
|
OST_PB_MSG_TYPE_REQUEST, method.index, len(req)) + req)
|
||||||
except EncodeError, e:
|
|
||||||
print 'ERROR: Failed to serialize %s arg for RPC %s() ' \
|
|
||||||
'to Drone %s (%s)' % (
|
|
||||||
type(request).__name__, method.name, self.peer, e)
|
|
||||||
sys.exit(1)
|
|
||||||
except socket.error, e:
|
|
||||||
print 'ERROR: Failed to invoke RPC %s() to Drone %s (%s)' % (
|
|
||||||
method.name, self.peer, e)
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
# receive and parse header
|
# receive and parse header
|
||||||
try:
|
|
||||||
hdr = ''
|
hdr = ''
|
||||||
while len(hdr) < OST_PB_MSG_HDR_SIZE:
|
while len(hdr) < OST_PB_MSG_HDR_SIZE:
|
||||||
chunk = self.sock.recv(OST_PB_MSG_HDR_SIZE - len(hdr))
|
chunk = self.sock.recv(OST_PB_MSG_HDR_SIZE - len(hdr))
|
||||||
if chunk == '':
|
if chunk == '':
|
||||||
raise RuntimeError("socket connection closed by peer")
|
raise PeerClosedConnError('connection closed by peer')
|
||||||
hdr = hdr + chunk
|
hdr = hdr + chunk
|
||||||
except socket.error, e:
|
|
||||||
print 'ERROR: Failed to receive msg reply for RPC %s() ' \
|
|
||||||
'from Drone %s (%s)' % (
|
|
||||||
method.name, self.peer, e)
|
|
||||||
sys.exit(1)
|
|
||||||
except RuntimeError, e:
|
|
||||||
print 'ERROR: Drone %s closed connection receiving msg reply ' \
|
|
||||||
'for RPC %s() (%s)' % (
|
|
||||||
self.peer, method.name, e)
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
(msg_type, method_index, resp_len) = struct.unpack('>HHI', hdr)
|
(msg_type, method_index, resp_len) = struct.unpack('>HHI', hdr)
|
||||||
|
|
||||||
# verify response method is same as the one requested
|
# receive and parse the actual response message
|
||||||
if method_index != method.index:
|
|
||||||
print 'ERROR: Received Reply for Method %d; expecting reply for ' \
|
|
||||||
'method %d (%s)' % (
|
|
||||||
method_index, method.index, method.name)
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
# receive and parse the actual response message
|
|
||||||
try:
|
|
||||||
resp = ''
|
resp = ''
|
||||||
while len(resp) < resp_len:
|
while len(resp) < resp_len:
|
||||||
chunk = self.sock.recv(resp_len - len(resp))
|
chunk = self.sock.recv(resp_len - len(resp))
|
||||||
if chunk == '':
|
if chunk == '':
|
||||||
raise RuntimeError("socket connection closed by peer")
|
raise PeerClosedConnError('connection closed by peer')
|
||||||
resp = resp + chunk
|
resp = resp + chunk
|
||||||
|
|
||||||
|
# verify response method is same as the one requested
|
||||||
|
if method_index != method.index:
|
||||||
|
raise RpcMismatchError('RPC mismatch',
|
||||||
|
expected = method.index, received = method_index)
|
||||||
|
|
||||||
|
if msg_type == OST_PB_MSG_TYPE_RESPONSE:
|
||||||
|
response = response_class()
|
||||||
|
response.ParseFromString(resp)
|
||||||
|
elif msg_type == OST_PB_MSG_TYPE_BLOB:
|
||||||
|
response = resp
|
||||||
|
else:
|
||||||
|
raise RpcError('unknown RPC msg type %d' % msg_type)
|
||||||
|
|
||||||
|
controller.response = response
|
||||||
|
|
||||||
except socket.error, e:
|
except socket.error, e:
|
||||||
print 'ERROR: Failed to receive reply for RPC %s() ' \
|
error = 'ERROR: RPC %s() to Drone %s failed (%s)' % (
|
||||||
'from Drone %s (%s)' % (
|
|
||||||
method.name, self.peer, e)
|
method.name, self.peer, e)
|
||||||
sys.exit(1)
|
raise
|
||||||
except RuntimeError, e:
|
except PeerClosedConnError, e:
|
||||||
print 'ERROR: Drone %s closed connection receiving reply ' \
|
error = 'ERROR: Drone %s closed connection receiving reply ' \
|
||||||
'for RPC %s() (%s)' % (
|
'for RPC %s() (%s)' % (
|
||||||
self.peer, method.name, e)
|
self.peer, method.name, e)
|
||||||
sys.exit(1)
|
raise
|
||||||
|
except EncodeError, e:
|
||||||
if msg_type == OST_PB_MSG_TYPE_RESPONSE:
|
error = 'ERROR: Failed to serialize %s arg for RPC %s() ' \
|
||||||
try:
|
'to Drone %s (%s)' % (
|
||||||
response = response_class()
|
type(request).__name__, method.name, self.peer, e)
|
||||||
response.ParseFromString(resp)
|
raise
|
||||||
except DecodeError, e:
|
except DecodeError, e:
|
||||||
print 'ERROR: Failed to parse %s response for RPC %s() ' \
|
error = 'ERROR: Failed to parse %s response for RPC %s() ' \
|
||||||
'from Drone %s (%s)' % (
|
'from Drone %s (%s)' % (
|
||||||
type(response).__name__, method.name, self.peer, e)
|
type(response).__name__, method.name, self.peer, e)
|
||||||
sys.exit(1)
|
raise
|
||||||
elif msg_type == OST_PB_MSG_TYPE_BLOB:
|
except RpcMismatchError, e:
|
||||||
response = resp
|
error = 'ERROR: Rpc Mismatch for RPC %s() (%s)' % (
|
||||||
else:
|
method.name, e)
|
||||||
print 'ERROR: unsupported msg type %d received in respone to %s' % \
|
raise
|
||||||
msg_type, method.name
|
except RpcError, e:
|
||||||
|
error = 'ERROR: Unknown reply received for RPC %s() (%s) ' % (
|
||||||
controller.response = response
|
method.name, e)
|
||||||
|
raise
|
||||||
|
finally:
|
||||||
|
if error:
|
||||||
|
print error
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user