From 537d01ac3e78dec6828e068979dcd061e8f68a98 Mon Sep 17 00:00:00 2001 From: Teddy Date: Sun, 25 Aug 2013 21:34:38 +0800 Subject: ptp fixed --- server/piztor/ptp.rst | 2 +- server/piztor/server.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) (limited to 'server') diff --git a/server/piztor/ptp.rst b/server/piztor/ptp.rst index 004bbd9..0fee00e 100644 --- a/server/piztor/ptp.rst +++ b/server/piztor/ptp.rst @@ -110,7 +110,7 @@ Piztor Transmission Protocol v0.3a :: +--1b---+------?b------+------4b-----+ - | 0x02 | AUTH_HEAD | USER_ID | + | 0x03 | AUTH_HEAD | USER_ID | +-uchar-+--------------+-----int-----+ - Response diff --git a/server/piztor/server.py b/server/piztor/server.py index 2397225..98c67c2 100644 --- a/server/piztor/server.py +++ b/server/piztor/server.py @@ -258,6 +258,50 @@ class LocationRequestHandler(RequestHandler): reply += struct.pack("!Ldd", user.id, loc.lat, loc.lng) return reply + +class UserInfoRequestHandler(RequestHandler): + + def handle(self, tr_data): + logger.info("Reading user info request data...") + + try: + token, = struct.unpack("!32s", tr_data[:32]) + username, tail = RequestHandler.trunc_padding(tr_data[32:]) + if username is None: + raise struct.error + uid, = struct.unpack("!L", tail) + except struct.error: + raise BadReqError("Location request: Malformed request body") + + logger.info("Trying to request locatin with " \ + "(token = {0}, gid = {1})" \ + .format(get_hex(token), gid)) + + session = self.Session() + uauth = RequestHandler.get_uauth(token, username, session) + # Auth failure + if uauth is None: + logger.warning("Authentication failure") + return struct.pack("!LBBL", LocationRequestHandler \ + ._location_request_response_size(0), + _OptCode.location_request, + _StatusCode.failure, + 0) + + ulist = session.query(UserModel).filter(UserModel.gid == gid).all() + reply = struct.pack( + "!LBBL", + LocationRequestHandler._location_request_response_size(len(ulist)), + _OptCode.location_request, + _StatusCode.sucess, + len(ulist)) + + for user in ulist: + loc = user.location + reply += struct.pack("!Ldd", user.id, loc.lat, loc.lng) + + return reply + handlers = [UserAuthHandler, LocationUpdateHandler, -- cgit v1.2.3-70-g09d2