import java.io.*;
import java.net.*;
class ClientHandler extends java.lang.Thread
{
Socket client;
Short index;
Server host;
DataOutputStream output;
BufferedReader input;
String name = null;
boolean running = true;
short helpLength = 6;
String[] help = new String[]
{"*** Help for the messaging thing.\n***\n",
"*** To quit, type '/#quit'\n",
"*** To change your name, type '/#name <new_name>'.\n",
"*** To send a private message, type '/#pm <recipient> <message>'.\n",
"*** Otherwise, simply type your message and press enter!\n***\n***\n***\n",
"*** You just lost the game!\n\n"};
short welcomeLength = 2;
String[] welcome = new String[]
{"*** Boo! You have connected. How amaxing!\n",
"*** Please identify yourself.\n"};
public ClientHandler(Socket handlee, Short arrayIndex, Server host)
{
client = handlee;
index = arrayIndex;
this.host = host;
try
{
output = new DataOutputStream(client.getOutputStream());
}
catch (IOException error)
{
System.out.println("Failed to get output stream for client "+index);
}
try
{
input = new BufferedReader(new InputStreamReader(client.getInputStream()));
}
catch (IOException error)
{
System.out.println("Failed to get input stream for client "+index);
}
}
public void run()
{
if (running)
{
try
{
for (int a=0;a<welcomeLength;a++)
{
output.writeChars(welcome[a]);
}
}
catch(IOException error)
{
System.out.println("Oops! Error sending welcome message.");
}
giveName();
host.sayAll("*** "+name+" has joined.");
System.out.println("Client "+index+" is called "+name);
while (running)
{
String receivedMessage=null;
try
{
receivedMessage = input.readLine();
}
catch (IOException error)
{
System.out.println("Error while receiving message.");
}
try
{
if (receivedMessage!=null)
{
if (receivedMessage.trim().equalsIgnoreCase("/#quit"))
{
output.writeChars("*** You are leaving the chat.\n");
quit();
}
else if (receivedMessage.trim().equalsIgnoreCase("/#help"))
{
for (int a=0;a<helpLength;a++)
{
output.writeChars(help[a]);
}
}
else if (receivedMessage.toLowerCase().startsWith("/#pm"))
{
String[] splitMessage = receivedMessage.split(" ",3);
if (splitMessage.length==3)
{
host.sayOne(splitMessage[1],"<from "+name+"> "+splitMessage[2],index);
}
else
{
output.writeChars("*** Please follow the correct format of '/#pm <recipient> <message>'.\n");
}
}
else if (receivedMessage.toLowerCase().startsWith("/#name"))
{
System.out.println("receivedMessage="+receivedMessage);
receivedMessage+=" ";
System.out.println("now receivedMessage="+receivedMessage);
String[] splitMessage = receivedMessage.split(" ",2);
System.out.println("splitMessage[0]="+splitMessage[0]);
System.out.println("splitMessage[1]="+splitMessage[1]);
resetName(splitMessage[1]);
}
else
{
if (receivedMessage.equalsIgnoreCase("help")||receivedMessage.equalsIgnoreCase("/help"))
{
output.writeChars("*** For help type '/#help'\n");
}
host.sayAll("<"+name+"> "+receivedMessage);
}
}
}
catch (IOException error)
{
System.out.println("Error while reacting after receiving message from client "+index);
}
}
}
}
public void quit()
{
try
{
stopThread();
input.close();
output.close();
client.close();
}
catch(IOException error)
{
System.out.println("Error while closing (input stream, output stream or client socket for) client "+index);
}
host.clientClosing(index);
}
public void giveName()
{
try
{
while (name == null)
{
name = input.readLine();
}
if (name.equals(name.split(" ")[0]))
{
if (host.getNameTaken(name,index))
{
output.writeChars("*** That name is taken. Please enter another.\n");
name = null;
giveName();
}
else
{
output.writeChars("*** Name accepted. To change your name type '/#name <new_name>'.\n");
}
}
else
{
output.writeChars("*** That name is invalid. Remember: spaces are not allowed.\n");
giveName();
}
}
catch (IOException error)
{
System.out.println("Error while setting name for client "+index);
}
}
public void resetName(String newName)
{
try
{
String[] splittedNewName = newName.split(" ");
if (newName.trim()!=null && newName.trim()!="" && newName!="")
{
if (newName.trim().equals(splittedNewName[0]))
{
newName = newName.trim();
if (host.getNameTaken(name,index))
{
output.writeChars("*** That name is taken. Please enter another.\n");
}
else
{
output.writeChars("*** New name accepted.\n");
System.out.println("Client "+index+" ("+name+") is now called "+newName);
name = newName;
}
}
else
{
output.writeChars("*** That name is invalid. Remember: spaces are not allowed.\n");
}
}
}
catch (IOException error)
{
System.out.println("Error while resetting name for client "+index);
}
}
public void say(String message)
{
try
{
output.writeChars(message);
}
catch (IOException error)
{
System.out.println("Error while sending message to client "+index);
}
}
public void stopThread()
{
running = false;
}
} |