summaryrefslogtreecommitdiff
path: root/server/piztor_server.py
blob: a19989a69fe11bcd4860500656b7e55058d086de (plain) (blame)
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
import sqlalchemy
import SocketServer, socket, select
import struct

from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

engine = create_engine('sqlite:///t.sqlite', echo = True)
Base = declarative_base()
Session = sessionmaker(bind=engine)

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(ReqInvalidError, self).__init__("Invalid token")

class DataManager(object):
    pass

class UserManager(DataManager):
    class User(Base):
        __tablename__ = 'users'
        id = Column(Integer, primary_key = True)
        username = Column(String)
        password = Column(String)

    def get_uid_by_token(self, token):
        try:
            return self.active_sessions[token]
        except:
            raise TokenInvalidError()

    def __init__(self):
        Base.metadata.create_all(engine)
        self.active_sessions = dict()

    def authentication_handle(self, opt_type, data, srv):
        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()
        q = session.query(User).filter(User.username == username)
        entry = q.first()
        if entry.password != password:  # Auth failed
            return struct.pack("!BL"
        
        return struct.pack("!BL", 0, 1234)
        

class MesgManager(DataManager):
    def mesg_sending_handle(self, opt_type, data, srv):
        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):
    def location_update_handle(self, opt_type, data, srv):
        print "Parsing Loc Data"
        try:
            if len(data) < 8:
                raise ReqInvalidError()
            sender_token, lat, lont = struct.unpack("!Ldd", data)
            print (sender_token, lat, lont)
            return struct.pack("!B", 2)
        except struct.error:
            raise ReqInvalidError()



class PiztorServer():

    user_mgr = UserManager()
    mesg_mgr = MesgManager()
    location_mgr = LocationManager()

    mgr_map = [ user_mgr.authentication_handle, 
                mesg_mgr.mesg_sending_handle, 
                location_mgr.location_update_handle ] 

    class GenericHandler(SocketServer.StreamRequestHandler):

        def handle(self):
            sock = self.request
            sock.setblocking(0)
            data = ""
            while True:
                ready = select.select([sock], [], [], 1)
                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:"
            print data
            print "===="

            if len(data) < 1: 
                raise ReqInvalidError()
            opt_id = struct.unpack("!B", data[0])[0]
            reply = PiztorServer.mgr_map[opt_id](opt_id, data[1:], self)
            sock.sendall(reply)
            sock.close()

    def __init__(self, host, port):
        srv = SocketServer.TCPServer((host, port), 
                                    PiztorServer.GenericHandler)
        srv.timeout = 2
        self.server = srv


    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()