From 04cd785f24d779996df8672938372467af6c9a82 Mon Sep 17 00:00:00 2001 From: Teddy Date: Fri, 30 Aug 2013 22:27:18 +0800 Subject: ... --- server/piztor/ptp.rst | 1 + server/piztor/server.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) (limited to 'server') diff --git a/server/piztor/ptp.rst b/server/piztor/ptp.rst index e174fe3..05fb17f 100644 --- a/server/piztor/ptp.rst +++ b/server/piztor/ptp.rst @@ -235,6 +235,7 @@ Piztor Transmission Protocol v2.0a - Request :: + +-------------+----8b------+------8b-----+----4b----+ | AUTH_HEAD | LATITUDE | LONGITUDE | DEADLINE | +-------------+---double---+---double----+---int----+ diff --git a/server/piztor/server.py b/server/piztor/server.py index d2d1974..dab0ccb 100644 --- a/server/piztor/server.py +++ b/server/piztor/server.py @@ -38,6 +38,7 @@ class _SectionSize: LATITUDE = 8 LONGITUDE = 8 PADDING = 1 + DEADLINE = 4 _MAX_AUTH_HEAD_SIZE = _SectionSize.USER_TOKEN + \ MAX_USERNAME_SIZE + \ @@ -57,6 +58,7 @@ class _OptCode: user_logout = 0x04 open_push_tunnel = 0x05 send_text_mesg = 0x06 + set_marker = 0x07 class _StatusCode: sucess = 0x00 @@ -576,6 +578,51 @@ class SendTextMessageHandler(RequestHandler): logger.info("Sent text mesg successfully!") return self.pack(struct.pack("!B", _StatusCode.sucess)) +class SetMarkerHandler(RequestHandler): + + _optcode = _OptCode.set_marker + _max_tr_data_size = _MAX_AUTH_HEAD_SIZE + \ + _SectionSize.LATITUDE + \ + _SectionSize.LONGITUDE + \ + _SectionSize.DEADLINE + + def handle(self, tr_data, conn): + self.check_size(tr_data) + logger.info("Reading set marker data...") + try: + token, = struct.unpack("!32s", tr_data[:32]) + username, tail = RequestHandler.trunc_padding(tr_data[32:]) + lat, lng, deadline = struct.unpack("!ddl", tail) + if username is None: + raise struct.error + except struct.error: + raise BadReqError("Send text mesg: Malformed request body") + + logger.info("Trying to send text mesg with " + "(token = {0}, username = {1})"\ + .format(get_hex(token), username)) + + uauth = RequestHandler.get_uauth(token, username, self.session) + # Authentication failure + if uauth is None: + logger.warning("Authentication failure") + return self.pack(struct.pack("!B", _StatusCode.failure)) + + pt = RequestHandler.push_tunnels + u = uauth.user + ulist = self.session.query(UserModel) \ + .filter(UserModel.sec_id == u.sec_id).all() + + for user in ulist: + uid = user.id + if uid == uauth.uid: continue + if pt.has_key(uid): + tunnel = pt[uid] + tunnel.add(PushTextMesgData(mesg)) + tunnel.push() + logger.info("Sent text mesg successfully!") + return self.pack(struct.pack("!B", _StatusCode.sucess)) + class PTP(Protocol, TimeoutMixin): -- cgit v1.2.3-70-g09d2