summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTeddy <ted.sybil@gmail.com>2013-08-30 23:10:46 +0800
committerTeddy <ted.sybil@gmail.com>2013-08-30 23:10:46 +0800
commit934a6b8ba8bc3fe84b0b9f384c5b925dca61ad4f (patch)
tree455edcb9d86e28b89fe4c9989d68257a2492f92c
parent29b6f83eca288a1bdc2b5cfcbbd1ae2651b97ae6 (diff)
ptp v2.0a std-compliant server
-rw-r--r--server/piztor/mesg_sender.py10
-rw-r--r--server/piztor/ptp_send.py19
-rw-r--r--server/piztor/server.py39
3 files changed, 48 insertions, 20 deletions
diff --git a/server/piztor/mesg_sender.py b/server/piztor/mesg_sender.py
index bbb112e..fb9b5a5 100644
--- a/server/piztor/mesg_sender.py
+++ b/server/piztor/mesg_sender.py
@@ -20,8 +20,10 @@ if len(argv) == 4:
token = user_auth(username, password)
-send_text_mesg(token, username, mesg)
-send_text_mesg(token, username, "a")
-send_text_mesg(token, username, "the last")
-update_location(token, username, 31.028616, 121.434661)
+set_marker(token, username, 123.456, 456.123, 0x7fffffff)
+#send_text_mesg(token, username, mesg)
+#send_text_mesg(token, username, "a")
+#send_text_mesg(token, username, "the last")
+#update_location(token, username, 31.028616, 121.434661)
+
logout(token, username)
diff --git a/server/piztor/ptp_send.py b/server/piztor/ptp_send.py
index d8f783f..22f9842 100644
--- a/server/piztor/ptp_send.py
+++ b/server/piztor/ptp_send.py
@@ -79,6 +79,13 @@ def gen_send_text_mesg(token, username, mesg):
data += chr(0)
return pack_data(0x06, data)
+def gen_set_marker(token, username, lat, lng, deadline):
+ data = pack("!32s", token)
+ data += username
+ data += chr(0)
+ data += pack("!ddL", lat, lng, deadline)
+ return pack_data(0x07, data)
+
def send(data):
received = bytes()
try:
@@ -166,6 +173,16 @@ def send_text_mesg(token, username, mesg):
except error:
logger.error("Send text mesg: can not parse the response")
+def set_marker(token, username, lat, lng, deadline):
+ resp = send(gen_set_marker(token, username, lat, lng, deadline))
+ try:
+ pl, optcode, status = unpack("!LBB", resp)
+ if pl != len(resp):
+ logger.error("Set marker: incorrect packet length")
+ print "status: " + str(status)
+ except error:
+ logger.error("Set marker: can not parse the response")
+
def open_push_tunnel(token, username):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
@@ -191,6 +208,6 @@ def open_push_tunnel(token, username):
break
print "received: " + str(len(received))
pl, optcode, fingerprint = unpack("!LB32s", received[:37])
- mesg = received[37:-1]
+ mesg = received[37:]
logger.info("Received a push: %s", get_hex(mesg))
sock.sendall(pack("!LB32s", 37, optcode, fingerprint))
diff --git a/server/piztor/server.py b/server/piztor/server.py
index 78a15b6..86ec383 100644
--- a/server/piztor/server.py
+++ b/server/piztor/server.py
@@ -29,7 +29,7 @@ engine = create_engine('mysql://' + db_path, echo = False, pool_size = 1024)
class _PermCode:
normal = 0x00
- sectoin = 0x01
+ section = 0x01
company = 0x02
class _SectionSize:
@@ -66,7 +66,8 @@ class _OptCode:
class _StatusCode:
sucess = 0x00
- failure = 0x01
+ auth_fail = 0x01
+ insuf_lvl = 0x02
class PushData(object):
from hashlib import sha256
@@ -84,7 +85,10 @@ class PushTextMesgData(PushData):
class PushLocationData(PushData):
def __init__(self, uid, lat, lng):
self.pack(0x01, struct.pack("!Ldd", uid, lat, lng))
-
+
+class PushMarkerData(PushData):
+ def __init__(self, lat, lng, deadline):
+ self.pack(0x02, struct.pack("!ddL", lat, lng, deadline))
class PushTunnel(object):
def __init__(self, uid):
@@ -269,7 +273,7 @@ class UserAuthHandler(RequestHandler):
_SectionSize.PADDING
_failed_response = \
- lambda self: self.pack(struct.pack("!B", _StatusCode.failure))
+ lambda self: self.pack(struct.pack("!B", _StatusCode.auth_fail))
def handle(self, tr_data, conn):
@@ -345,7 +349,7 @@ class UpdateLocationHandler(RequestHandler):
# Authentication failure
if uauth is None:
logger.warning("Authentication failure")
- return self.pack(struct.pack("!B", _StatusCode.failure))
+ return self.pack(struct.pack("!B", _StatusCode.auth_fail))
loc = uauth.user.location
loc.lat = lat
@@ -388,7 +392,7 @@ class UserInfoHandler(RequestHandler):
_SectionSize.GROUP_ID
_failed_response = \
- lambda self : self.pack(struct.pack("!B", _StatusCode.failure))
+ lambda self : self.pack(struct.pack("!B", _StatusCode.auth_fail))
def handle(self, tr_data, conn):
@@ -464,7 +468,7 @@ class UpdateSubscription(RequestHandler):
# Authentication failure
if uauth is None:
logger.warning("Authentication failure")
- return self.pack(struct.pack("!B", _StatusCode.failure))
+ return self.pack(struct.pack("!B", _StatusCode.auth_fail))
uauth.user.sub = map(self._find_group, sub_list)
self.session.commit()
@@ -496,7 +500,7 @@ class UserLogoutHandler(RequestHandler):
# Authentication failure
if uauth is None:
logger.warning("Authentication failure")
- return self.pack(struct.pack("!B", _StatusCode.failure))
+ return self.pack(struct.pack("!B", _StatusCode.auth_fail))
pt = RequestHandler.push_tunnels
uid = uauth.uid
pt[uid].close()
@@ -530,7 +534,7 @@ class OpenPushTunnelHandler(RequestHandler):
# Authentication failure
if uauth is None:
logger.warning("Authentication failure")
- return self.pack(struct.pack("!B", _StatusCode.failure))
+ return self.pack(struct.pack("!B", _StatusCode.auth_fail))
tunnel = RequestHandler.push_tunnels[uauth.uid]
tunnel.connect(conn)
@@ -565,7 +569,7 @@ class SendTextMessageHandler(RequestHandler):
# Authentication failure
if uauth is None:
logger.warning("Authentication failure")
- return self.pack(struct.pack("!B", _StatusCode.failure))
+ return self.pack(struct.pack("!B", _StatusCode.auth_fail))
pt = RequestHandler.push_tunnels
u = uauth.user
@@ -610,23 +614,27 @@ class SetMarkerHandler(RequestHandler):
# Authentication failure
if uauth is None:
logger.warning("Authentication failure")
- return self.pack(struct.pack("!B", _StatusCode.failure))
+ return self.pack(struct.pack("!B", _StatusCode.auth_fail))
pt = RequestHandler.push_tunnels
u = uauth.user
if u.perm == _PermCode.section:
ulist = self.session.query(UserModel) \
.filter(UserModel.sec_id == u.sec_id).all()
- else if u.perm == _PermCode.section:
+ elif u.perm == _PermCode.company:
+ ulist = self.session.query(UserModel) \
+ .filter(UserModel.comp_id == u.comp_id).all()
+ else:
+ return self.pack(struct.pack("!B", _StatusCode.insuf_lvl))
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.add(PushMarkerData(lat, lng, deadline))
tunnel.push()
- logger.info("Sent text mesg successfully!")
+ logger.info("Set marker successfully!")
return self.pack(struct.pack("!B", _StatusCode.sucess))
@@ -638,7 +646,8 @@ class PTP(Protocol, TimeoutMixin):
UpdateSubscription,
UserLogoutHandler,
OpenPushTunnelHandler,
- SendTextMessageHandler]
+ SendTextMessageHandler,
+ SetMarkerHandler]
handler_num = len(handlers)