Advertisement
dig090

ChatServer for SO question

May 20th, 2012
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.10 KB | None | 0 0
  1. package com.j256.ormlite;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.io.PrintWriter;
  7. import java.net.ServerSocket;
  8. import java.net.Socket;
  9. import java.util.Hashtable;
  10.  
  11. public class ChatServer {
  12.     private static int port = 8001;
  13.  
  14.     public static void main(String[] args) {
  15.  
  16.         ServerSocket server = null;
  17.         try {
  18.             server = new ServerSocket(port);
  19.         } catch (IOException e) {
  20.             System.err.println("Could not listen on port: " + port);
  21.             System.err.println(e);
  22.             System.exit(1);
  23.         }
  24.  
  25.         Socket client = null;
  26.         while (true) {
  27.             try {
  28.                 client = server.accept();
  29.             } catch (IOException e) {
  30.                 System.err.println("Accept failed.");
  31.                 System.err.println(e);
  32.                 System.exit(1);
  33.             }
  34.             /* start a new thread to handle this client */
  35.             Thread t = new Thread(new ClientConn(client));
  36.             t.start();
  37.         }
  38.     }
  39.  
  40.     private static class ChatServerProtocol {
  41.         private String nick;
  42.         private ClientConn conn;
  43.  
  44.         private static Hashtable<String, ClientConn> nicks = new Hashtable<String, ClientConn>();
  45.  
  46.         private static final String msg_OK = "OK";
  47.         private static final String msg_INVALID = "INVALID COMMAND";
  48.         private static final String msg_SEND_FAILED = "FAILED TO SEND";
  49.  
  50.         private boolean add_nick(String nick, ClientConn c) {
  51.             if (nicks.containsKey(nick)) {
  52.                 return false;
  53.             } else {
  54.                 nicks.put(nick, c);
  55.                 return true;
  56.             }
  57.         }
  58.  
  59.         private boolean remove_nick() {
  60.             if (nick == null || !(nicks.containsKey(nick))) {
  61.                 return false;
  62.             } else {
  63.                 nicks.remove(nick);
  64.                 return true;
  65.             }
  66.         }
  67.  
  68.         public ChatServerProtocol(ClientConn c) {
  69.             nick = null;
  70.             conn = c;
  71.         }
  72.  
  73.         private boolean sendMsg(String recipient, String msg) {
  74.             if (nicks.containsKey(recipient)) {
  75.                 ClientConn c = nicks.get(recipient);
  76.                 c.sendMsg(nick + ": " + msg);
  77.                 return true;
  78.             } else {
  79.                 return false;
  80.             }
  81.         }
  82.  
  83.         public String process(String msg) {
  84.  
  85.             String output = "";
  86.             if (msg.startsWith("Autore: ")) {
  87.                 String tryauthor = msg.substring(8);
  88.                 if (add_nick(tryauthor, this.conn)) {
  89.                     // log("Benvenuto autore " + tryauthor);
  90.                     this.nick = tryauthor;
  91.                     output = "Benvenuto autore " + tryauthor;
  92.                 } else {
  93.                     output = "Errore: autore gia' collegato";
  94.                 }
  95.             }
  96.  
  97.             else if (msg.startsWith("msg")) {
  98.                 String[] msg_parts = msg.split(":");
  99.                 for (int i = 0; i < msg_parts.length; i++) {
  100.                     System.out.println(msg_parts[i]);
  101.                 }
  102.                 String msg_type = msg_parts[0];
  103.                 if (msg_type.equals("msg")) {
  104.                     if (msg_parts.length < 3)
  105.                         output = msg_INVALID;
  106.                     if (sendMsg(msg_parts[1], msg_parts[2]))
  107.                         output = msg_OK;
  108.                     else
  109.                         output = msg_SEND_FAILED;
  110.                 } else {
  111.                     output = msg_INVALID;
  112.                 }
  113.             }
  114.  
  115.             return output;
  116.         }
  117.     }
  118.  
  119.     private static class ClientConn implements Runnable {
  120.         private Socket client;
  121.         private BufferedReader in = null;
  122.         private PrintWriter out = null;
  123.  
  124.         public ClientConn(Socket client) {
  125.             this.client = client;
  126.             try {
  127.                 /* obtain an input stream to this client ... */
  128.                 in = new BufferedReader(new InputStreamReader(client.getInputStream()));
  129.                 /* ... and an output stream to the same client */
  130.                 out = new PrintWriter(client.getOutputStream(), true);
  131.             } catch (IOException e) {
  132.                 System.err.println(e);
  133.                 return;
  134.             }
  135.         }
  136.  
  137.         public void run() {
  138.             try {
  139.                 ChatServerProtocol protocol = new ChatServerProtocol(this);
  140.                 /*
  141.                  * loop reading lines from the client which are processed according to our protocol and the resulting
  142.                  * response is sent back to the client
  143.                  */
  144.                 while (true) {
  145.                     String msg = in.readLine();
  146.                     if (msg == null || msg.equals("quit")) {
  147.                         break;
  148.                     }
  149.                     String response = protocol.process(msg);
  150.                     out.println("SERVER: " + response);
  151.                 }
  152.                 this.close();
  153.                 protocol.remove_nick();
  154.             } catch (IOException e) {
  155.                 System.err.println(e);
  156.             }
  157.         }
  158.  
  159.         public void sendMsg(String msg) {
  160.             out.println(msg);
  161.         }
  162.  
  163.         public void close() throws IOException {
  164.             in.close();
  165.             out.close();
  166.             client.close();
  167.         }
  168.     }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement