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");
}
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
}
// 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});
+}
// 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
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;
+ }
}
-