-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
101 lines (84 loc) · 3.29 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
# methods: insert, update, query
# call Network class
'''
Simple socket server using threads
from: http://www.binarytides.com/python-socket-server-code-example/
'''
import socket
import sys
from QueryEngine import QueryEngine
from QueryEngine import QueryType
from enum import Enum
from thread import *
from Constants import Constants
from db import db
HOST = '127.0.0.1' # Symbolic name meaning all available interfaces
PORT = 8000 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
#Bind socket to local host and port
try:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, Constants.PORT))
except socket.error as msg:
print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
sys.exit()
print 'Socket bind complete'
#Start listening on socket
s.listen(10)
print 'Socket now listening'
#Function for handling connections. This will be used to create threads
def clientthread(conn):
qe = QueryEngine()
#Sending message to connected client
#conn.send('Welcome to the server. Type something and hit enter\n') #send only takes string
#infinite loop so that function do not terminate and thread do not end.
while True:
#Receiving from client
reply = ''
data = conn.recv(1024)
#print data
params = qe.deserialize(data)
param_type = params[0]
print param_type
print 'PARAMS', params
if param_type == QueryType.SELECT:
reply = db.select(msToSec(params[1]), params[2])
elif param_type == QueryType.INSERT:
if params[2]=='':
pass
else:
db.insert(msToSec(params[1]),params[2], params[3])
reply = "inserting " + data
#reply = db.insert(params[1:])
elif param_type == QueryType.UPDATE:
reply = "updating"
#reply = db.update(params[1:])
elif param_type == QueryType.SELECTRANGE:
#dbResponse = list(db.selectRangeForDisplay(msToSec(params[1]), msToSec(params[2]), params[3]))
dbResponse = list(db.selectRangeAndInterval(msToSec(params[1]), msToSec(params[2]), params[3],params[4]))
dbResponse.insert(0, QueryType.SERVER_REPLY)
reply = qe.serialize(dbResponse)
elif param_type == QueryType.INC_AVG:
reply = db.getRunningAverage(params[1]);
elif param_type == QueryType.INC_COUNT:
reply = db.getAggregateCountInRange(msToSec(params[1]), msToSec(params[2]), params[3])
elif param_type == QueryType.INC_VAR:
reply = db.getRunningVariance(params[1]);
else:
# throw exception
reply = "Invalid arguments, should be start with SELECT, INSERT, or UPDATE"
print reply
conn.sendall(reply)
#conn.close()
conn.close()
def msToSec(milliseconds):
return milliseconds/1000
#now keep talking with the client
while 1:
#wait to accept a connection - blocking call
conn, addr = s.accept()
print 'Connected with ' + addr[0] + ':' + str(addr[1])
#start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.
start_new_thread(clientthread ,(conn,))
s.close()