+++ /dev/null
-public class Inventory {
- // Inventory flags
- flag TransInitialize;
-
- // Transaction variables
- int numitems;
- HashMap map;
-
- // Constructor
- public Inventory(){
- map = new HashMap();
- }
-
- public Inventory(int howmany) {
- numitems = howmany;// howmany keeps track of the number of items
- // in the inventory
- map = new HashMap();
- }
-
- // Add item to a list of inventory
- public int additem(String name, int quantity, int price){
- ItemInfo newitem = new ItemInfo(quantity, price);
- // Get the item from hash
- if (map.containsKey(name) == false) {
- map.put(name, newitem);
- } else {
- ItemInfo i = (ItemInfo) map.get(name);
- i.quantity += quantity;
- i.price = price;
- map.put(name, i);
- }
- return 0;
- }
-
- // Buy item from a given list of inventory
- public int buyitem(String name, int quantity){
- if (map.containsKey(name) == false) {
- System.printString("Error - Item does not exist");
- return -1;
- } else {
- ItemInfo i = (ItemInfo) map.get(name);
- if (i.quantity == 0) {
- System.printString("Error - Item unavailable");
- return -1;
- }
- if ((i.quantity-quantity) < 0 ) {
- System.printString("Error - Available qty is less: Cannot Buy\n");
- return -1;
- } else {
- i.quantity -= quantity;
- map.put(name, i);
- return i.price;
- }
- }
- return 0;
- }
-
- //Display the inventory list
- public String inventory(){
- HashMapIterator i = new HashMapIterator(map, 0);
- HashMapIterator j = new HashMapIterator(map, 1);
- StringBuffer sb = new StringBuffer("");
- while (i.hasNext() == true) {
- Object o = i.next();
- String name = o.toString();
- ItemInfo oo = (ItemInfo) j.next();
- sb.append(name);
- sb.append(" ");
- Integer q = new Integer(oo.quantity);
- sb.append(q.toString());
- sb.append(" ");
- Integer p = new Integer(oo.price);
- sb.append(p.toString());
- sb.append("\n");
- }
- String item = new String(sb);
- return item;
- }
-}
+++ /dev/null
-class ItemInfo {
- int quantity;
- int price;
- ItemInfo(int x, int y) {
- quantity = x;
- price = y;
- }
-}
+++ /dev/null
-public class Logger {
- //Logger flag
- flag Initialize;
- FileOutputStream fos;
-
-
- //Constructor
- public Logger(){
- fos=new FileOutputStream("./Tests/htmlfiles/request.log");//Open request.log file
- }
-
- //Logs filename as per client requests
- public void logrequest(String filename){
- String request = new String("\nNew Request received: ");
- fos.write(request.getBytes());
- fos.write(filename.getBytes());
- fos.flush();
- }
-
- public void logrequest(){
- String request = new String("\nNew Request received: ");
- fos.write(request.getBytes());
- fos.flush();
- }
-
- public void closerequest() {
- fos.close();
- }
-}
+++ /dev/null
-/* Startup object is generated with the initialstate flag set by the
- * system to start the computation up */
-
-// Create New ServerSocket
-task Startup(StartupObject s {initialstate}) {
- System.printString("W> Starting\n");
- ServerSocket ss = new ServerSocket(9000);
- System.printString("W> Creating ServerSocket\n");
- Logger log = new Logger() {Initialize};
- Inventory inventorylist = new Inventory(){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, !TransPending, WebInitialize};
- ss.accept(web);
- System.printString("W> Connected... \n");
-}
-
-// Process the incoming http request
-task ProcessRequest(WebServerSocket web{IOPending && WebInitialize}) {
-//task ProcessRequest(WebServerSocket web{IOPending}) {
- System.printString("W> Inside ProcessRequest... \n");
- if (web.clientrequest()) {
- if(web.checktrans()==false)
- // Not special transaction , do normal filesending
- taskexit(web {WritePending, LogPending,!WebInitialize}); //Sets the WritePending and LogPending flag true
- else
- // Invoke special inventory transaction
- taskexit(web {TransPending, LogPending,!WebInitialize});
- }
-}
-
-//Do the WriteIO on server socket and send the requested file to Client
-task SendFile(WebServerSocket web{WritePending}) {
- System.printString("W> Inside SendFile ... \n");
- web.sendfile();
- web.close();
- taskexit(web {!WritePending});
-}
-
-// Log the Client request
-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");
- // Parse
- int op = web.parseTransaction();
- // Check for the kind of operation
- if (op == 0 ) { /* Add */
- System.printString("DEBUG > Calling add transaction\n");
- Integer qty = new Integer(web.parsed[2]);
- Integer price = new Integer(web.parsed[3]);
- int ret = inventorylist.additem(web.parsed[1], qty.intValue(), price.intValue());
- if (ret == 0) {
- web.httpresponse();
- StringBuffer s = new StringBuffer("Added Item ");
- s.append(web.parsed[1]);
- s.append(" Quantity ");
- s.append(web.parsed[2]);
- s.append(" Price ");
- s.append(web.parsed[3]);
- s.append("\n");
- String towrite = new String(s);
- web.write(towrite.getBytes());
- } else {
- web.httpresponse();
- String s = new String("Error encountered");
- web.write(s.getBytes());
- }
- } else if (op == 1) { /* Buy */
- System.printString("DEBUG > Calling buy transaction\n");
- Integer qty = new Integer(web.parsed[2]);
- int ret = inventorylist.buyitem(web.parsed[1], qty.intValue());
- if (ret >= 0) {
- web.httpresponse();
- StringBuffer s = new StringBuffer("Bought item ");
- s.append(web.parsed[1]);
- s.append(" Quantity ");
- s.append(web.parsed[2]);
- s.append(" Cost ");
- Integer cost = new Integer(ret*qty.intValue());
- String c = cost.toString();
- s.append(c);
- String towrite = new String(s);
- web.write(towrite.getBytes());
- } else {
- web.httpresponse();
- String s = new String("Error encountered");
- web.write(s.getBytes());
- }
- } else if (op == 2) { /* Inventory */
- System.printString("DEBUG > Calling inventory transaction\n");
- web.httpresponse();
- String towrite = inventorylist.inventory();
- web.write(towrite.getBytes());
- } else { /* Error */
- System.printString("T > Error - Unknown transaction\n");
- }
- //Invoke close operations
- web.close();
- taskexit(web {!TransPending});
-}
+++ /dev/null
-public class WebServerSocket extends Socket {
- // Websocket flag
- flag LogPending;
- flag WritePending;
- flag TransPending;
- flag WebInitialize;
-
- //Filename requested by the client
- String filename;
- String[] parsed;
- String prefix;
-
- //Constructor
- public WebServerSocket(){
- parsed = new String[4];
- }
-
- //Send the http header for web browser display
- public void httpresponse(){
- StringBuffer header = new StringBuffer("HTTP/1.0 200 OK\n");
- header.append("Content-type: text/html\n");
- header.append("\n\n");
- String temp_str = new String(header);
- write(temp_str.getBytes());
- return;
-
- }
-
- // Send the html file , read from file one byte at a time
- public void sendfile() {
- StringBuffer req_file = new StringBuffer("./Tests/htmlfiles/");
- req_file.append(filename);
- String filepath = new String(req_file);
- FileInputStream def_file = new FileInputStream(filepath);
- int status = def_file.getfd();//Checks if the file is present in
- //current directory
- httpresponse();
- if (status == -1){
- StringBuffer response = new StringBuffer("404: not found: ");//Send 404 error if
- // file not found
- response.append(filename);
- String buffer = new String(response);
- write(buffer.getBytes());
- def_file.close();
- return;
- }
- byte buf[] = new byte[16];
- int ret;
-
- while ((ret = def_file.read(buf)) > 0) {// Read from file and write
- // one byte at a time into the socket
- byte tosend[] = new byte[ret];
- for (int i = 0; i < ret; i++) {
- tosend[i] = buf[i];
- }
- write(tosend);
- //String str = new String(tosend);
- }
- def_file.close();
- }
-
- //Read the client request and extract the filename from it
- public boolean clientrequest(){
- byte b1[] = new byte[1024];
- int numbytes=read(b1);//Read client request from web server socket
- String curr=(new String(b1)).subString(0, numbytes);
- if (prefix!=null) {
- StringBuffer sb=new StringBuffer(prefix);
- sb.append(curr);
- curr=sb.toString();
- }
- prefix=curr;
- if(prefix.indexOf("\r\n\r\n")>=0) {
-
- int index = prefix.indexOf('/');//Parse the GET client request to find filename
- int end = prefix.indexOf('H');
- filename = prefix.subString((index+1), (end-1));
- System.printString("\n");
- return true;
- }
- return false;
- }
-
- // 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;
- }
- }
-
- //Parse for the substrings in the filename and use it to obtain the
- //kind of operation, name of item, quantity of item, price of item
- //e.g. trans_add_car_2_10000 is the filename
- //store in the parsed[] string , add,car,2,1000
- public int parseTransaction(){
- int start = filename.indexOf('_');
- String s = filename.subString(start+1);
-
- if (s.startsWith("add")==true){
- System.printString("DEBUG > ADD\n");
- int i1 = s.indexOf('_');
- parsed[0] = new String(s.subString(0,i1));
-
- String s1 = s.subString(i1+1);
- int i2 = s1.indexOf('_');
- parsed[1] = new String(s1.subString(0,i2));
-
- String s2 = s1.subString(i2+1);
- int i3 = s2.indexOf('_');
- parsed[2] = new String(s2.subString(0,i3));
-
- String s3 = s2.subString(i3+1);
- parsed[3] = s3;
-
- return 0;
-
- }
- if (s.startsWith("buy")==true){
- System.printString("DEBUG > BUY\n");
- int i1 = s.indexOf('_');
- parsed[0] = s.subString(0,i1);
-
- String s1 = s.subString(i1+1);
- int i2 = s1.indexOf('_');
- parsed[1] = s1.subString(0,i2);
-
- String s2 = s1.subString(i2+1);
- parsed[2] = s2;
-
- parsed[3] = "";
-
- return 1;
- }
- if (s.startsWith("inventory")==true){
- System.printString("DEBUG > INVENTORY\n");
- return 2;
-
- }
- // Error transaction
- return -1;
- }
-}