You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
1.7 KiB

  1. import sys
  2. sys.path.insert(0, './tmsp')
  3. from wire import *
  4. from server import *
  5. # tmsp application interface
  6. class CounterApplication():
  7. def __init__(self):
  8. self.hashCount = 0
  9. self.txCount = 0
  10. self.commitCount = 0
  11. def open(self):
  12. return CounterAppContext(self)
  13. class CounterAppContext():
  14. def __init__(self, app):
  15. self.app = app
  16. self.hashCount = app.hashCount
  17. self.txCount = app.txCount
  18. self.commitCount = app.commitCount
  19. self.serial = False
  20. def echo(self, msg):
  21. return msg, 0
  22. def info(self):
  23. return ["hash, tx, commit counts:%d, %d, %d"%(self.hashCount, self.txCount, self.commitCount)], 0
  24. def set_option(self, key, value):
  25. if key == "serial" and value == "on":
  26. self.serial = True
  27. return 0
  28. def append_tx(self, txBytes):
  29. if self.serial:
  30. txByteArray = bytearray(txBytes)
  31. if len(txBytes) >= 2 and txBytes[:2] == "0x":
  32. txByteArray = hex2bytes(txBytes[2:])
  33. txValue = decode_big_endian(BytesBuffer(txByteArray), len(txBytes))
  34. if txValue != self.txCount:
  35. return None, 1
  36. self.txCount += 1
  37. return None, 0
  38. def get_hash(self):
  39. self.hashCount += 1
  40. if self.txCount == 0:
  41. return "", 0
  42. h = encode_big_endian(self.txCount, 8)
  43. h.reverse()
  44. return str(h), 0
  45. def commit(self):
  46. self.commitCount += 1
  47. return 0
  48. def rollback(self):
  49. return 0
  50. def add_listener(self):
  51. return 0
  52. def rm_listener(self):
  53. return 0
  54. def event(self):
  55. return
  56. if __name__ == '__main__':
  57. l = len(sys.argv)
  58. if l == 1:
  59. port = 46658
  60. elif l == 2:
  61. port = int(sys.argv[1])
  62. else:
  63. print "too many arguments"
  64. quit()
  65. print 'TMSP Demo APP (Python)'
  66. app = CounterApplication()
  67. server = TMSPServer(app, port)
  68. server.main_loop()