From 016af459bb7148e6115e9013107f583b67c14469 Mon Sep 17 00:00:00 2001 From: Teddy Date: Thu, 29 Aug 2013 09:48:13 +0800 Subject: fixed activation & deactivation problem --- server/piztor/mesg_sender.py | 1 + server/piztor/server.py | 23 +++++++++++++++-------- 2 files changed, 16 insertions(+), 8 deletions(-) (limited to 'server') diff --git a/server/piztor/mesg_sender.py b/server/piztor/mesg_sender.py index e3ad6e4..e9135bd 100644 --- a/server/piztor/mesg_sender.py +++ b/server/piztor/mesg_sender.py @@ -21,3 +21,4 @@ if len(argv) == 4: uid, token = user_auth(username, password) send_text_mesg(token, username, mesg) +logout(token, username) diff --git a/server/piztor/server.py b/server/piztor/server.py index a58a301..ae79c76 100644 --- a/server/piztor/server.py +++ b/server/piztor/server.py @@ -84,9 +84,9 @@ class PushTunnel(object): self.pending = deque() self.conn = None - def __del__(self): + def close(self): if self.conn: - self.conn.loseConnection() + self.conn.transport.loseConnection() def add(self, pdata): logger.info("-- Push data enqued --") @@ -97,7 +97,7 @@ class PushTunnel(object): length, optcode, fingerprint = struct.unpack("!LB32s", data) if front.finger_print != fingerprint: raise PiztorError - logger.info("-- Push data confirmed --") + logger.info("-- Push data confirmed by client --") self.push() def push(self): @@ -112,12 +112,15 @@ class PushTunnel(object): self.conn.transport.write(front.data) def connect(self, conn): - print conn conn.tunnel = self + if self.conn: # only one long-connection per user + self.conn.transport.loseConnection() self.conn = conn - def on_connection_lost(self): - self.conn = None + def on_connection_lost(self, conn): + if conn == self.conn: + print "*** clear conn ***" + self.conn = None class RequestHandler(object): push_tunnels = dict() @@ -436,7 +439,10 @@ class UserLogoutHandler(RequestHandler): return struct.pack("!LBB", self._response_size, _OptCode.user_logout, _StatusCode.failure) - del RequestHandler.push_tunnels[uauth.uid] + pt = RequestHandler.push_tunnels + uid = uauth.uid + pt[uid].close() + del pt[uid] uauth.regen_token() logger.info("User Logged out successfully!") self.session.commit() @@ -534,6 +540,7 @@ class SendTextMessageHandler(RequestHandler): if pt.has_key(uid): tunnel = pt[uid] tunnel.add(PushTextMesgData(mesg)) + tunnel.push() logger.info("Sent text mesg successfully!") return struct.pack("!LBB", self._response_size, _OptCode.send_text_mesg, @@ -621,7 +628,7 @@ class PTP(Protocol, TimeoutMixin): def connectionLost(self, reason): if self.tunnel: - self.tunnel.on_connection_lost() + self.tunnel.on_connection_lost(self) logger.info("The connection is lost") self.setTimeout(None) -- cgit v1.2.3-70-g09d2