for (int j = 0; j < lc.nLookUp; j++) {
int rdwr = rand.nextInt(100);
int rwkey = rand.nextInt(lc.nobjs);
- Integer key = new Integer(rwkey);
int operation;
if (rdwr < lc.rdprob) {
operation = 1; //read from hashmap
for(int i=0; i<nthreads; i++) {
lus[i].join();
}
+ System.println("Finished");
}
/**
--- /dev/null
+import java.net.*;
+import java.util.*;
+import java.io.*;
+import java.lang.System;
+
+public class LookUpClient {
+ /**
+ * Total number of transactions
+ **/
+ private int numtrans;
+
+ /**
+ * The total number of objects created
+ **/
+ private int nobjs;
+
+ /**
+ * The probability of initiating a look up
+ * the read probability % between 0-99
+ **/
+ private int rdprob;
+
+ /**
+ * The number of look up operations
+ **/
+ private int nLookUp;
+
+ public LookUpClient() {
+ }
+
+ public LookUpClient(int numtrans, int nobjs, int rdprob, int nLookUp) {
+ this.numtrans = numtrans;
+ this.nobjs = nobjs;
+ this.rdprob = rdprob;
+ this.nLookUp = nLookUp;
+ }
+
+ public static void main(String[] args) {
+ LookUpClient lc = new LookUpClient();
+ LookUpClient.parseCmdLine(args, lc);
+ Socket sock = null;
+ InputStream in = null;
+ OutputStream out = null;
+ try {
+ sock = new Socket("dw-8.eecs.uci.edu",9001);
+ in = sock.getInputStream();
+ out = sock.getOutputStream();
+ } catch (UnknownHostException e) {
+ System.err.println("Don't know about host: dw-8.eecs.uci.edu");
+ System.exit(1);
+ } catch (IOException e) {
+ System.out.println("Read failed " + e);
+ }
+
+ for (int i = 0; i < lc.numtrans; i++) {
+ Random rand = new Random(i);
+ for (int j = 0; j < lc.nLookUp; j++) {
+ int rdwr = rand.nextInt(100);
+ int rwkey = rand.nextInt(lc.nobjs);
+ int operation;
+ if (rdwr < lc.rdprob) {
+ operation = 1; //read from hashmap
+ } else {
+ operation = 2;//update hashmap
+ }
+ lc.doLookUp(operation, sock, rwkey, in, out);
+ }
+ }
+ /** Special character to terminate computation **/
+ String op = new String("t");
+ try{
+ out.write(op.getBytes());
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Call to do a read/ write on socket
+ **/
+ public void doLookUp(int operation, Socket sock, int key, InputStream in, OutputStream out){
+ String op;
+ if (operation == 1) {
+ try {
+ out.write(fillBytes(operation, key));
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ /* Read from server */
+ byte b[] = new byte[4];
+ try {
+ in.read(b);
+ } catch (IOException e) {
+ System.out.println("Read failed " + e);
+ }
+ } else {
+ try {
+ out.write(fillBytes(operation, key));
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ /*
+ * Convert int to a byte array
+ **/
+ byte[] fillBytes(int operation, int key) {
+ byte[] b = new byte[5];
+ if(operation == 1) {
+ b[0] = (byte)'r';
+ } else {
+ b[0] = (byte)'w';
+ }
+ int bitmask = 0xFF;
+ for(int i = 1; i < 5; i++){
+ int offset = (3-(i-1)) * 8;
+ int tmp = ((key>>offset) & bitmask);
+ b[i] = (byte)tmp;
+ }
+ return b;
+ }
+
+ /**
+ * Parse the command line options.
+ **/
+ public static void parseCmdLine(String args[], LookUpClient lc) {
+ int i = 0;
+ String arg;
+ while(i < args.length && args[i].startsWith("-")) {
+ arg = args[i++];
+ //check options
+ if(arg.equals("-nObjs")) {
+ if(i < args.length) {
+ lc.nobjs = new Integer(args[i++]).intValue();
+ }
+ } else if (arg.equals("-nTrans")) {
+ if(i < args.length) {
+ lc.numtrans = new Integer(args[i++]).intValue();
+ }
+ } else if(arg.equals("-probRead")) {
+ if(i < args.length) {
+ lc.rdprob = new Integer(args[i++]).intValue();
+ }
+ } else if(arg.equals("-nLookUp")) {
+ if(i < args.length) {
+ lc.nLookUp = new Integer(args[i++]).intValue();
+ }
+ } else if(arg.equals("-h")) {
+ lc.usage();
+ }
+ }
+
+ if(lc.nobjs == 0 || lc.numtrans == 0)
+ lc.usage();
+ }
+
+ /**
+ * The usage routine which describes the program options.
+ **/
+ public void usage() {
+ System.out.print("usage: ./Client.bin -nObjs <objects in hashmap> -nTrans <number of transactions> -probRead <read probability> -nLookUp <number of lookups>\n");
+ System.out.print(" -nObjs the number of objects to be inserted into distributed hashmap\n");
+ System.out.print(" -nTrans the number of transactions to run\n");
+ System.out.print(" -probRead the probability of read given a transaction\n");
+ System.out.print(" -nLookUp the number of lookups per transaction\n");
+ System.out.print(" -h help with usage\n");
+ }
+}
--- /dev/null
+import java.net.*;
+import java.util.*;
+
+public class LookUpServerExample {
+ /**
+ * Number of objects in the hash table
+ **/
+ int nobjs;
+
+ /**
+ * Number of threads
+ **/
+ int nthreads;
+
+ public LookUpServerExample() {
+ }
+
+ public LookUpServerExample(int nobjs, int nthreads) {
+ this.nobjs = nobjs;
+ this.nthreads = nthreads;
+ }
+
+ public static void main(String args[]) {
+ LookUpServerExample luse = new LookUpServerExample();
+ LookUpServerExample.parseCmdLine(args, luse);
+
+ /**
+ * Create shared hashmap and put values
+ **/
+ HashMap hmap;
+ hmap = new HashMap();
+ for(int i = 0; i<luse.nobjs; i++) {
+ Integer key = new Integer(i);
+ Integer val = new Integer(i*i);
+ hmap.put(key,val);
+ }
+
+ try {
+ ServerSocket ss = new ServerSocket(9001);
+ acceptConnection(ss, hmap, luse.nthreads);
+ } catch (Exception e) {
+ System.out.println("Server socket create error " + e);
+ }
+ }
+
+ public static void acceptConnection(ServerSocket ss, HashMap hmap, int nthreads) {
+ LookUpServerThread[] lus = new LookUpServerThread[nthreads];
+ for(int i=0; i<nthreads; i++) {
+ Socket s = null;
+ try {
+ s = ss.accept();
+ lus[i] = new LookUpServerThread(s, hmap);
+ lus[i].start();
+ } catch (Exception e) {
+ System.out.println("Server accept error " + e);
+ }
+ }
+
+ for(int i=0; i<nthreads; i++) {
+ try {
+ lus[i].join();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+
+ System.out.println("Finished");
+ }
+
+ /**
+ * Parse the command line options.
+ **/
+ public static void parseCmdLine(String args[], LookUpServerExample lse) {
+ int i = 0;
+ String arg;
+ while(i < args.length && args[i].startsWith("-")) {
+ arg = args[i++];
+ //check options
+ if(arg.equals("-N")) {
+ if(i < args.length) {
+ lse.nthreads = new Integer(args[i++]).intValue();
+ }
+ } else if(arg.equals("-nObjs")) {
+ if(i < args.length) {
+ lse.nobjs = new Integer(args[i++]).intValue();
+ }
+ } else if(arg.equals("-h")) {
+ lse.usage();
+ }
+ }
+
+ if(lse.nobjs == 0)
+ lse.usage();
+ }
+
+ /**
+ * The usage routine which describes the program options.
+ **/
+ public void usage() {
+ System.out.print("usage: ./Server.bin -N <threads> -nObjs <objects in hashmap>\n");
+ System.out.print(" -N the number of threads\n");
+ System.out.print(" -nObjs the number of objects to be inserted into distributed hashmap\n");
+ System.out.print(" -h help with usage\n");
+ }
+}
--- /dev/null
+import java.io.*;
+import java.net.*;
+import java.util.*;
+
+public class LookUpServerThread extends Thread {
+ HashMap hmap;
+ Socket sock;
+
+ public LookUpServerThread(Socket s, HashMap h) {
+ hmap = h;
+ sock = s;
+ }
+
+ public void run() {
+ while(true) {
+ byte b[] = new byte[1];
+ InputStream in = null;
+ OutputStream out = null;
+ int numbytes;
+ try {
+ in = sock.getInputStream();
+ out = sock.getOutputStream();
+ numbytes = in.read(b);
+ } catch (IOException e) {
+ System.out.println("Read failed " + e);
+ }
+
+ /* terminate if opcode sent is "t" */
+ if(b[0] == (byte)'t') {
+ try {
+ in.close();
+ out.close();
+ sock.close();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ break;
+ } else {
+ byte b1[] = new byte[4];
+ try {
+ numbytes = in.read(b1);
+ } catch (IOException e) {
+ System.out.println("Read failed " + e);
+ }
+ int val = getKey(b1);
+ Integer keyitem = new Integer(val);
+ /* read from hashmap if opcode sent is "r" */
+ if(b[0] == (byte)'r') {
+ Integer tmpval = doRead(this, keyitem);
+ //Write object to socket for client
+ try {
+ out.write(intToByteArray(tmpval.intValue()));
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ } else {
+ /* update hashmap if opcode sent is "w" */
+ doUpdate(this, keyitem);
+ }
+ }
+ }
+ }
+
+ /**
+ * Synchromize threads accessing hashmap to read key
+ **/
+
+ synchronized Integer doRead(LookUpServerThread lusth, Integer key) {
+ //Read object
+ Object value = lusth.hmap.get(key);
+ Integer val = (Integer) value;
+ return val;
+ }
+
+ /**
+ * Synchromize threads accessing hashmap to update key,value pair
+ **/
+ synchronized void doUpdate(LookUpServerThread lusth, Integer key) {
+ //Write into hmap
+ Random rand = new Random(0);
+ int val = rand.nextInt(200);
+ Integer value = new Integer(val);
+ Object oldvalue = lusth.hmap.put(key, value);
+ return;
+ }
+
+ /*
+ * Convert byte array into int type
+ **/
+
+ int getKey(byte[] b) {
+ int val;
+ val = ((b[0] & 0xFF) << 24) + ((b[1] & 0xFF) << 16) + ((b[2] & 0xFF) << 8) + (b[3] & 0xFF);
+ return val;
+ }
+
+ byte[] intToByteArray(int value) {
+ byte[] b = new byte[4];
+ for (int i = 0; i < 4; i++) {
+ int offset = (b.length - 1 - i) * 8;
+ b[i] = (byte) ((value >> offset) & 0xFF);
+ }
+ return b;
+ }
+}
--- /dev/null
+MAINCLASS1=LookUpServerExample
+MAINCLASS2=LookUpClient
+default:
+ javac ${MAINCLASS1}.java
+ javac ${MAINCLASS2}.java
+run:
+ java ${MAINCLASS1}
+ java ${MAINCLASS2}
+
+clean:
+ rm *.class