Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //...Irrelevant bits ommitted...//
- public class Server
- {
- // The server object reference
- static Server server;
- // Declarations:
- private ArrayList<ObjectOutputStream> clientOutputStreams; // out streams
- private ArrayList<String> takenNames = new ArrayList<>(); // taken names
- private InetAddress ip;
- private final int serverPort; // the port the server is running on
- private static ObjectOutputStream changer; // the last person to change names
- private ArrayList<ObjectOutputStream> shitList = new ArrayList<>();
- private HashMap <InetAddress, ObjectOutputStream> ipMap =
- new HashMap<>(); // <ip, outputstream>
- //...Irrelevant bits ommited...//
- public void tellEveryone(Message message, InetAddress senderIP)
- {
- //...//
- // If someone is on my shitlist,
- if(shitList.contains(ipMap.get(senderIP)))
- {
- // Warn them of their sins...
- Message nopeMessage = new Message(Message.TYPE.SERVER,
- "You may not send any messages until you change your name!",
- "Server");
- try
- {
- ipMap.get(senderIP).writeObject(nopeMessage);
- }
- catch(IOException e)
- {
- e.printStackTrace();
- }
- }
- public class ServerHandler implements Runnable
- {
- @Override
- public void run()
- {
- // Create a list of client out streams to send stuff...
- clientOutputStreams = new ArrayList<>();
- try // To establish a connection with clients
- {
- // Create server socket...
- ServerSocket serverSocket = new ServerSocket(serverPort);
- while(true) // Will always run! Blocks!
- {
- // Assign a client socket to any new socket connections...
- // (The var used here is temp, but will be passed off soon.)
- Socket clientSocket = serverSocket.accept();
- // Get's the ip of the client that connected...
- ip = clientSocket.getInetAddress();
- System.out.println(ip + " " + "connected.");
- // Create ooStream to send messages to client...
- ObjectOutputStream ooStream =
- new ObjectOutputStream(
- clientSocket.getOutputStream());
- // Add the client oo stream to the list of outputs...
- clientOutputStreams.add(ooStream);
- // Add user IP data to map of ip's
- ipMap.putIfAbsent(ip, ooStream);
- // Create new thread to run inner class ClientHandler...
- Thread t = new Thread(new ClientHandler(clientSocket));
- // Running the thread makes it safe to overwrite the...
- // ...clientsocket variable.
- t.start();
- }
- }
- catch (IOException e)
- {
- System.out.println("Exception in server.run()");
- // TODO: Revise
- e.printStackTrace();
- }
- }
- }
- public class ClientHandler implements Runnable
- {
- private ObjectInputStream oInStream; // The client's input stream.
- private Socket socket; // Socket to the client
- public ClientHandler(Socket clientSocket)
- {
- try // to create an input stream...
- {
- socket = clientSocket; // <-- The one passed in to the method
- // Potential error from previous version... REMOVE WHEN TESTED
- oInStream = new ObjectInputStream(socket.getInputStream());
- }
- catch(IOException e)
- {
- System.out.println("Error establishing input stream");
- }
- }
- @Override
- public void run()
- {
- Message message;
- try // To process incoming messages...
- {
- while(socket.isClosed() == false) // If the socket is open...
- {
- // While there are more messages...
- // Also assigns to the message var.
- while((message = (Message)oInStream.readObject()) != null)
- {
- // Passes on the message and sender info.
- if(message.getType() == Message.TYPE.NAME_REQUEST)
- {
- changer = ipMap.get(socket.getInetAddress());
- System.out.println(socket.getInetAddress());
- System.out.println(changer.toString());
- handleNameRequests(message);
- }
- else
- {
- tellEveryone(message, ip); // TEST CHANGE- DELETED IF TEST
- }
- }
- // TEST TEST TEST
- synchronized(clientOutputStreams)
- {
- int index =
- clientOutputStreams.indexOf(
- socket.getOutputStream());
- clientOutputStreams.remove(index);
- System.out.println("Removed the client in sync");
- }
- }
- // TEST TEST TEST
- socket.close(); // TEST CLOSING SOCKET WHEN DONE.
- System.out.println("Sock closed after while loop in ch run()");
- }
- catch(IOException e)
- {
- System.out.println("IOException caught when "
- + "reading message.");
- }
- catch (ClassNotFoundException e)
- {
- System.out.println("Some poor sap is going to have to debug"
- + "this!");
- }
- finally
- {
- // THIS WHOLE BLOCK: TEST TEST TEST
- try
- {
- oInStream.close();
- System.out.println("just closed oinStream");
- }
- catch(IOException e)
- {
- e.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement