diff options
author | Teddy <[email protected]> | 2013-08-29 14:28:27 +0800 |
---|---|---|
committer | Teddy <[email protected]> | 2013-08-29 14:28:27 +0800 |
commit | d922c4debc06158fd65a9b3d4e57165ae87e1d1d (patch) | |
tree | cbb02f37d69b29f6311ce131d87ea627b0621516 | |
parent | 95c63e2cdc05f687a976e9e87e1f17998c20349b (diff) |
fixed continous data issue && blocking problem in pushing notification
-rw-r--r-- | server/piztor/import.py | 12 | ||||
-rw-r--r-- | server/piztor/mesg_sender.py | 5 | ||||
-rw-r--r-- | server/piztor/model.py | 74 | ||||
-rw-r--r-- | server/piztor/ptp_send.py | 12 | ||||
-rw-r--r-- | server/piztor/server.py | 39 |
5 files changed, 83 insertions, 59 deletions
diff --git a/server/piztor/import.py b/server/piztor/import.py index 5b23299..4896325 100644 --- a/server/piztor/import.py +++ b/server/piztor/import.py @@ -5,11 +5,11 @@ from model import * path = "root:helloworld@localhost/piztor" class UserData: - def __init__(self, username, password, comp_id, sec_id, sex): + def __init__(self, username, password, comp_id, sec_no, sex): self.username = username self.password = password - self.comp_id = comp_id - self.sec_id = sec_id + self.comp_id = int(comp_id) + self.sec_no = int(sec_no) self.sex = sex def create_database(): @@ -24,8 +24,10 @@ def import_user_data(data): for user in data: um = UserModel(username = user.username, comp_id = user.comp_id, - sec_id = user.sec_id, + sec_no = user.sec_no, + sec_id = user.comp_id * 256 + user.sec_no, sex = user.sex) + print um.sec_id um.auth = UserAuth(user.password) um.location = LocationInfo(lat = 0, lng = 0) session.add(um) @@ -46,7 +48,7 @@ if __name__ == '__main__': data.append(UserData(username = line[0], password = line[1], comp_id = line[2], - sec_id = line[3], + sec_no = line[3], sex = line[4])) create_database() diff --git a/server/piztor/mesg_sender.py b/server/piztor/mesg_sender.py index e9135bd..64349a3 100644 --- a/server/piztor/mesg_sender.py +++ b/server/piztor/mesg_sender.py @@ -21,4 +21,7 @@ if len(argv) == 4: uid, token = user_auth(username, password) send_text_mesg(token, username, mesg) -logout(token, username) +send_text_mesg(token, username, "a") +update_location(token, username, 1.2345, 2.468) +update_location(token, username, 1.2345, 2.468) +#logout(token, username) diff --git a/server/piztor/model.py b/server/piztor/model.py index 460aa38..b1a7804 100644 --- a/server/piztor/model.py +++ b/server/piztor/model.py @@ -25,13 +25,13 @@ class _TableName: # avoid typoes CompanySub = 'comp_sub' SectionSub = 'sec_sub' -comp_sub_assoc = Table(_TableName.CompanySub, Base.metadata, - Column('uid', Integer, ForeignKey(_TableName.UserModel + '.id')), - Column('comp_id', Integer, ForeignKey(_TableName.CompanyInfo + '.id'))) - -sec_sub_assoc = Table(_TableName.SectionSub, Base.metadata, - Column('uid', Integer, ForeignKey(_TableName.UserModel + '.id')), - Column('sec_id', Integer, ForeignKey(_TableName.SectionInfo + '.id'))) +#comp_sub_assoc = Table(_TableName.CompanySub, Base.metadata, +# Column('uid', Integer, ForeignKey(_TableName.UserModel + '.id')), +# Column('comp_id', Integer, ForeignKey(_TableName.CompanyInfo + '.id'))) +# +#sec_sub_assoc = Table(_TableName.SectionSub, Base.metadata, +# Column('uid', Integer, ForeignKey(_TableName.UserModel + '.id')), +# Column('sec_id', Integer, ForeignKey(_TableName.SectionInfo + '.id'))) class UserModel(Base): __tablename__ = _TableName.UserModel @@ -42,15 +42,17 @@ class UserModel(Base): sex = Column(Boolean, nullable = False) sec_no = Column(TINYINT) - comp_id = Column(TINYINT, ForeignKey(_TableName.CompanyInfo + '.id')) - sec_id = Column(TINYINT, ForeignKey(_TableName.SectionInfo + '.id')) - - comp_sub = relationship(_TableName.CompanyInfo, - secondary = comp_sub_assoc, - backref = "subscribers") - sec_sub = relationship(_TableName.SectionInfo, - secondary = sec_sub_assoc, - backref = "subscribers") +# comp_id = Column(TINYINT, ForeignKey(_TableName.CompanyInfo + '.id')) + comp_id = Column(TINYINT) +# sec_id = Column(TINYINT, ForeignKey(_TableName.SectionInfo + '.id')) + sec_id = Column(Integer) + +# comp_sub = relationship(_TableName.CompanyInfo, +# secondary = comp_sub_assoc, +# backref = "subscribers") +# sec_sub = relationship(_TableName.SectionInfo, +# secondary = sec_sub_assoc, +# backref = "subscribers") location = None auth = None @@ -115,23 +117,23 @@ class UserAuth(Base): def get_token(self): return self.token - -class CompanyInfo(Base): - __tablename__ = _TableName.CompanyInfo - __table_args__ = _table_typical_settings - - id = Column(TINYINT, primary_key = True) - marker_lat = Column(Float(precesion = 64), nullable = False) - market_lng = Column(Float(precesion = 64), nullable = False) - - subscribers = None - -class SectionInfo(Base): - __tablename__ = _TableName.SectionInfo - __table_args__ = _table_typical_settings - - id = Column(TINYINT, primary_key = True) - marker_lat = Column(Float(precesion = 64), nullable = False) - market_lng = Column(Float(precesion = 64), nullable = False) - - subscribers = None +# +#class CompanyInfo(Base): +# __tablename__ = _TableName.CompanyInfo +# __table_args__ = _table_typical_settings +# +# id = Column(TINYINT, primary_key = True) +# marker_lat = Column(Float(precesion = 64), nullable = False) +# marker_lng = Column(Float(precesion = 64), nullable = False) +# +# subscribers = None +# +#class SectionInfo(Base): +# __tablename__ = _TableName.SectionInfo +# __table_args__ = _table_typical_settings +# +# id = Column(TINYINT, primary_key = True) +# marker_lat = Column(Float(precesion = 64), nullable = False) +# market_lng = Column(Float(precesion = 64), nullable = False) +# +# subscribers = None diff --git a/server/piztor/ptp_send.py b/server/piztor/ptp_send.py index 7c55f07..745eeb5 100644 --- a/server/piztor/ptp_send.py +++ b/server/piztor/ptp_send.py @@ -25,7 +25,7 @@ class _SectionSize: PADDING = 1 host = "localhost" #"localhost" -port = 2222 +port = 2223 def gen_auth(username, password): length = _SectionSize.LENGTH + \ @@ -234,9 +234,9 @@ def open_push_tunnel(token, username): sock.sendall(data) print get_hex(sock.recv(6)) - length = -1 while True: received = bytes() + length = -1 while True: if len(received) > 4: length, optcode = unpack("!LB", received[:5]) @@ -245,14 +245,14 @@ def open_push_tunnel(token, username): break rd, wr, err = select([sock], [], []) if rd: - buff = sock.recv(4096) + buff = sock.recv(1) if len(buff) == 0: break received += buff else: break - print len(received) + print "received: " + str(len(received)) pl, optcode, fingerprint = unpack("!LB32s", received[:37]) mesg = received[37:-1] - logger.info("Received a push: %s", mesg) - sock.sendall(pack("!LB32s", 37, 0x00, fingerprint)) + 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 5635242..a4101ef 100644 --- a/server/piztor/server.py +++ b/server/piztor/server.py @@ -71,8 +71,8 @@ class PushData(object): 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 + buff = struct.pack("!L", _SectionSize.LENGTH + len(buff)) + buff + self.data = buff class PushTextMesgData(PushData): def __init__(self, mesg): @@ -80,13 +80,14 @@ class PushTextMesgData(PushData): class PushLocationData(PushData): def __init__(self, uid, lat, lng): - self.pack(0x01, struct.pack("!dd", lat, lng)) + self.pack(0x01, struct.pack("!Ldd", uid, lat, lng)) class PushTunnel(object): def __init__(self): self.pending = deque() self.conn = None + self.blocked = False def close(self): if self.conn: @@ -102,9 +103,12 @@ class PushTunnel(object): if front.finger_print != fingerprint: raise PiztorError logger.info("-- Push data confirmed by client --") + self.blocked = False self.push() def push(self): + if self.blocked: + return print "Pushing via " + str(self) print "Pending size: " + str(len(self.pending)) logger.info("Pushing...") @@ -113,6 +117,8 @@ class PushTunnel(object): front = self.pending.popleft() self.pending.appendleft(front) self.conn.transport.write(front.data) + logger.info("-- Wrote push: %s --", get_hex(front.data)) + self.blocked = True def connect(self, conn): conn.tunnel = self @@ -279,6 +285,18 @@ class LocationUpdateHandler(RequestHandler): logger.info("Location is updated sucessfully") self.session.commit() + + pt = RequestHandler.push_tunnels + ulist = self.session.query(UserModel) \ + .filter(UserModel.sec_id == uauth.user.sec_id).all() + pdata = PushLocationData(uauth.uid, lat, lng) + for user in ulist: + uid = user.id + if pt.has_key(uid): + tunnel = pt[uid] + tunnel.add(pdata) + tunnel.push() + return struct.pack("!LBB", self._response_size, _OptCode.location_update, _StatusCode.sucess) @@ -599,26 +617,25 @@ class PTP(Protocol, TimeoutMixin): if self.length > PTP._MAX_REQUEST_SIZE: print self.length, PTP._MAX_REQUEST_SIZE raise BadReqError("The size of remaining part is too big") - if len(self.buff) == self.length: + + if len(self.buff) >= self.length: + buff = self.buff[:self.length] + self.buff = self.buff[self.length:] if self.tunnel: # received push response - self.tunnel.on_receive(self.buff) - self.buff = bytes() + self.tunnel.on_receive(buff) self.length = -1 return h = PTP.handlers[self.optcode]() - reply = h.handle(self.buff[5:], self) + reply = h.handle(buff[5:], self) logger.info("Wrote: %s", get_hex(reply)) self.transport.write(reply) if self.tunnel: logger.info("Blocking the client...") self.tunnel.push() - self.buff = bytes() self.length = -1 self.setTimeout(None) return self.transport.loseConnection() - elif len(self.buff) > self.length: - raise BadReqError("The actual length is larger than promised") except BadReqError as e: logger.warn("Rejected a bad request: %s", str(e)) self.transport.loseConnection() @@ -651,6 +668,6 @@ from twisted.internet import reactor f = PTPFactory() f.protocol = PTP -reactor.listenTCP(2222, f) +reactor.listenTCP(2223, f) logger.warning("The server is lanuched") reactor.run() |