Java version of lookupservice
authoradash <adash>
Tue, 24 Feb 2009 07:23:48 +0000 (07:23 +0000)
committeradash <adash>
Tue, 24 Feb 2009 07:23:48 +0000 (07:23 +0000)
Robust/src/Benchmarks/Distributed/LookUpService/java/LookUpClient.java [new file with mode: 0644]
Robust/src/Benchmarks/Distributed/LookUpService/java/LookUpServerExample.java [new file with mode: 0644]
Robust/src/Benchmarks/Distributed/LookUpService/java/LookUpServerThread.java [new file with mode: 0644]
Robust/src/Benchmarks/Distributed/LookUpService/java/makefile [new file with mode: 0644]

diff --git a/Robust/src/Benchmarks/Distributed/LookUpService/java/LookUpClient.java b/Robust/src/Benchmarks/Distributed/LookUpService/java/LookUpClient.java
new file mode 100644 (file)
index 0000000..b2a3303
--- /dev/null
@@ -0,0 +1,122 @@
+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 = new Socket("dw-8.eecs.uci.edu",9001);
+
+    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);
+        Integer key = new Integer(rwkey);
+        int operation;
+        if (rdwr < lc.rdprob) {
+          operation = 1; //read from hashmap
+        } else {
+          operation = 2;//update hashmap
+        }
+        lc.doLookUp(operation, sock, key);
+      }
+    }
+    /** Special character to terminate computation **/
+    String op = new String("t");
+    sock.write(op.getBytes());
+  }
+
+  /**
+   * Call to do a read/ write on socket
+   **/
+  public void doLookUp(int operation, Socket sock, Integer key){
+    String op;
+    if (operation == 1) {
+      op = new String("r");
+      sock.write(op.getBytes());
+      sock.write(key.intToByteArray());
+      byte b[] = new byte[4];
+      int numbytes = sock.read(b);
+    } else {
+      op = new String("w");
+      sock.write(op.getBytes());
+      sock.write(key.intToByteArray());
+    }
+  }
+
+  /**
+   * 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.printString("usage: ./Client.bin -nObjs <objects in hashmap> -nTrans <number of transactions> -probRead <read probability> -nLookUp <number of lookups>\n");
+    System.printString("    -nObjs the number of objects to be inserted into distributed hashmap\n");
+    System.printString("    -nTrans the number of transactions to run\n");
+    System.printString("    -probRead the probability of read given a transaction\n");
+    System.printString("    -nLookUp the number of lookups per transaction\n");
+    System.printString("    -h help with usage\n");
+  }
+}
diff --git a/Robust/src/Benchmarks/Distributed/LookUpService/java/LookUpServerExample.java b/Robust/src/Benchmarks/Distributed/LookUpService/java/LookUpServerExample.java
new file mode 100644 (file)
index 0000000..8fb9f54
--- /dev/null
@@ -0,0 +1,91 @@
+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;
+    System.println("nobjs = "+nobjs+" nthreads= "+nthreads);
+  }
+
+  public static int 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);
+    }
+
+    ServerSocket ss = new ServerSocket(9001);
+    acceptConnection(ss, hmap, luse.nthreads);
+  }
+
+  public static void acceptConnection(ServerSocket ss, HashMap hmap, int nthreads) {
+    LookUpServerThread[] lus = new LookUpServerThread[nthreads];
+    System.println("Here");
+    for(int i=0; i<nthreads; i++) {
+      Socket s = ss.accept();
+      lus[i] = new LookUpServerThread(s, hmap);
+      lus[i].start();
+    }
+
+    for(int i=0; i<nthreads; i++) {
+      lus[i].join();
+    }
+
+    System.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.printString("usage: ./Server.bin -N <threads> -nObjs <objects in hashmap>\n");
+    System.printString("    -N the number of threads\n");
+    System.printString("    -nObjs the number of objects to be inserted into distributed hashmap\n");
+    System.printString("    -h help with usage\n");
+  }
+}
diff --git a/Robust/src/Benchmarks/Distributed/LookUpService/java/LookUpServerThread.java b/Robust/src/Benchmarks/Distributed/LookUpService/java/LookUpServerThread.java
new file mode 100644 (file)
index 0000000..42a58b5
--- /dev/null
@@ -0,0 +1,59 @@
+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];
+      int numbytes = sock.read(b);
+      String str1 = (new String(b)).subString(0, numbytes);
+      /* terminate if opcode sent is "t" */
+      if(str1.equalsIgnoreCase("t")) {
+        sock.close();
+        break;
+      } else {
+        byte b1[] = new byte[4];
+        numbytes = sock.read(b1);
+        int val = b1[3];
+        Integer keyitem = new Integer(val);
+        /* read from hashmap if opcode sent is "r" */
+        if(str1.equalsIgnoreCase("r")) {
+          doRead(this, keyitem);
+        } else {
+        /* update hashmap if opcode sent is "w" */
+          doUpdate(this, keyitem);
+        }
+      }
+    }
+  }
+
+  /**
+   * Synchromize threads accessing hashmap to read key
+   **/
+
+  synchronized void doRead(LookUpServerThread lusth, Integer key) {
+    //Read object
+    Object value = lusth.hmap.get(key);
+    Integer val = (Integer) value;
+    //Write object to socket for client
+    lusth.sock.write(val.intToByteArray());
+    return;
+  }
+
+  /**
+   * 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;
+  }
+}
diff --git a/Robust/src/Benchmarks/Distributed/LookUpService/java/makefile b/Robust/src/Benchmarks/Distributed/LookUpService/java/makefile
new file mode 100644 (file)
index 0000000..dd63bef
--- /dev/null
@@ -0,0 +1,15 @@
+MAINCLASS1=LookUpServerExample
+MAINCLASS2=LookUpClient
+SRC1=${MAINCLASS1}.java \
+        LookUpServerThread.java
+SRC2=${MAINCLASS2}.java 
+FLAGS= -thread -optimize -mainclass ${MAINCLASS1}
+FLAGS1= -thread -optimize -mainclass ${MAINCLASS2}
+
+default :
+       ../../../../buildscript ${FLAGS} -o Server ${SRC1}
+       ../../../../buildscript ${FLAGS1} -o Client ${SRC2}
+
+clean:
+       rm -rf tmpbuilddirectory
+       rm *.bin