Permalink
Browse files

new version of voterdemoserver.py

  • Loading branch information...
jmeehan16 committed Aug 26, 2014
1 parent 3aae46e commit 0db74ad8ccab1001d16abe98a2213b588ac0c727
Showing with 93 additions and 93 deletions.
  1. +59 −33 tools/{voterdemoserver-simple.py → voterdemoserver-old.py}
  2. +34 −60 tools/voterdemoserver.py
@@ -14,18 +14,19 @@
SSTORE_PORT = 9512
FILE = "../demo/demo-votes.txt"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
h_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
h_lock = Semaphore(1)
s_lock = Semaphore(1)
votes = []
s_index = 0
h_index = 0
h_votes = Queue.Queue()
s_votes = Queue.Queue()
waittime = 0.001
#first_stop = True
print 'Socket created'
try:
s.bind((HOST,PORT))
h_socket.bind((HOST,HSTORE_PORT))
s_socket.bind((HOST,SSTORE_PORT))
except:
@@ -34,69 +35,81 @@
print 'Socket bind complete'
s.listen(2)
h_socket.listen(10)
s_socket.listen(10)
print 'Socket now listening'
def getvotes(filename):
f = open(filename, 'r')
global votes
global h_votes
global s_votes
for line in f:
votes.append(line)
h_votes.put(line)
s_votes.put(line)
f.close()
def s_popvotes(conn, lock):
def popvotes(conn, votes, lock):
global waittime
global s_index
global votes
while True:
data = conn.recv(1024)
if data == 'closing':
print "CLOSING"
break
lock.acquire()
conn.sendall(votes[s_index])
s_index+=1
conn.sendall(votes.get())
time.sleep(waittime)
lock.release()
s_index = 0
conn.close()
def h_popvotes(conn, lock):
global waittime
global h_index
global votes
while True:
data = conn.recv(1024)
if data == 'closing':
print "CLOSING"
break
lock.acquire()
conn.sendall(votes[h_index])
h_index+=1
time.sleep(waittime)
lock.release()
h_index = 0
conn.close()
def hthread():
global h_votes
global h_socket
global h_lock
while True:
conn, addr = h_socket.accept()
print 'H-Store Votes connected with ' + addr[0] + ':' + str(addr[1])
start_new_thread(h_popvotes, (conn,h_lock))
start_new_thread(popvotes, (conn,h_votes,h_lock))
h_socket.close()
def sthread():
global s_votes
global s_socket
global s_lock
while True:
conn, addr = s_socket.accept()
print 'S-Store Votes connected with ' + addr[0] + ':' + str(addr[1])
start_new_thread(s_popvotes, (conn,s_lock))
start_new_thread(popvotes, (conn,s_votes,s_lock))
s_socket.close()
def bothConnected(conn, conn2):
global h_lock
global s_lock
#global b_socket
#global first_stop
data = conn.recv(1024)
#if data == "h-store ready":
# h_lock.acquire()
#elif data == "s-store ready":
# s_lock.acquire()
data2 = conn2.recv(1024)
#if data2 == "h-store ready":
# h_lock.acquire()
#elif data2 == "s-store ready":
# s_lock.acquire()
if (data == "h-store ready" and data2 == "s-store ready") or (data2 == "h-store ready" and data == "s-store ready"):
print "READY"
conn.sendall("READY\n")
conn2.sendall("READY\n")
else:
print "ERROR: Unexpected message."
#h_lock.release()
#s_lock.release()
conn.close()
conn2.close()
parser = argparse.ArgumentParser(description='Starts running the vote feeder for h-store and/or s-store.')
parser.add_argument('-w','--wait', help='wait in between sending next vote (in seconds)', type=float, default=0.001)
@@ -110,5 +123,18 @@ def sthread():
print(waittime)
getvotes(FILE)
start_new_thread(hthread, ())
sthread()
start_new_thread(sthread, ())
while True:
conn, addr = s.accept()
print 'Connected with ' + addr[0] + ':' + str(addr[1])
conn.sendall("READY\n")
#conn2, addr = s.accept()
#print 'Connected with ' + addr[0] + ':' + str(addr[1])
#bothConnected(conn, conn2)
#start_new_thread(clientthread, (conn,))
s.close()
View
94 tools/voterdemoserver.py 100755 → 100644
@@ -14,19 +14,18 @@
SSTORE_PORT = 9512
FILE = "../demo/demo-votes.txt"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
h_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
h_lock = Semaphore(1)
s_lock = Semaphore(1)
h_votes = Queue.Queue()
s_votes = Queue.Queue()
votes = []
s_index = 0
h_index = 0
waittime = 0.001
#first_stop = True
print 'Socket created'
try:
s.bind((HOST,PORT))
h_socket.bind((HOST,HSTORE_PORT))
s_socket.bind((HOST,SSTORE_PORT))
except:
@@ -35,81 +34,69 @@
print 'Socket bind complete'
s.listen(2)
h_socket.listen(10)
s_socket.listen(10)
print 'Socket now listening'
def getvotes(filename):
f = open(filename, 'r')
global h_votes
global s_votes
global votes
for line in f:
h_votes.put(line)
s_votes.put(line)
votes.append(line)
f.close()
def popvotes(conn, votes, lock):
def s_popvotes(conn, lock):
global waittime
global s_index
global votes
while True:
data = conn.recv(1024)
if data == 'closing':
print "CLOSING"
print "Closing S-Store connection"
break
lock.acquire()
conn.sendall(votes.get())
conn.sendall(votes[s_index])
s_index+=1
time.sleep(waittime)
lock.release()
s_index = 0
conn.close()
def h_popvotes(conn, lock):
global waittime
global h_index
global votes
while True:
data = conn.recv(1024)
if data == 'closing':
print "Closing H-Store connection"
break
lock.acquire()
conn.sendall(votes[h_index])
h_index+=1
time.sleep(waittime)
lock.release()
h_index = 0
conn.close()
def hthread():
global h_votes
global h_socket
global h_lock
while True:
conn, addr = h_socket.accept()
print 'H-Store Votes connected with ' + addr[0] + ':' + str(addr[1])
start_new_thread(popvotes, (conn,h_votes,h_lock))
start_new_thread(h_popvotes, (conn,h_lock))
h_socket.close()
def sthread():
global s_votes
global s_socket
global s_lock
while True:
conn, addr = s_socket.accept()
print 'S-Store Votes connected with ' + addr[0] + ':' + str(addr[1])
start_new_thread(popvotes, (conn,s_votes,s_lock))
start_new_thread(s_popvotes, (conn,s_lock))
s_socket.close()
def bothConnected(conn, conn2):
global h_lock
global s_lock
#global b_socket
#global first_stop
data = conn.recv(1024)
#if data == "h-store ready":
# h_lock.acquire()
#elif data == "s-store ready":
# s_lock.acquire()
data2 = conn2.recv(1024)
#if data2 == "h-store ready":
# h_lock.acquire()
#elif data2 == "s-store ready":
# s_lock.acquire()
if (data == "h-store ready" and data2 == "s-store ready") or (data2 == "h-store ready" and data == "s-store ready"):
print "READY"
conn.sendall("READY\n")
conn2.sendall("READY\n")
else:
print "ERROR: Unexpected message."
#h_lock.release()
#s_lock.release()
conn.close()
conn2.close()
parser = argparse.ArgumentParser(description='Starts running the vote feeder for h-store and/or s-store.')
parser.add_argument('-w','--wait', help='wait in between sending next vote (in seconds)', type=float, default=0.001)
@@ -123,18 +110,5 @@ def bothConnected(conn, conn2):
print(waittime)
getvotes(FILE)
start_new_thread(hthread, ())
start_new_thread(sthread, ())
while True:
conn, addr = s.accept()
print 'Connected with ' + addr[0] + ':' + str(addr[1])
conn.sendall("READY\n")
#conn2, addr = s.accept()
#print 'Connected with ' + addr[0] + ':' + str(addr[1])
#bothConnected(conn, conn2)
#start_new_thread(clientthread, (conn,))
s.close()
sthread()

0 comments on commit 0db74ad

Please sign in to comment.