start of new file
[IRC.git] / Robust / src / Benchmarks / Conglomerator / Tag / Nrm / WebServerExample.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         taskexit(l{!query,done}{});
26     }
27     String str=new String(buffer, 0, numchars);
28     if (l.data==null) {
29         l.data=str;
30     } else
31         l.data=l.data+str;
32     taskexit;
33 }
34
35 task LookupG(Google l{initialstate}) {
36     String query="GET /"+l.url+" HTTP/1.1\r\nConnection: close\r\nHost:"+l.hostname+"\r\n\r\n";
37     l.connect(l.hostname, 80);
38     l.write(query.getBytes());
39     taskexit(l{!initialstate, query});
40 }
41
42 task ReceiveQueryG(Google l{query&&IOPending}) {
43     byte[] buffer=new byte[1024];
44     int numchars=l.read(buffer);
45     if (numchars<=0) {
46         l.fix();
47         l.close();
48         taskexit(l{!query,done}{});
49     }
50     String str=new String(buffer, 0, numchars);
51     if (l.data==null) {
52         l.data=str;
53     } else
54         l.data=l.data+str;
55         
56     taskexit;
57 }
58
59 task LookupW(Weather l{initialstate}) {
60     String query="GET /"+l.url+" HTTP/1.1\r\nConnection: close\r\nHost:"+l.hostname+"\r\n\r\n";
61     l.connect(l.hostname, 80);
62     l.write(query.getBytes());
63     taskexit(l{!initialstate, query});
64 }
65
66 task ReceiveQueryW(Weather l{query&&IOPending}) {
67     byte[] buffer=new byte[1024];
68     int numchars=l.read(buffer);
69     if (numchars<=0) {
70         l.fix();
71         l.close();
72         taskexit(l{!query,done}{});
73     }
74     String str=new String(buffer, 0, numchars);
75     if (l.data==null) {
76         l.data=str;
77     } else
78         l.data=l.data+str;
79     taskexit;
80 }
81
82
83
84 //Listen for a request and accept request 
85 task AcceptConnection(ServerSocket ss{SocketPending}) {
86     //  System.printString("W> Waiting for connection...\n");
87     tag t=new tag(link);
88     WebServerSocket web = new WebServerSocket() {!WritePending, !TransPending, WebInitialize}{t};
89     MySocket ms=new MySocket(){}{t};
90     ss.accept(ms);
91 //      System.printString("W> Connected... \n");
92 }
93
94 // Process the incoming http request 
95 task ProcessRequest(WebServerSocket web{WebInitialize}{link l}, MySocket s{IOPending}{link l}) {
96         if (web.clientrequest(s)) {
97             if(web.checktrans()==false)
98                 // Not special transaction , do normal filesending      
99                 taskexit(web {WritePending, LogPending,!WebInitialize}); //Sets the WritePending and LogPending flag true 
100             else {
101                 Weather w=new Weather(){initialstate}{l};
102                 Google g=new Google(){initialstate}{l};
103                 Stock st=new Stock(){initialstate}{l};
104                 taskexit(web {TransPending, LogPending,!WebInitialize});
105             }
106         }
107 }
108
109 //Do the WriteIO on server socket and send the requested file to Client
110 task SendFile(WebServerSocket web{WritePending}{link l}, MySocket s{}{link l}) {
111 //      System.printString("W> Inside SendFile ... \n");
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 }