changes to build script to increase java heap memory
[IRC.git] / Robust / src / Benchmarks / WebServerTag / 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 //      System.printString("W> Starting\n");
7         ServerSocket ss = new ServerSocket(9000);
8 //      System.printString("W> Creating ServerSocket\n");
9         Logger log = new Logger() {Initialize};
10         Inventory inventorylist = new Inventory(){TransInitialize};
11         taskexit(s {!initialstate}); /* Turns initial state flag off, so this task won't refire */
12 }
13
14 //Listen for a request and accept request 
15 task AcceptConnection(ServerSocket ss{SocketPending}) {
16     //  System.printString("W> Waiting for connection...\n");
17     tag t=new tag(link);
18         WebServerSocket web = new WebServerSocket() {!WritePending, !TransPending, WebInitialize}{t};
19         ss.accept(t);
20 //      System.printString("W> Connected... \n");
21 }
22
23 // Process the incoming http request 
24 task ProcessRequest(WebServerSocket web{WebInitialize}{link l}, Socket s{IOPending}{link l}) {
25         if (web.clientrequest(s)) {
26             if(web.checktrans()==false)
27                 // Not special transaction , do normal filesending      
28                 taskexit(web {WritePending, LogPending,!WebInitialize}); //Sets the WritePending and LogPending flag true 
29             else 
30                 // Invoke special inventory transaction
31                 taskexit(web {TransPending, LogPending,!WebInitialize});
32         }
33 }
34
35 //Do the WriteIO on server socket and send the requested file to Client
36 task SendFile(WebServerSocket web{WritePending}{link l}, Socket s{}{link l}) {
37 //      System.printString("W> Inside SendFile ... \n");
38         web.sendfile(s);
39         s.close();
40         taskexit(web {!WritePending});
41 }
42
43 // Log the Client request
44 task LogRequest(WebServerSocket web{LogPending}, Logger log{Initialize}) {
45 //Task fired when both
46 // LogPending and Initialize flags are true 
47 //      System.printString("L > Inside logrequest\n");
48         log.logrequest(web.filename);
49         taskexit(web {!LogPending});
50 }
51
52 //Transaction on Inventory
53 task Transaction(WebServerSocket web{TransPending}{link l}, Inventory inventorylist{TransInitialize},Socket s{}{link l}){ //Task for WebServerTransactions
54 //      System.printString("T > Inside Transaction\n");
55         // Parse
56         int op = web.parseTransaction();
57         // Check for the kind of operation
58         if (op == 0 ) { /* Add */
59 //              System.printString("DEBUG > Calling add transaction\n");
60                 Integer qty = new Integer(web.parsed[2]);
61                 Integer price = new Integer(web.parsed[3]);
62                 int ret = inventorylist.additem(web.parsed[1], qty.intValue(), price.intValue());
63                 if (ret == 0) {
64                         web.httpresponse(s);
65                         StringBuffer st = new StringBuffer("Added Item ");
66                         st.append(web.parsed[1]);
67                         st.append(" Quantity ");
68                         st.append(web.parsed[2]);
69                         st.append(" Price ");
70                         st.append(web.parsed[3]);
71                         st.append("\n");
72                         String towrite = new String(st);
73                         s.write(towrite.getBytes());
74                 } else {
75                         web.httpresponse(s);
76                         String st = new String("Error encountered");
77                         s.write(st.getBytes());
78                 }       
79         } else if (op == 1) { /* Buy */
80 //              System.printString("DEBUG > Calling buy transaction\n");
81                 Integer qty = new Integer(web.parsed[2]);
82                 int ret = inventorylist.buyitem(web.parsed[1], qty.intValue());
83                 if (ret >= 0) {
84                         web.httpresponse(s);
85                         StringBuffer st = new StringBuffer("Bought item ");
86                         st.append(web.parsed[1]);
87                         st.append(" Quantity ");
88                         st.append(web.parsed[2]);
89                         st.append(" Cost ");
90                         Integer cost = new Integer(ret*qty.intValue());
91                         String c = cost.toString();
92                         st.append(c);
93                         String towrite = new String(st);
94                         s.write(towrite.getBytes());
95                 } else {
96                         web.httpresponse(s);
97                         String st = new String("Error encountered");
98                         s.write(st.getBytes());
99                 }
100         } else if (op == 2) { /* Inventory */
101 //              System.printString("DEBUG > Calling inventory transaction\n");
102                 web.httpresponse(s);
103                 inventorylist.inventory(s);     
104         } else { /* Error */ 
105 //              System.printString("T > Error - Unknown transaction\n");
106         }
107         //Invoke close operations
108         s.close();
109         taskexit(web {!TransPending});
110 }