summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTeddy <[email protected]>2013-08-23 11:10:58 +0800
committerTeddy <[email protected]>2013-08-23 11:10:58 +0800
commita44601d57a7b96d69848d5bdd2fa6f3e442d0c85 (patch)
tree09c9c37152e4b9e8da95b024f50986f3a7f28273
parent5c471d58e11ec21b8bee3c7e7a2fb8436952b64d (diff)
...
-rw-r--r--server/client.py19
-rw-r--r--server/piztor_server.py36
-rw-r--r--server/rush.py12
3 files changed, 48 insertions, 19 deletions
diff --git a/server/client.py b/server/client.py
index cfe9133..40754a9 100644
--- a/server/client.py
+++ b/server/client.py
@@ -1,6 +1,11 @@
import socket
import sys
from struct import *
+from random import random
+from time import sleep
+
+def get_hex(data):
+ return "".join([hex(ord(c))[2:].zfill(2) for c in data])
HOST, PORT = "localhost", 9999
@@ -18,6 +23,7 @@ def send(data):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))
+ print "sent."
sock.sendall(data)
sock.shutdown(socket.SHUT_WR)
received = sock.recv(1024)
@@ -25,11 +31,16 @@ def send(data):
print "adf"
sock.close()
- print "Sent: {}".format(data)
- print "Received: {}".format(received)
+ print "Sent".format(get_hex(data))
+ print "Received: {}".format(get_hex(data))
return received
rec = send(gen_auth("hello", "world"))
opt, token, status = unpack("!BLB", rec)
-token = 1
-send(gen_update_location(token, 23.33, -54.44))
+print "status:" + str(status)
+
+for i in range(10):
+ rec = send(gen_update_location(token, random(), random()))
+ opc, status = unpack("!BB", rec)
+ print "status:" + str(status)
+ sleep(10)
diff --git a/server/piztor_server.py b/server/piztor_server.py
index d36df0d..f0107b6 100644
--- a/server/piztor_server.py
+++ b/server/piztor_server.py
@@ -6,11 +6,15 @@ from sqlalchemy import create_engine
from sqlalchemy import Column, Integer, String, Float
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
+from random import randint
engine = create_engine('sqlite:///t.sqlite', echo = True)
Base = declarative_base()
Session = sessionmaker(bind=engine)
+def get_hex(data):
+ return "".join([hex(ord(c))[2:].zfill(2) for c in data])
+
class PiztorError(Exception):
def __init__(self, msg):
self.err_msg = msg
@@ -46,14 +50,12 @@ class UserManager(DataManager):
token = Column(Integer)
def get_user_by_token(self, token):
- try:
- session = Session()
- User = UserManager.User
- q = session.query(User).filter(User.token == token)
- entry = q.first()
- return entry
- except:
+ session = Session()
+ User = UserManager.User
+ entries = session.query(User).filter(User.token == token).all()
+ if len(entries) == 0:
raise TokenInvalidError()
+ return entries[0]
def authentication_handle(self, opt_type, data):
print "Parsing User Data"
@@ -74,16 +76,19 @@ class UserManager(DataManager):
print (username, password)
session = Session()
- q = session.query(UserManager.User). \
- filter(UserManager.User.username == username)
- entry = q.first()
+ entries = session.query(UserManager.User). \
+ filter(UserManager.User.username == username).all()
+ if len(entries) == 0:
+ return struct.pack("!BLB", 0, 0, 1)
+ entry = entries[0]
if entry.password != password: # Auth failed
print "Login failed!"
- return struct.pack("!BlB", 0, 0, 1)
+ return struct.pack("!BLB", 0, 0, 1)
else: # Succeeded
print "Logged in sucessfully!"
entry.token = randint(0, 2147483647)
- return struct.pack("!BlB", 0, token, 0)
+ session.commit()
+ return struct.pack("!BLB", 0, entry.token, 0)
class MesgManager(DataManager):
@@ -126,8 +131,10 @@ class LocationManager(DataManager):
entry.lat = lat
entry.lng = lng
session.commit()
+ print "Location update succeeded!"
return struct.pack("!BB", 2, 0)
except TokenInvalidError:
+ print "Location update failed!"
return struct.pack("!BB", 2, 1)
except struct.error:
raise ReqInvalidError()
@@ -142,7 +149,7 @@ class PiztorServer():
sock.setblocking(0)
data = ""
while True:
- ready = select.select([sock], [], [], 1)
+ ready = select.select([sock], [], [], 10)
if not ready[0]:
raise ReqReadError()
buff = sock.recv(4096)
@@ -152,8 +159,7 @@ class PiztorServer():
data += buff
sock.shutdown(socket.SHUT_RD)
- print "Got the data:"
- print data
+ print "Got the data:" + get_hex(data)
print "===="
if len(data) < 1:
diff --git a/server/rush.py b/server/rush.py
new file mode 100644
index 0000000..e86bb63
--- /dev/null
+++ b/server/rush.py
@@ -0,0 +1,12 @@
+from subprocess import Popen
+procs = []
+
+try:
+ for i in xrange(100):
+ procs.append(Popen(["python", "client.py"]))
+ while True: pass
+
+except KeyboardInterrupt:
+ print "killing"
+ for p in procs:
+ p.kill()