start of new file
[IRC.git] / Robust / src / Benchmarks / Conglomerator / Tag / WebServerSocket.java
1 public class WebServerSocket {
2         flag LogPending;
3         flag WritePending;
4         flag TransPending;
5         flag WebInitialize;
6         //Filename requested by the client 
7         String filename;
8         String[] parsed; 
9         String prefix;
10                 
11         //Constructor
12         public WebServerSocket(){
13                 parsed = new String[4];
14         }
15         
16         //Send the http header for web browser display  
17         public void httpresponse(Socket s){
18                 StringBuffer header = new StringBuffer("HTTP/1.0 200 OK\n");
19                 header.append("Content-type: text/html\n");
20                 header.append("\n\n");
21                 String temp_str = new String(header);
22                 s.write(temp_str.getBytes());
23                 return;
24
25         }
26         
27         // Send the html file , read from file one byte at a time       
28         public void sendfile(Socket s) {
29                 StringBuffer req_file = new StringBuffer("./htmlfiles/");
30                 req_file.append(filename);
31                 String filepath = new String(req_file);
32                 FileInputStream def_file = new FileInputStream(filepath);
33                 int status = def_file.getfd();//Checks if the file is present in 
34                                               //current directory       
35                 httpresponse(s);
36                 if (status == -1){
37                         StringBuffer response = new StringBuffer("404: not found: ");//Send 404 error if
38                                                                                      // file not found
39                         response.append(filename);
40                         String buffer = new String(response);
41                         s.write(buffer.getBytes());
42                         def_file.close();
43                         return;
44                 }
45                 byte buf[] = new byte[16];
46                 int ret;
47                 
48                 while ((ret = def_file.read(buf)) > 0) {// Read from file and write 
49                                                         // one byte at a time into the socket 
50                         byte tosend[] = new byte[ret];
51                         for (int i = 0; i < ret; i++) {
52                                 tosend[i] = buf[i];
53                         }
54                         s.write(tosend);
55                         //String str = new String(tosend);
56                 }
57                 def_file.close();
58         }
59
60         //Read the client request and extract the filename from it      
61         public boolean clientrequest(Socket s){
62                 byte b1[] = new byte[1024];
63                 int numbytes=s.read(b1);//Read client request from web server socket
64                 String curr=(new String(b1)).subString(0, numbytes);
65                 if (prefix!=null) {
66                     StringBuffer sb=new StringBuffer(prefix);
67                     sb.append(curr);
68                     curr=sb.toString();
69                 }
70                 prefix=curr;
71                 if(prefix.indexOf("\r\n\r\n")>=0) {
72
73                     int index = prefix.indexOf('/');//Parse the GET client request to find filename
74                     int end = prefix.indexOf('H');
75                     filename = prefix.subString((index+1), (end-1));
76                     return true;
77                 }
78                 return false;
79         }
80         
81         // Parse  for the prefix in the client request
82         // This is helpful to find if the prefix is a special transaction
83         public boolean checktrans(){
84                 if (filename.startsWith("portal") == true) {
85                         return true;
86                 } else {
87                         return false;
88                 }
89         }
90
91         //Parse for the substrings in the filename and use it to obtain the
92         //kind of operation, name of item, quantity of item, price of item
93         //e.g. trans_add_car_2_10000 is the filename
94         //store in the parsed[] string , add,car,2,1000
95     public int parseTransaction(){
96         int start = filename.indexOf('_');
97         String s = filename.subString(start+1);
98         
99         if (s.startsWith("add")==true){
100             //          System.printString("DEBUG > ADD\n");
101             int i1 = s.indexOf('_');
102             parsed[0] = new String(s.subString(0,i1));
103             
104             int i2 = s.indexOf('_',i1+1);
105             parsed[1] = new String(s.subString(i1+1,i2));
106             
107             int i3 = s.indexOf('_',i2+1);
108             parsed[2] = new String(s.subString(i2+1,i3));
109             
110             String s3 = s.subString(i3+1);
111             parsed[3] = s3;
112             
113             return 0;
114             
115         }
116         if (s.startsWith("buy")==true){
117             //          System.printString("DEBUG > BUY\n");
118             int i1 = s.indexOf('_');
119             parsed[0] = s.subString(0,i1);
120             
121             int i2 = s.indexOf('_', i1+1);
122             parsed[1] = s.subString(i1+1,i2);
123             
124             String s2 = s.subString(i2+1);
125             parsed[2] = s2;
126             
127             parsed[3] = "";
128             
129             return 1;
130         }
131         if (s.startsWith("inventory")==true){
132             //          System.printString("DEBUG > INVENTORY\n");
133             return 2;
134             
135         }
136         // Error transaction
137         return -1;
138     }
139 }