package com.j256.ormlite;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Hashtable;
public class ChatServer {
private static int port = 8001;
public static void main(String[] args) {
ServerSocket server = null;
try {
server = new ServerSocket(port);
} catch (IOException e) {
System.err.println("Could not listen on port: " + port);
System.err.println(e);
System.exit(1);
}
Socket client = null;
while (true) {
try {
client = server.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.err.println(e);
System.exit(1);
}
/* start a new thread to handle this client */
Thread t = new Thread(new ClientConn(client));
t.start();
}
}
private static class ChatServerProtocol {
private String nick;
private ClientConn conn;
private static Hashtable<String, ClientConn> nicks = new Hashtable<String, ClientConn>();
private static final String msg_OK = "OK";
private static final String msg_INVALID = "INVALID COMMAND";
private static final String msg_SEND_FAILED = "FAILED TO SEND";
private boolean add_nick(String nick, ClientConn c) {
if (nicks.containsKey(nick)) {
return false;
} else {
nicks.put(nick, c);
return true;
}
}
private boolean remove_nick() {
if (nick == null || !(nicks.containsKey(nick))) {
return false;
} else {
nicks.remove(nick);
return true;
}
}
public ChatServerProtocol(ClientConn c) {
nick = null;
conn = c;
}
private boolean sendMsg(String recipient, String msg) {
if (nicks.containsKey(recipient)) {
ClientConn c = nicks.get(recipient);
c.sendMsg(nick + ": " + msg);
return true;
} else {
return false;
}
}
public String process(String msg) {
String output = "";
if (msg.startsWith("Autore: ")) {
String tryauthor = msg.substring(8);
if (add_nick(tryauthor, this.conn)) {
// log("Benvenuto autore " + tryauthor);
this.nick = tryauthor;
output = "Benvenuto autore " + tryauthor;
} else {
output = "Errore: autore gia' collegato";
}
}
else if (msg.startsWith("msg")) {
String[] msg_parts = msg.split(":");
for (int i = 0; i < msg_parts.length; i++) {
System.out.println(msg_parts[i]);
}
String msg_type = msg_parts[0];
if (msg_type.equals("msg")) {
if (msg_parts.length < 3)
output = msg_INVALID;
if (sendMsg(msg_parts[1], msg_parts[2]))
output = msg_OK;
else
output = msg_SEND_FAILED;
} else {
output = msg_INVALID;
}
}
return output;
}
}
private static class ClientConn implements Runnable {
private Socket client;
private BufferedReader in = null;
private PrintWriter out = null;
public ClientConn(Socket client) {
this.client = client;
try {
/* obtain an input stream to this client ... */
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
/* ... and an output stream to the same client */
out = new PrintWriter(client.getOutputStream(), true);
} catch (IOException e) {
System.err.println(e);
return;
}
}
public void run() {
try {
ChatServerProtocol protocol = new ChatServerProtocol(this);
/*
* loop reading lines from the client which are processed according to our protocol and the resulting
* response is sent back to the client
*/
while (true) {
String msg = in.readLine();
if (msg == null || msg.equals("quit")) {
break;
}
String response = protocol.process(msg);
out.println("SERVER: " + response);
}
this.close();
protocol.remove_nick();
} catch (IOException e) {
System.err.println(e);
}
}
public void sendMsg(String msg) {
out.println(msg);
}
public void close() throws IOException {
in.close();
out.close();
client.close();
}
}
}