-
Notifications
You must be signed in to change notification settings - Fork 9
/
server.py
executable file
·184 lines (168 loc) · 5.47 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/python
import time
import re
import site
import os
import socket
import struct
import logging
import json
# load mockup data from json file
with open('example_import.json') as data_file:
data = json.load(data_file)
# load mockup data from json file
with open('example_import_mar2017.json') as data_file:
data_mar2017 = json.load(data_file)
try:
from kafka import KeyedProducer, KafkaConsumer, KafkaClient
except ImportError:
pass
# Use included module
site.addsitedir('lib/')
from kafka import KeyedProducer, KafkaConsumer, KafkaClient
# Define globals
TPC_RESPONSE = "Response"
TPC_REQUEST = "Request"
KAFKA_GRP_ID = "1"
#DEBUG = False
DEBUG = True
# Get default gateway from /proc and use it as host address of Zookeeper
def get_default_gateway_linux():
# open /rpc/net/route for reading
with open("/proc/net/route") as fh:
# read line by line
for line in fh:
#split fields on space
fields = line.strip().split()
# disregard fields we do not need
if fields[1] != '00000000' or not int(fields[3], 16) & 2:
continue
# covert read field to ipv4 format
return socket.inet_ntoa(struct.pack("<L", int(fields[2], 16)))
def getVersion(data):
jdata = json.loads(data)
# this is for Nov2016 - KafkaMappedConnector.scala
if 'version' in jdata:
return json.loads(data)["version"]
# this is for Mar2017 later - KafkaMappedConnector_vMar2017
elif 'messageFormat' in jdata:
return json.loads(data)["messageFormat"]
# Split message and extract function name and arguments
# then pass them to obp.py for further processing
#
def processMessage(message):
reqFunc = None
reqArgs = None
decoded = message.decode()
# extract function name
version = getVersion(decoded)
print(version)
# check if function name exists in obp.py
module_name='obp_v'+version
obp = __import__(module_name)
reqFunc = obp.getFuncName(decoded)
print(reqFunc)
# return error if empty
if reqFunc == None:
return '{"error":"empty request"}'
# return error if function name if not alphanumeric
if not re.match("^[a-zA-Z0-9_-]*$", reqFunc):
return '{"error":"llegal request"}'
if (hasattr(obp, reqFunc)):
# extract function arguments
reqArgs = obp.getArguments(decoded)
print(reqArgs)
# create dictionary if not empty
if reqArgs != None:
# set the data according to version
if version =="Nov2016" :
obp.data = data
else:
obp.data = data_mar2017
# execute function from obp.py and return result
return getattr(obp, reqFunc)(reqArgs)
return '{"error":"arguments missing"}'
else:
return '{"error":"unknown request"}'
# determine if running on localhost or in docker container
kafka_host = "localhost:9092"
try:
os.environ["ADVERTISED_HOST"]
except KeyError:
pass
else:
kafka_host = os.environ["ADVERTISED_HOST"] + ":9092"
print("Connecting to " + kafka_host + "...")
# try connecting to Kafka until successful
disconnected = True
while (disconnected):
try:
status = KeyedProducer( KafkaClient(kafka_host) )
disconnected = False
except Exception as e:
pass
disconnected = True
print("Waiting for " + kafka_host + " to become available...")
time.sleep(3)
# send initial status messages
try:
status.send_messages( TPC_REQUEST.encode("UTF8"), "status","check".encode("UTF8"))
status.send_messages( TPC_RESPONSE.encode("UTF8"), "status","check".encode("UTF8"))
except Exception as e:
pass
print("Connected to " + kafka_host + ".")
# init logger
logging.basicConfig(format='%(asctime)s.%(msecs)s:%(name)s:%(thread)d:%(levelname)s:%(process)d:%(message)s', level=logging.ERROR)
# Main loop waits indefinitely for requests
# then passes them to processMessage()
#
while (True):
try:
# kafka producer
producer = KeyedProducer( KafkaClient(kafka_host) )
if (DEBUG):
if (producer):
print("producer: OK")
consumer = KafkaConsumer( TPC_REQUEST,
group_id=KAFKA_GRP_ID,
bootstrap_servers=[kafka_host] )
if (DEBUG):
if (consumer):
print("consumer: OK")
else:
print("consumer: ERROR")
# wait for new message in queue
if (DEBUG):
print("Connected. Waiting for messages...")
for message in consumer:
start_time = time.time()
if (DEBUG):
# debug output
print("%s:%d:%d: key=%s value=%s" % ( message.topic,
message.partition,
message.offset,
message.key,
message.value))
# skip processing of internal status messages
if message.key == "status" and message.value.decode() == "check":
next
# send received message to processing
result = processMessage(message.value)
#time.sleep(1)
if (DEBUG):
# debug output
print(result)
print("")
if result != None:
# send result message back to kafka
producer.send_messages( TPC_RESPONSE.encode("UTF8"),
message.key,
result.encode("UTF8"))
print("--- %s seconds ---" % (time.time() - start_time))
except Exception as e:
pass
print ("Exception: %s" % e)
time.sleep(1)
# print disconnect message, sleep for a while, and try to reconnect
print("Info: Kafka disconnected. Reconnecting...")
time.sleep(1)