From: bdemsky Date: Fri, 27 Apr 2007 02:42:51 +0000 (+0000) Subject: more benchmarks from Danish Lakhani and Jason Jung's EECS 221 Project X-Git-Tag: preEdgeChange~612 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=e7387e0510c3a9fbf2a82c08c7dc63f3bae1fda1;hp=07eae03140fa8124f538421317646cf5af64cc39;p=IRC.git more benchmarks from Danish Lakhani and Jason Jung's EECS 221 Project --- diff --git a/Robust/src/Benchmarks/BankApp/BankAccount.java b/Robust/src/Benchmarks/BankApp/BankAccount.java new file mode 100644 index 00000000..b3067f0f --- /dev/null +++ b/Robust/src/Benchmarks/BankApp/BankAccount.java @@ -0,0 +1,70 @@ +public class BankAccount +{ + //can't init here, won't compile, do it in the constructor + + //nine digits + String AccountNumber; //field #1 + + //account owner's name + //always 10 chars + //pad with @ + String FirstName; //field #2 + String MiddleName; //field #3 + String LastName; //field #4 + + //1 == Savings + //2 == Checking + //3 == Teller + String AccountType; //field #5 + + //ints only, should use floats in the future + //1234567890 + //assumes balance does is never negative + //always 10 chars + //pad with @ + String Balance; //field #6 + + //four digits + String PIN; //field #7 + + public BankAccount() + { + + } + + public BankAccount(String account, String first, String middle, String last, String type, String balance, String pin) + { + if(account != null) + AccountNumber = account; + if(first != null) + FirstName = first; + if(middle != null) + MiddleName = middle; + if(last != null) + LastName = last; + if(type != null) + AccountType = type; + if(balance != null) + Balance = balance; + if(pin != null) + PIN = pin; + } + + public void modifyAccount(String account, String first, String middle, String last, String type, String balance, String pin) + { + if(account != null) + AccountNumber = account; + if(first != null) + FirstName = first; + if(middle != null) + MiddleName = middle; + if(last != null) + LastName = last; + if(type != null) + AccountType = type; + if(balance != null) + Balance = balance; + if(pin != null) + PIN = pin; + } +} diff --git a/Robust/src/Benchmarks/BankApp/BankApp.java b/Robust/src/Benchmarks/BankApp/BankApp.java new file mode 100644 index 00000000..7d9bb9af --- /dev/null +++ b/Robust/src/Benchmarks/BankApp/BankApp.java @@ -0,0 +1,357 @@ +//Banking Application Server + +/* Startup object is generated with the initialstate flag set by the + * system to start the computation up */ + +task Startup(StartupObject s{initialstate}) +{ + System.printString("Starting\n"); + ServerSocket ss = new ServerSocket(8080); + System.printString("Creating ServerSocket\n"); + BankDatabase Bank = new BankDatabase(){DatabaseInit}; + taskexit(s{!initialstate}); /* Turns initial state flag off, so this task won't refire */ +} + +task AcceptConnection(ServerSocket ss{SocketPending}) +{ + BankAppSocket bas = new BankAppSocket(){BASocketInit}; + ss.accept(bas); + System.printString("Connected\n"); +} + +//i think this task could probably be broken up into smaller tasks +task ProcessRequest(BankAppSocket bas{IOPending && BASocketInit}, BankDatabase Bank{DatabaseInit}) +{ + String message = new String(bas.receive()); + //System.printString(message); + + //login + if(message.startsWith("1")) + { + String account = message.subString(1, 10); + String pin = message.subString(10, 14); + + for(int i = 0; i < Bank.numOfAccounts; i++) + { + if(Bank.database[i].AccountNumber.equals(account) && Bank.database[i].PIN.equals(pin)) + { + bas.send("Login OK"); + //System.printString("Login OK"); + } + else + { + bas.send("Login Error"); + //System.printString("Login Error"); + } + } + } + //logout + else if(message.startsWith("2")) + { + String account = message.subString(1, 10); + + //find the account + for(int i = 0; i < Bank.numOfAccounts; i++) + { + if(Bank.database[i].AccountNumber.equals(account)) + { + bas.send("Logout OK"); + //System.printString("Logout OK"); + } + else + { + bas.send("Logout Error"); + //System.printString("Logout Error"); + } + } + } + //create + else if(message.startsWith("3")) + { + String account = message.subString(1, 10); + String first = message.subString(10, 20); + String middle = message.subString(20, 30); + String last = message.subString(30, 40); + String type = message.subString(40, 41); + String balance = message.subString(41, 51); + String pin = message.subString(51, 55); + + //find first empty space + int id = -1; + for(int i = 0; i < Bank.numOfAccounts; i++) + { + if(Bank.database[i].AccountNumber.equals("@@@@@@@@@")) + id = i; + } + + if(id != -1) + { + //should check for input errors first but... + Bank.database[id].AccountNumber = first; + Bank.database[id].FirstName = middle; + Bank.database[id].MiddleName = last; + Bank.database[id].LastName = last; + Bank.database[id].AccountType = type; + Bank.database[id].Balance = balance; + Bank.database[id].PIN = pin; + + Bank.numOfAccounts++; + + bas.send(Bank.database[id].AccountNumber); + //System.printString(Bank.database[id].AccountNumber); + } + else + { + bas.send("Create Error"); + //System.printString("Create Error"); + } + } + //delete + else if(message.startsWith("4")) + { + String account = message.subString(1, 10); + + //find the account + int id = -1; + for(int i = 0; i < Bank.numOfAccounts; i++) + { + if(Bank.database[i].AccountNumber.equals(account)) + id = i; + } + + if(id != -1) + { + Bank.database[id].AccountNumber = "@@@@@@@@@@"; + Bank.database[id].FirstName = "@@@@@@@@@@"; + Bank.database[id].MiddleName = "@@@@@@@@@@"; + Bank.database[id].LastName = "@@@@@@@@@@"; + Bank.database[id].AccountType = "@"; + Bank.database[id].Balance = "@@@@@@@@@@"; + Bank.database[id].PIN = "@@@@"; + Bank.numOfAccounts--; + + bas.send("Close Account OK"); + //System.printString("Close Account OK"); + } + else + { + bas.send("Close Account Error"); + //System.printString("Close Account Error"); + } + } + //modify + else if(message.startsWith("5")) + { + String account = message.subString(1, 10); + String field = message.subString(10, 11); + //two digits 00-99 + String numBytes = message.subString(11, 13); + String data = message.subString(13, 13 + Integer.parseInt(numBytes)); + + //find the account + int id = -1; + for(int i = 0; i < Bank.numOfAccounts; i++) + { + if(Bank.database[i].AccountNumber.equals(account)) + id = i; + } + + if(id != -1) + { + //maybe shouldn't allow changes to some of these fields + if(field.equals("1")) + { + Bank.database[id].AccountNumber = data; + } + else if(field.equals("2")) + { + Bank.database[id].FirstName = data; + } + else if(field.equals("3")) + { + Bank.database[id].MiddleName = data; + } + else if(field.equals("4")) + { + Bank.database[id].LastName = data; + } + else if(field.equals("5")) + { + Bank.database[id].AccountType = data; + } + else if(field.equals("6")) + { + Bank.database[id].Balance = data; + } + else if(field.equals("7")) + { + Bank.database[id].PIN = data; + } + + bas.send("Modify OK"); + //System.printString("Modify OK"); + } + else + { + bas.send("Modify Error"); + //System.printString("Modify Error"); + } + } + //check account info + else if(message.startsWith("6")) + { + String account = message.subString(1, 10); + + //find the account + int id = -1; + for(int i = 0; i < Bank.numOfAccounts; i++) + { + if(Bank.database[i].AccountNumber.equals(account)) + id = i; + } + + if(id != -1) + { + StringBuffer strBuffer = new StringBuffer(Bank.database[id].AccountNumber); + strBuffer.append(Bank.database[id].FirstName); + strBuffer.append(Bank.database[id].MiddleName); + strBuffer.append(Bank.database[id].LastName); + strBuffer.append(Bank.database[id].AccountType); + strBuffer.append(Bank.database[id].Balance); + strBuffer.append(Bank.database[id].PIN); + + bas.send(strBuffer.toString()); + //System.printString(strBuffer.toString()); + } + else + { + bas.send("Check Account Info Error"); + //System.printString("Check Account Info Error"); + } + + } + //deposit + //more string operations or a Float Object could be useful here + else if(message.startsWith("7")) + { + String account = message.subString(1, 10); + //two digits 00-99 + //dollar part only + String numBytes = message.subString(10, 12); + //get dollars + String data = message.subString(12, 12 + Integer.parseInt(numBytes)); + + + //find the account + int id = -1; + for(int i = 0; i < Bank.numOfAccounts; i++) + { + if(Bank.database[i].AccountNumber.equals(account)) + id = i; + } + + if(id != -1) + { + Integer sum = new Integer(Integer.parseInt(Bank.database[id].Balance) + Integer.parseInt(data)); + + StringBuffer sumBuffer = new StringBuffer(sum.toString()); + + int padding = 10 - sumBuffer.length(); + + for(int i = 0; i < padding; i++) + { + sumBuffer.append("@"); + } + + //assumes no overflow + Bank.database[id].Balance = sumBuffer.toString(); + + bas.send("Deposit OK"); + //System.printString("Deposit OK"); + } + else + { + bas.send("Deposit Error"); + //System.printString("Deposit Error"); + } + } + //withdraw + else if(message.startsWith("8")) + { + String account = message.subString(1, 10); + //two digits 00-99 + //dollar part only + String numBytes = message.subString(10, 12); + //get dollars + String data = message.subString(12, 12 + Integer.parseInt(numBytes)); + + //find the account + int id = -1; + for(int i = 0; i < Bank.numOfAccounts; i++) + { + if(Bank.database[i].AccountNumber.equals(account)) + id = i; + } + + if(id != -1) + { + Integer difference = new Integer(Integer.parseInt(Bank.database[id].Balance) - Integer.parseInt(data)); + + if(difference.intValue() >= 0) + { + StringBuffer difBuffer = new StringBuffer(difference.toString()); + + int padding = 10 - difBuffer.length(); + + for(int i = 0; i < padding; i++) + { + difBuffer.append("@"); + } + + //assumes no overflow + Bank.database[id].Balance = difBuffer.toString(); + + bas.send("Withdraw OK"); + //System.printString("Withdraw OK"); + } + else + { + bas.send("Overdraw Error"); + //System.printString("Overdraw Error"); + } + } + else + { + bas.send("Withdraw Error"); + //System.printString("Withdraw Error"); + } + } + //check balance + else if(message.startsWith("9")) + { + String account = message.subString(1, 10); + + int id = -1; + for(int i = 0; i < Bank.numOfAccounts; i++) + { + if(Bank.database[i].AccountNumber.equals(account)) + id = i; + } + + if(id != -1) + { + bas.send(Bank.database[id].Balance); + //System.printString(Bank.database[id].Balance); + } + else + { + bas.send("Check Balance Error"); + //System.printString("Check Balance Error"); + } + } + else + { + bas.send("Message Error"); + //System.printString("Message Error"); + } +} diff --git a/Robust/src/Benchmarks/BankApp/BankAppRead.dat b/Robust/src/Benchmarks/BankApp/BankAppRead.dat new file mode 100644 index 00000000..57754948 --- /dev/null +++ b/Robust/src/Benchmarks/BankApp/BankAppRead.dat @@ -0,0 +1 @@ +101010101Tony@@@@@@Stone@@@@@Smith@@@@@2123456@@@@8888 \ No newline at end of file diff --git a/Robust/src/Benchmarks/BankApp/BankAppSocket.java b/Robust/src/Benchmarks/BankApp/BankAppSocket.java new file mode 100644 index 00000000..cca10826 --- /dev/null +++ b/Robust/src/Benchmarks/BankApp/BankAppSocket.java @@ -0,0 +1,26 @@ +public class BankAppSocket extends Socket +{ + flag BASocketInit; + + public BankAppSocket() + { + + } + + public void send(String message) + { + write(message.getBytes()); + } + + public String receive() + { + byte buffer[] = new byte[64]; + + int numbytes = read(buffer); + + //it's subString() not substring() like in Java + String message = (new String(buffer)).subString(0, numbytes); + + return message; + } +} diff --git a/Robust/src/Benchmarks/BankApp/BankAppWrite.dat b/Robust/src/Benchmarks/BankApp/BankAppWrite.dat new file mode 100644 index 00000000..57754948 --- /dev/null +++ b/Robust/src/Benchmarks/BankApp/BankAppWrite.dat @@ -0,0 +1 @@ +101010101Tony@@@@@@Stone@@@@@Smith@@@@@2123456@@@@8888 \ No newline at end of file diff --git a/Robust/src/Benchmarks/BankApp/BankDatabase.java b/Robust/src/Benchmarks/BankApp/BankDatabase.java new file mode 100644 index 00000000..9b4ff94d --- /dev/null +++ b/Robust/src/Benchmarks/BankApp/BankDatabase.java @@ -0,0 +1,107 @@ +public class BankDatabase +{ + flag DatabaseInit; + + BankAccount[] database; + int numOfAccounts; + + public BankDatabase() + { + //6 pre-created accounts + numOfAccounts = 6; + + //10 account limit + database = new BankAccount[10]; + + for(int i = 0; i < 10; i++) + { + database[i] = new BankAccount(); + } + + //some hardcoded values + database[0].modifyAccount("123456789", "John@@@@@@", "Q@@@@@@@@@", "Public@@@@", "1", "256000001@", "2007"); + database[1].modifyAccount("987654321", "Nancy@@@@@", "H@@@@@@@@@", "Private@@@", "2", "166@@@@@@@", "1234"); + database[2].modifyAccount("000111000", "Paul@@@@@@", "Wellington", "Franks@@@@", "1", "454225@@@@", "0000"); + database[3].modifyAccount("211411911", "Felix@@@@@", "the@@@@@@@", "Cat@@@@@@@", "3", "0@@@@@@@@@", "9999"); + database[4].modifyAccount("111000111", "Paul@@@@@@", "Wellington", "Franks@@@@", "2", "1128989@@@", "0000"); + //empty + database[5].modifyAccount("@@@@@@@@@", "@@@@@@@@@@", "@@@@@@@@@@", "@@@@@@@@@@", "@", "@@@@@@@@@@", "@@@@"); + database[6].modifyAccount("@@@@@@@@@", "@@@@@@@@@@", "@@@@@@@@@@", "@@@@@@@@@@", "@", "@@@@@@@@@@", "@@@@"); + database[7].modifyAccount("@@@@@@@@@", "@@@@@@@@@@", "@@@@@@@@@@", "@@@@@@@@@@", "@", "@@@@@@@@@@", "@@@@"); + database[8].modifyAccount("@@@@@@@@@", "@@@@@@@@@@", "@@@@@@@@@@", "@@@@@@@@@@", "@", "@@@@@@@@@@", "@@@@"); + database[9].modifyAccount("@@@@@@@@@", "@@@@@@@@@@", "@@@@@@@@@@", "@@@@@@@@@@", "@", "@@@@@@@@@@", "@@@@"); + + //test read into database[5] + ReadFile(5); + + //test write from database[5] + WriteFile(database[5].AccountNumber, database[5].FirstName, database[5].MiddleName, database[5].LastName, database[5].AccountType, database[5].Balance, database[5].PIN); + } + + /* what, no destructor? + public ~BankDatabase() + { + //test write from database[5] + }*/ + + public void ReadFile(int index) + { + //need to check if read/write works the way I think it does + String filename="BankAppRead.dat"; + FileInputStream fis = new FileInputStream(filename); + + byte account[] = new byte[9]; + byte first[] = new byte[10]; + byte middle[] = new byte[10]; + byte last[] = new byte[10]; + byte type[] = new byte[1]; + byte balance[] = new byte[10]; + byte pin[] = new byte[4]; + + //read one account for now + fis.read(account); + fis.read(first); + fis.read(middle); + fis.read(last); + fis.read(type); + fis.read(balance); + fis.read(pin); + + fis.close(); + + String S1 = new String(account); + //System.printString(S1); + String S2 = new String(first); + //System.printString(S2); + String S3 = new String(middle); + //System.printString(S3); + String S4 = new String(last); + //System.printString(S4); + String S5 = new String(type); + //System.printString(S5); + String S6 = new String(balance); + //System.printString(S6); + String S7 = new String(pin); + //System.printString(S7); + + //read into one account for now + database[index].modifyAccount(S1, S2, S3, S4, S5, S6, S7); + } + + public void WriteFile(String account, String first, String middle, String last, String type, String balance, String pin) + { + String filename="BankAppWrite.dat"; + FileOutputStream fos = new FileOutputStream(filename); + + //write one account for now + fos.write(account.getBytes()); + fos.write(first.getBytes()); + fos.write(middle.getBytes()); + fos.write(last.getBytes()); + fos.write(type.getBytes()); + fos.write(balance.getBytes()); + fos.write(pin.getBytes()); + + fos.close(); + } +} diff --git a/Robust/src/Benchmarks/BankAppJava/BankAppClientTeller.java b/Robust/src/Benchmarks/BankAppJava/BankAppClientTeller.java new file mode 100644 index 00000000..a74c0b3b --- /dev/null +++ b/Robust/src/Benchmarks/BankAppJava/BankAppClientTeller.java @@ -0,0 +1,374 @@ +// Bank App in Java + +// Author: Danish Lakhani + +// BankAppServer - Bank Application Server that services one client at a time + +import java.io.*; +import java.net.*; +import java.util.*; + +class BankAppClientTeller +{ + static int SUCCESS = 0; + static int ERROR = 1; + + static int LOGIN_ACCOUNT = 1; + static int LOGIN_PIN = 2; + + static int ACCOUNT_SAVINGS = 1; + static int ACCOUNT_CHECKING = 2; + static int ACCOUNT_TELLER = 3; + + private Socket mySocket = null; + private PrintWriter out = null; + private BufferedReader in = null; + private static int serverPort = 44444; + + public BankAppClientTeller() + { + } + + private void establishConnection() + throws IOException + { + System.out.println("Connecting to server..."); + mySocket = new Socket("localhost", serverPort); + out = new PrintWriter(mySocket.getOutputStream(), true); + in = new BufferedReader(new InputStreamReader(mySocket.getInputStream())); + System.out.println("Connection Established!"); + } + + private void closeConnection() + throws IOException + { + if (out != null) + out.close(); + if (in != null) + in.close(); + if (mySocket != null) + mySocket.close(); + } + + private void displayMenu() + { + System.out.println("\nBankAppClientTeller"); + System.out.println("----------------"); + System.out.println("1. Login"); + System.out.println("2. Logout"); + System.out.println("3. Deposit"); + System.out.println("4. Withdraw"); + System.out.println("5. Check Balance"); + System.out.println("6. Open Account"); + System.out.println("7. Close Account"); + System.out.println("8. Modify Account"); + System.out.println("9. Check Account Info"); + System.out.println("0. Exit\n"); + System.out.print("Enter Choice: "); + return; + } + + private void startClient() + throws IOException + { + int clientQuit = 0; + int status = SUCCESS; + int isConnected = 0; + boolean loggedIn = false; + BufferedReader local_in = new BufferedReader(new InputStreamReader(System.in)); + while (clientQuit == 0) + { + if (!loggedIn) + establishConnection(); + isConnected = 1; + while (isConnected == 1) + { + displayMenu(); + String input = local_in.readLine(); + int selection = Integer.parseInt(input); + String response; + switch (selection) + { + case 0: + System.out.println("Exitting..."); + out.println("EXIT"); + System.exit(0); + break; + case 1: + System.out.println("Login"); + out.println("LOGIN"); + response = in.readLine(); + if (response.equals("OK")) + { + System.out.print("Enter account number: "); + String accountNumber = local_in.readLine(); + System.out.print("Enter PIN: "); + String pinNumber = local_in.readLine(); + out.println(accountNumber); + out.println(pinNumber); + response = in.readLine(); + if (response.equals(accountNumber)) + { + System.out.println("Login Successful! Account: " + response); + loggedIn = true; + } + else + { + System.out.println(response); + } + } + else + { + System.out.println(response); + } + break; + case 2: + System.out.println("Logout"); + out.println("LOGOUT"); + response = in.readLine(); + if (response.equals("OK")) + { + response = in.readLine(); + System.out.println("Logout Successful! Account: " + response); + } + else + { + System.out.println(response); + } + break; + case 3: + System.out.println("Deposit"); + out.println("TELLERDEPOSIT"); + response = in.readLine(); + if (response.equals("OK")) + { + System.out.print("Enter Account Number: "); + String accNum = local_in.readLine(); + out.println(accNum); + response = in.readLine(); + if (!response.equals("OK")) + { + System.out.println(response); + break; + } + System.out.print("Enter Deposit Amount: "); + String depAmount = local_in.readLine(); + out.println(depAmount); + response = in.readLine(); + if (response.equals("OK")) + { + response = in.readLine(); + } + } + System.out.println(response); + break; + case 4: + System.out.println("Withdraw"); + out.println("TELLERWITHDRAW"); + response = in.readLine(); + if (response.equals("OK")) + { + System.out.print("Enter Account Number: "); + String accNum = local_in.readLine(); + out.println(accNum); + response = in.readLine(); + if (!response.equals("OK")) + { + System.out.println(response); + break; + } + System.out.print("Enter Withdrawal Amount: "); + String wdAmount = local_in.readLine(); + out.println(wdAmount); + response = in.readLine(); + if (response.equals("OK")) + { + response = in.readLine(); + } + } + System.out.println(response); + break; + case 5: + System.out.println("Check Balance"); + out.println("TELLERCHECK"); + response = in.readLine(); + if (response.equals("OK")) + { + System.out.print("Enter Account Number: "); + String accNum = local_in.readLine(); + out.println(accNum); + response = in.readLine(); + if (!response.equals("OK")) + { + System.out.println(response); + break; + } + response = in.readLine(); + } + System.out.println(response); + break; + case 6: + System.out.println("Account Open"); + out.println("TELLEROPEN"); + response = in.readLine(); + if (!response.equals("OK")) + { + System.out.println(response); + break; + } + System.out.print("Enter Account Number: "); + out.println(local_in.readLine()); + System.out.print("Enter First Name: "); + out.println(local_in.readLine()); + System.out.print("Enter Middle Name: "); + out.println(local_in.readLine()); + System.out.print("Enter Last Name: "); + out.println(local_in.readLine()); + System.out.print("Enter Account Type: "); + out.println(local_in.readLine()); + System.out.print("Enter Initial Balance: "); + out.println(local_in.readLine()); + System.out.print("Enter PIN: "); + out.println(local_in.readLine()); + response = in.readLine(); + if (response.equals("OK")) + response = in.readLine(); + System.out.println(response); + break; + case 7: + System.out.println("Account Close"); + out.println("TELLERCLOSE"); + response = in.readLine(); + if (!response.equals("OK")) + { + System.out.println(response); + break; + } + System.out.print("Enter Account Number: "); + out.println(local_in.readLine()); + response = in.readLine(); + if (response.equals("OK")) + response = in.readLine(); + System.out.println(response); + break; + case 8: + System.out.println("Modify Account"); + out.println("TELLERMODIFY"); + response = in.readLine(); + if (!response.equals("OK")) + { + System.out.println(response); + break; + } + System.out.print("Enter Account Number: "); + String accNum = local_in.readLine(); + out.println(accNum); + response = in.readLine(); + if (!response.equals("OK")) + { + System.out.println(response); + break; + } + int done = 0; + while (done == 0) + { + System.out.println("1. Change Name"); + System.out.println("2. Change Type"); + System.out.println("3. Change PIN"); + System.out.println("4. Change Balance"); + System.out.println("5. Done\n"); + System.out.print("Enter Choice: "); + int choice = Integer.parseInt(local_in.readLine()); + switch (choice) + { + case 1: + out.println("CHANGENAME"); + System.out.print("Enter New First Name: "); + out.println(local_in.readLine()); + System.out.print("Enter New Middle Name: "); + out.println(local_in.readLine()); + System.out.print("Enter New Last Name: "); + out.println(local_in.readLine()); + response = in.readLine(); + if (!response.equals("OK")) + { + System.out.println(response); + } + break; + case 2: + out.println("CHANGETYPE"); + System.out.print("Enter New Account Type: "); + out.println(local_in.readLine()); + response = in.readLine(); + if (!response.equals("OK")) + { + System.out.println(response); + } + break; + case 3: + out.println("CHANGEPIN"); + System.out.print("Enter New PIN: "); + out.println(local_in.readLine()); + response = in.readLine(); + if (!response.equals("OK")) + { + System.out.println(response); + } + break; + case 4: + out.println("CHANGEBALANCE"); + System.out.print("Enter New Balance: "); + out.println(local_in.readLine()); + response = in.readLine(); + if (!response.equals("OK")) + { + System.out.println(response); + } + break; + case 5: + done = 1; + out.println("DONE"); + break; + default: + System.out.println("Invalid selection"); + break; + } + } + response = in.readLine(); + System.out.println(response); + break; + case 9: + System.out.println("View Account"); + out.println("TELLERVIEW"); + response = in.readLine(); + if (!response.equals("OK")) + { + System.out.println(response); + break; + } + System.out.print("Enter Account Number: "); + String accNumber = local_in.readLine(); + out.println(accNumber); + response = in.readLine(); + if (response.equals("OK")) + response = in.readLine(); + System.out.println(response); + break; + default: + System.out.println("Invalid Selection"); + break; + } + System.out.println("Press Enter to Continue..."); + local_in.readLine(); + } + } + } + + public static void main(String [] args) + throws IOException + { + System.out.println("BankAppClientTeller in Java"); + BankAppClientTeller client = new BankAppClientTeller(); + client.startClient(); + } +} diff --git a/Robust/src/Benchmarks/BankAppJava/BankAppServer.java b/Robust/src/Benchmarks/BankAppJava/BankAppServer.java new file mode 100644 index 00000000..0ebf40b1 --- /dev/null +++ b/Robust/src/Benchmarks/BankAppJava/BankAppServer.java @@ -0,0 +1,697 @@ +// Bank App in Java + +// Author: Danish Lakhani + +// BankAppServer - Bank Application Server that services one client at a time + +import java.io.*; +import java.net.*; +import java.util.*; + +class BankAppServer +{ + static int SUCCESS = 0; + static int ERROR = 1; + + static int LOGIN_ACCOUNT = 1; + static int LOGIN_PIN = 2; + + static int ACCOUNT_SAVINGS = 1; + static int ACCOUNT_CHECKING = 2; + static int ACCOUNT_TELLER = 3; + + private ServerSocket serverSocket = null; + private Socket clientSocket = null; + private PrintWriter out = null; + private BufferedReader in = null; + private static int serverPort = 44444; + private AccountDatabase accounts = null; + + private boolean isLoggedIn = false; + private Integer activeAccount = 0; + private Integer tellerCode = 0; + + public BankAppServer() + { +// initializeServer(); + } + + private void initializeServer() + { + //Initialize Database + accounts = new AccountDatabase(); + + //Initialize Server Socket + System.out.print("Creating Server Socket..."); + try { + serverSocket = new ServerSocket(serverPort); + } catch (IOException e) { + System.out.println("Cannot listen on port " + serverPort); + System.exit(-1); + } + System.out.println("Done"); + } + + private void establishConnection() + throws IOException + { + System.out.print("Waiting for connection..."); + try { + clientSocket = serverSocket.accept(); + } catch (IOException e) { + System.out.println("Accept failed"); + System.exit(-1); + } + out = new PrintWriter(clientSocket.getOutputStream(), true); + in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); + System.out.println("Connection Established!"); + } + + private int authenticateUser(Integer accountNumber, Integer pin) + { + if (!accounts.accountExists(accountNumber)) + return LOGIN_ACCOUNT; + if (accounts.getPin(accountNumber) != pin) + return LOGIN_PIN; + return SUCCESS; + } + + private int login() + throws IOException + { + out.println("OK"); + Integer accountNumber = new Integer(in.readLine()); + System.out.println("Account number: " + accountNumber); + Integer pin = new Integer(in.readLine()); + System.out.println("PIN: " + pin); + System.out.println("Authenticating..."); + int status = authenticateUser(accountNumber, pin); + if (status == SUCCESS) + { + out.println(accountNumber); + isLoggedIn = true; + tellerCode = 0; + activeAccount = 0; + if (accounts.isTeller(accountNumber)) + tellerCode = accountNumber; + else + activeAccount = accountNumber; + System.out.println("Logged Success"); + return SUCCESS; + } + else { + if (status == LOGIN_ACCOUNT) + out.println("ERROR: login failed: Account " + accountNumber + " does not exist."); + else + out.println("ERROR: login failed: Incorrect pin."); + } + System.out.println("Login Failed"); + return ERROR; + } + + private void closeConnection() + throws IOException + { + out.close(); + in.close(); + clientSocket.close(); +// serverSocket.close(); + } + + private void processDeposit(Integer accountNumber) throws IOException + { + String inVal; + if (!accounts.accountExists(accountNumber)) + { + out.println("ERROR: Account " + accountNumber + " not found."); + return; + } + + out.println("OK"); + + //Get Deposit Amount + inVal = in.readLine(); + Double depAmount = new Double(inVal); + if (depAmount <= 0) + { + out.println("ERROR: Negative or zero deposit amount"); + return; + } + + accounts.deposit(accountNumber, depAmount); + out.println("OK"); + out.println("$" + depAmount + " deposited successfully! Account: " + accountNumber + " New Balance: " + accounts.getBalance(accountNumber)); + return; + } + + private void processWithdrawal(Integer accountNumber) throws IOException + { + String inVal; + if (!accounts.accountExists(accountNumber)) + { + out.println("ERROR: Account " + accountNumber + " not found."); + return; + } + + out.println("OK"); + + //Get withdrawal amount + inVal = in.readLine(); + Double wdAmount = new Double(inVal); + if (wdAmount <= 0) + { + out.println("ERROR: Negative or zero withdrawal amount"); + return; + } +// else if (wdAmount > accounts.getBalance(accountNumber)) +// { +// out.println("ERROR: Insufficient funds. Balance = " + accounts.getBalance(accountNumber)); +// return; +// } + + accounts.withdraw(accountNumber, wdAmount); + out.println("OK"); + out.println("$" + wdAmount + " withdrawn successfully! Account: " + accountNumber + " New Balance: " + accounts.getBalance(accountNumber)); + return; + } + + private void processBalanceCheck(Integer accountNumber) + { + out.println("OK"); + out.println("Account: " + accountNumber + " Balance: " + accounts.getBalance(accountNumber)); + return; + } + + private void startServer() + throws IOException + { + int serverQuit = 0; + int status = SUCCESS; + int isConnected = 0; + initializeServer(); + while (serverQuit == 0) + { + establishConnection(); + isConnected = 1; + accounts.loadDatabase(); + while (isConnected == 1) + { + System.out.println("Waiting for request..."); + // Wait for requests + String request = in.readLine(); + if (request == null) + continue; + + System.out.println("Request: " + request); + + // Service requests + if (request.equals("EXIT")) + { + accounts.storeDatabase(); + isLoggedIn = false; + activeAccount = 0; + tellerCode = 0; + closeConnection(); + isConnected = 0; + continue; + } + + if (request.equals("LOGIN")) + { + if (isLoggedIn) + { + out.println("ERROR: Already logged in. Please logout."); + continue; + } + status = login(); + if (status == ERROR) + { +// isConnected = 0; + continue; + } + } + + if (!isLoggedIn) + { + out.println("ERROR: Not logged in"); + continue; + } + + if (request.equals("LOGOUT")) + { + out.println("OK"); + if (tellerCode == 0) + out.println(activeAccount); + else + out.println(tellerCode); + accounts.storeDatabase(); + isLoggedIn = false; + activeAccount = 0; + tellerCode = 0; +// closeConnection(); +// isConnected = 0; + } + + if (request.equals("DEPOSIT")) + { + processDeposit(activeAccount); + } + + if (request.equals("WITHDRAW")) + { + processWithdrawal(activeAccount); + } + + if (request.equals("CHECK")) + { + processBalanceCheck(activeAccount); + } + + if (request.equals("TELLERDEPOSIT")) + { + if (tellerCode == 0) + { + out.println("ERROR: Teller not logged in"); + continue; + } + out.println("OK"); + Integer acc = new Integer(in.readLine()); + processDeposit(acc); + } + + if (request.equals("TELLERWITHDRAW")) + { + if (tellerCode == 0) + { + out.println("ERROR: Teller not logged in"); + continue; + } + out.println("OK"); + Integer acc = new Integer(in.readLine()); + processWithdrawal(acc); + } + + if (request.equals("TELLERCHECK")) + { + if (tellerCode == 0) + { + out.println("ERROR: Teller not logged in"); + continue; + } + out.println("OK"); + Integer acc = new Integer(in.readLine()); + processBalanceCheck(acc); + } + + if (request.equals("TELLEROPEN")) + { + if (tellerCode == 0) + { + out.println("ERROR: Teller not logged in"); + continue; + } + out.println("OK"); + Integer accNum = new Integer(in.readLine()); + String fName = in.readLine(); + String mName = in.readLine(); + String lName = in.readLine(); + Integer accType = new Integer(in.readLine()); + Double bal = new Double(in.readLine()); + Integer pNum = new Integer(in.readLine()); + status = accounts.openAccount(accNum, fName, mName, lName, accType, bal, pNum); + if (status == ERROR) + { + out.println("ERROR: Account " + accNum + " already exists."); + continue; + } + out.println("OK"); + out.println("Account Number: " + accNum + " " + + "Customer Name: " + fName + " " + mName + " " + lName + " " + + "Account Type: " + ((accType == ACCOUNT_SAVINGS)?"SAVINGS":(accType == ACCOUNT_CHECKING)?"CHECKING":"TELLER") + " " + + "Balance: $" + bal + " " + + "PIN: " + pNum + " "); + } + + if (request.equals("TELLERCLOSE")) + { + if (tellerCode == 0) + { + out.println("ERROR: Teller not logged in"); + continue; + } + out.println("OK"); + Integer accNum = new Integer(in.readLine()); + status = accounts.closeAccount(accNum); + if (status == ERROR) + { + out.println("ERROR: Account " + accNum + " does not exist."); + continue; + } + out.println("OK"); + out.println("Account " + accNum + " closed successfully"); + } + + if (request.equals("TELLERMODIFY")) + { + if (tellerCode == 0) + { + out.println("ERROR: Teller not logged in"); + continue; + } + out.println("OK"); + Integer accNum = new Integer(in.readLine()); + if (!accounts.accountExists(accNum)) + { + out.println("ERROR: Account " + accNum + " does not exist."); + continue; + } + out.println("OK"); + String inVal; + while (!(inVal = in.readLine()).equals("DONE")) + { + if (inVal.equals("CHANGENAME")) + { + String fName = in.readLine(); + String mName = in.readLine(); + String lName = in.readLine(); + accounts.modifyName(accNum, fName, mName, lName); + out.println("OK"); + } + else if (inVal.equals("CHANGETYPE")) + { + Integer newType = new Integer(in.readLine()); + if (newType.intValue() < 1 || newType.intValue() > 3) + { + out.println("ERROR: Invalid account type: " + newType + ". Must be 1-3."); + continue; + } + accounts.modifyType(accNum, newType); + out.println("OK"); + } + else if (inVal.equals("CHANGEPIN")) + { + Integer newPin = new Integer(in.readLine()); + if ((newPin < 0) || (newPin > 9999)) + { + out.println("ERROR: Invalid pin " + newPin + ". Must be 0000-9999."); + continue; + } + accounts.modifyPin(accNum, newPin); + out.println("OK"); + } + else if (inVal.equals("CHANGEBALANCE")) + { + Double newBal = new Double(in.readLine()); + accounts.modifyBalance(accNum, newBal); + out.println("OK"); + } + } + out.println("Account Number: " + accNum + " " + + "Customer Name: " + accounts.nameString(accNum) + " " + + "Account Type: " + accounts.typeString(accNum) + " " + + "Balance: $" + accounts.getBalance(accNum) + " " + + "PIN: " + accounts.getPin(accNum) + " "); + } + + if (request.equals("TELLERVIEW")) + { + if (tellerCode == 0) + { + out.println("ERROR: Teller not logged in"); + continue; + } + out.println("OK"); + Integer accNum = new Integer(in.readLine()); + if (!accounts.accountExists(accNum)) + { + out.println("ERROR: Account " + accNum + " does not exist."); + continue; + } + out.println("OK"); + out.println("Account Number: " + accNum + " " + + "Customer Name: " + accounts.nameString(accNum) + " " + + "Account Type: " + accounts.typeString(accNum) + " " + + "Balance: $" + accounts.getBalance(accNum) + " " + + "PIN: " + accounts.getPin(accNum) + " "); + } + } + } + } + + public static void main(String [] args) + throws IOException + { + System.out.println("BankAppServer in Java"); + BankAppServer server = new BankAppServer(); + server.startServer(); + } +} + +class AccountEntry +{ + Integer accountNumber; + String firstName; + String middleName; + String lastName; + Integer accountType; + Double balance; + Integer pin; + + public AccountEntry(Integer accNum, String fName, String mName, String lName, Integer accType, Double bal, Integer pNum) + { + accountNumber = accNum; + firstName = fName; + middleName = mName; + lastName = lName; + accountType = accType; + balance = bal; + pin = pNum; + } +} + + +class AccountDatabase +{ + static int ACCOUNT_SAVINGS = 1; + static int ACCOUNT_CHECKING = 2; + static int ACCOUNT_TELLER = 3; + static int SUCCESS = 0; + static int ERROR = 1; + + static String dbfilename = "accts.txt"; + + Vector entries = null; + + public AccountDatabase() + { + entries = new Vector(); + } + + public void loadDatabase() + { + entries.removeAllElements(); + try { + BufferedReader fin = new BufferedReader(new FileReader(dbfilename)); + String str; + while ((str = fin.readLine()) != null) + { + Integer accNum = new Integer(str); + String fName = fin.readLine(); + String mName = fin.readLine(); + String lName = fin.readLine(); + Integer accType = new Integer(fin.readLine()); + Double bal = new Double(fin.readLine()); + Integer pNum = new Integer(fin.readLine()); + AccountEntry newEntry = new AccountEntry(accNum, fName, mName, lName, accType, bal, pNum); + entries.add(newEntry); + } + fin.close(); + } catch (IOException e) { + System.out.println("Cannot open database file"); + System.exit(-1); + } + printAccounts(); + } + + public void storeDatabase() + { + try { + BufferedWriter fout = new BufferedWriter(new FileWriter(dbfilename)); + for (int i = 0; i < entries.size(); i++) + { + AccountEntry acc = (AccountEntry)entries.elementAt(i); + fout.write(acc.accountNumber.toString()); + fout.newLine(); + fout.write(acc.firstName); + fout.newLine(); + fout.write(acc.middleName); + fout.newLine(); + fout.write(acc.lastName); + fout.newLine(); + fout.write(acc.accountType.toString()); + fout.newLine(); + fout.write(acc.balance.toString()); + fout.newLine(); + fout.write(acc.pin.toString()); + fout.newLine(); + } + fout.close(); + } catch (IOException e) { + System.out.println("Cannot write to database file"); + System.exit(-1); + } + } + + public AccountEntry getAccount(Integer accNum) + { + for (int i = 0; i < entries.size(); i++) + { + AccountEntry acc = (AccountEntry)entries.elementAt(i); + if (acc.accountNumber.equals(accNum)) + return acc; + } + return null; + } + + public void deposit(Integer accNum, Double amount) + { + AccountEntry acc = getAccount(accNum); + acc.balance += amount; + } + + public void withdraw(Integer accNum, Double amount) + { + AccountEntry acc = getAccount(accNum); + acc.balance -= amount; + } + + public Double getBalance(Integer accNum) + { + AccountEntry acc = getAccount(accNum); + return acc.balance; + } + + public int getPin(Integer accNum) + { + AccountEntry acc = getAccount(accNum); + if (acc != null) + return acc.pin.intValue(); + return -1; + } + + public boolean accountExists(Integer accNum) + { + AccountEntry acc = getAccount(accNum); + if (acc != null) + return true; + return false; + } + + public boolean isTeller(Integer accNum) + { + AccountEntry acc = getAccount(accNum); + if (acc.accountType.equals(ACCOUNT_TELLER)) + return true; + return false; + } + + public Integer openAccount(Integer accNum, String fName, String mName, String lName, Integer accType, Double bal, Integer pNum) + { + if (accountExists(accNum)) + return ERROR; + AccountEntry acc = new AccountEntry(accNum, fName, mName, lName, accType, bal, pNum); + entries.add(acc); + return SUCCESS; + } + + public Integer closeAccount(Integer accNum) + { + if (accountExists(accNum)) + { + AccountEntry acc = getAccount(accNum); + entries.remove(acc); + return SUCCESS; + } + else + return ERROR; + } + + public String nameString(Integer accNum) + { + AccountEntry acc = getAccount(accNum); + if (acc != null) + { + return (acc.firstName + " " + acc.middleName + " " + acc.lastName); + } + return ""; + } + + public String typeString(Integer accNum) + { + AccountEntry acc = getAccount(accNum); + if (acc != null) + { + return ((acc.accountType == ACCOUNT_SAVINGS)?"SAVINGS":(acc.accountType == ACCOUNT_CHECKING)?"CHECKING":"TELLER"); + } + return ""; + } + + public void modifyName(Integer accNum, String fName, String mName, String lName) + { + AccountEntry acc = getAccount(accNum); + if (acc != null) + { + acc.firstName = fName; + acc.middleName = mName; + acc.lastName = lName; + } + return; + } + + public void modifyType(Integer accNum, Integer newType) + { + AccountEntry acc = getAccount(accNum); + if (acc != null) + { + acc.accountType = newType; + } + return; + } + + public void modifyPin(Integer accNum, Integer newPin) + { + AccountEntry acc = getAccount(accNum); + if (acc != null) + { + acc.pin = newPin; + } + return; + } + + public void modifyBalance(Integer accNum, Double newBal) + { + AccountEntry acc = getAccount(accNum); + if (acc != null) + { + acc.balance = newBal; + } + return; + } + + public void printAccounts() + { + System.out.println("entries.size = " + entries.size()); + for (int i = 0; i < entries.size(); i++) + { + System.out.println("Entry " + i); + AccountEntry acc = entries.elementAt(i); + System.out.println("1 " + acc.accountNumber.toString()); + System.out.println("2 " + acc.firstName); + System.out.println("3 " + acc.middleName); + System.out.println("4 " + acc.lastName); + System.out.println("5 " + acc.accountType.toString()); + System.out.println("6 " + acc.balance.toString()); + System.out.println("7 " + acc.pin.toString()); + } + } +} diff --git a/Robust/src/Benchmarks/BankAppJava/BankAppTestClient.java b/Robust/src/Benchmarks/BankAppJava/BankAppTestClient.java new file mode 100644 index 00000000..a2cc7f1b --- /dev/null +++ b/Robust/src/Benchmarks/BankAppJava/BankAppTestClient.java @@ -0,0 +1,48 @@ +// Bank App in Java + +// Author: Danish Lakhani + +import java.io.*; +import java.net.*; + +class BankAppTestClient +{ + public static void main(String [] args) + throws IOException + { + BufferedReader local_in = new BufferedReader(new InputStreamReader(System.in)); + String sendline; + + System.out.println("Client"); + + sendline = local_in.readLine(); +if (sendline == null) + sendline = "localhost"; + System.out.println("Connecting to server..."); + Socket mySocket = new Socket(sendline, 8000); + + System.out.println("Connected!!"); + + PrintWriter out = new PrintWriter(mySocket.getOutputStream(), true); + BufferedReader in = new BufferedReader(new InputStreamReader(mySocket.getInputStream())); + + + + while (true) + { + System.out.print("Send: "); + sendline = local_in.readLine(); + + if (!sendline.equals("no")) + { + out.println(sendline); + } + else + { + System.out.print("Reading: "); + String inString = in.readLine(); + System.out.println(inString); + } + } + } +} diff --git a/Robust/src/Benchmarks/BankAppJava/accts.txt b/Robust/src/Benchmarks/BankAppJava/accts.txt new file mode 100644 index 00000000..b75b5be0 --- /dev/null +++ b/Robust/src/Benchmarks/BankAppJava/accts.txt @@ -0,0 +1,14 @@ +1111 +Danish +Salim +Lakhani +1 +2000.0 +1234 +1000 +Danish +Teller +Lakhani +3 +0.0 +2000 diff --git a/Robust/src/Benchmarks/TTT/Board.java b/Robust/src/Benchmarks/TTT/Board.java new file mode 100644 index 00000000..8f4a3d88 --- /dev/null +++ b/Robust/src/Benchmarks/TTT/Board.java @@ -0,0 +1,93 @@ +public class Board { + // TicTacToe Board flags + flag init; + + int[][] board; + + int winningplayer; + + public Board() { + winningplayer = -1; + board = new int[3][3]; + for (int i = 0; i < 3; i++) + for (int j = 0; j < 3; j++) + board[i][j] = 0; + } + + public int makeMove(int row, int col) { + if (boardFull() == 1) { + winningplayer = 0; + return 2; + } + if (board[row][col] != 0) { // Space taken + return -1; + } + else { + board[row][col] = 1; + if (checkForWin(1) == 1) { // Check if player won + winningplayer = 1; + return 2; + } + // Computer makes move + if (computerMakeMove() == 1) { // If made move successful + if (checkForWin(2) == 1) { // Check if computer won + winningplayer = 2; + return 2; + } + } + else { // Board full, no winner + winningplayer = 0; + return 2; + } + } + return 1; + } + + public int boardFull() { + for (int i = 0; i < 3; i++) + for (int j = 0; j < 3; j++) + if (board[i][j] == 0) + return 0; + return 1; + } + + public int computerMakeMove() { + for (int i = 0; i < 3; i++) + for (int j = 0; j < 3; j++) + if (board[i][j] == 0) { + board[i][j] = 2; + return 1; + } + return 0; + } + + public int checkForWin(int p) { + // Add logic for checking if player p wins + // Horiz + + if ((board[0][0] == p) && (board[0][1] == p) && (board[0][2] == p) || + (board[1][0] == p) && (board[1][1] == p) && (board[1][2] == p) || + (board[2][0] == p) && (board[2][1] == p) && (board[2][2] == p)) { + return 1; + } + + // Vert + if ((board[0][0] == p) && (board[1][0] == p) && (board[2][0] == p) || + (board[0][1] == p) && (board[1][1] == p) && (board[2][1] == p) || + (board[0][2] == p) && (board[1][2] == p) && (board[2][2] == p)) { + return 1; + } + + //Diag + if ((board[0][0] == p) && (board[1][1] == p) && (board[2][2] == p) || + (board[0][2] == p) && (board[1][1] == p) && (board[2][0] == p)) { + return 1; + } + + return 0; + } + + public int winner() { + return winningplayer; + } +} diff --git a/Robust/src/Benchmarks/TTT/TTTServer.java b/Robust/src/Benchmarks/TTT/TTTServer.java new file mode 100644 index 00000000..30bb7064 --- /dev/null +++ b/Robust/src/Benchmarks/TTT/TTTServer.java @@ -0,0 +1,58 @@ +/* Startup object is generated with the initialstate flag set by the + * system to start the computation up */ + +// Create ServerSocket +task Startup(StartupObject s {initialstate}) { + System.printString("TTT Server Starting...\n"); + ServerSocket ss = new ServerSocket(8000); + System.printString("Creating ServerSocket\n"); + Board tttBoard = new Board() {init}; + taskexit(s {!initialstate}); // Turn off initial state flag +} + +//Listen for a request and accept request +task AcceptConnection(ServerSocket ss{SocketPending}) { + System.printString("Waiting for connection...\n"); + TTTServerSocket ttts = new TTTServerSocket() {TTTSInitialize}; + System.printString("Calling accept...\n"); + ss.accept(ttts); + System.printString("Connected...\n"); +} + +// Process incoming requests +task ProcessRequest(TTTServerSocket ttts{IOPending && TTTSInitialize}) { + System.printString("Request received..."); + int action = ttts.receive(); + if (action == 1) { // Make move + taskexit(ttts {MakeMove}); + } + else { // Send Error + taskexit(ttts {SendError}); + } +} + +task ProcessMove(TTTServerSocket ttts{MakeMove}, Board tttBoard{init}) { + System.printString("Processing player's move..."); + int result = tttBoard.makeMove(ttts.getRow(), ttts.getCol()); + if (result == 1) { //Move made, send board display + taskexit(ttts {!MakeMove, SendBoard}); + } + else if (result == 2) { //Move made, game over + taskexit(ttts {!MakeMove, SendDone}); + } + else {// Error + taskexit(ttts {!MakeMove, SendError}); + } +} + +task SendBoardDisplay(TTTServerSocket ttts{SendBoard}, Board tttBoard{init}) { + ttts.sendBoardDisplay(tttBoard); +} + +task GameOver(TTTServerSocket ttts{SendDone}, Board tttBoard{init}) { + ttts.sendDone(tttBoard.winner()); +} + +task SendErrorMessage(TTTServerSocket ttts{SendError}, Board tttBoard{init}) { + ttts.sendError(); +} diff --git a/Robust/src/Benchmarks/TTT/TTTServerSocket.java b/Robust/src/Benchmarks/TTT/TTTServerSocket.java new file mode 100644 index 00000000..577a248a --- /dev/null +++ b/Robust/src/Benchmarks/TTT/TTTServerSocket.java @@ -0,0 +1,101 @@ +public class TTTServerSocket extends Socket { + // TTTServerSocket flags + flag TTTSInitialize; + + flag MakeMove; + flag SendError; + flag SendBoard; + flag SendDone; + + String request; + int row, col; + + //Constructor + public TTTServerSocket(){ + System.printString("Constructing TTTServerSocket....\n"); + } + + public int receive() + { + byte b1[] = new byte[1024]; + read(b1); + request = new String(b1); + System.printString("request: "); + System.printString(request); + if (parseTransaction() == 1) { + return 1; + } + return 0; + } + + // Parse request + public int parseTransaction(){ + int start = request.indexOf('_'); + String s = request.subString(start+1); +//_move:3:3 + if (s.startsWith("move")==true){ + //Get row + int i1 = s.indexOf(':'); + String rowStr = new String(s.subString(i1+1, i1+2)); + row = Integer.parseInt(rowStr); + + //Get col + String s2 = new String(s.subString(i1+2)); + int i2 = s2.indexOf(':'); + String colStr = new String(s.subString(i2+1, i2+2)); + col = Integer.parseInt(colStr); + return 1; + + } + // Error transaction + return -1; + } + + public int getRow(){ + return row; + } + public int getCol(){ + return col; + } + + public void sendBoardDisplay(Board theBoard) { + StringBuffer line1 = new String ("display_"); + + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + if (theBoard.board[i][j] == 1) + line1.append("X"); + else if (theBoard.board[i][j] == 2) + line1.append("O"); + else + line1.append("-"); + } + line1.append("_"); + } + String towrite = new String(line1); + write(towrite.getBytes()); + return; + } + + public void sendDone(int winner) { + StringBuffer line1 = new String ("done_"); + if (winner == 0) + line1.append("tie"); + else if (winner == 1) + line1.append("player"); + else + line1.append("computer"); + + String towrite = new String(line1); + write(towrite.getBytes()); + return; + } + + public void sendError() { + StringBuffer line1 = new String ("error_wrongmove"); + + String towrite = new String(line1); + write(towrite.getBytes()); + return; + } +} diff --git a/Robust/src/Benchmarks/TTTJava/TTTServer.java b/Robust/src/Benchmarks/TTTJava/TTTServer.java new file mode 100644 index 00000000..8806080e --- /dev/null +++ b/Robust/src/Benchmarks/TTTJava/TTTServer.java @@ -0,0 +1,293 @@ +import java.net.*; +import java.io.*; + +public class TTTServer +{ + //the tictactoe game board + //2d 3x3 char array + private static char[][] board; + //keeps track of how many turns have past + private static int numOfTurns; + //ints used to store the location of the cell the user will input + private static int row; + private static int col; + private static boolean notDone; + + private static void resetGame() + { + numOfTurns = 0; + row = 0; + col = 0; + + for(int i = 0; i < 3; i++) + { + for(int j = 0; j < 3; j++) + { + board[i][j] = ' '; + } + } + } + + private static void displayBoard() + { + System.out.println("--------------------"); + System.out.println("[R,C][ 1 ][ 2 ][ 3 ]"); + System.out.println("--------------------"); + System.out.println("[ 1 ]| " + board[0][0] + " | " + board[0][1] + + " | " + board[0][2] + " | "); + System.out.println("--------------------"); + System.out.println("[ 2 ]| " + board[1][0] + " | " + board[1][1] + + " | " + board[1][2] + " | "); + System.out.println("--------------------"); + System.out.println("[ 3 ]| " + board[2][0] + " | " + board[2][1] + + " | " + board[2][2] + " | "); + System.out.println("--------------------"); + } + + //put the move on the board and update numOfTurns + private static void markMove(char xo) + { + board[row - 1][col - 1] = xo; + numOfTurns++; + } + + //check for a winner or a tie + //true == winner or tie + private static boolean checkWinner(char xo) + { + //horizontal win + if(board[0][0] == xo && board[0][0] == board[0][1] && + board[0][1] == board[0][2]) + { + System.out.println(xo + " is the winner!"); + return true; + } + //horizontal win + else if(board[1][0] == xo && board[1][0] == board[1][1] && + board[1][1] == board[1][2]) + { + System.out.println(xo + " is the winner!"); + return true; + } + //horizontal win + else if(board[2][0] == xo && board[2][0] == board[2][1] && + board[2][1] == board[2][2]) + { + System.out.println(xo + " is the winner!"); + return true; + } + //vertial win + else if(board[0][0] == xo && board[0][0] == board[1][0] && + board[1][0] == board[2][0]) + { + System.out.println(xo + " is the winner!"); + return true; + } + //vertial win + else if(board[0][1] == xo && board[0][1] == board[1][1] && + board[1][1] == board[2][1]) + { + System.out.println(xo + " is the winner!"); + return true; + } + //vertial win + else if(board[0][2] == xo && board[0][2] == board[1][2] && + board[1][2] == board[2][2]) + { + System.out.println(xo + " is the winner!"); + return true; + } + //diagonal win + else if(board[0][0] == xo && board[0][0] == board[1][1] && + board[1][1] == board[2][2]) + { + System.out.println(xo + " is the winner!"); + return true; + } + //diagonal win + else if(board[0][2] == xo && board[0][2] == board[1][1] && + board[1][1] == board[2][0]) + { + System.out.println(xo + " is the winner!"); + return true; + } + //tie game + //board is full + else if(numOfTurns == 9) + { + System.out.println("Tie Game!"); + return true; + } + //no winner yet + else + return false; + } + + //the logic that happens for each turn for X or O + public static void turnLogic(char xo, int r, int c) + { + if(xo == 'X') + { + System.out.println("\n" + xo + "'s turn."); + System.out.println("Please enter your move as two separate integers: "); + //-1 -1 to quit + row = readInt(); + col = readInt(); + System.out.println("\nYou entered (" + row + "," + col + ")\n"); + } + else if(xo == 'O') + { + System.out.println("\n" + xo + "'s turn."); + row = r; + col = c; + } + + markMove(xo); + displayBoard(); + + //check for a winner and quit cond. + if(checkWinner(xo) || (row == -1 && col == -1)) + notDone = false; + } + + public static void main(String[] args) + { + //the sockets to be used + ServerSocket mySocket = null; + Socket myConnection= null; + //string that will hold the message to be received and sent + String myString = null; + //input buffer + BufferedReader myInput = null; + //output buffer + PrintStream myOutput = null; + //loop variable + notDone = true; + + board = new char[3][3]; + resetGame(); + + //start server + try + { + //create new socket + mySocket = new ServerSocket(8080); + System.out.println("Server Port: " + mySocket.getLocalPort()); + + displayBoard(); + + //accept incoming tcp connection + myConnection = mySocket.accept(); + + //create and read the message from the client to the input buffer + myInput = new BufferedReader(new InputStreamReader(myConnection.getInputStream())); + + //create and print the message to the output buffer + myOutput = new PrintStream(myConnection.getOutputStream()); + + //loop for nine times at most + while(numOfTurns != 9 && notDone == true) + { + switch(numOfTurns % 2) + { + //even case + case 0: + turnLogic('X', 0, 0); + myOutput.println(row + " " + col); + break; + + //odd case + case 1: + myString = myInput.readLine(); + + //was "quit" received? + if(myString.equals("quit")) + { + notDone = false; + } + else + { + int r = Integer.parseInt(myString.substring(0, 1)); + int c = Integer.parseInt(myString.substring(2, 3)); + turnLogic('O', r, c); + } + break; + + //should not happen + default: + System.out.println("Program Error!"); + break; + } + } + + //close buffers + myInput.close(); + myOutput.close(); + + //close socket + myConnection.close(); + } + catch(IOException e) + { + System.err.println(e); + System.exit(1); + } + } + + //some good stuff to read user ints, maybe too much? + public static int readInt() throws NumberFormatException + { + String inputString = null; + inputString = readWord(); + + return Integer.parseInt(inputString); + } + + //read a lot + public static String readWord() + { + String result = ""; + char next; + + next = readChar(); + while (Character.isWhitespace(next)) + next = readChar(); + + while(!(Character.isWhitespace(next))) + { + result = result + next; + next = readChar(); + } + + if (next == '\r') + { + next = readChar(); + + if (next != '\n') + { + System.err.println("Error."); + System.exit(1); + } + } + + return result; + } + + //read one + public static char readChar() + { + int charAsInt = -1; + + try + { + charAsInt = System.in.read(); + } + catch(IOException e) + { + System.err.println(e); + System.exit(1); + } + + return (char)charAsInt; + } +}