From 0a0ce15830884f6d75e05967e60a73f8a622093a Mon Sep 17 00:00:00 2001 From: adash Date: Tue, 24 Feb 2009 07:23:48 +0000 Subject: [PATCH] Java version of lookupservice --- .../LookUpService/java/LookUpClient.java | 122 ++++++++++++++++++ .../java/LookUpServerExample.java | 91 +++++++++++++ .../java/LookUpServerThread.java | 59 +++++++++ .../Distributed/LookUpService/java/makefile | 15 +++ 4 files changed, 287 insertions(+) create mode 100644 Robust/src/Benchmarks/Distributed/LookUpService/java/LookUpClient.java create mode 100644 Robust/src/Benchmarks/Distributed/LookUpService/java/LookUpServerExample.java create mode 100644 Robust/src/Benchmarks/Distributed/LookUpService/java/LookUpServerThread.java create mode 100644 Robust/src/Benchmarks/Distributed/LookUpService/java/makefile diff --git a/Robust/src/Benchmarks/Distributed/LookUpService/java/LookUpClient.java b/Robust/src/Benchmarks/Distributed/LookUpService/java/LookUpClient.java new file mode 100644 index 00000000..b2a33034 --- /dev/null +++ b/Robust/src/Benchmarks/Distributed/LookUpService/java/LookUpClient.java @@ -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 -nTrans -probRead -nLookUp \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 index 00000000..8fb9f549 --- /dev/null +++ b/Robust/src/Benchmarks/Distributed/LookUpService/java/LookUpServerExample.java @@ -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 -nObjs \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 index 00000000..42a58b5f --- /dev/null +++ b/Robust/src/Benchmarks/Distributed/LookUpService/java/LookUpServerThread.java @@ -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 index 00000000..dd63bef9 --- /dev/null +++ b/Robust/src/Benchmarks/Distributed/LookUpService/java/makefile @@ -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 -- 2.34.1