add new files for jvm implementation
[IRC.git] / Robust / src / Benchmarks / Distributed / LookUpService / jvm / LookUpClient.java
1 import java.net.*;
2 import java.util.*;
3 import java.io.*;
4 import java.lang.System;
5
6 public class LookUpClient {
7   /**
8    * Total number of transactions
9    **/
10   private int numtrans;
11
12   /**
13    * The total number of objects created
14    **/
15   private int nobjs;
16
17   /**
18    * The probability of initiating a look up
19    * the read probability % between 0-99
20    **/
21   private int rdprob;
22
23   /**
24    * The number of look up operations
25    **/
26   private int nLookUp;
27
28   public LookUpClient() {
29   }
30
31   public LookUpClient(int numtrans, int nobjs, int rdprob, int nLookUp) {
32     this.numtrans = numtrans;
33     this.nobjs = nobjs;
34     this.rdprob = rdprob;
35     this.nLookUp = nLookUp;
36   }
37
38   public static void main(String[] args) {
39     LookUpClient lc = new LookUpClient();
40     LookUpClient.parseCmdLine(args, lc);
41     Socket sock = null;
42     InputStream in = null;
43     OutputStream out = null;
44     try {
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");
50       System.exit(1);
51     } catch (IOException e) {
52       System.out.println("Read failed " + e);
53     }
54
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);
60         int operation;
61         if (rdwr < lc.rdprob) {
62           operation = 1; //read from hashmap
63         } else {
64           operation = 2;//update hashmap
65         }
66         lc.doLookUp(operation, sock, rwkey, in, out);
67       }
68     }
69     /** Special character to terminate computation **/
70     String op = new String("t");
71     try{
72       out.write(op.getBytes());
73     } catch (Exception e) {
74       e.printStackTrace();
75     }
76   }
77
78   /**
79    * Call to do a read/ write on socket
80    **/
81   public void doLookUp(int operation, Socket sock, int key, InputStream in, OutputStream out){
82     String op;
83     if (operation == 1) {
84       try {
85         out.write(fillBytes(operation, key));
86       } catch (Exception e) {
87         e.printStackTrace();
88       }
89       /* Read from server */
90       byte b[] = new byte[4];
91       try {
92         in.read(b);
93       } catch (IOException e) {
94         System.out.println("Read failed " + e);
95       }
96     } else {
97       try {
98         out.write(fillBytes(operation, key));
99       } catch (Exception e) {
100         e.printStackTrace();
101       }
102     }
103   }
104
105   /*
106    * Convert int to a byte array 
107    **/
108   byte[] fillBytes(int operation, int key) {
109     byte[] b = new byte[5];
110     if(operation == 1) {
111       b[0] = (byte)'r';
112     } else { 
113       b[0] = (byte)'w';
114     }
115     int bitmask = 0xFF;
116     for(int i = 1; i < 5; i++){
117       int offset = (3-(i-1)) * 8;
118       int tmp = ((key>>offset) & bitmask);
119       b[i] = (byte)tmp;
120     }
121     return b;
122   }
123
124   /**
125    * Parse the command line options.
126    **/
127   public static void parseCmdLine(String args[], LookUpClient lc) {
128     int i = 0;
129     String arg;
130     while(i < args.length && args[i].startsWith("-")) {
131       arg = args[i++];
132       //check options
133       if(arg.equals("-nObjs")) {
134         if(i < args.length) {
135           lc.nobjs = new Integer(args[i++]).intValue();
136         }
137       } else if (arg.equals("-nTrans")) {
138         if(i < args.length) {
139           lc.numtrans =  new Integer(args[i++]).intValue();
140         }
141       } else if(arg.equals("-probRead")) {
142         if(i < args.length) {
143           lc.rdprob = new Integer(args[i++]).intValue();
144         }
145       } else if(arg.equals("-nLookUp")) {
146         if(i < args.length) {
147           lc.nLookUp = new Integer(args[i++]).intValue();
148         }
149       } else if(arg.equals("-h")) {
150         lc.usage();
151       }
152     }
153
154     if(lc.nobjs == 0  || lc.numtrans == 0)
155       lc.usage();
156   }
157
158   /**
159    * The usage routine which describes the program options.
160    **/
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");
168   }
169 }