-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
93 lines (72 loc) · 2.51 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# https://gist.github.com/ingoogni/a7f7c0fed9e96b3545e9a8c6139d80b0
# pip install cherrypy
# ------------------------------------------
import os
import sys
import kth
import cherrypy
import cherrypy_SSE
# import time
# import threading
# import random
# A typical reorganization consists of one incoming and zero outgoing blocks.
def subscribe_blockchain_handler(ec, fork_height, incoming, outgoing):
if execut.stopped or ec == 1:
return False
if ec != 0:
execut.stop()
return False
# Nothing to do here.
if not incoming or incoming.count == 0:
return True
channel = 'cpu'
msg = "event: time\ndata: {}\n\n".format(str(fork_height))
cherrypy.engine.publish(channel, msg)
return True
class Root():
@cherrypy.expose
def pubcpu(self):
"""
publishes data from the subscribed channel...
"""
channel = 'cpu'
doorman = cherrypy_SSE.Portier(channel)
cherrypy.response.headers["Content-Type"] = "text/event-stream"
def pub():
for message in doorman.messages():
# print('-*-*-*-*-*-*-* message: ')
# print(message)
try:
yield message
except GeneratorExit:
# cherrypy shuts down the generator when the client
# disconnects. Catch disconnect and unsubscribe to clean up
doorman.unsubscribe()
return
return pub()
pubcpu._cp_config = {'response.stream': True}
@cherrypy.expose
def index(self):
return open('index.html')
if __name__ == '__main__':
# with kth.Executor("/home/fernando/execution_tests/btc_mainnet.cfg", sys.stdout, sys.stderr) as execut:
# with kth.Executor("/home/fernando/execution_tests/btc_mainnet.cfg") as execut:
execut = kth.Executor("/home/fernando/execution_tests/btc_mainnet.cfg", sys.stdout, sys.stderr)
if not os.path.isdir("./blockchain"):
res = execut.init_chain()
print(res)
res = execut.run_wait()
print(res)
execut.chain.subscribe_blockchain(subscribe_blockchain_handler)
conf = {
'global': {
# 'server.socket_host': "10.0.0.4",
'server.socket_host': "127.0.0.1",
'server.socket_port': 8080,
'server.thread_pool': 25,
'server.socket_queue_size': 10,
'engine.autoreload.on': False,
}
}
cherrypy.quickstart(Root(), config=conf)
execut._destroy()