From af84e3060d5f4f3e97be483e6616c6f3cd872309 Mon Sep 17 00:00:00 2001
From: Teddy <ted.sybil@gmail.com>
Date: Fri, 30 Aug 2013 16:34:22 +0800
Subject: ptp v2.0 std-compliant server

---
 server/piztor/mesg_sender.py |  2 --
 server/piztor/prober.py      | 12 +++++++++---
 server/piztor/server.py      | 28 +++++++++++++++-------------
 3 files changed, 24 insertions(+), 18 deletions(-)

diff --git a/server/piztor/mesg_sender.py b/server/piztor/mesg_sender.py
index 41f2858..2242fc4 100644
--- a/server/piztor/mesg_sender.py
+++ b/server/piztor/mesg_sender.py
@@ -22,7 +22,5 @@ if len(argv) == 4:
 token = user_auth(username, password)
 send_text_mesg(token, username, mesg)
 send_text_mesg(token, username, "a")
-update_location(token, username, 1.2345, 2.468)
-update_location(token, username, 1.2345, 2.468)
 send_text_mesg(token, username, "the last")
 logout(token, username)
diff --git a/server/piztor/prober.py b/server/piztor/prober.py
index 623089f..5f57a94 100644
--- a/server/piztor/prober.py
+++ b/server/piztor/prober.py
@@ -6,8 +6,8 @@ from ptp_send import *
 
 username = "haha"
 password = "haha"
-#username = "1234567890123456789012"
-#password = "world12345678901234567890"
+#username = "12345678901234567890"
+#password = "world123456789012345"
 
 if len(argv) == 2:
     host = argv[1]
@@ -20,5 +20,11 @@ if len(argv) == 3:
 token = user_auth(username, password)
 
 update_sub(token, username, 
-        [(23, 15), (23, 14)])
+        [
+            (23, 15), (23, 15), (23, 255), (23, 255),
+            (23, 15), (23, 15), (23, 255), (23, 255),
+            (23, 15), (23, 15), (23, 255), (23, 255),
+            (23, 15), (23, 15), (23, 255), (23, 255),
+            (23, 15), (23, 15), 
+            ])
 user_info(token, username, 23, 15)
diff --git a/server/piztor/server.py b/server/piztor/server.py
index 6f45ad8..b944f20 100644
--- a/server/piztor/server.py
+++ b/server/piztor/server.py
@@ -81,7 +81,8 @@ class PushLocationData(PushData):
                 
 
 class PushTunnel(object):
-    def __init__(self):
+    def __init__(self, uid):
+        self.uid = uid
         self.pending = deque()
         self.conn = None
         self.blocked = False
@@ -109,7 +110,7 @@ class PushTunnel(object):
     def push(self):
         if self.blocked:
             return
-        print "Pushing via " + str(self)
+        print "Pushing via " + str(self.uid)
         print "Pending size: " + str(len(self.pending))
         logger.info("Pushing...")
         if (self.conn is None) or len(self.pending) == 0:
@@ -175,11 +176,11 @@ class RequestHandler(object):
                     .filter(UserAuth.token == token).one()
 
             if uauth.user.username != username:
-                logger.warning("Toke and username mismatch")
+                logger.warning("Token and username mismatch")
                 return None
             uid = uauth.uid
             if not cls.push_tunnels.has_key(uid):
-                cls.push_tunnels[uid] = PushTunnel()
+                cls.push_tunnels[uid] = PushTunnel(uid)
             return uauth
 
         except NoResultFound:
@@ -324,7 +325,7 @@ class UpdateLocationHandler(RequestHandler):
                 raise struct.error
             lat, lng = struct.unpack("!dd", tail)
         except struct.error:
-            raise BadReqError("Location update: Malformed request body")
+            raise BadReqError("Update location: Malformed request body")
 
         logger.info("Trying to update location with "
                     "(token = {0}, username = {1}, lat = {2}, lng = {3})"\
@@ -377,7 +378,7 @@ class UserInfoHandler(RequestHandler):
                         _SectionSize.GROUP_ID
 
     _failed_response = \
-            lambda : self.pack(struct.pack("!B", _StatusCode.failure))
+            lambda self : self.pack(struct.pack("!B", _StatusCode.failure))
 
 
     def handle(self, tr_data, conn):
@@ -392,7 +393,7 @@ class UserInfoHandler(RequestHandler):
         except struct.error:
             raise BadReqError("User info request: Malformed request body")
 
-        logger.info("Trying to user info with " \
+        logger.info("Trying to get user info with " \
                     "(token = {0}, gid = {1})" \
             .format(get_hex(token), gid))
 
@@ -421,23 +422,24 @@ class UpdateSubscription(RequestHandler):
                         _SectionSize.GROUP_ID * _MAX_SUB_LIST_SIZE + \
                         _SectionSize.PADDING
 
-    def _find_or_create_group(self, gid):
+    def _find_group(self, gid):
         q = self.session.query(GroupInfo).filter(GroupInfo.id == gid)
         entry = q.first()
         if not entry:
-            entry = GroupInfo(gid = gid)
+            raise BadReqError("Group not found")
         self.session.commit()
         return entry
 
     def handle(self, tr_data, conn):
+        print _MAX_AUTH_HEAD_SIZE
+        print _SectionSize.GROUP_ID * _MAX_SUB_LIST_SIZE
+        print _SectionSize.PADDING
+
         self.check_size(tr_data)
-        print get_hex(tr_data)
         logger.info("Reading update subscription data...")
         try:
             token, = struct.unpack("!32s", tr_data[:32])
-            print "a"
             username, tail = RequestHandler.trunc_padding(tr_data[32:])
-            print "b"
             if username is None: 
                 raise struct.error
             sub_list = RequestHandler.unpack_sub_list(tail)
@@ -454,7 +456,7 @@ class UpdateSubscription(RequestHandler):
             logger.warning("Authentication failure")
             return self.pack(struct.pack("!B", _StatusCode.failure))
 
-        uauth.user.sub = map(self._find_or_create_group, sub_list)
+        uauth.user.sub = map(self._find_group, sub_list)
         self.session.commit()
         logger.info("Subscription is updated sucessfully")
 
-- 
cgit v1.2.3-70-g09d2