From b6e5eac9702e8fb1c47aebddd613933706a0a36a Mon Sep 17 00:00:00 2001 From: Teddy Date: Fri, 23 Aug 2013 08:24:05 +0800 Subject: ... --- server/piztor_server.py | 39 ++++++++++++++++++++++++++++++++++----- server/ptp.rst | 6 +++--- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/server/piztor_server.py b/server/piztor_server.py index 663d2fa..924e268 100644 --- a/server/piztor_server.py +++ b/server/piztor_server.py @@ -1,10 +1,15 @@ import sqlalchemy -import SocketServer -import socket -import select -import time +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 @@ -22,10 +27,30 @@ 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): print "Parsing User Data" pos = -1 @@ -40,7 +65,10 @@ class UserManager(DataManager): raise ReqInvalidError() username = data[0:pos] password = data[pos + 1:] + + print "Trying to login with following info:" print (username, password) + return struct.pack("!BL", 0, 1234) @@ -105,7 +133,7 @@ class PiztorServer(): if len(data) < 1: raise ReqInvalidError() opt_id = struct.unpack("!B", data[0])[0] - reply = PiztorServer.mgr_map[opt_id](opt_id, data[1:]) + reply = PiztorServer.mgr_map[opt_id](opt_id, data[1:], self) sock.sendall(reply) sock.close() @@ -125,5 +153,6 @@ class PiztorServer(): print "Server shutdown" if __name__ == "__main__": + ps = PiztorServer("localhost", 9999) ps.run() diff --git a/server/ptp.rst b/server/ptp.rst index ed5151a..a061878 100644 --- a/server/ptp.rst +++ b/server/ptp.rst @@ -33,9 +33,9 @@ Piztor Transmission Protocol v0.1 :: - +--1b---+-----4b-----+ - | 0x00 | USER_TOKEN | - +-uchar-+-----int----+ + +--1b---+-----4b-----+----1b----+ + | 0x00 | USER_TOKEN | RESERVED | + +-uchar-+-----int----+---uchar--+ - Message Sending -- cgit v1.2.3-70-g09d2