From 3a7a46b33cb7654ebb1a3f8f85ae4f0b8de78f88 Mon Sep 17 00:00:00 2001 From: Teddy Date: Thu, 22 Aug 2013 17:48:07 +0800 Subject: added demo server --- server/client.py | 31 ++++++++++++ server/piztor_server.py | 126 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 157 insertions(+) create mode 100644 server/client.py create mode 100644 server/piztor_server.py (limited to 'server') diff --git a/server/client.py b/server/client.py new file mode 100644 index 0000000..b1a6d86 --- /dev/null +++ b/server/client.py @@ -0,0 +1,31 @@ +import socket +import sys +from struct import * + +HOST, PORT = "localhost", 9999 +data = "" + +sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + +#data = pack("!B", 0) +#data += "hello" +#data += "\0" +#data += "world" + +#data = pack("!Bll", 1, 1234, 5678) +#data += "hello, world!" + +data = pack("!Bldd", 2, 1234, 123.123, 12323.23222) +print data + +try: + + sock.connect((HOST, PORT)) + sock.sendall(data) + sock.shutdown(socket.SHUT_WR) + received = sock.recv(1024) +finally: + sock.close() + +print "Sent: {}".format(data) +print "Received: {}".format(received[0]) diff --git a/server/piztor_server.py b/server/piztor_server.py new file mode 100644 index 0000000..3809f22 --- /dev/null +++ b/server/piztor_server.py @@ -0,0 +1,126 @@ +import sqlalchemy +import SocketServer +import socket +import select +import time +import struct + +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 DataManager(object): + pass + +class UserManager(DataManager): + def 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 (username, password) + return struct.pack("!Bl", 0, 1234) + + +class MesgManager(DataManager): + def 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): + def handle(self, opt_type, data): + 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(): + + mgr_map = [ UserManager(), + MesgManager(), + LocationManager() ] + + 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].handle( + opt_id, data[1:]) + 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() -- cgit v1.2.3-70-g09d2