start of new file
[IRC.git] / Robust / src / Benchmarks / Conglomerator / Tag / WebServerExampleBUGSTUDY.java
1 /* Startup object is generated with the initialstate flag set by the
2  *  system to start the computation up */
3
4 // Create New ServerSocket
5 task Startup(StartupObject s {initialstate}) {
6         ServerSocket ss = new ServerSocket(9000);
7         Logger log = new Logger() {Initialize};
8         Inventory inventorylist = new Inventory(){TransInitialize};
9         taskexit(s {!initialstate}); /* Turns initial state flag off, so this task won't refire */
10 }
11
12 task LookupS(Stock l{initialstate}) {
13     String query="GET /"+l.url+" HTTP/1.1\r\nConnection: close\r\nHost:"+l.hostname+"\r\n\r\n";
14     l.connect(l.hostname, 80);
15     l.write(query.getBytes());
16     taskexit(l{initialstate, query});
17 }
18
19 task ReceiveQueryS(Stock l{query&&IOPending}) {
20     byte[] buffer=new byte[1024];
21     int numchars=l.read(buffer);
22     if (numchars<=0) {
23         l.fix();
24         l.close();
25         System.printString("Before exiting ReceiveQueryS...\n");
26         taskexit(l{!query,done}{});
27     }
28     String str=new String(buffer, 0, numchars);
29     if (l.data==null) {
30         l.data=str;
31     } else
32         l.data=l.data+str;
33     taskexit;
34 }
35
36 task LookupG(Google l{initialstate}) {
37     String query="GET /"+l.url+" HTTP/1.1\r\nConnection: close\r\nHost:"+l.hostname+"\r\n\r\n";
38     l.connect(l.hostname, 80);
39     l.write(query.getBytes());
40     taskexit(l{!initialstate, query});
41 }
42
43 task ReceiveQueryG(Google l{query&&IOPending}) {
44     byte[] buffer=new byte[1024];
45     int numchars=l.read(buffer);
46     if (numchars<=0) {
47         l.fix();
48         l.close();
49         taskexit(l{!query,done}{});
50     }
51     String str=new String(buffer, 0, numchars);
52     if (l.data==null) {
53         l.data=str;
54     } else
55         l.data=l.data+str;
56         
57     taskexit;
58 }
59
60 task LookupW(Weather l{initialstate}) {
61     String query="GET /"+l.url+" HTTP/1.1\r\nConnection: close\r\nHost:"+l.hostname+"\r\n\r\n";
62     l.connect(l.hostname, 80);
63     l.write(query.getBytes());
64     taskexit(l{!initialstate, query});
65 }
66
67 task ReceiveQueryW(Weather l{query&&IOPending}) {
68     byte[] buffer=new byte[1024];
69     int numchars=l.read(buffer);
70     if (numchars<=0) {
71         l.fix();
72         l.close();
73         taskexit(l{!query,done}{});
74     }
75     String str=new String(buffer, 0, numchars);
76     if (l.data==null) {
77         l.data=str;
78     } else
79         l.data=l.data+str;
80     taskexit;
81 }
82
83
84
85 //Listen for a request and accept request 
86 task AcceptConnection(ServerSocket ss{SocketPending}) {
87     //  System.printString("W> Waiting for connection...\n");
88     tag t=new tag(link);
89     WebServerSocket web = new WebServerSocket() {!WritePending, !TransPending, WebInitialize}{t};
90     MySocket ms=new MySocket(){}{t};
91     ss.accept(ms);
92 //      System.printString("W> Connected... \n");
93 }
94
95 // Process the incoming http request 
96 task ProcessRequest(WebServerSocket web{WebInitialize}{link l}, MySocket s{IOPending}{link l}) {
97         if (web.clientrequest(s)) {
98             if(web.checktrans()==false)
99                 // Not special transaction , do normal filesending      
100                 taskexit(web {WritePending, LogPending,!WebInitialize}); //Sets the WritePending and LogPending flag true 
101             else {
102                 Weather w=new Weather(){initialstate}{l};
103                 Google g=new Google(){initialstate}{l};
104                 Stock st=new Stock(){initialstate}{l};
105                 taskexit(web {TransPending, LogPending,!WebInitialize});
106             }
107         }
108 }
109
110 //Do the WriteIO on server socket and send the requested file to Client
111 task SendFile(WebServerSocket web{WritePending}{link l}, MySocket s{}{link l}) {
112         web.sendfile(s);
113         s.close();
114         taskexit(web {!WritePending});
115 }
116
117 // Log the Client request
118 task LogRequest(WebServerSocket web{LogPending}, Logger log{Initialize}) {
119 //Task fired when both
120 // LogPending and Initialize flags are true 
121 //      System.printString("L > Inside logrequest\n");
122         log.logrequest(web.filename);
123         taskexit(web {!LogPending});
124 }
125
126 //Transaction on Inventory
127 task Transaction(WebServerSocket web{TransPending}{link l}, Weather weather{done}{link l}, Google g{done}{link l}, MySocket s{}{link l}, Stock st{done}{link l}){ //Task for WebServerTransactions
128         web.httpresponse(s);
129         s.write(("<html>").getBytes());
130         s.write(weather.data.getBytes());
131         s.write(g.data.getBytes());
132         s.write(st.data.getBytes());
133         s.write(("</html>").getBytes());
134         s.close();
135         taskexit(web {!TransPending});
136 }