Buggy code for transaction check TO DO: correct the Task Transaction
authoradash <adash>
Fri, 3 Nov 2006 18:09:08 +0000 (18:09 +0000)
committeradash <adash>
Fri, 3 Nov 2006 18:09:08 +0000 (18:09 +0000)
Robust/src/Tests/WebServerExample.java
Robust/src/Tests/WebServerSocket.java

index c0e482be5215bb1641ea4abfac97a6e548d266fd..b43a0a26b6911883c611afbb52dc6458f7e9eee2 100644 (file)
@@ -7,13 +7,14 @@ task Startup(StartupObject s {initialstate}) {
        ServerSocket ss = new ServerSocket(9000);
        System.printString("W> Creating ServerSocket\n");
        Logger log = new Logger() {Initialize};
+       Inventory inventorylist = new Inventory(3){TransInitialize};
        taskexit(s {!initialstate}); /* Turns initial state flag off, so this task won't refire */
 }
 
 //Listen for a request and accept request 
 task AcceptConnection(ServerSocket ss{SocketPending}) {
        System.printString("W> Waiting for connection...\n");
-       WebServerSocket web = new WebServerSocket() {!WritePending};
+       WebServerSocket web = new WebServerSocket() {!WritePending, !TransPending};
        ss.accept(web);
        System.printString("W> Connected... \n");
 }
@@ -22,7 +23,12 @@ task AcceptConnection(ServerSocket ss{SocketPending}) {
 task ProcessRequest(WebServerSocket web{IOPending}) {
        System.printString("W> Inside ProcessRequest... \n");
        web.clientrequest();
-       taskexit(web {WritePending, LogPending}); //Sets the WritePending and LogPending flag true 
+       if(web.checktrans()==false)
+               // Not special transaction , do normal filesending      
+               taskexit(web {WritePending, LogPending}); //Sets the WritePending and LogPending flag true 
+       else 
+               // Invoke special inventory transaction
+               taskexit(web {TransPending, LogPending});
 }
 
 //Do the WriteIO on server socket and send the requested file to Client
@@ -34,9 +40,38 @@ task SendFile(WebServerSocket web{WritePending}) {
 }
 
 // Log the Client request
-task LogRequest(WebServerSocket web{LogPending}, Logger log{Initialize}) {//Task fired when both
-                                                                        // LogPending and Initialize flags are true 
+task LogRequest(WebServerSocket web{LogPending}, Logger log{Initialize}) {
+//Task fired when both
+// LogPending and Initialize flags are true 
        System.printString("L > Inside logrequest\n");
        log.logrequest(web.filename);
        taskexit(web {!LogPending});
 }
+
+//Transaction on Inventory
+task Transaction(WebServerSocket web{TransPending}, Inventory inventorylist{TransInitialize}){ //Task for WebServerTransactions
+       System.printString("T > Inside Transaction\n");
+       char[] op = new char[10];
+       // Parse
+       web.parseTransaction();
+       // Check for the kind of operation
+       op = web.parsed[0].toCharArray();
+       if (op[0] == 'a') {
+               Integer qty = new Integer(web.parsed[1]);
+               Integer price = new Integer(web.parsed[2]);
+               inventorylist.additem(web.parsed[0], qty.intValue(), price.intValue());
+       } else if (op[0] == 'b') {
+               Integer qty = new Integer(web.parsed[1]);
+               Integer price = new Integer(web.parsed[2]);
+               inventorylist.buyitem(web.parsed[0], qty.intValue(), price.intValue());
+       } else if (op[0] == 'i') {
+               inventorylist.inventory();
+       } else {
+               System.printString("T > Error - Unknown transaction\n");
+       }
+
+       //Invoke operations
+
+       web.close();
+       taskexit(web {!TransPending});
+}
index 3ef5589c2bf66e1db0a8f0ab0497e15e4998dc23..f94bba605ed42b9482b09588a58daf99082099e0 100644 (file)
@@ -2,11 +2,14 @@ public class WebServerSocket extends Socket {
        // Websocket flag
        flag LogPending;
        flag WritePending;
+       flag TransPending;
        //Filename requested by the client 
        String filename;
-       
+       String[] parsed; 
+               
        //Constructor
        public WebServerSocket(){
+               parsed = new String[4];
        }
        
        //Send the http header for web browser display  
@@ -54,15 +57,62 @@ public class WebServerSocket extends Socket {
                def_file.close();
        }
 
-       //Discover what the client wants and handle their request       
+       //Read the client request and extract the filename from it      
        public int clientrequest(){
                byte b1[] = new byte[1024];
                read(b1);//Read client request from web server socket
                String clientreq = new String(b1);
+               System.printString(clientreq);
+               System.printString("\n");
                int index = clientreq.indexOf('/');//Parse the GET client request to find filename
                int end = clientreq.indexOf('H');
                filename = clientreq.subString((index+1), (end-1));
+               System.printString(filename);
+               System.printString("\n");
                return 0;
        }
+       
+       // Parse  for the prefix in the client request
+       // This is helpful to find if the prefix is a special transaction
+       public boolean checktrans(){
+               if (filename.startsWith("trans") == true) {
+                       return true;
+               } else {
+                       return false;
+               }
+       }
+       
+       //Process special request
+       public void parseTransaction() {
+               //System.printString("DEBUG -> Inside parseTransaction");       
+               int start = filename.indexOf('_');
+               String s = filename.subString(start+1);
+               int n = 4;
+/*
+               if (s.startsWith("add")== true) {
+                       n = 4;
+               }
+               if (s.startsWith("buy")== true) {
+                       n = 3;
+               }
+               if (s.startsWith("inventory")== true) {
+                       n = 1;
+               }
+*/
+               for (int i = 0; i < n; i++) {
+                       int index;
+                       if (i == n-1) 
+                               index = s.indexOf('.');
+                       else 
+                               index = s.indexOf('_');
+                       parsed[i]  = s.subString(0,index);
+                       String tmp = s.subString(index+1);
+                       s = tmp;
+               }
+               for (int i = 0; i < 4; i++) {
+                       System.printString("\n Parsing : ");
+                       System.printString(parsed[i]);
+               }
+               return;
+       }       
 }
-