bug fix and boost socket performance for writes
authoradash <adash>
Wed, 25 Feb 2009 02:37:58 +0000 (02:37 +0000)
committeradash <adash>
Wed, 25 Feb 2009 02:37:58 +0000 (02:37 +0000)
Robust/src/Benchmarks/Distributed/LookUpService/java/LookUpClient.java
Robust/src/Benchmarks/Distributed/LookUpService/java/LookUpServerExample.java
Robust/src/Benchmarks/Distributed/LookUpService/java/LookUpServerThread.java

index b2a33034a54170aae2fccb100fa41e73384c3a09..df331840618ed60cc75e0b4baf05a57c00a16ab0 100644 (file)
@@ -48,7 +48,7 @@ public class LookUpClient {
         } else {
           operation = 2;//update hashmap
         }
-        lc.doLookUp(operation, sock, key);
+        lc.doLookUp(operation, sock, rwkey);
       }
     }
     /** Special character to terminate computation **/
@@ -59,21 +59,34 @@ public class LookUpClient {
   /**
    * Call to do a read/ write on socket
    **/
-  public void doLookUp(int operation, Socket sock, Integer key){
+  public void doLookUp(int operation, Socket sock, int key){
     String op;
     if (operation == 1) {
-      op = new String("r");
-      sock.write(op.getBytes());
-      sock.write(key.intToByteArray());
+      sock.write(fillBytes(operation, key));
       byte b[] = new byte[4];
       int numbytes = sock.read(b);
     } else {
-      op = new String("w");
-      sock.write(op.getBytes());
-      sock.write(key.intToByteArray());
+      sock.write(fillBytes(operation, key));
     }
   }
 
+  /*
+   * 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';
+    }
+    for(int i = 1; i < 5; i++){
+      int offset = (3-(i-1)) * 8;
+      b[i] = (byte) ((key >> offset) & 0xFF);
+    }
+    return b;
+  }
+
   /**
    * Parse the command line options.
    **/
index 8fb9f5491897d7d300aad5f49e8fba565eeeb81d..6152f45c60f2a4b2570c2fb9cf997b6ab624a02c 100644 (file)
@@ -15,7 +15,6 @@ public class 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[]) {
@@ -39,7 +38,6 @@ public class LookUpServerExample {
 
   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);
@@ -49,8 +47,6 @@ public class LookUpServerExample {
     for(int i=0; i<nthreads; i++) {
       lus[i].join();
     }
-
-    System.println("Finished");
   }
 
   /**
index 42a58b5fbbfe7b1ae08d062ade23dc515c2da927..20ac75e3ff43af98d9a54f65ca1c4a0a1bef22ea 100644 (file)
@@ -19,11 +19,13 @@ public class LookUpServerThread extends Thread {
       } else {
         byte b1[] = new byte[4];
         numbytes = sock.read(b1);
-        int val = b1[3];
+        int val =  getKey(b1);
         Integer keyitem = new Integer(val);
         /* read from hashmap if opcode sent is "r" */
         if(str1.equalsIgnoreCase("r")) {
-          doRead(this, keyitem);
+          Integer tmpval = doRead(this, keyitem);
+          //Write object to socket for client
+          sock.write(tmpval.intToByteArray());
         } else {
         /* update hashmap if opcode sent is "w" */
           doUpdate(this, keyitem);
@@ -36,13 +38,11 @@ public class LookUpServerThread extends Thread {
    * Synchromize threads accessing hashmap to read key
    **/
 
-  synchronized void doRead(LookUpServerThread lusth, Integer key) {
+  synchronized Integer 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;
+    return val;
   }
 
   /**
@@ -56,4 +56,14 @@ public class LookUpServerThread extends Thread {
     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;
+  }
 }