new benchmark
[IRC.git] / Robust / src / Benchmarks / Conglomerator / Tag / Opt / WebServerExampleOpt.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     taskexit;
56 }
57
58 task LookupW(Weather l{initialstate}) {
59     String query="GET /"+l.url+" HTTP/1.1\r\nConnection: close\r\nHost:"+l.hostname+"\r\n\r\n";
60     l.connect(l.hostname, 80);
61     l.write(query.getBytes());
62     taskexit(l{!initialstate, query});
63 }
64
65 task ReceiveQueryW(Weather l{query&&IOPending}) {
66     byte[] buffer=new byte[1024];
67     int numchars=l.read(buffer);
68     if (numchars<=0) {
69         l.fix();
70         l.close();
71         taskexit(l{!query,done}{});
72     }
73     String str=new String(buffer, 0, numchars);
74     if (l.data==null) {
75         l.data=str;
76     } else
77         l.data=l.data+str;
78     taskexit;
79 }
80
81
82
83 //Listen for a request and accept request 
84 task AcceptConnection(ServerSocket ss{SocketPending}) {
85     //  System.printString("W> Waiting for connection...\n");
86     tag t=new tag(link);
87     WebServerSocket web = new WebServerSocket() {!WritePending, !TransPending, WebInitialize}{t};
88     MySocket ms=new MySocket(){}{t};
89     ss.accept(ms);
90 //      System.printString("W> Connected... \n");
91 }
92
93 // Process the incoming http request 
94 task ProcessRequest(WebServerSocket web{WebInitialize}{link l}, MySocket s{IOPending}{link l}) {
95         if (web.clientrequest(s)) {
96             if(web.checktrans()==false)
97                 // Not special transaction , do normal filesending      
98                 taskexit(web {WritePending, LogPending,!WebInitialize}); //Sets the WritePending and LogPending flag true 
99             else {
100                 Weather w=new Weather(){initialstate}{l};
101                 Google g=new Google(){initialstate}{l};
102                 Stock st=new Stock(){initialstate}{l};
103                 taskexit(web {TransPending, LogPending,!WebInitialize});
104             }
105         }
106 }
107
108 //Do the WriteIO on server socket and send the requested file to Client
109 task SendFile(WebServerSocket web{WritePending}{link l}, MySocket s{}{link l}) {
110 //      System.printString("W> Inside SendFile ... \n");
111         web.sendfile(s);
112         s.close();
113         taskexit(web {!WritePending});
114 }
115
116 // Log the Client request
117 task LogRequest(WebServerSocket web{LogPending}, Logger log{Initialize}) {
118 //Task fired when both
119 // LogPending and Initialize flags are true 
120 //      System.printString("L > Inside logrequest\n");
121         log.logrequest(web.filename);
122         taskexit(web {!LogPending});
123 }
124
125 //Transaction on Inventory
126 task Transaction(WebServerSocket web{TransPending}{link l}, optional Weather weather{done}{link l}, optional Google g{done}{link l}, MySocket s{}{link l}, optional Stock st{done}{link l}){ //Task for WebServerTransactions
127         web.httpresponse(s);
128         s.write(("<html>").getBytes());
129         if (isavailable(weather))
130             s.write(weather.data.getBytes());
131         if (isavailable(g))
132             s.write(g.data.getBytes());
133         if (isavailable(st)) {
134             s.write(st.data.getBytes());
135         }
136         s.write(("</html>").getBytes());
137         s.close();
138         taskexit(web {!TransPending});
139 }