package mug13; /* * This is the thread used by MainServerThread.java * * @author Brett McMillian * @author Louisa Poythress * */ import java.net.*; import java.io.*; import java.util.*; public class ServerThread extends Thread { //*************** // Fields //*************** private Socket clientSocket = null; //When clientLogin is set to true, MainServerThread //adds the user to the hashtable of clients boolean clientLogin = false; //Input and output streams for sending and receiving //from a client ObjectInputStream oisFromClient; ObjectOutputStream oosFromServer; //mainST is needed for accessing the Hashtable in //MainServerThread MainServerThread mainST; //*************** // Constructors //*************** public ServerThread(Socket clientSocket, MainServerThread mst) { this.clientSocket = clientSocket; this.mainST = mst; } //*************** // Methods //*************** public void run() { try{ //Open an input stream. oisFromClient = new ObjectInputStream(clientSocket.getInputStream()); //Open an output stream. oosFromServer = new ObjectOutputStream(clientSocket.getOutputStream()); //Create a STUB world object World test = new StubWorld(); while (true) { //Receive information from a client. String string1 = (String) oisFromClient.readObject(); String string2 = (String) oisFromClient.readObject(); String string3 = (String) oisFromClient.readObject(); //If string3 equals "login", search for string1 and string2 //in the login file. if (string3.equals("login")) { FileReader textFR = new FileReader("login.txt"); BufferedReader textFLR = new BufferedReader(textFR); while (true) { textFLR.skip(6); String username = (String) textFLR.readLine(); textFLR.skip(10); String password = (String) textFLR.readLine(); try { if ((username.equals(string1)) && (password.equals(string2))) { oosFromServer.writeObject("Login Succeeded.."); //Set the name of the thread to the username setName(username); //Tell MainServerThread to add the client //to the hashtable synchronized (this) { clientLogin = true; notifyAll(); } break; } } //If the user does not exist or the password is incorrect, //return "Login Failed" catch (NullPointerException e) { oosFromServer.writeObject("Login Failed"); break; } } //Close the readers textFR.close(); textFLR.close(); } //If string3 equals "update", then update the string1 user with string2 password else if (string3.equals("update")) { //Copy the login file into an array Object[] oldLoginFile = (new LoginFileCopier()).copyFile("login.txt"); //Rewrite the login file with string1 user's new string2 password (new LoginFileWriter()).writeNewFile("login.txt", oldLoginFile, string1, string2); //Send information back to a client. oosFromServer.writeObject("Password updated."); } //If string1 equals broadcast, run the broadcast thread //and break the while loop else if (string1.equals("broadcast")) { //Receive the broadcast from the client String message = (String) oisFromClient.readObject(); String[] listOfPeople = (String[]) oisFromClient.readObject(); //newOOS will be used for sending messages to the clients //in listOfPeople ObjectOutputStream newOOS = null; //For each client in listOfPeople, send them message for (int i = 0; i < listOfPeople.length; i++) { newOOS = (ObjectOutputStream) (this.mainST.clients.get(listOfPeople[i])); newOOS.writeObject(message); } } //If string1 equals "exit", break the while loop else if (string1.equals("exit")) { this.mainST.clients.remove(this.getName()); break; } //If string3 equals "none", call the perform method with only //string1 and string2 as parameters else if (string3.equals("none")) oosFromServer.writeObject(test.perform(new Action(string1, string2))); //Otherwise call the perform method using string1, string2, //and string3 else oosFromServer.writeObject(test.perform(new Action(string1, string2, string3))); } //Close the input and output streams. oisFromClient.close(); oosFromServer.close(); } catch (IOException e) { System.out.println("IO Error"); return; } catch (ClassNotFoundException e) { return; } } }