bug fixes
[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 run() {
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             inventorylist.inventory(sock);      
95         } else { /* Error */ 
96             //          System.printString("T > Error - Unknown transaction\n");
97         }
98         //Invoke close operations
99         sock.close();
100     }
101
102     
103     //Send the http header for web browser display      
104     public void httpresponse(){
105         StringBuffer header = new StringBuffer("HTTP/1.0 200 OK\n");
106         header.append("Content-type: text/html\n");
107         header.append("\n\n");
108         String temp_str = new String(header);
109         sock.write(temp_str.getBytes());
110         return;
111         
112     }
113     
114     // Send the html file , read from file one byte at a time   
115     public void sendfile() {
116         StringBuffer req_file = new StringBuffer("./htmlfiles/");
117         req_file.append(filename);
118         String filepath = new String(req_file);
119         FileInputStream def_file = new FileInputStream(filepath);
120         int status = def_file.getfd();//Checks if the file is present in 
121         //current directory     
122         httpresponse();
123         if (status == -1){
124             StringBuffer response = new StringBuffer("404: not found: ");//Send 404 error if
125             // file not found
126             response.append(filename);
127             String buffer = new String(response);
128             sock.write(buffer.getBytes());
129             def_file.close();
130             return;
131         }
132         byte buf[] = new byte[16];
133         int ret;
134         
135         while ((ret = def_file.read(buf)) > 0) {// Read from file and write 
136             // one byte at a time into the socket 
137             byte tosend[] = new byte[ret];
138             for (int i = 0; i < ret; i++) {
139                 tosend[i] = buf[i];
140             }
141             sock.write(tosend);
142             //String str = new String(tosend);
143         }
144         def_file.close();
145     }
146     
147     //Read the client request and extract the filename from it  
148     public boolean clientrequest(){
149         byte b1[] = new byte[1024];
150         int numbytes=sock.read(b1);//Read client request from web server socket
151         String curr=(new String(b1)).subString(0, numbytes);
152         if (prefix!=null) {
153             StringBuffer sb=new StringBuffer(prefix);
154             sb.append(curr);
155             curr=sb.toString();
156         }
157         prefix=curr;
158         if(prefix.indexOf("\r\n\r\n")>=0) {
159             
160             int index = prefix.indexOf('/');//Parse the GET client request to find filename
161             int end = prefix.indexOf('H');
162             filename = prefix.subString((index+1), (end-1));
163             //      System.printString("\n");
164             return true;
165         }
166         return false;
167     }
168     
169     // Parse  for the prefix in the client request
170     // This is helpful to find if the prefix is a special transaction
171     public boolean checktrans(){
172         if (filename.startsWith("trans") == true) {
173             return true;
174         } else {
175             return false;
176         }
177     }
178     
179     //Parse for the substrings in the filename and use it to obtain the
180     //kind of operation, name of item, quantity of item, price of item
181     //e.g. trans_add_car_2_10000 is the filename
182     //store in the parsed[] string , add,car,2,1000
183     public int parseTransaction(){
184         int start = filename.indexOf('_');
185         String s = filename.subString(start+1);
186         
187         if (s.startsWith("add")==true){
188             //          System.printString("DEBUG > ADD\n");
189             int i1 = s.indexOf('_');
190             parsed[0] = new String(s.subString(0,i1));
191             
192             int i2 = s.indexOf('_',i1+1);
193             parsed[1] = new String(s.subString(i1+1,i2));
194             
195             int i3 = s.indexOf('_',i2+1);
196             parsed[2] = new String(s.subString(i2+1,i3));
197             
198             String s3 = s.subString(i3+1);
199             parsed[3] = s3;
200             
201             return 0;
202             
203         }
204         if (s.startsWith("buy")==true){
205             //          System.printString("DEBUG > BUY\n");
206             int i1 = s.indexOf('_');
207             parsed[0] = s.subString(0,i1);
208             
209             int i2 = s.indexOf('_', i1+1);
210             parsed[1] = s.subString(i1+1,i2);
211             
212             String s2 = s.subString(i2+1);
213             parsed[2] = s2;
214             
215             parsed[3] = "";
216             
217             return 1;
218         }
219         if (s.startsWith("inventory")==true){
220             //          System.printString("DEBUG > INVENTORY\n");
221             return 2;
222             
223         }
224         // Error transaction
225         return -1;
226     }
227 }