diff options
-rw-r--r-- | server/piztor/easy_import.py | 82 | ||||
-rw-r--r-- | server/piztor/mesg_sender.py | 5 | ||||
-rw-r--r-- | server/piztor/server.py | 35 |
3 files changed, 105 insertions, 17 deletions
diff --git a/server/piztor/easy_import.py b/server/piztor/easy_import.py new file mode 100644 index 0000000..e103de7 --- /dev/null +++ b/server/piztor/easy_import.py @@ -0,0 +1,82 @@ +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker +from model import * + +path = "root:helloworld@localhost/piztor" + +class UserData: + def __init__(self, username, nickname, password, + comp_no, sec_no, sex, perm, sub): + self.username = username + self.nickname = nickname + self.password = password + self.comp_no = int(comp_no) + self.sec_no = int(sec_no) + self.sex = int(sex) + self.perm = perm + self.sub = sub + +def create_database(): + engine = create_engine('mysql://' + path, echo = True) + Base.metadata.drop_all(engine) + Base.metadata.create_all(engine) + +def find_or_create_group(comp_no, sec_no, session): + gid = UserModel.to_gid(comp_no, sec_no) + q = session.query(GroupInfo).filter(GroupInfo.id == gid) + entry = q.first() + if not entry: + entry = GroupInfo(gid = gid) + return entry + + +def import_user_data(data): + engine = create_engine('mysql://' + path, echo = True) + Session = sessionmaker(bind = engine) + session = Session() + + for user in data: + um = UserModel(username = user.username, + nickname = user.nickname, + sex = user.sex, + comp_no = user.comp_no, + sec_no = user.sec_no, + perm = user.perm) + + for cn, sn in user.sub: + print cn, sn + g = find_or_create_group(int(cn), int(sn), session) + um.sub.append(g) + um.auth = UserAuth(user.password) + um.location = LocationInfo(lat = 0, lng = 0) + session.add(um) + session.commit() + +if __name__ == '__main__': + + from sys import argv, exit + if len(argv) != 2: + print "Usage: " + argv[0] + " FILE" + exit(0) + + data = list() + with open(argv[1], 'r') as f: + while True: + line = f.readline().split() + if len(line) == 0: break + idx = 0 + comp_no = line[3] + sec_no = line[4] + sub = [ (comp_no, sec_no), (comp_no, 0xff) ] + data.append(UserData(username = line[0], + nickname = line[1], + password = line[0], + comp_no = comp_no, + sec_no = sec_no, + sex = line[2], + perm = line[5], + sub = sub)) + + + create_database() + import_user_data(data) diff --git a/server/piztor/mesg_sender.py b/server/piztor/mesg_sender.py index 80232ec..f7df236 100644 --- a/server/piztor/mesg_sender.py +++ b/server/piztor/mesg_sender.py @@ -20,10 +20,11 @@ if len(argv) == 4: token = user_auth(username, password) -set_marker(token, username, 123.456, 456.123, 0x7fffffff) +update_location(token, username, 31.028616, 121.434661) +update_location(token, username, 31.028616, 121.434661) +set_marker(token, username, 10.028716, 121.545661, 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/server.py b/server/piztor/server.py index ec70566..9435ccc 100644 --- a/server/piztor/server.py +++ b/server/piztor/server.py @@ -115,7 +115,7 @@ class PushTunnel(object): length, optcode, fingerprint = struct.unpack("!LB32s", data) if front.finger_print != fingerprint: raise PiztorError - logger.info("-- Push data confirmed by client --") + logger.info("-- Push data confirmed by client %s --", str(self.uid)) self.blocked = False self.push() @@ -130,7 +130,7 @@ 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)) + logger.info("-- Wrote push: %s --", get_hex(front.data)) self.blocked = True def clear(self): @@ -725,16 +725,23 @@ class PTP(Protocol, TimeoutMixin): self.setTimeout(self.factory.timeout) def response(self, buff): - h = PTP.handlers[self.optcode]() - 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.length = -1 - return - self.transport.loseConnection() + try: + h = PTP.handlers[self.optcode]() + 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.length = -1 + return + self.transport.loseConnection() + except BadReqError as e: + logger.warn("Rejected a bad request: %s", str(e)) + self.transport.loseConnection() + except DBCorruptionError: + logger.error("*** Database corruption ***") + def dataReceived(self, data): self.buff += data @@ -763,12 +770,10 @@ class PTP(Protocol, TimeoutMixin): return self.setTimeout(None) reactor.callFromThread(self.response, buff) + #self.response(buff) except BadReqError as e: logger.warn("Rejected a bad request: %s", str(e)) self.transport.loseConnection() - except DBCorruptionError: - logger.error("*** Database corruption ***") - self.transport.loseConnection() def connectionLost(self, reason): if self.tunnel: |