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