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 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 */
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};
19 System.printString("W> Connected... \n");
22 // Process the incoming http request
23 task ProcessRequest(WebServerSocket web{IOPending}) {
24 System.printString("W> Inside ProcessRequest... \n");
26 if(web.checktrans()==false)
27 // Not special transaction , do normal filesending
28 taskexit(web {WritePending, LogPending}); //Sets the WritePending and LogPending flag true
30 // Invoke special inventory transaction
31 taskexit(web {TransPending, LogPending});
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");
39 taskexit(web {!WritePending});
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});
51 //Transaction on Inventory
52 task Transaction(WebServerSocket web{TransPending}, Inventory inventorylist{TransInitialize}){ //Task for WebServerTransactions
53 System.printString("T > Inside Transaction\n");
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());
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]);
73 s.append(web.parsed[3]);
75 String towrite = new String(s);
76 web.write(towrite.getBytes());
79 String s = new String("Error encountered");
80 web.write(s.getBytes());
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());
88 StringBuffer s = new StringBuffer("Bought item ");
89 s.append(web.parsed[1]);
90 s.append(" Quantity ");
91 s.append(web.parsed[2]);
93 Integer cost = new Integer(ret*qty.intValue());
94 String c = cost.toString();
96 String towrite = new String(s);
97 web.write(towrite.getBytes());
100 String s = new String("Error encountered");
101 web.write(s.getBytes());
103 } else if (op == 2) { /* Inventory */
104 System.printString("DEBUG > Calling inventory transaction\n");
105 String towrite = inventorylist.inventory();
106 web.write(towrite.getBytes());
108 System.printString("T > Error - Unknown transaction\n");
112 taskexit(web {!TransPending});