summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
authorTeddy <[email protected]>2013-08-26 08:50:47 +0800
committerTeddy <[email protected]>2013-08-26 08:50:47 +0800
commit7a606637907476942c1f6686137ada6898d7eb14 (patch)
tree1510da0a6764f18a66958ebae6587353ac6e019c /server
parent537d01ac3e78dec6828e068979dcd061e8f68a98 (diff)
purged the repo
Diffstat (limited to 'server')
-rw-r--r--server/piztor/prob.py8
-rw-r--r--server/piztor/ptp.rst2
-rw-r--r--server/piztor/server.py69
3 files changed, 59 insertions, 20 deletions
diff --git a/server/piztor/prob.py b/server/piztor/prob.py
index 4ccc957..57ce2fe 100644
--- a/server/piztor/prob.py
+++ b/server/piztor/prob.py
@@ -33,6 +33,14 @@ def gen_request_location(token, username, gid):
return data
+def gen_request_user_info(token, username, uid):
+ length = 4 + 1 + 32 + len(username) + 1 + 4
+ data = pack("!LB32s", length, 0x03, token)
+ data += username
+ data += chr(0)
+ data += pack("!L", uid)
+ return data
+
def send(data):
received = None
try:
diff --git a/server/piztor/ptp.rst b/server/piztor/ptp.rst
index 0fee00e..9fc5ff4 100644
--- a/server/piztor/ptp.rst
+++ b/server/piztor/ptp.rst
@@ -132,4 +132,4 @@ Piztor Transmission Protocol v0.3a
``INFO_KEY`` :
:``0x00``: gid (value is a 4-byte ``long int``)
- :``0x01``: sex (value is a 1-byte ``boolean``: ``True`` for male, ``False`` for female)
+ :``0x01``: sex (value is a 1-byte ``boolean``: ``0x01`` for male, ``0x00`` for female)
diff --git a/server/piztor/server.py b/server/piztor/server.py
index 98c67c2..89c7760 100644
--- a/server/piztor/server.py
+++ b/server/piztor/server.py
@@ -48,6 +48,7 @@ class _OptCode:
user_auth = 0x00
location_update = 0x01
location_request= 0x02
+ user_info_request = 0x03
class _StatusCode:
sucess = 0x00
@@ -261,6 +262,35 @@ class LocationRequestHandler(RequestHandler):
class UserInfoRequestHandler(RequestHandler):
+ _failed_response_size = \
+ _SectionSize.LENGTH + \
+ _SectionSize.OPT_ID + \
+ _SectionSize.STATUS
+
+ _fail_response = \
+ struct.pack("!LBB", _failed_response_size,
+ _OptCode.user_info_request,
+ _StatusCode.failure)
+
+ @classmethod
+ def pack_int(cls, val):
+ return struct.pack("!L", val)
+
+ @classmethod
+ def pack_bool(cls, val):
+ return struct.pack("!B", 0x01 if val else 0x00)
+
+ _code_map = {0x00 : ('gid', pack_int),
+ 0x01 : ('sex', pack_bool)}
+
+ @classmethod
+ def pack_entry(cls, user, entry_code):
+ attr, pack_method = _code_map(entry_code)
+ info_key = entry_code
+ info_value = getattr(user, attr)
+ return struct.pack("!B", info_key) + pack_method(info_value) + \
+ struct.pack("!B", 0x00)
+
def handle(self, tr_data):
logger.info("Reading user info request data...")
@@ -271,41 +301,42 @@ class UserInfoRequestHandler(RequestHandler):
raise struct.error
uid, = struct.unpack("!L", tail)
except struct.error:
- raise BadReqError("Location request: Malformed request body")
+ raise BadReqError("User info request: Malformed request body")
logger.info("Trying to request locatin with " \
- "(token = {0}, gid = {1})" \
- .format(get_hex(token), gid))
+ "(token = {0}, uid = {1})" \
+ .format(get_hex(token), uid))
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)
+ return UserInfoRequestHandler._fail_response
+ # TODO: check the relationship between user and quser
+ user = uauth.user
- 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))
+ reply = struct.pack("!BB", _OptCode.user_info_request,
+ _StatusCode.sucess)
+ try:
+ quser = session.query(UserModel) \
+ .filter(UserModel.id == uid).one()
+ except NoResultFound:
+ logger.info("No such user: {0}".format(username))
+ return UserInfoRequestHandler._fail_response
- for user in ulist:
- loc = user.location
- reply += struct.pack("!Ldd", user.id, loc.lat, loc.lng)
+ except MultipleResultsFound:
+ raise DBCorruptedError()
+ for code in _code_map:
+ reply += UserInfoRequestHandler.pack_entry(quser, code)
return reply
handlers = [UserAuthHandler,
LocationUpdateHandler,
- LocationRequestHandler]
+ LocationRequestHandler,
+ UserInfoRequestHandler]
def check_header(header):
return 0 <= header < len(handlers)