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