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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
import sqlalchemy
import SocketServer, socket, select
import struct
import os
from sqlalchemy import create_engine
from sqlalchemy import Column, Integer, String, Float
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from random import randint
engine = create_engine('sqlite:///t.sqlite', echo = False)
Base = declarative_base()
Session = sessionmaker(bind=engine)
def get_hex(data):
return "".join([hex(ord(c))[2:].zfill(2) for c in data])
class PiztorError(Exception):
def __init__(self, msg):
self.err_msg = msg
def __str__(self, msg):
return self.err_msg
class ConnectionError(PiztorError):
pass
class ReqReadError(ConnectionError):
def __init__(self):
super(ReqReadError, self).__init__("Error while reading request")
class ReqInvalidError(ConnectionError):
def __init__(self):
super(ReqInvalidError, self).__init__("Invalid request")
class TokenInvalidError(ConnectionError):
def __init__(self):
super(TokenInvalidError, self).__init__("Invalid token")
class DataManager(object):
def __init__(self, piz_srv):
self.piz_srv = piz_srv
class UserManager(DataManager):
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key = True)
gid = Column(Integer)
username = Column(String)
password = Column(String)
token = Column(Integer)
def get_user_by_token(self, token):
session = Session()
User = UserManager.User
entries = session.query(User).filter(User.token == token).all()
if len(entries) == 0:
raise TokenInvalidError()
return entries[0]
def authentication_handle(self, opt_type, data):
print "Parsing User Data"
pos = -1
for i in xrange(0, len(data)):
if data[i] == '\0':
print i
if pos != -1:
raise ReqInvalidError()
pos = i
break
if pos == -1:
raise ReqInvalidError()
username = data[0:pos]
password = data[pos + 1:]
print "Trying to login with following info:"
print (username, password)
session = Session()
entries = session.query(UserManager.User). \
filter(UserManager.User.username == username).all()
if len(entries) == 0:
return struct.pack("!BLB", 0, 0, 1)
entry = entries[0]
if entry.password != password: # Auth failed
print "Login failed!"
return struct.pack("!BLB", 0, 0, 1)
else: # Succeeded
print "Logged in sucessfully!"
entry.token = randint(0, 2147483647)
session.commit()
return struct.pack("!BLB", 0, entry.token, 0)
class MesgManager(DataManager):
def mesg_sending_handle(self, opt_type, data):
print "Parsing Mesg Data"
try:
if len(data) < 8:
raise ReqInvalidError()
sender_token, recv_id = struct.unpack("!LL", data[:8])
msg = data[8:]
print (sender_token, recv_id, msg)
return struct.pack("!B", 1)
except struct.error:
raise ReqInvalidError()
class LocationManager(DataManager):
class LocationInfo(Base):
__tablename__ = "location_info"
uid = Column(Integer, primary_key = True)
lat = Column(Float(precesion = 64))
lng = Column(Float(precesion = 64))
# More: last_update
def location_update_handle(self, opt_type, data):
print "Parsing a Location Update"
try:
if len(data) < 8:
raise ReqInvalidError()
sender_token, lat, lng = struct.unpack("!Ldd", data)
print "Updating location data with following info:"
print (sender_token, lat, lng)
user = self.piz_srv. \
user_mgr.get_user_by_token(sender_token)
session = Session()
LInfo = LocationManager.LocationInfo
q = session.query(LInfo).filter(LInfo.uid == user.id)
entry = q.first()
entry.lat = lat
entry.lng = lng
session.commit()
print "Location update succeeded!"
return struct.pack("!BB", 2, 0)
except TokenInvalidError:
print "Location update failed!"
return struct.pack("!BB", 2, 1)
except struct.error:
raise ReqInvalidError()
def location_request_handle(self, opt_type, data):
print "Parsing a Location Request"
try:
if len(data) != 8:
raise ReqInvalidError()
sender_token, gid = struct.unpack("!LL", data)
print "Requesting location data with following info:"
print (sender_token, gid)
session = Session()
UInfo = UserManager.User
LInfo = LocationManager.LocationInfo
user_list = session.query(UInfo).filter(UInfo.gid == gid).all()
reply = struct.pack("!BL", 3, len(user_list))
for user in user_list:
loc = session.query(LInfo).filter(LInfo.uid == user.id).first()
reply += struct.pack("!Ldd", user.id, loc.lat, loc.lng)
print get_hex(reply)
return reply
except struct.error:
raise ReqInvalidError()
class PiztorServer():
class GenericHandler(SocketServer.StreamRequestHandler):
def handle(self):
print self.piz_srv
sock = self.request
sock.settimeout(100)
# sock.setblocking(0)
data = ""
try:
while True:
# ready = select.select([sock], [], [], 10)
# if not ready[0]:
# raise ReqReadError()
buff = sock.recv(4096)
if len(buff) == 0:
break # terminated
else:
data += buff
sock.shutdown(socket.SHUT_RD)
print "Got the data:" + get_hex(data)
if len(data) < 1:
print "invalid length"
raise ReqInvalidError()
opt_id = struct.unpack("!B", data[0])[0]
print opt_id
reply = self.piz_srv.mgr_map[opt_id](opt_id, data[1:])
sock.sendall(reply)
finally:
sock.close()
class ForkingEchoServer(SocketServer.ForkingMixIn, SocketServer.TCPServer):
pass
def __init__(self, host, port):
PiztorServer.GenericHandler.piz_srv = self
srv = PiztorServer.ForkingEchoServer((host, port),
PiztorServer.GenericHandler)
srv.request_queue_size = 100
# srv.timeout = 2
self.server = srv
self.user_mgr = UserManager(self)
self.mesg_mgr = MesgManager(self)
self.location_mgr = LocationManager(self)
self.mgr_map = [ self.user_mgr.authentication_handle,
self.mesg_mgr.mesg_sending_handle,
self.location_mgr.location_update_handle,
self.location_mgr.location_request_handle]
Base.metadata.create_all(engine)
def run(self):
try:
self.server.serve_forever()
except KeyboardInterrupt:
print "Exiting..."
self.server.shutdown()
print "Server shutdown"
if __name__ == "__main__":
ps = PiztorServer("localhost", 9999)
ps.run()
|