Permalink
Browse files

fixed votes generator to not need to be shut down

  • Loading branch information...
jmeehan16 committed Aug 26, 2014
1 parent 120cc3a commit 3aae46ea68d77b0655b12b125d5b7fd3080f868d
@@ -49,9 +49,9 @@
private LinkedList<PhoneCall> callList;
private ListIterator<PhoneCall> callIterator;
Socket socket;
BufferedReader in;
PrintWriter out;
private Socket socket;
private BufferedReader in;
private PrintWriter out;
// Initialize some common constants and variables
private static final String[] AREA_CODE_STRS = ("907,205,256,334,251,870,501,479" +
@@ -190,5 +190,19 @@ public PhoneCall receive()
return null;
}
}
public void closeConnections()
{
try {
out.print("closing");
out.flush();
in.close();
out.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@@ -141,6 +141,11 @@ protected boolean runOnce() throws IOException {
return false;
}
}
@Override
public void stopCallback() {
switchboard.closeConnections();
}
@Override
public String[] getTransactionDisplayNames() {
@@ -45,7 +45,7 @@
public static final String CONTESTANTS_FILE = "logs/hstorecontestants.txt";
public static final String VOTE_FILE = "demo/demo-votes.txt";
public static final int DELETE_CODE = -1;
public static final boolean SOCKET_CONTROL = true;
public static final boolean SOCKET_CONTROL = false;
public static final String HOST_PREFIX = "istc7";
public static final String HOST_PREFIX_2 = "istc9";
@@ -183,13 +183,15 @@ private void printResults(int numVotes) throws IOException
VoltTable[] v = voltExecuteSQL();
VoterDemoHStoreUtil.writeToFile(v, tableNames, numVotes);
tableNames = new ArrayList<String>();
voltQueueSQL(getAllRemainingContestants);
tableNames.add("RemainingContestants");
voltQueueSQL(getRemovedContestants);
tableNames.add("RemovedContestants");
v = voltExecuteSQL();
VoterDemoHStoreUtil.writeToContestantsFile(v, tableNames, numVotes);
}
@@ -49,9 +49,9 @@
private LinkedList<PhoneCall> callList;
private ListIterator<PhoneCall> callIterator;
Socket socket;
BufferedReader in;
PrintWriter out;
private Socket socket;
private BufferedReader in;
private PrintWriter out;
// Initialize some common constants and variables
private static final String[] AREA_CODE_STRS = ("907,205,256,334,251,870,501,479" +
@@ -191,5 +191,19 @@ public PhoneCall receive()
return null;
}
}
public void closeConnections()
{
try {
out.print("closing");
out.flush();
in.close();
out.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@@ -127,6 +127,11 @@ protected boolean runOnce() throws IOException {
return response;
}
@Override
public void stopCallback() {
switchboard.closeConnections();
}
@Override
public String[] getTransactionDisplayNames() {
@@ -45,7 +45,7 @@
public static final String CONTESTANTS_FILE = "logs/sstorecontestants.txt";
public static final String VOTE_FILE = "demo/demo-votes.txt";
public static final int DELETE_CODE = -1;
public static final boolean SOCKET_CONTROL = true;
public static final boolean SOCKET_CONTROL = false;
public static final String HOST_PREFIX = "istc9";
public static final String HOST_PREFIX_2 = "istc7";
@@ -183,13 +183,15 @@ private void printResults(int numVotes) throws IOException
VoltTable[] v = voltExecuteSQL();
VoterDemoSStoreUtil.writeToFile(v, tableNames, numVotes);
tableNames = new ArrayList<String>();
voltQueueSQL(getAllRemainingContestants);
tableNames.add("RemainingContestants");
voltQueueSQL(getRemovedContestants);
tableNames.add("RemovedContestants");
v = voltExecuteSQL();
VoterDemoSStoreUtil.writeToContestantsFile(v, tableNames, numVotes);
}
@@ -0,0 +1,114 @@
import socket
import sys, argparse
import time
import Queue
from threading import Semaphore
from thread import *
hready = False
sready = False
HOST = ''
PORT = 9510
HSTORE_PORT = 9511
SSTORE_PORT = 9512
FILE = "../demo/demo-votes.txt"
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
waittime = 0.001
#first_stop = True
print 'Socket created'
try:
h_socket.bind((HOST,HSTORE_PORT))
s_socket.bind((HOST,SSTORE_PORT))
except:
print 'Bind failed.'
sys.exit()
print 'Socket bind complete'
h_socket.listen(10)
s_socket.listen(10)
print 'Socket now listening'
def getvotes(filename):
f = open(filename, 'r')
global votes
for line in f:
votes.append(line)
f.close()
def s_popvotes(conn, 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
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_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))
h_socket.close()
def sthread():
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))
s_socket.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)
parser.add_argument('-f','--file', help='filename to read', default="demo-votes.txt")
args = parser.parse_args()
waittime = args.wait
FILE = args.file
print(FILE)
print(waittime)
getvotes(FILE)
start_new_thread(hthread, ())
sthread()
View
@@ -53,6 +53,9 @@ def popvotes(conn, votes, lock):
global waittime
while True:
data = conn.recv(1024)
if data == 'closing':
print "CLOSING"
break
lock.acquire()
conn.sendall(votes.get())
time.sleep(waittime)

0 comments on commit 3aae46e

Please sign in to comment.