Skip to content

Commit

Permalink
fixed votes generator to not need to be shut down
Browse files Browse the repository at this point in the history
  • Loading branch information
john committed Aug 26, 2014
1 parent 120cc3a commit 3aae46e
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 8 deletions.
Expand Up @@ -49,9 +49,9 @@ public class PhoneCallGenerator {

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" +
Expand Down Expand Up @@ -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();
}
}

}
Expand Up @@ -141,6 +141,11 @@ protected boolean runOnce() throws IOException {
return false;
}
}

@Override
public void stopCallback() {
switchboard.closeConnections();
}

@Override
public String[] getTransactionDisplayNames() {
Expand Down
Expand Up @@ -45,7 +45,7 @@ public abstract class VoterDemoHStoreConstants {
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";
Expand Down
Expand Up @@ -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);

}


Expand Down
Expand Up @@ -49,9 +49,9 @@ public class PhoneCallGenerator {

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" +
Expand Down Expand Up @@ -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();
}
}

}
Expand Up @@ -127,6 +127,11 @@ protected boolean runOnce() throws IOException {
return response;

}

@Override
public void stopCallback() {
switchboard.closeConnections();
}

@Override
public String[] getTransactionDisplayNames() {
Expand Down
Expand Up @@ -45,7 +45,7 @@ public abstract class VoterDemoSStoreConstants {
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";
Expand Down
Expand Up @@ -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);

}


Expand Down
114 changes: 114 additions & 0 deletions tools/voterdemoserver-simple.py
@@ -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()

3 changes: 3 additions & 0 deletions tools/voterdemoserver.py
Expand Up @@ -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)
Expand Down

0 comments on commit 3aae46e

Please sign in to comment.