summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTeddy <ted.sybil@gmail.com>2013-08-27 14:59:16 +0800
committerTeddy <ted.sybil@gmail.com>2013-08-27 14:59:16 +0800
commit6d555f78a87659e53bd3724bf468d9775744f876 (patch)
tree725ed5084b10c78b08d12bc2366fb7e8dd43e47d
parentc9fbb5840a5afed7c4eebb8852d922cb6de362cc (diff)
little opt
-rw-r--r--server/piztor/model.py28
-rw-r--r--server/piztor/prober.py105
-rw-r--r--server/piztor/server.py44
3 files changed, 114 insertions, 63 deletions
diff --git a/server/piztor/model.py b/server/piztor/model.py
index 8916e3a..961f67f 100644
--- a/server/piztor/model.py
+++ b/server/piztor/model.py
@@ -16,20 +16,31 @@ class _TableName: # avoid typoes
class UserModel(Base):
__tablename__ = _TableName.UserModel
+ __table_args__ = {
+ 'mysql_engine' : 'InnoDB',
+ 'mysql_charset' : 'utf8',
+ 'mysql_auto_increment' : '1'}
id = Column(Integer, primary_key = True)
- gid = Column(Integer)
- username = Column(String(MAX_USERNAME_SIZE))
- sex = Column(Boolean)
+ gid = Column(Integer, nullable = False)
+ username = Column(String(MAX_USERNAME_SIZE),
+ unique = True, nullable = False)
+ sex = Column(Boolean, nullable = False)
location = None
auth = None
class LocationInfo(Base):
+ __table_args__ = {
+ 'mysql_engine' : 'InnoDB',
+ 'mysql_charset' : 'utf8',
+ 'mysql_auto_increment' : '1'}
+
__tablename__ = _TableName.LocationInfo
- uid = Column(Integer, ForeignKey(_TableName.UserModel + '.id'), primary_key = True)
- lat = Column(Float(precesion = 64))
- lng = Column(Float(precesion = 64))
+ uid = Column(Integer, ForeignKey(_TableName.UserModel + '.id'),
+ primary_key = True)
+ lat = Column(Float(precesion = 64), nullable = False)
+ lng = Column(Float(precesion = 64), nullable = False)
user = relationship("UserModel", uselist = False,
backref = backref("location", uselist = False,
cascade = "all, delete-orphan"))
@@ -49,6 +60,11 @@ def _random_binary_string(length):
return urandom(length)
class UserAuth(Base):
+ __table_args__ = {
+ 'mysql_engine' : 'InnoDB',
+ 'mysql_charset' : 'utf8',
+ 'mysql_auto_increment' : '1'}
+
__tablename__ = _TableName.UserAuth
uid = Column(Integer, ForeignKey(_TableName.UserModel + '.id'), primary_key = True)
diff --git a/server/piztor/prober.py b/server/piztor/prober.py
index 248f717..6c28364 100644
--- a/server/piztor/prober.py
+++ b/server/piztor/prober.py
@@ -1,6 +1,7 @@
import socket
from struct import *
from random import random
+from select import select
def get_hex(data):
return "".join([hex(ord(c))[2:].zfill(2) for c in data])
@@ -43,13 +44,21 @@ def gen_request_user_info(token, username, uid):
return data
def send(data):
- received = None
+ received = bytes()
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
- print len(data)
+ #print len(data)
sock.sendall(data)
- received = sock.recv(1024)
+ while True:
+ rd, wr, err = select([sock], [], [], 10)
+ if rd:
+ buff = sock.recv(4096)
+ if len(buff) == 0:
+ break
+ received += buff
+ else:
+ break
finally:
sock.close()
return received
@@ -61,6 +70,7 @@ password = "world"
#username = "1234567890123456789012"
#password = "world12345678901234567890"
gid = 1
+failed_cnt = 0
if len(argv) == 2:
host = argv[1]
@@ -71,46 +81,71 @@ if len(argv) == 3:
for i in xrange(10):
resp = send(gen_auth(username, password))
- pl, optcode, status, uid, token = unpack("!LBBL32s", resp)
- print "size: " + str((pl, len(resp)))
- print "opt: " + str(optcode)
- print "status: " + str(status)
- print "uid: " + str(uid)
- print "token: " + get_hex(token)
+ try:
+ pl, optcode, status, uid, token = unpack("!LBBL32s", resp)
+ except:
+ print "fuck1"
+ failed_cnt += 1
+ continue
+ if pl != len(resp): print "God!"
+# print "size: " + str((pl, len(resp)))
+# print "opt: " + str(optcode)
+# print "status: " + str(status)
+# print "uid: " + str(uid)
+# print "token: " + get_hex(token)
resp = send(gen_update_location(token, username, random(), random()))
- pl, optcode, status = unpack("!LBB", resp)
- print "size: " + str((pl, len(resp)))
- print "opt: " + str(optcode)
- print "status: " + str(status)
+ try:
+ pl, optcode, status = unpack("!LBB", resp)
+ except:
+ print "fuck2"
+ if pl != len(resp): print "God!"
+# print "size: " + str((pl, len(resp)))
+# print "opt: " + str(optcode)
+# print "status: " + str(status)
resp = send(gen_request_location(token, username, gid))
- print len(resp)
- pl, optcode, status = unpack("!LBB", resp[:6])
- print "size: " + str((pl, len(resp)))
+ try:
+ pl, optcode, status = unpack("!LBB", resp[:6])
+ except:
+ print "fuck3"
+ if pl != len(resp): print "God!"
+# print "size: " + str((pl, len(resp)))
idx = 6
- print "length: " + str(len(resp[6:]))
- while idx < pl:
- print len(resp[idx:idx + 20])
- uid, lat, lng = unpack("!Ldd", resp[idx:idx + 20])
- idx += 20
- print (uid, lat, lng)
+# print "length: " + str(len(resp[6:]))
+ try:
+ while idx < pl:
+# print len(resp[idx:idx + 20])
+ uid, lat, lng = unpack("!Ldd", resp[idx:idx + 20])
+ idx += 20
+ except:
+ print "fuck4"
+# print (uid, lat, lng)
resp = send(gen_request_user_info(token, username, uid))
- pl, optcode, status = unpack("!LBB", resp[:6])
- print "size: " + str((pl, len(resp)))
+ try:
+ pl, optcode, status = unpack("!LBB", resp[:6])
+ except:
+ print "fuck5"
+ if pl != len(resp): print "God!"
+# print "size: " + str((pl, len(resp)))
idx = 6
- while idx < pl:
- info_key, = unpack("!B", resp[idx:idx + 1])
- idx += 1
- print info_key
- if info_key == 0x00:
- info_value, = unpack("!L", resp[idx:idx + 4])
- idx += 4
- elif info_key == 0x01:
- info_value, = unpack("!B", resp[idx:idx + 1])
+ try:
+ while idx < pl:
+ info_key, = unpack("!B", resp[idx:idx + 1])
idx += 1
- print (info_key, info_value)
+# print info_key
+ if info_key == 0x00:
+ info_value, = unpack("!L", resp[idx:idx + 4])
+ idx += 4
+ elif info_key == 0x01:
+ info_value, = unpack("!B", resp[idx:idx + 1])
+ idx += 1
+# print (info_key, info_value)
+ except:
+ print "fuck6"
from time import sleep
-# sleep(10)
+ sleep(10)
+
+print failed_cnt
diff --git a/server/piztor/server.py b/server/piztor/server.py
index fae5ca7..1560735 100644
--- a/server/piztor/server.py
+++ b/server/piztor/server.py
@@ -22,7 +22,8 @@ db_path = "root:helloworld@localhost/piztor"
FORMAT = "%(asctime)-15s %(message)s"
logging.basicConfig(format = FORMAT)
logger = logging.getLogger('piztor_server')
-logger.setLevel(logging.INFO)
+logger.setLevel(logging.WARN)
+engine = create_engine('mysql://' + db_path, echo = False, pool_size = 1024)
class _SectionSize:
@@ -56,13 +57,12 @@ class _StatusCode:
class RequestHandler(object):
def __init__(self):
- self.engine = create_engine('mysql://' + db_path, echo = False)
- Session = sessionmaker(bind = self.engine)
+ Session = sessionmaker(bind = engine)
self.session = Session()
def __del__(self):
self.session.close()
- self.engine.dispose()
+# self.engine.dispose()
def check_size(self, tr_data):
if len(tr_data) > self._max_tr_data_size:
@@ -157,7 +157,7 @@ class UserAuthHandler(RequestHandler):
else:
logger.info("Logged in sucessfully: {0}".format(username))
uauth.regen_token()
- logger.info("New token generated: " + get_hex(uauth.token))
+ #logger.info("New token generated: " + get_hex(uauth.token))
self.session.commit()
return struct.pack("!LBBL32s", UserAuthHandler._response_size,
_OptCode.user_auth,
@@ -189,10 +189,10 @@ class LocationUpdateHandler(RequestHandler):
except struct.error:
raise BadReqError("Location update: Malformed request body")
- logger.info("Trying to update location with "
- "(token = {0}, username = {1}, lat = {2}, lng = {3})"\
- .format(get_hex(token), username, lat, lng))
-
+# logger.info("Trying to update location with "
+# "(token = {0}, username = {1}, lat = {2}, lng = {3})"\
+# .format(get_hex(token), username, lat, lng))
+#
uauth = RequestHandler.get_uauth(token, username, self.session)
# Authentication failure
if uauth is None:
@@ -235,9 +235,9 @@ class LocationInfoHandler(RequestHandler):
except struct.error:
raise BadReqError("Location request: Malformed request body")
- logger.info("Trying to request locatin with " \
- "(token = {0}, gid = {1})" \
- .format(get_hex(token), gid))
+# logger.info("Trying to request locatin with " \
+# "(token = {0}, gid = {1})" \
+# .format(get_hex(token), gid))
uauth = RequestHandler.get_uauth(token, username, self.session)
# Auth failure
@@ -304,9 +304,9 @@ class UserInfoHandler(RequestHandler):
except struct.error:
raise BadReqError("User info request: Malformed request body")
- logger.info("Trying to request locatin with " \
- "(token = {0}, uid = {1})" \
- .format(get_hex(token), uid))
+# logger.info("Trying to request locatin with " \
+# "(token = {0}, uid = {1})" \
+# .format(get_hex(token), uid))
uauth = RequestHandler.get_uauth(token, username, self.session)
# Auth failure
@@ -383,7 +383,7 @@ class PTP(Protocol, TimeoutMixin):
if len(self.buff) == self.length:
h = PTP.handlers[self.optcode]()
reply = h.handle(self.buff[5:])
- logger.info("Wrote: %s", get_hex(reply))
+# logger.info("Wrote: %s", get_hex(reply))
self.transport.write(reply)
self.transport.loseConnection()
elif len(self.buff) > self.length:
@@ -405,12 +405,12 @@ class PTPFactory(Factory):
def buildProtocol(self, addr):
return PTP(self)
-#if os.name!='nt':
-# from twisted.internet import epollreactor
-# epollreactor.install()
-#else:
-# from twisted.internet import iocpreactor
-# iocpreactor.install()
+if os.name!='nt':
+ from twisted.internet import epollreactor
+ epollreactor.install()
+else:
+ from twisted.internet import iocpreactor
+ iocpreactor.install()
from twisted.internet import reactor