-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_pm.py
executable file
·117 lines (90 loc) · 3.88 KB
/
server_pm.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# Importing section
import http
import json
import sys
import argparse
import logging
from http.server import HTTPServer, BaseHTTPRequestHandler
from urllib.parse import urlparse
from tendo.singleton import SingleInstance
from classes.pm_sidechain_interface import PMSidechainInterface
# --------------------------------------------------------------------------- #
# Functions
# --------------------------------------------------------------------------- #
# Handler request
def handler_class(pmsi_obj, cfg_obj, logger_obj):
class RequestHandler(BaseHTTPRequestHandler):
"""!
Constructor
"""
def __init__(self, *args, **kwargs):
self.pmsi = pmsi_obj
self.cfg = cfg_obj
self.logger = logger_obj
super(RequestHandler, self).__init__(*args, **kwargs)
def manage_response(self, status, msg):
self.send_response(status)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(bytes(msg, 'utf8'))
"""!
Catch a GET request
"""
def do_GET(self):
if 'checkTx' in self.path:
req_status, req_data = self.pmsi.check_tx(self.path.split('/')[-1])
req_data = json.dumps({'code': req_data}, indent=2)
elif 'account' in self.path:
req_status = http.HTTPStatus.OK
req_data = json.dumps(self.pmsi.get_account_info(), indent=2)
elif 'energyPrices' in self.path:
req_status = http.HTTPStatus.OK
req_data = json.dumps(self.pmsi.calc_forecast_energy_prices(), indent=2)
else:
req_status, req_data = self.pmsi.do_query(self.path)
self.logger.info('GET %s?%s HTTP/1.1" %i' % (urlparse(self.path).path, urlparse(self.path).query, req_status))
self.manage_response(req_status, str(req_data))
"""!
Catch a POST request
"""
def do_POST(self):
data_string = self.rfile.read(int(self.headers['Content-Length']))
data_string = data_string.decode('UTF-8')
# Check if the transaction is related to tokens management
if 'Tokens' in self.path:
req_status, tx_hash, msg = pmsi.do_token_transaction(self.path, json.loads(data_string))
else:
req_status, tx_hash, msg = pmsi.do_application_transaction(self.path, json.loads(data_string))
logger.info('POST %s?%s HTTP/1.1" %i' % (urlparse(self.path).path, urlparse(self.path).query, req_status))
self.manage_response(req_status, json.dumps({'tx_hash': tx_hash, 'msg': msg}))
return RequestHandler
# Main
if __name__ == "__main__":
# get input arguments
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('-c', help='configuration file')
arg_parser.add_argument('-l', help='log file')
args = arg_parser.parse_args()
cfg = json.loads(open(args.c).read())
# set logging object
logger = logging.getLogger()
logger.setLevel(logging.INFO)
if not args.l:
log_file = None
else:
log_file = args.l
logger = logging.getLogger()
logging.basicConfig(format='%(asctime)-15s::%(threadName)s::%(levelname)s::%(funcName)s::%(message)s',
level=logging.INFO, filename=log_file)
logger.info('Starting program')
pmsi = PMSidechainInterface(cfg, logger, True)
# REST server thread
HandlerClass = handler_class(pmsi, cfg, logger)
httpd = HTTPServer((cfg['server']['host'], cfg['server']['port']), HandlerClass)
# Try to launch the server
try:
logging.info('Server up on socket %s:%i' % (cfg['server']['host'], cfg['server']['port']))
httpd.serve_forever()
except Exception as e:
logger.error('EXCEPTION: %s' % str(e))
logger.info('Exit program')