4 import java.lang.System;
6 public class LookUpClient {
8 * Total number of transactions
13 * The total number of objects created
18 * The probability of initiating a look up
19 * the read probability % between 0-99
24 * The number of look up operations
28 public LookUpClient() {
31 public LookUpClient(int numtrans, int nobjs, int rdprob, int nLookUp) {
32 this.numtrans = numtrans;
35 this.nLookUp = nLookUp;
38 public static void main(String[] args) {
39 LookUpClient lc = new LookUpClient();
40 LookUpClient.parseCmdLine(args, lc);
42 InputStream in = null;
43 OutputStream out = null;
45 sock = new Socket("dw-8.eecs.uci.edu",9001);
46 in = sock.getInputStream();
47 out = sock.getOutputStream();
48 } catch (UnknownHostException e) {
49 System.err.println("Don't know about host: dw-8.eecs.uci.edu");
51 } catch (IOException e) {
52 System.out.println("Read failed " + e);
55 for (int i = 0; i < lc.numtrans; i++) {
56 Random rand = new Random(i);
57 for (int j = 0; j < lc.nLookUp; j++) {
58 int rdwr = rand.nextInt(100);
59 int rwkey = rand.nextInt(lc.nobjs);
61 if (rdwr < lc.rdprob) {
62 operation = 1; //read from hashmap
64 operation = 2;//update hashmap
66 lc.doLookUp(operation, sock, rwkey, in, out);
69 /** Special character to terminate computation **/
70 String op = new String("t");
72 out.write(op.getBytes());
73 } catch (Exception e) {
79 * Call to do a read/ write on socket
81 public void doLookUp(int operation, Socket sock, int key, InputStream in, OutputStream out){
85 out.write(fillBytes(operation, key));
86 } catch (Exception e) {
89 /* Read from server */
90 byte b[] = new byte[4];
93 } catch (IOException e) {
94 System.out.println("Read failed " + e);
98 out.write(fillBytes(operation, key));
99 } catch (Exception e) {
106 * Convert int to a byte array
108 byte[] fillBytes(int operation, int key) {
109 byte[] b = new byte[5];
116 for(int i = 1; i < 5; i++){
117 int offset = (3-(i-1)) * 8;
118 int tmp = ((key>>offset) & bitmask);
125 * Parse the command line options.
127 public static void parseCmdLine(String args[], LookUpClient lc) {
130 while(i < args.length && args[i].startsWith("-")) {
133 if(arg.equals("-nObjs")) {
134 if(i < args.length) {
135 lc.nobjs = new Integer(args[i++]).intValue();
137 } else if (arg.equals("-nTrans")) {
138 if(i < args.length) {
139 lc.numtrans = new Integer(args[i++]).intValue();
141 } else if(arg.equals("-probRead")) {
142 if(i < args.length) {
143 lc.rdprob = new Integer(args[i++]).intValue();
145 } else if(arg.equals("-nLookUp")) {
146 if(i < args.length) {
147 lc.nLookUp = new Integer(args[i++]).intValue();
149 } else if(arg.equals("-h")) {
154 if(lc.nobjs == 0 || lc.numtrans == 0)
159 * The usage routine which describes the program options.
161 public void usage() {
162 System.out.print("usage: ./Client.bin -nObjs <objects in hashmap> -nTrans <number of transactions> -probRead <read probability> -nLookUp <number of lookups>\n");
163 System.out.print(" -nObjs the number of objects to be inserted into distributed hashmap\n");
164 System.out.print(" -nTrans the number of transactions to run\n");
165 System.out.print(" -probRead the probability of read given a transaction\n");
166 System.out.print(" -nLookUp the number of lookups per transaction\n");
167 System.out.print(" -h help with usage\n");