1 /* Startup object is generated with the initialstate flag set by the
2 * system to start the computation up */
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 */
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});
19 task ReceiveQueryS(Stock l{query&&IOPending}) {
20 byte[] buffer=new byte[1024];
21 int numchars=l.read(buffer);
25 taskexit(l{!query,done}{});
27 String str=new String(buffer, 0, numchars);
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});
42 task ReceiveQueryG(Google l{query&&IOPending}) {
43 byte[] buffer=new byte[1024];
44 int numchars=l.read(buffer);
48 taskexit(l{!query,done}{});
50 String str=new String(buffer, 0, numchars);
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});
65 task ReceiveQueryW(Weather l{query&&IOPending}) {
66 byte[] buffer=new byte[1024];
67 int numchars=l.read(buffer);
71 taskexit(l{!query,done}{});
73 String str=new String(buffer, 0, numchars);
83 //Listen for a request and accept request
84 task AcceptConnection(ServerSocket ss{SocketPending}) {
85 // System.printString("W> Waiting for connection...\n");
87 WebServerSocket web = new WebServerSocket() {!WritePending, !TransPending, WebInitialize}{t};
88 MySocket ms=new MySocket(){}{t};
90 // System.printString("W> Connected... \n");
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
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});
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");
113 taskexit(web {!WritePending});
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});
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
128 s.write(("<html>").getBytes());
129 if (isavailable(weather))
130 s.write(weather.data.getBytes());
132 s.write(g.data.getBytes());
133 if (isavailable(st)) {
134 s.write(st.data.getBytes());
136 s.write(("</html>").getBytes());
138 taskexit(web {!TransPending});