summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
authorTeddy <[email protected]>2013-08-29 11:38:13 +0800
committerTeddy <[email protected]>2013-08-29 11:38:13 +0800
commit95c63e2cdc05f687a976e9e87e1f17998c20349b (patch)
tree22e68152c1012b8cd95748a86719f52c3f86cccb /server
parent0adb6bf0fed763b6afdf25da469c32a473c3bdd0 (diff)
ptp v1.0b
Diffstat (limited to 'server')
-rw-r--r--server/piztor/ptp.rst428
-rw-r--r--server/piztor/server.py19
2 files changed, 229 insertions, 218 deletions
diff --git a/server/piztor/ptp.rst b/server/piztor/ptp.rst
index d5a5fde..19eab65 100644
--- a/server/piztor/ptp.rst
+++ b/server/piztor/ptp.rst
@@ -1,232 +1,242 @@
-Piztor Transmission Protocol v1.0a
+Piztor Transmission Protocol v1.0b
----------------------------------
-- General
-
- - Request
-
- ::
-
- +---4b---+---1b---+-------?b--------+
- | LENGTH | OPT_ID | SPECIFIC DATA |
- +--int---+-uchar--+-----------------+
-
- - Response
-
- ::
-
- +---4b---+---1b---+------?b---------+
- | LENGTH | OPT_ID | SPECIFIC DATA |
- +--int---+-uchar--+-----------------+
-
- Notice:
-
- - In following sections, ``LENGTH`` part is left out for clarity.
- - ``PADDING`` has value ``0``.
- - ``string`` type structure:
+- Pull
+ - General
+
+ - Request
+
::
-
- +-------?b-------+---------+
- | STRING_CONTENT | PADDING |
- +----------------+---------+
-
- - ``AUTH_HEAD`` structure:
-
+
+ +---4b---+---1b---+-------?b--------+
+ | LENGTH | OPT_ID | SPECIFIC_DATA |
+ +--int---+-uchar--+-----------------+
+
+ - Response
+
::
+
+ +---4b---+---1b---+------?b---------+
+ | LENGTH | OPT_ID | SPECIFIC_DATA |
+ +--int---+-uchar--+-----------------+
+
+ Notice:
+
+ - In following sections, ``LENGTH`` part is left out for clarity.
+ - ``PADDING`` has value ``0``.
+ - ``string`` type structure:
+
+ ::
+
+ +-------?b-------+---------+
+ | STRING_CONTENT | PADDING |
+ +----------------+---------+
+
+ - ``AUTH_HEAD`` structure:
+
+ ::
+
+ +----32b-----+----?b----+
+ | USER_TOKEN | USERNAME |
+ +----raw-----+--string--+
+
+ - Authentication
+
+ - Request
+
+ ::
+
+ +--1b---+-----?b---+----?b----+
+ | 0x00 | USERNAME | PASSWORD |
+ +-uchar-+--string--+--string--+
+
+ - Response
+
+ ::
+
+ +--1b---+---1b---+---4b----+----32b-----+
+ | 0x00 | STATUS | USER_ID | USER_TOKEN |
+ +-uchar-+--uchar-+---int---+----raw-----+
+
+ ``STATUS`` :
+
+ - ``0x00`` for success
+ - ``0x01`` for failure
+
+ - Location Update
+
+ - Request
+
+ ::
+
+ +--1b---+-----?b------+----8b------+------8b-----+
+ | 0x01 | AUTH_HEAD | LATITUDE | LONGITUDE |
+ +-uchar-+-------------+---double---+---double----+
+
+ - Response
+
+ ::
+
+ +--1b---+---1b---+
+ | 0x01 | STATUS |
+ +-uchar-+--uchar-+
+
+ ``STATUS`` :
+
+ - ``0x00`` for success
+ - ``0x01`` for invalid token
+
+ - Location Information
+
+ - Request
+
+ ::
+
+ +--1b---+------?b------+------2b-----+
+ | 0x02 | AUTH_HEAD | GROUP_ID |
+ +-uchar-+--------------+-------------+
+
+ ``GROUP_ID``:
+
+ ::
+
+ +---1b----+---1b---+
+ | COMP_ID | SEC_ID |
+ +--uchar--+-uchar--+
+
+ - Response
+
+ ::
+
+ +--1b---+---1b---+------20b-------+-----+
+ | 0x02 | STATUS | LOCATION_ENTRY | ... |
+ +-uchar-+-uchar--+----------------+-----+
+
+ ``LOCATION_ENTRY`` :
+
+ ::
+
+ +---4b----+----8b----+-----8b----+
+ | USER_ID | LATITUDE | LONGITUDE |
+ +---int---+--double--+--double---+
+
+ - User Information
+
+ - Request
+
+ ::
+
+ +--1b---+------?b------+------4b-----+
+ | 0x03 | AUTH_HEAD | USER_ID |
+ +-uchar-+--------------+-----int-----+
+
+ - Response
+
+ ::
+
+ +--1b---+---1b---+------?b-----+-----+
+ | 0x03 | STATUS | UINFO_ENTRY | ... |
+ +-uchar-+-uchar--+-------------+-----+
+
+ ``UINFO_ENTRY`` :
+
+ ::
+
+ +----1b----+-----?b-----+
+ | INFO_KEY | INFO_VALUE |
+ +--uchar---+------------+
+
+ ``INFO_KEY`` :
+
+ :``0x00``: gid (value is a 2-byte ``GROUP_ID``)
+ :``0x01``: sex (value is a 1-byte ``boolean``: ``0x01`` for male, ``0x00`` for female)
+
+ - User Logout
+
+ - Request
+
+ ::
+
+ +--1b--+-----?b------+
+ | 0x04 | AUTH_HEAD |
+ +------+-------------+
+
+ - Response
+
+ ::
+
+ +--1b--+---1b---+
+ | 0x04 | STATUS |
+ +------+--------+
+
+ - Open Push Tunnel
+
+ - Request
+
+ ::
+
+ +--1b--+-----?b------+
+ | 0x05 | AUTH_HEAD |
+ +------+-------------+
+
+ - Response
+
+ ::
+
+ +--1b--+---1b---+
+ | 0x05 | STATUS |
+ +------+--------+
+
+ - Send Text Message
+
+ - Request
+
+ ::
+
+ +--1b--+----?b-----+----?b----+
+ | 0x06 | AUTH_HEAD | MESSAGE |
+ +------+-----------+--string--+
+
+ - Response
+
+ ::
+
+ +--1b--+---1b---+
+ | 0x06 | STATUS |
+ +------+--------+
- +----32b-----+----?b----+
- | USER_TOKEN | USERNAME |
- +----raw-----+--string--+
-
-- Authentication
-
- - Request
-
- ::
-
- +--1b---+-----?b---+----?b----+
- | 0x00 | USERNAME | PASSWORD |
- +-uchar-+--string--+--string--+
-
- - Response
-
- ::
-
- +--1b---+---1b---+---4b----+----32b-----+
- | 0x00 | STATUS | USER_ID | USER_TOKEN |
- +-uchar-+--uchar-+---int---+----raw-----+
-
- ``STATUS`` :
-
- - ``0x00`` for success
- - ``0x01`` for failure
-
-- Location Update
-
- - Request
-
- ::
-
- +--1b---+-----?b------+----8b------+------8b-----+
- | 0x01 | AUTH_HEAD | LATITUDE | LONGITUDE |
- +-uchar-+-------------+---double---+---double----+
-
- - Response
-
- ::
-
- +--1b---+---1b---+
- | 0x01 | STATUS |
- +-uchar-+--uchar-+
-
- ``STATUS`` :
-
- - ``0x00`` for success
- - ``0x01`` for invalid token
-
-- Location Information
-
- - Request
-
- ::
-
- +--1b---+------?b------+------2b-----+
- | 0x02 | AUTH_HEAD | GROUP_ID |
- +-uchar-+--------------+-------------+
-
- ``GROUP_ID``:
-
- ::
-
- +---1b----+---1b---+
- | COMP_ID | SEC_ID |
- +--uchar--+-uchar--+
-
- - Response
-
- ::
-
- +--1b---+---1b---+------20b-------+-----+
- | 0x02 | STATUS | LOCATION_ENTRY | ... |
- +-uchar-+-uchar--+----------------+-----+
-
- ``LOCATION_ENTRY`` :
-
- ::
-
- +---4b----+----8b----+-----8b----+
- | USER_ID | LATITUDE | LONGITUDE |
- +---int---+--double--+--double---+
-
-- User Information
+- Push Notification
- - Request
+ - General Request
::
- +--1b---+------?b------+------4b-----+
- | 0x03 | AUTH_HEAD | USER_ID |
- +-uchar-+--------------+-----int-----+
-
- - Response
-
- ::
+ +---1b---+-------32b--------+-------?b------+
+ | OPT_ID | PUSH_FINGERPRINT | SPECIFIC_DATA |
+ +--------+------------------+---------------+
- +--1b---+---1b---+------?b-----+-----+
- | 0x03 | STATUS | UINFO_ENTRY | ... |
- +-uchar-+-uchar--+-------------+-----+
+ - Acknowledgement
- ``UINFO_ENTRY`` :
-
::
- +----1b----+-----?b-----+
- | INFO_KEY | INFO_VALUE |
- +--uchar---+------------+
-
- ``INFO_KEY`` :
-
- :``0x00``: gid (value is a 2-byte ``GROUP_ID``)
- :``0x01``: sex (value is a 1-byte ``boolean``: ``0x01`` for male, ``0x00`` for female)
-
-- User Logout
-
- - Request
-
- ::
-
- +--1b--+-----?b------+
- | 0x04 | AUTH_HEAD |
- +------+-------------+
-
- - Response
-
- ::
-
- +--1b--+---1b---+
- | 0x04 | STATUS |
- +------+--------+
-
-- Open Push Tunnel
-
- - Request
-
- ::
-
- +--1b--+-----?b------+
- | 0x05 | AUTH_HEAD |
- +------+-------------+
-
- - Response
-
- ::
-
- +--1b--+---1b---+
- | 0x05 | STATUS |
- +------+--------+
-
-- Send Text Message
-
- - Request
-
- ::
-
- +--1b--+----?b-----+----?b----+
- | 0x06 | AUTH_HEAD | MESSAGE |
- +------+-----------+--string--+
-
- - Response
-
- ::
-
- +--1b--+---1b---+
- | 0x06 | STATUS |
- +------+--------+
-
-- Push
-
- - Acknowledgement
-
- ::
-
- +--1b--+-------32b---------+
- | 0x00 | PUSH_FINGERPRINT |
- +------+-------------------+
+ +---1b---+-------32b---------+
+ | OPT_ID | PUSH_FINGERPRINT |
+ +--------+-------------------+
- Text Message
- ::
+ ::
- +--1b--+-------32b---------+----?b----+
- | 0x00 | PUSH_FINGERPRINT | MESSAGE |
- +------+-------------------+--string--+
+ ----+----?b----+
+ ... | MESSAGE |
+ ----+--string--+
- User Location Update
- ::
+ ::
- +--1b--+-------32b---------+-------?b-------+
- | 0x01 | PUSH_FINGERPRINT | LOCATION_ENTRY |
- +------+-------------------+----------------+
+ ----+-------?b-------+
+ ... | LOCATION_ENTRY |
+ ----+----------------+
diff --git a/server/piztor/server.py b/server/piztor/server.py
index 6901400..5635242 100644
--- a/server/piztor/server.py
+++ b/server/piztor/server.py
@@ -67,19 +67,20 @@ class _StatusCode:
class PushData(object):
from hashlib import sha256
- def __init__(self, data):
- self.data = data
+ def pack(self, optcode, data):
self.finger_print = sha256(data).digest()
+ buff = struct.pack("!B32s", optcode, self.finger_print)
+ buff += data
+ buff = struc.pack("!L", _SectionSize.LENGTH + len(buff)) + buff
+ self.data = data
class PushTextMesgData(PushData):
def __init__(self, mesg):
- self.finger_print = sha256(mesg).digest()
- logger.info("Mesg: %s", mesg)
- buff = struct.pack("!B32s", 0x00, self.finger_print)
- buff += mesg
- buff += chr(0)
- buff = struct.pack("!L", _SectionSize.LENGTH + len(buff)) + buff
- self.data = buff
+ self.pack(0x00, mesg + chr(0))
+
+class PushLocationData(PushData):
+ def __init__(self, uid, lat, lng):
+ self.pack(0x01, struct.pack("!dd", lat, lng))
class PushTunnel(object):