Commit ee1ecf56 authored by Philipp Auersperg's avatar Philipp Auersperg

fit for python 3

parent d8f1337b
......@@ -28,7 +28,10 @@ class ProcServerPythonException(Exception):
"""Runtime exception"""
def __init__(self, ex):
self.exception = _STATUS_CODE_BAD_RESPONSE
self.message = ex.message
try:
self.message = ex.message
except:
self.message = str(ex)
def __str__(self):
return "ProcBridge Server Exception [Python] %s:%s" % (self.exception.__class__, self.message)
......@@ -40,11 +43,12 @@ def bytes2long(buf):
:param buf: bytestring
:return:
"""
res = ord(buf[0]) +\
(ord(buf[1]) << 8) +\
(ord(buf[2]) << 16) +\
(ord(buf[3]) << 24)
return res
return int.from_bytes(buf, 'little')
# res = ord(buf[0]) +\
# (ord(buf[1]) << 8) +\
# (ord(buf[2]) << 16) +\
# (ord(buf[3]) << 24)
# return res
def long2bytes(x):
......@@ -53,14 +57,15 @@ def long2bytes(x):
:param x: 32bit int
:return: 4 byte array
"""
bytes=''.join(map(chr,
[
x & 255,
(x >> 8) & 255,
(x >> 16) & 255,
(x >> 24) & 255
]))
return bytes
return x.to_bytes(4, byteorder='little')
# bytes=''.join(map(chr,
# [
# x & 255,
# (x >> 8) & 255,
# (x >> 16) & 255,
# (x >> 24) & 255
# ]))
# return bytes
def _read_bytes(s, count):
......@@ -101,7 +106,7 @@ def _read_socket(s):
status_code = _read_bytes(s, 1)
if len(status_code) != 1:
raise Exception(_ERROR_MSG_MALFORMED_DATA)
code = ord(status_code[0])
code = int.from_bytes(status_code , byteorder='little')
# 4. RESERVED (2 bytes)
reserved = _read_bytes(s, 2)
......@@ -113,13 +118,13 @@ def _read_socket(s):
if len(len_bytes) != 4:
raise Exception(_ERROR_MSG_MALFORMED_DATA)
json_len = bytes2long(len_bytes)
json_len = int.from_bytes(len_bytes, 'little')
# 6. JSON OBJECT
text_bytes = _read_bytes(s, json_len)
if len(text_bytes) != json_len:
raise Exception(_ERROR_MSG_MALFORMED_DATA)
obj = json.loads(text_bytes, encoding='utf-8')
obj = json.loads(text_bytes.decode('utf-8'))
return code, obj
......@@ -137,16 +142,16 @@ def _write_socket(s, status_code, json_obj):
# 2. VERSION
s.sendall(b'\x01\x00')
# 3. STATUS CODE
s.sendall(chr(status_code))
s.sendall(status_code.to_bytes(1, 'little'))
# 4. RESERVED 2 BYTES
s.sendall(b'\x00\x00')
# 5. LENGTH (little endian)
json_text = json.dumps(json_obj)
json_bytes = json_text #bytes(json_text, encoding='utf-8')
json_bytes = bytes(json_text, encoding='utf-8')
# len_bytes = len(json_bytes).to_bytes(4, byteorder='little')
len_bytes = len(json_bytes)
bytes_len_bytes = long2bytes(len_bytes)
# bytes_len_bytes = long2bytes(len_bytes)
len_bytes = long2bytes(len_bytes)
s.sendall(len_bytes)
......@@ -235,7 +240,7 @@ def _write_error_response(s, json_obj, resp_to=-1):
})
def _write_bad_response(s, message):
def _write_bad_response(s, message, resp_to=-1):
"""
passes a protocol exception
:param s: socket conn
......@@ -243,7 +248,8 @@ def _write_bad_response(s, message):
:return: None
"""
_write_socket(s, _STATUS_CODE_BAD_RESPONSE, {
_KEY_MSG: message
_KEY_MSG: message,
RESP_TO : resp_to
})
......@@ -388,6 +394,8 @@ class Delegate(object):
handlers = {}
def __call__(self, api, kw, conn):
if not api in self.handlers:
raise ProcServerPythonException("no handler for: %s found" % api)
meth = self.handlers[api]
return meth(self, conn=conn, **kw)
......@@ -413,13 +421,14 @@ def _start_connection(server, s):
try:
while server.started:
api, body = _read_request(s)
print 'api:', api, body
print('api:', api, body)
if api == API_CLOSE:
print 'closing'
print('closing')
break
try:
resp_to = -1
reply = server.delegate(api, body, conn=s)
print 'result:', reply
print('result:', reply)
# if result is not a dict, convert it to a dict containing 'result'
if not isinstance(reply, dict):
......@@ -433,12 +442,13 @@ def _start_connection(server, s):
_write_good_response(s, reply, resp_to=resp_to)
except ProcServerPythonException as ex:
resp_to=body[REQ_ID]
print 'resp_to:', resp_to
print('resp_to:', resp_to)
if server.started:
_write_error_response(s, ex.message, resp_to=resp_to)
except Exception as ex:
print("Exception:", ex, resp_to)
if server.started:
_write_bad_response(s, str(ex))
_write_bad_response(s, str(ex), resp_to=resp_to)
except Exception as e: #TODO: fix that seriously
raise
......
print "------------------------------------------serv"
print("------------------------------------------serv")
import time
import procbridge
print 'procbridge imported'
print('procbridge imported')
delegate = procbridge.Delegate()
@delegate.api
def echo(self, echo, **kw):
print "echo:", echo
print("echo:", echo)
# time.sleep(3)
return "Hello:" + echo
......@@ -39,5 +39,5 @@ def runit():
server.wait_for_stop()
print('bye!')
print 'starting...'
print('starting...')
runit()
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment