summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
authorTeddy <[email protected]>2013-08-23 20:59:14 +0800
committerTeddy <[email protected]>2013-08-23 20:59:14 +0800
commit12b1d4bc7e56c7de8bf325f702cff1ae114c4db0 (patch)
treebfb3b1c60622da6de0e6a5d1d2f258eadeff12c3 /server
parent34692fa869ae60e7c35e196542941df845e1fee7 (diff)
implemented more in spec
Diffstat (limited to 'server')
-rw-r--r--server/client.py30
-rw-r--r--server/piztor_server.py88
-rw-r--r--server/rush.py8
3 files changed, 86 insertions, 40 deletions
diff --git a/server/client.py b/server/client.py
index 40754a9..7ab3c0d 100644
--- a/server/client.py
+++ b/server/client.py
@@ -7,7 +7,7 @@ from time import sleep
def get_hex(data):
return "".join([hex(ord(c))[2:].zfill(2) for c in data])
-HOST, PORT = "localhost", 9999
+HOST, PORT = "192.168.1.101", 9990
def gen_auth(username, password):
data = pack("!B", 0)
@@ -19,28 +19,40 @@ def gen_auth(username, password):
def gen_update_location(token, lat, lont):
return pack("!BLdd", 2, token, lat, lont)
+def gen_request_location(token, gid):
+ return pack("!BLL", 3, token, gid)
+
def send(data):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))
- print "sent."
+# print "Client " + str(sys.argv[1]) + ": connected"
sock.sendall(data)
+ print get_hex(data)
+# print "Client " + str(sys.argv[1]) + ": sent"
sock.shutdown(socket.SHUT_WR)
+# print "Client " + str(sys.argv[1]) + ": shutdown"
received = sock.recv(1024)
finally:
print "adf"
sock.close()
- print "Sent".format(get_hex(data))
+ print "Sent {}".format(get_hex(data))
print "Received: {}".format(get_hex(data))
return received
+#print "Client spawned:" + str(sys.argv[1])
rec = send(gen_auth("hello", "world"))
opt, token, status = unpack("!BLB", rec)
-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)
+rec = send(gen_update_location(token, random(), random()))
+opc, status = unpack("!BB", rec)
+
+rec = send(gen_request_location(token, 1))
+opc, length = unpack("!BL", rec[:5])
+idx = 5
+for i in xrange(length):
+ uid, lat, lng = unpack("!Ldd", rec[idx:idx + 20])
+ print (uid, lat, lng)
+ idx += 20
+# sleep(60)
diff --git a/server/piztor_server.py b/server/piztor_server.py
index 5667866..603c27c 100644
--- a/server/piztor_server.py
+++ b/server/piztor_server.py
@@ -1,6 +1,7 @@
import sqlalchemy
import SocketServer, socket, select
import struct
+import os
from sqlalchemy import create_engine
from sqlalchemy import Column, Integer, String, Float
@@ -45,6 +46,7 @@ class UserManager(DataManager):
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key = True)
+ gid = Column(Integer)
username = Column(String)
password = Column(String)
token = Column(Integer)
@@ -114,12 +116,12 @@ class LocationManager(DataManager):
# More: last_update
def location_update_handle(self, opt_type, data):
- print "Parsing Loc Data"
+ print "Parsing a Location Update"
try:
if len(data) < 8:
raise ReqInvalidError()
sender_token, lat, lng = struct.unpack("!Ldd", data)
- print "Updateing location data with following info:"
+ print "Updating location data with following info:"
print (sender_token, lat, lng)
user = self.piz_srv. \
@@ -139,6 +141,27 @@ class LocationManager(DataManager):
except struct.error:
raise ReqInvalidError()
+ def location_request_handle(self, opt_type, data):
+ print "Parsing a Location Request"
+ try:
+ if len(data) != 8:
+ raise ReqInvalidError()
+ sender_token, gid = struct.unpack("!LL", data)
+ print "Requesting location data with following info:"
+ print (sender_token, gid)
+ session = Session()
+ UInfo = UserManager.User
+ LInfo = LocationManager.LocationInfo
+ user_list = session.query(UInfo).filter(UInfo.gid == gid).all()
+ reply = struct.pack("!BL", 3, len(user_list))
+ for user in user_list:
+ loc = session.query(LInfo).filter(LInfo.uid == user.id).first()
+ reply += struct.pack("!Ldd", user.id, loc.lat, loc.lng)
+ print get_hex(reply)
+ return reply
+ except struct.error:
+ raise ReqInvalidError()
+
class PiztorServer():
@@ -146,34 +169,42 @@ class PiztorServer():
def handle(self):
sock = self.request
- sock.setblocking(0)
+ sock.settimeout(10)
+# sock.setblocking(0)
data = ""
- while True:
- ready = select.select([sock], [], [], 10)
- if not ready[0]:
- raise ReqReadError()
- buff = sock.recv(4096)
- if len(buff) == 0:
- break # terminated
- else:
- data += buff
- sock.shutdown(socket.SHUT_RD)
-
- print "Got the data:" + get_hex(data)
- print "===="
-
- if len(data) < 1:
- raise ReqInvalidError()
- opt_id = struct.unpack("!B", data[0])[0]
- reply = self.piz_srv.mgr_map[opt_id](opt_id, data[1:])
- sock.sendall(reply)
- sock.close()
+ try:
+ while True:
+# ready = select.select([sock], [], [], 10)
+# if not ready[0]:
+# raise ReqReadError()
+ buff = sock.recv(4096)
+ if len(buff) == 0:
+ break # terminated
+ else:
+ data += buff
+ sock.shutdown(socket.SHUT_RD)
+
+ print "Got the data:" + get_hex(data)
+
+ if len(data) < 1:
+ print "invalid length"
+ raise ReqInvalidError()
+ opt_id = struct.unpack("!B", data[0])[0]
+ print opt_id
+ reply = self.piz_srv.mgr_map[opt_id](opt_id, data[1:])
+ sock.sendall(reply)
+ finally:
+ sock.close()
+
+ class ForkingEchoServer(SocketServer.ForkingMixIn, SocketServer.TCPServer):
+ pass
def __init__(self, host, port):
PiztorServer.GenericHandler.piz_srv = self
- srv = SocketServer.TCPServer((host, port),
- PiztorServer.GenericHandler)
- srv.timeout = 2
+ srv = PiztorServer.ForkingEchoServer((host, port),
+ PiztorServer.GenericHandler)
+ srv.request_queue_size = 100
+# srv.timeout = 2
self.server = srv
self.user_mgr = UserManager(self)
@@ -182,7 +213,8 @@ class PiztorServer():
self.mgr_map = [ self.user_mgr.authentication_handle,
self.mesg_mgr.mesg_sending_handle,
- self.location_mgr.location_update_handle ]
+ self.location_mgr.location_update_handle,
+ self.location_mgr.location_request_handle]
Base.metadata.create_all(engine)
@@ -197,5 +229,5 @@ class PiztorServer():
if __name__ == "__main__":
- ps = PiztorServer("localhost", 9999)
+ ps = PiztorServer("192.168.1.101", 9990)
ps.run()
diff --git a/server/rush.py b/server/rush.py
index e86bb63..f01804c 100644
--- a/server/rush.py
+++ b/server/rush.py
@@ -2,9 +2,11 @@ from subprocess import Popen
procs = []
try:
- for i in xrange(100):
- procs.append(Popen(["python", "client.py"]))
- while True: pass
+ for i in xrange(10):
+ p = Popen(["python", "client.py", str(i)])
+ procs.append(p)
+ #p.wait()
+ print "done"
except KeyboardInterrupt:
print "killing"