From cc5677c657bb8fa3f88cd1dcb3d86d9732ecca42 Mon Sep 17 00:00:00 2001 From: adash Date: Thu, 26 Feb 2009 03:41:41 +0000 Subject: [PATCH] add new files for jvm implementation --- .../LookUpService/java/LookUpClient.java | 1 - .../java/LookUpServerExample.java | 1 + .../LookUpService/jvm/LookUpClient.java | 169 ++++++++++++++++++ .../jvm/LookUpServerExample.java | 105 +++++++++++ .../LookUpService/jvm/LookUpServerThread.java | 105 +++++++++++ .../Distributed/LookUpService/jvm/makefile | 11 ++ 6 files changed, 391 insertions(+), 1 deletion(-) create mode 100644 Robust/src/Benchmarks/Distributed/LookUpService/jvm/LookUpClient.java create mode 100644 Robust/src/Benchmarks/Distributed/LookUpService/jvm/LookUpServerExample.java create mode 100644 Robust/src/Benchmarks/Distributed/LookUpService/jvm/LookUpServerThread.java create mode 100644 Robust/src/Benchmarks/Distributed/LookUpService/jvm/makefile diff --git a/Robust/src/Benchmarks/Distributed/LookUpService/java/LookUpClient.java b/Robust/src/Benchmarks/Distributed/LookUpService/java/LookUpClient.java index df331840..93f6e84d 100644 --- a/Robust/src/Benchmarks/Distributed/LookUpService/java/LookUpClient.java +++ b/Robust/src/Benchmarks/Distributed/LookUpService/java/LookUpClient.java @@ -41,7 +41,6 @@ public class LookUpClient { 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 diff --git a/Robust/src/Benchmarks/Distributed/LookUpService/java/LookUpServerExample.java b/Robust/src/Benchmarks/Distributed/LookUpService/java/LookUpServerExample.java index 6152f45c..0228cf4d 100644 --- a/Robust/src/Benchmarks/Distributed/LookUpService/java/LookUpServerExample.java +++ b/Robust/src/Benchmarks/Distributed/LookUpService/java/LookUpServerExample.java @@ -47,6 +47,7 @@ public class LookUpServerExample { for(int i=0; i>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 -nTrans -probRead -nLookUp \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"); + } +} diff --git a/Robust/src/Benchmarks/Distributed/LookUpService/jvm/LookUpServerExample.java b/Robust/src/Benchmarks/Distributed/LookUpService/jvm/LookUpServerExample.java new file mode 100644 index 00000000..882a74a6 --- /dev/null +++ b/Robust/src/Benchmarks/Distributed/LookUpService/jvm/LookUpServerExample.java @@ -0,0 +1,105 @@ +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 -nObjs \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"); + } +} diff --git a/Robust/src/Benchmarks/Distributed/LookUpService/jvm/LookUpServerThread.java b/Robust/src/Benchmarks/Distributed/LookUpService/jvm/LookUpServerThread.java new file mode 100644 index 00000000..512c404e --- /dev/null +++ b/Robust/src/Benchmarks/Distributed/LookUpService/jvm/LookUpServerThread.java @@ -0,0 +1,105 @@ +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; + } +} diff --git a/Robust/src/Benchmarks/Distributed/LookUpService/jvm/makefile b/Robust/src/Benchmarks/Distributed/LookUpService/jvm/makefile new file mode 100644 index 00000000..b9408140 --- /dev/null +++ b/Robust/src/Benchmarks/Distributed/LookUpService/jvm/makefile @@ -0,0 +1,11 @@ +MAINCLASS1=LookUpServerExample +MAINCLASS2=LookUpClient +default: + javac ${MAINCLASS1}.java + javac ${MAINCLASS2}.java +run: + java ${MAINCLASS1} + java ${MAINCLASS2} + +clean: + rm *.class -- 2.34.1