-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from readevalprint/python3
Python3 example
- Loading branch information
Showing
6 changed files
with
521 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import sys | ||
|
||
from tmsp.wire import hex2bytes, decode_big_endian, encode_big_endian | ||
from tmsp.server import TMSPServer | ||
from tmsp.reader import BytesBuffer | ||
|
||
|
||
class CounterApplication(): | ||
|
||
def __init__(self): | ||
self.hashCount = 0 | ||
self.txCount = 0 | ||
self.commitCount = 0 | ||
|
||
def open(self): | ||
return CounterAppContext(self) | ||
|
||
|
||
class CounterAppContext(): | ||
|
||
def __init__(self, app): | ||
self.app = app | ||
self.hashCount = app.hashCount | ||
self.txCount = app.txCount | ||
self.commitCount = app.commitCount | ||
self.serial = False | ||
|
||
def echo(self, msg): | ||
return msg, 0 | ||
|
||
def info(self): | ||
return ["hash, tx, commit counts:%d, %d, %d" % (self.hashCount, | ||
self.txCount, | ||
self.commitCount)], 0 | ||
|
||
def set_option(self, key, value): | ||
if key == "serial" and value == "on": | ||
self.serial = True | ||
return 0 | ||
|
||
def append_tx(self, txBytes): | ||
if self.serial: | ||
txByteArray = bytearray(txBytes) | ||
if len(txBytes) >= 2 and txBytes[:2] == "0x": | ||
txByteArray = hex2bytes(txBytes[2:]) | ||
txValue = decode_big_endian( | ||
BytesBuffer(txByteArray), len(txBytes)) | ||
if txValue != self.txCount: | ||
return None, 1 | ||
self.txCount += 1 | ||
return None, 0 | ||
|
||
def get_hash(self): | ||
self.hashCount += 1 | ||
if self.txCount == 0: | ||
return "", 0 | ||
h = encode_big_endian(self.txCount, 8) | ||
h.reverse() | ||
return h.decode(), 0 | ||
|
||
def commit(self): | ||
self.commitCount += 1 | ||
return 0 | ||
|
||
def rollback(self): | ||
return 0 | ||
|
||
def add_listener(self): | ||
return 0 | ||
|
||
def rm_listener(self): | ||
return 0 | ||
|
||
def event(self): | ||
return | ||
|
||
|
||
if __name__ == '__main__': | ||
l = len(sys.argv) | ||
if l == 1: | ||
port = 46658 | ||
elif l == 2: | ||
port = int(sys.argv[1]) | ||
else: | ||
print("too many arguments") | ||
quit() | ||
|
||
print('TMSP Demo APP (Python)') | ||
|
||
app = CounterApplication() | ||
server = TMSPServer(app, port) | ||
server.main_loop() |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from .wire import decode_string | ||
|
||
# map type_byte to message name | ||
message_types = { | ||
0x01: "echo", | ||
0x02: "flush", | ||
0x03: "info", | ||
0x04: "set_option", | ||
0x21: "append_tx", | ||
0x22: "get_hash", | ||
0x23: "commit", | ||
0x24: "rollback", | ||
0x25: "add_listener", | ||
0x26: "rm_listener", | ||
} | ||
|
||
# return the decoded arguments of tmsp messages | ||
|
||
|
||
class RequestDecoder(): | ||
|
||
def __init__(self, reader): | ||
self.reader = reader | ||
|
||
def echo(self): | ||
return decode_string(self.reader) | ||
|
||
def flush(self): | ||
return | ||
|
||
def info(self): | ||
return | ||
|
||
def set_option(self): | ||
return decode_string(self.reader), decode_string(self.reader) | ||
|
||
def append_tx(self): | ||
return decode_string(self.reader) | ||
|
||
def get_hash(self): | ||
return | ||
|
||
def commit(self): | ||
return | ||
|
||
def rollback(self): | ||
return | ||
|
||
def add_listener(self): | ||
# TODO | ||
return | ||
|
||
def rm_listener(self): | ||
# TODO | ||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
|
||
# Simple read() method around a bytearray | ||
|
||
|
||
class BytesBuffer(): | ||
|
||
def __init__(self, b): | ||
self.buf = b | ||
self.readCount = 0 | ||
|
||
def count(self): | ||
return self.readCount | ||
|
||
def reset_count(self): | ||
self.readCount = 0 | ||
|
||
def size(self): | ||
return len(self.buf) | ||
|
||
def peek(self): | ||
return self.buf[0] | ||
|
||
def write(self, b): | ||
# b should be castable to byte array | ||
self.buf += bytearray(b) | ||
|
||
def read(self, n): | ||
if len(self.buf) < n: | ||
print("reader err: buf less than n") | ||
# TODO: exception | ||
return | ||
self.readCount += n | ||
r = self.buf[:n] | ||
self.buf = self.buf[n:] | ||
return r | ||
|
||
# Buffer bytes off a tcp connection and read them off in chunks | ||
|
||
|
||
class ConnReader(): | ||
|
||
def __init__(self, conn): | ||
self.conn = conn | ||
self.buf = bytearray() | ||
|
||
# blocking | ||
def read(self, n): | ||
while n > len(self.buf): | ||
moreBuf = self.conn.recv(1024) | ||
if not moreBuf: | ||
raise IOError("dead connection") | ||
self.buf = self.buf + bytearray(moreBuf) | ||
|
||
r = self.buf[:n] | ||
self.buf = self.buf[n:] | ||
return r |
Oops, something went wrong.