pep8 style fixes
This commit is contained in:
parent
16e5e4e8c8
commit
06b48fe99f
|
@ -1,14 +1,12 @@
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
sys.path.insert(0, './tmsp')
|
|
||||||
|
|
||||||
from wire import *
|
from wire import hex2bytes, decode_big_endian, encode_big_endian
|
||||||
from server import *
|
from server import TMSPServer
|
||||||
|
from reader import BytesBuffer
|
||||||
|
|
||||||
|
|
||||||
# tmsp application interface
|
|
||||||
|
|
||||||
class CounterApplication():
|
class CounterApplication():
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.hashCount = 0
|
self.hashCount = 0
|
||||||
self.txCount = 0
|
self.txCount = 0
|
||||||
|
@ -17,7 +15,9 @@ class CounterApplication():
|
||||||
def open(self):
|
def open(self):
|
||||||
return CounterAppContext(self)
|
return CounterAppContext(self)
|
||||||
|
|
||||||
|
|
||||||
class CounterAppContext():
|
class CounterAppContext():
|
||||||
|
|
||||||
def __init__(self, app):
|
def __init__(self, app):
|
||||||
self.app = app
|
self.app = app
|
||||||
self.hashCount = app.hashCount
|
self.hashCount = app.hashCount
|
||||||
|
@ -29,7 +29,9 @@ class CounterAppContext():
|
||||||
return msg, 0
|
return msg, 0
|
||||||
|
|
||||||
def info(self):
|
def info(self):
|
||||||
return ["hash, tx, commit counts:%d, %d, %d"%(self.hashCount, self.txCount, self.commitCount)], 0
|
return ["hash, tx, commit counts:%d, %d, %d" % (self.hashCount,
|
||||||
|
self.txCount,
|
||||||
|
self.commitCount)], 0
|
||||||
|
|
||||||
def set_option(self, key, value):
|
def set_option(self, key, value):
|
||||||
if key == "serial" and value == "on":
|
if key == "serial" and value == "on":
|
||||||
|
@ -41,7 +43,8 @@ class CounterAppContext():
|
||||||
txByteArray = bytearray(txBytes)
|
txByteArray = bytearray(txBytes)
|
||||||
if len(txBytes) >= 2 and txBytes[:2] == "0x":
|
if len(txBytes) >= 2 and txBytes[:2] == "0x":
|
||||||
txByteArray = hex2bytes(txBytes[2:])
|
txByteArray = hex2bytes(txBytes[2:])
|
||||||
txValue = decode_big_endian(BytesBuffer(txByteArray), len(txBytes))
|
txValue = decode_big_endian(
|
||||||
|
BytesBuffer(txByteArray), len(txBytes))
|
||||||
if txValue != self.txCount:
|
if txValue != self.txCount:
|
||||||
return None, 1
|
return None, 1
|
||||||
self.txCount += 1
|
self.txCount += 1
|
||||||
|
|
|
@ -1,21 +1,24 @@
|
||||||
from wire import *
|
from wire import decode_string
|
||||||
|
|
||||||
# map type_byte to message name
|
# map type_byte to message name
|
||||||
message_types = {
|
message_types = {
|
||||||
0x01 : "echo",
|
0x01: "echo",
|
||||||
0x02 : "flush",
|
0x02: "flush",
|
||||||
0x03 : "info",
|
0x03: "info",
|
||||||
0x04 : "set_option",
|
0x04: "set_option",
|
||||||
0x21 : "append_tx",
|
0x21: "append_tx",
|
||||||
0x22 : "get_hash",
|
0x22: "get_hash",
|
||||||
0x23 : "commit",
|
0x23: "commit",
|
||||||
0x24 : "rollback",
|
0x24: "rollback",
|
||||||
0x25 : "add_listener",
|
0x25: "add_listener",
|
||||||
0x26 : "rm_listener",
|
0x26: "rm_listener",
|
||||||
}
|
}
|
||||||
|
|
||||||
# return the decoded arguments of tmsp messages
|
# return the decoded arguments of tmsp messages
|
||||||
|
|
||||||
|
|
||||||
class RequestDecoder():
|
class RequestDecoder():
|
||||||
|
|
||||||
def __init__(self, reader):
|
def __init__(self, reader):
|
||||||
self.reader = reader
|
self.reader = reader
|
||||||
|
|
||||||
|
@ -50,5 +53,3 @@ class RequestDecoder():
|
||||||
def rm_listener(self):
|
def rm_listener(self):
|
||||||
# TODO
|
# TODO
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
|
|
||||||
# Simple read() method around a bytearray
|
# Simple read() method around a bytearray
|
||||||
|
|
||||||
|
|
||||||
class BytesBuffer():
|
class BytesBuffer():
|
||||||
|
|
||||||
def __init__(self, b):
|
def __init__(self, b):
|
||||||
self.buf = b
|
self.buf = b
|
||||||
self.readCount = 0
|
self.readCount = 0
|
||||||
|
@ -32,7 +35,10 @@ class BytesBuffer():
|
||||||
return r
|
return r
|
||||||
|
|
||||||
# Buffer bytes off a tcp connection and read them off in chunks
|
# Buffer bytes off a tcp connection and read them off in chunks
|
||||||
|
|
||||||
|
|
||||||
class ConnReader():
|
class ConnReader():
|
||||||
|
|
||||||
def __init__(self, conn):
|
def __init__(self, conn):
|
||||||
self.conn = conn
|
self.conn = conn
|
||||||
self.buf = bytearray()
|
self.buf = bytearray()
|
||||||
|
|
|
@ -1,16 +1,18 @@
|
||||||
import socket
|
import socket
|
||||||
import select
|
import select
|
||||||
import sys
|
import sys
|
||||||
import os
|
|
||||||
|
|
||||||
|
|
||||||
from wire import *
|
from wire import decode_varint, encode
|
||||||
from reader import *
|
from reader import BytesBuffer
|
||||||
from msg import *
|
from msg import RequestDecoder, message_types
|
||||||
|
|
||||||
# hold the asyncronous state of a connection
|
# hold the asyncronous state of a connection
|
||||||
# ie. we may not get enough bytes on one read to decode the message
|
# ie. we may not get enough bytes on one read to decode the message
|
||||||
|
|
||||||
|
|
||||||
class Connection():
|
class Connection():
|
||||||
|
|
||||||
def __init__(self, fd, appCtx):
|
def __init__(self, fd, appCtx):
|
||||||
self.fd = fd
|
self.fd = fd
|
||||||
self.appCtx = appCtx
|
self.appCtx = appCtx
|
||||||
|
@ -27,10 +29,14 @@ class Connection():
|
||||||
this.recBuf.write(data)
|
this.recBuf.write(data)
|
||||||
|
|
||||||
# TMSP server responds to messges by calling methods on the app
|
# TMSP server responds to messges by calling methods on the app
|
||||||
|
|
||||||
|
|
||||||
class TMSPServer():
|
class TMSPServer():
|
||||||
|
|
||||||
def __init__(self, app, port=5410):
|
def __init__(self, app, port=5410):
|
||||||
self.app = app
|
self.app = app
|
||||||
self.appMap = {} # map conn file descriptors to (appContext, reqBuf, resBuf, msgDecoder)
|
# map conn file descriptors to (appContext, reqBuf, resBuf, msgDecoder)
|
||||||
|
self.appMap = {}
|
||||||
|
|
||||||
self.port = port
|
self.port = port
|
||||||
self.listen_backlog = 10
|
self.listen_backlog = 10
|
||||||
|
@ -64,14 +70,14 @@ class TMSPServer():
|
||||||
print "connection closed"
|
print "connection closed"
|
||||||
|
|
||||||
def handle_recv(self, r):
|
def handle_recv(self, r):
|
||||||
# appCtx, recBuf, resBuf, conn
|
# appCtx, recBuf, resBuf, conn
|
||||||
conn = self.appMap[r]
|
conn = self.appMap[r]
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
print "recv loop"
|
print "recv loop"
|
||||||
# check if we need more data first
|
# check if we need more data first
|
||||||
if conn.inProgress:
|
if conn.inProgress:
|
||||||
if conn.msgLength == 0 or conn.recBuf.size() < conn.msgLength:
|
if (conn.msgLength == 0 or conn.recBuf.size() < conn.msgLength):
|
||||||
conn.recv()
|
conn.recv()
|
||||||
else:
|
else:
|
||||||
if conn.recBuf.size() == 0:
|
if conn.recBuf.size() == 0:
|
||||||
|
@ -94,17 +100,17 @@ class TMSPServer():
|
||||||
|
|
||||||
# now we can decode the message
|
# now we can decode the message
|
||||||
|
|
||||||
# first read the request type and get the particular msg decoder
|
# first read the request type and get the particular msg
|
||||||
|
# decoder
|
||||||
typeByte = conn.recBuf.read(1)
|
typeByte = conn.recBuf.read(1)
|
||||||
typeByte = int(typeByte[0])
|
typeByte = int(typeByte[0])
|
||||||
resTypeByte = typeByte+0x10
|
resTypeByte = typeByte + 0x10
|
||||||
req_type = message_types[typeByte]
|
req_type = message_types[typeByte]
|
||||||
|
|
||||||
if req_type == "flush":
|
if req_type == "flush":
|
||||||
# messages are length prefixed
|
# messages are length prefixed
|
||||||
conn.resBuf.write(encode(1))
|
conn.resBuf.write(encode(1))
|
||||||
conn.resBuf.write([resTypeByte])
|
conn.resBuf.write([resTypeByte])
|
||||||
sent = conn.fd.send(str(conn.resBuf.buf))
|
|
||||||
conn.msgLength = 0
|
conn.msgLength = 0
|
||||||
conn.inProgress = False
|
conn.inProgress = False
|
||||||
conn.resBuf = BytesBuffer(bytearray())
|
conn.resBuf = BytesBuffer(bytearray())
|
||||||
|
@ -121,7 +127,7 @@ class TMSPServer():
|
||||||
conn.inProgress = False
|
conn.inProgress = False
|
||||||
|
|
||||||
req_f = getattr(conn.appCtx, req_type)
|
req_f = getattr(conn.appCtx, req_type)
|
||||||
if req_args == None:
|
if req_args is None:
|
||||||
res = req_f()
|
res = req_f()
|
||||||
elif isinstance(req_args, tuple):
|
elif isinstance(req_args, tuple):
|
||||||
res = req_f(*req_args)
|
res = req_f(*req_args)
|
||||||
|
@ -147,7 +153,7 @@ class TMSPServer():
|
||||||
else:
|
else:
|
||||||
enc, encRet = encode(res), encode(ret_code)
|
enc, encRet = encode(res), encode(ret_code)
|
||||||
# messages are length prefixed
|
# messages are length prefixed
|
||||||
conn.resBuf.write(encode(len(enc)+len(encRet)+1))
|
conn.resBuf.write(encode(len(enc) + len(encRet) + 1))
|
||||||
conn.resBuf.write([resTypeByte])
|
conn.resBuf.write([resTypeByte])
|
||||||
conn.resBuf.write(encRet)
|
conn.resBuf.write(encRet)
|
||||||
conn.resBuf.write(enc)
|
conn.resBuf.write(enc)
|
||||||
|
@ -164,13 +170,15 @@ class TMSPServer():
|
||||||
self.handle_conn_closed(r)
|
self.handle_conn_closed(r)
|
||||||
return
|
return
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print "error reading from connection", str(e) # sys.exc_info()[0] # TODO better
|
# sys.exc_info()[0] # TODO better
|
||||||
|
print "error reading from connection", str(e)
|
||||||
self.handle_conn_closed(r)
|
self.handle_conn_closed(r)
|
||||||
return
|
return
|
||||||
|
|
||||||
def main_loop(self):
|
def main_loop(self):
|
||||||
while not self.shutdown:
|
while not self.shutdown:
|
||||||
r_list, w_list, _ = select.select(self.read_list, self.write_list, [], 2.5)
|
r_list, w_list, _ = select.select(
|
||||||
|
self.read_list, self.write_list, [], 2.5)
|
||||||
|
|
||||||
for r in r_list:
|
for r in r_list:
|
||||||
if (r == self.listener):
|
if (r == self.listener):
|
||||||
|
@ -178,7 +186,7 @@ class TMSPServer():
|
||||||
self.handle_new_connection(r)
|
self.handle_new_connection(r)
|
||||||
|
|
||||||
# undo adding to read list ...
|
# undo adding to read list ...
|
||||||
except rameError as e:
|
except NameError as e:
|
||||||
print "Could not connect due to NameError:", e
|
print "Could not connect due to NameError:", e
|
||||||
except TypeError as e:
|
except TypeError as e:
|
||||||
print "Could not connect due to TypeError:", e
|
print "Could not connect due to TypeError:", e
|
||||||
|
@ -187,14 +195,12 @@ class TMSPServer():
|
||||||
else:
|
else:
|
||||||
self.handle_recv(r)
|
self.handle_recv(r)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def handle_shutdown(self):
|
def handle_shutdown(self):
|
||||||
for r in self.read_list:
|
for r in self.read_list:
|
||||||
r.close()
|
r.close()
|
||||||
for w in self.write_list:
|
for w in self.write_list:
|
||||||
try:
|
try:
|
||||||
w.close()
|
w.close()
|
||||||
except: pass
|
except:
|
||||||
|
pass
|
||||||
self.shutdown = True
|
self.shutdown = True
|
||||||
|
|
||||||
|
|
|
@ -2,9 +2,11 @@
|
||||||
# the decoder works off a reader
|
# the decoder works off a reader
|
||||||
# the encoder returns bytearray
|
# the encoder returns bytearray
|
||||||
|
|
||||||
|
|
||||||
def hex2bytes(h):
|
def hex2bytes(h):
|
||||||
return bytearray(h.decode('hex'))
|
return bytearray(h.decode('hex'))
|
||||||
|
|
||||||
|
|
||||||
def bytes2hex(b):
|
def bytes2hex(b):
|
||||||
if type(b) in (str, unicode):
|
if type(b) in (str, unicode):
|
||||||
return "".join([hex(ord(c))[2:].zfill(2) for c in b])
|
return "".join([hex(ord(c))[2:].zfill(2) for c in b])
|
||||||
|
@ -17,23 +19,28 @@ def uvarint_size(i):
|
||||||
if i == 0:
|
if i == 0:
|
||||||
return 0
|
return 0
|
||||||
for j in xrange(1, 8):
|
for j in xrange(1, 8):
|
||||||
if i < 1<<j*8:
|
if i < 1 << j * 8:
|
||||||
return j
|
return j
|
||||||
return 8
|
return 8
|
||||||
|
|
||||||
# expects i < 2**size
|
# expects i < 2**size
|
||||||
|
|
||||||
|
|
||||||
def encode_big_endian(i, size):
|
def encode_big_endian(i, size):
|
||||||
if size == 0:
|
if size == 0:
|
||||||
return bytearray()
|
return bytearray()
|
||||||
return encode_big_endian(i/256, size-1) + bytearray([i%256])
|
return encode_big_endian(i / 256, size - 1) + bytearray([i % 256])
|
||||||
|
|
||||||
|
|
||||||
def decode_big_endian(reader, size):
|
def decode_big_endian(reader, size):
|
||||||
if size == 0:
|
if size == 0:
|
||||||
return 0
|
return 0
|
||||||
firstByte = reader.read(1)[0]
|
firstByte = reader.read(1)[0]
|
||||||
return firstByte*(256**(size-1)) + decode_big_endian(reader, size-1)
|
return firstByte * (256 ** (size - 1)) + decode_big_endian(reader, size - 1)
|
||||||
|
|
||||||
# ints are max 16 bytes long
|
# ints are max 16 bytes long
|
||||||
|
|
||||||
|
|
||||||
def encode_varint(i):
|
def encode_varint(i):
|
||||||
negate = False
|
negate = False
|
||||||
if i < 0:
|
if i < 0:
|
||||||
|
@ -48,32 +55,40 @@ def encode_varint(i):
|
||||||
return bytearray([size]) + big_end
|
return bytearray([size]) + big_end
|
||||||
|
|
||||||
# returns the int and whats left of the byte array
|
# returns the int and whats left of the byte array
|
||||||
|
|
||||||
|
|
||||||
def decode_varint(reader):
|
def decode_varint(reader):
|
||||||
size = reader.read(1)[0]
|
size = reader.read(1)[0]
|
||||||
if size == 0:
|
if size == 0:
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
negate = True if size > int(0xF0) else False
|
negate = True if size > int(0xF0) else False
|
||||||
if negate: size = size -0xF0
|
if negate:
|
||||||
|
size = size - 0xF0
|
||||||
i = decode_big_endian(reader, size)
|
i = decode_big_endian(reader, size)
|
||||||
if negate: i = i*(-1)
|
if negate:
|
||||||
|
i = i * (-1)
|
||||||
return i
|
return i
|
||||||
|
|
||||||
|
|
||||||
def encode_string(s):
|
def encode_string(s):
|
||||||
size = encode_varint(len(s))
|
size = encode_varint(len(s))
|
||||||
return size + bytearray(s)
|
return size + bytearray(s)
|
||||||
|
|
||||||
|
|
||||||
def decode_string(reader):
|
def decode_string(reader):
|
||||||
length = decode_varint(reader)
|
length = decode_varint(reader)
|
||||||
return str(reader.read(length))
|
return str(reader.read(length))
|
||||||
|
|
||||||
|
|
||||||
def encode_list(s):
|
def encode_list(s):
|
||||||
b = bytearray()
|
b = bytearray()
|
||||||
map(b.extend, map(encode, s))
|
map(b.extend, map(encode, s))
|
||||||
return encode_varint(len(s)) + b
|
return encode_varint(len(s)) + b
|
||||||
|
|
||||||
|
|
||||||
def encode(s):
|
def encode(s):
|
||||||
if s == None:
|
if s is None:
|
||||||
return bytearray()
|
return bytearray()
|
||||||
if isinstance(s, int):
|
if isinstance(s, int):
|
||||||
return encode_varint(s)
|
return encode_varint(s)
|
||||||
|
@ -85,13 +100,11 @@ def encode(s):
|
||||||
print "UNSUPPORTED TYPE!", type(s), s
|
print "UNSUPPORTED TYPE!", type(s), s
|
||||||
|
|
||||||
|
|
||||||
import binascii
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
ns = [100,100,1000,256]
|
ns = [100, 100, 1000, 256]
|
||||||
ss = [2,5,5,2]
|
ss = [2, 5, 5, 2]
|
||||||
bs = map(encode_big_endian, ns,ss)
|
bs = map(encode_big_endian, ns, ss)
|
||||||
ds = map(decode_big_endian, bs,ss)
|
ds = map(decode_big_endian, bs, ss)
|
||||||
print ns
|
print ns
|
||||||
print [i[0] for i in ds]
|
print [i[0] for i in ds]
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue