/
server.py
52 lines (38 loc) · 1.49 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
import Data_pb2
from twisted.protocols import basic
from twisted.internet import reactor, protocol
# from twisted.python import log
from data import STORE, RETRIEVE
from db import dbpool, SQL_CREATE_CAR_TABLE, get_car_info_by_serial_number, insert_car_info
# log.startLogging(sys.stdout)
class CarCenter(basic.LineReceiver):
def dataReceived(self, data):
proto_class = Data_pb2.Data()
proto_class.ParseFromString(data)
print proto_class
action = proto_class.action
# log.msg("Server action: {}".format(action))
if action == STORE: # save to DB
car_data = proto_class.car
# log.msg("Store: {}".format(car_data))
insert_car_info(car_data)
if action == RETRIEVE:
serial_number = str(proto_class.car.serialNumber)
print "get data from DB, serial_number={}".format(serial_number)
d = get_car_info_by_serial_number(serial_number)
# TODO: if no data were found need to send correctly notify client
d.addCallback(self.send_results)
def send_results(self, results):
# TODO: use protobuf structure to send it back to server
s = ''
for elt in results:
s += str(elt)
self.transport.write(s)
def main():
dbpool.runQuery(SQL_CREATE_CAR_TABLE)
factory = protocol.ServerFactory()
factory.protocol = CarCenter
reactor.listenTCP(8000, factory)
reactor.run()
if __name__ == '__main__':
main()