Kouksi44

SecureServer.java

Nov 25th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.91 KB | None | 0 0
  1.  
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.DataOutputStream;
  5. import java.io.DataOutputStream;
  6. import java.io.InputStreamReader;
  7. import java.io.ObjectInputStream;
  8. import java.io.ObjectOutputStream;
  9. import java.io.Serializable;
  10. import java.net.InetAddress;
  11. import java.net.ServerSocket;
  12. import java.net.Socket;
  13. import java.util.ArrayList;
  14. import java.io.IOException;
  15.  
  16.   enum MessageType {
  17.     MESSAGE, DISCONNECT, GETCLIENTS
  18. };
  19.  
  20. public class SecureServer {
  21.     ServerSocket sSocket;
  22.     ArrayList<SecureClientHandler> clientList = new ArrayList<SecureClientHandler>();
  23.    
  24.     /**
  25.      * Starts the server. Should only be called once.
  26.      * @param args Currently unused.
  27.      */
  28.     public static void main ( String[] args ) {
  29.         SecureServer server = new SecureServer(7007);
  30.         server.start();
  31.        
  32.     }
  33.    
  34.     /**
  35.      * Constructor for the main server class.
  36.      * @param  port The port on which the ServerSocket will be listening
  37.      */
  38.     public SecureServer(int port) {
  39.         try {
  40.             sSocket = new ServerSocket(port);
  41.         } catch (IOException e) {
  42.             e.printStackTrace();
  43.         }
  44.        
  45.     }
  46.    
  47.     /**
  48.      * This runs the server loop. Not sure if this class should also extend Thread or implement Runnable to change this into a run() method
  49.      */
  50.     public void start() {
  51.         while (true) {
  52.             Socket client = null;
  53.             try {
  54.                 client = sSocket.accept();
  55.             } catch ( IOException e) {
  56.                 e.printStackTrace();
  57.             }
  58.            
  59.             SecureClientHandler secureClient = new SecureClientHandler(client,this);
  60.             clientList.add(secureClient);
  61.             try {
  62.                 secureClient.run();
  63.             } catch(Exception e) {
  64.                 System.out.println(e.getStackTrace());
  65.             }
  66.         }
  67.     }
  68.    
  69.     /**
  70.      * Returns a list of client descriptors that include information about the nickname (will later be stored in a database) or
  71.      * the InetAddress of the client
  72.      * @return List of clients
  73.      */
  74.     public ArrayList<InetAddress> getClients() {
  75.         ArrayList<InetAddress> clients = new ArrayList<InetAddress>();
  76.         for (SecureClientHandler s : clientList) {
  77.             clients.add(s.getSocket().getInetAddress());
  78.         }
  79.         return clients;
  80.     }
  81.    
  82.     /**
  83.      * Resolves an InetAddress to the SecureClientHandler that holds the socket connection to that address
  84.      * @param  address The InetAddress to resolve
  85.      * @return         A SecureClientHandler instance that fits with the address
  86.      */
  87.     public SecureClientHandler resolveClientHandler(InetAddress address) throws ClientNotFoundException {
  88.         for (SecureClientHandler h : clientList) {
  89.             if ( address.equals(h.getSocket().getInetAddress() ) ) {
  90.                 return h;
  91.             }
  92.         }
  93.         throw new ClientNotFoundException("Address could not be resolved to a currently available client");
  94.     }
  95.    
  96.     public void disconnect(SecureClientHandler client, MessageType reason) {
  97.         try {
  98.             client.sendMessage(new SecureMessage("",MessageType.DISCONNECT,InetAddress.getByName("127.0.0.1"),InetAddress.getLocalHost()));
  99.         } catch(Exception e) {
  100.             e.printStackTrace();
  101.         }
  102.     }
  103.    
  104.    
  105. }
Advertisement
Add Comment
Please, Sign In to add comment