Benchmarks
[IRC.git] / Robust / src / Benchmarks / WebServerJava / WebServerThread.java
1 public class WebServerThread extends Thread {
2     //Filename requested by the client 
3     String filename;
4     String[] parsed; 
5     String prefix;
6     Logger log;
7     Inventory inventorylist;
8     Socket sock;
9
10     //Constructor
11     public WebServerThread(Socket s, Logger log, Inventory inventory){
12         parsed = new String[4];
13         this.log=log;
14         this.sock=s;
15         this.inventorylist=inventory;
16     }
17     
18     public void start() {
19         // Process the incoming http request 
20         while (!clientrequest()) {
21         }
22         if(checktrans()==false) {
23             // Not special transaction , do normal filesending  
24             SendFile();
25             LogRequest();
26         } else {
27             // Invoke special inventory transaction
28             Transaction();
29             LogRequest();
30         }
31     }
32     
33     //Do the WriteIO on server socket and send the requested file to Client
34     public void SendFile() {
35         sendfile();
36         sock.close();
37     }
38
39     // Log the Client request
40     public void LogRequest() {
41         log.logrequest(filename);
42     }
43     
44     //Transaction on Inventory
45     public void Transaction() {
46         // Parse
47         int op = parseTransaction();
48         // Check for the kind of operation
49         if (op == 0 ) { /* Add */
50             //          System.printString("DEBUG > Calling add transaction\n");
51             Integer qty = new Integer(parsed[2]);
52             Integer price = new Integer(parsed[3]);
53             int ret = inventorylist.additem(parsed[1], qty.intValue(), price.intValue());
54             if (ret == 0) {
55                 httpresponse();
56                 StringBuffer s = new StringBuffer("Added Item ");
57                 s.append(parsed[1]);
58                 s.append(" Quantity ");
59                 s.append(parsed[2]);
60                 s.append(" Price ");
61                 s.append(parsed[3]);
62                 s.append("\n");
63                 String towrite = new String(s);
64                 sock.write(towrite.getBytes());
65             } else {
66                 httpresponse();
67                 String s = new String("Error encountered");
68                 sock.write(s.getBytes());
69             }   
70         } else if (op == 1) { /* Buy */
71             //          System.printString("DEBUG > Calling buy transaction\n");
72             Integer qty = new Integer(parsed[2]);
73             int ret = inventorylist.buyitem(parsed[1], qty.intValue());
74             if (ret >= 0) {
75                 httpresponse();
76                 StringBuffer s = new StringBuffer("Bought item ");
77                 s.append(parsed[1]);
78                 s.append(" Quantity ");
79                 s.append(parsed[2]);
80                 s.append(" Cost ");
81                 Integer cost = new Integer(ret*qty.intValue());
82                 String c = cost.toString();
83                 s.append(c);
84                 String towrite = new String(s);
85                 sock.write(towrite.getBytes());
86             } else {
87                 httpresponse();
88                 String s = new String("Error encountered");
89                 sock.write(s.getBytes());
90             }
91         } else if (op == 2) { /* Inventory */
92             //          System.printString("DEBUG > Calling inventory transaction\n");
93             httpresponse();
94             String towrite = inventorylist.inventory(); 
95             sock.write(towrite.getBytes());
96         } else { /* Error */ 
97             //          System.printString("T > Error - Unknown transaction\n");
98         }
99         //Invoke close operations
100         sock.close();
101     }
102
103     
104     //Send the http header for web browser display      
105     public void httpresponse(){
106         StringBuffer header = new StringBuffer("HTTP/1.0 200 OK\n");
107         header.append("Content-type: text/html\n");
108         header.append("\n\n");
109         String temp_str = new String(header);
110         sock.write(temp_str.getBytes());
111         return;
112         
113     }
114     
115     // Send the html file , read from file one byte at a time   
116     public void sendfile() {
117         StringBuffer req_file = new StringBuffer("./htmlfiles/");
118         req_file.append(filename);
119         String filepath = new String(req_file);
120         FileInputStream def_file = new FileInputStream(filepath);
121         int status = def_file.getfd();//Checks if the file is present in 
122         //current directory     
123         httpresponse();
124         if (status == -1){
125             StringBuffer response = new StringBuffer("404: not found: ");//Send 404 error if
126             // file not found
127             response.append(filename);
128             String buffer = new String(response);
129             sock.write(buffer.getBytes());
130             def_file.close();
131             return;
132         }
133         byte buf[] = new byte[16];
134         int ret;
135         
136         while ((ret = def_file.read(buf)) > 0) {// Read from file and write 
137             // one byte at a time into the socket 
138             byte tosend[] = new byte[ret];
139             for (int i = 0; i < ret; i++) {
140                 tosend[i] = buf[i];
141             }
142             sock.write(tosend);
143             //String str = new String(tosend);
144         }
145         def_file.close();
146     }
147     
148     //Read the client request and extract the filename from it  
149     public boolean clientrequest(){
150         byte b1[] = new byte[1024];
151         int numbytes=sock.read(b1);//Read client request from web server socket
152         String curr=(new String(b1)).subString(0, numbytes);
153         if (prefix!=null) {
154             StringBuffer sb=new StringBuffer(prefix);
155             sb.append(curr);
156             curr=sb.toString();
157         }
158         prefix=curr;
159         if(prefix.indexOf("\r\n\r\n")>=0) {
160             
161             int index = prefix.indexOf('/');//Parse the GET client request to find filename
162             int end = prefix.indexOf('H');
163             filename = prefix.subString((index+1), (end-1));
164             System.printString("\n");
165             return true;
166         }
167         return false;
168     }
169     
170     // Parse  for the prefix in the client request
171     // This is helpful to find if the prefix is a special transaction
172     public boolean checktrans(){
173         if (filename.startsWith("trans") == true) {
174             return true;
175         } else {
176             return false;
177         }
178     }
179     
180     //Parse for the substrings in the filename and use it to obtain the
181     //kind of operation, name of item, quantity of item, price of item
182     //e.g. trans_add_car_2_10000 is the filename
183     //store in the parsed[] string , add,car,2,1000
184     public int parseTransaction(){
185         int start = filename.indexOf('_');
186         String s = filename.subString(start+1);
187         
188         if (s.startsWith("add")==true){
189             //          System.printString("DEBUG > ADD\n");
190             int i1 = s.indexOf('_');
191             parsed[0] = new String(s.subString(0,i1));
192             
193             String s1 = s.subString(i1+1);
194             int i2 = s1.indexOf('_');
195             parsed[1] = new String(s1.subString(0,i2));
196             
197             String s2 = s1.subString(i2+1);
198             int i3 = s2.indexOf('_');
199             parsed[2] = new String(s2.subString(0,i3));
200             
201             String s3 = s2.subString(i3+1);
202             parsed[3] = s3;
203             
204             return 0;
205             
206         }
207         if (s.startsWith("buy")==true){
208             //          System.printString("DEBUG > BUY\n");
209             int i1 = s.indexOf('_');
210             parsed[0] = s.subString(0,i1);
211             
212             String s1 = s.subString(i1+1);
213             int i2 = s1.indexOf('_');
214             parsed[1] = s1.subString(0,i2);
215             
216             String s2 = s1.subString(i2+1);
217             parsed[2] = s2;
218             
219             parsed[3] = "";
220             
221             return 1;
222         }
223         if (s.startsWith("inventory")==true){
224             //          System.printString("DEBUG > INVENTORY\n");
225             return 2;
226             
227         }
228         // Error transaction
229         return -1;
230     }
231 }