Chat.java

Chat.java · 4.8 KB

From the archive Chatter.zip

Download the archive   « Back to the archive contents

import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;

class Chat
{
	ArrayList<ServerCommunicator> connections;
	
	static Chat chat;
	UserCommunicator userCommunicator;
	ServerCommunicator serverCommunicator;
	
	Scanner commandScanner;
	String commandString;
	
	int portNumber;
	
	public static void main(String args[])
	{
		chat = new Chat();
		chat.reset();
	}
	
	public Chat()
	{
		connections = new ArrayList();
		
		portNumber = 4444;
	}
	
	public void reset()
	{
		try
		{
			for(int i = 0; i < connections.size(); i++)
			{
				connections.get(i).socket.close();
			}
		}		
		catch(Exception e)
		{
			System.out.println(Utility.getTimestamp() + "Socket connection didn't close.");
		}
		
		connections = new ArrayList();
		
		portNumber = 4444;
		
		userCommunicator = new UserCommunicator(this);
		userCommunicator.start();
	}
	
	public void userCommand(String command)
	{
		commandScanner = new Scanner(command);
		
		if(commandScanner.hasNext())
		{
			commandString = commandScanner.next();

			if(commandString.equalsIgnoreCase("quit") || commandString.equalsIgnoreCase("exit") || commandString.equalsIgnoreCase("q") || commandString.equalsIgnoreCase("x"))
			{
				try
				{
					for(int i = 0; i < connections.size(); i++)
					{
						connections.get(i).send("connectionclosing");
						connections.get(i).socket.close();
					}
				
					System.out.print(Utility.getTimestamp() + "Goodbye!\n");
					
					System.exit(0);
				}
				catch(Exception e)
				{
					System.out.println(Utility.getTimestamp() + "Socket connection didn't close.");
				}
			}
			
			else if(commandString.equalsIgnoreCase("send") || commandString.equalsIgnoreCase("say") || commandString.equalsIgnoreCase("s"))
			{				
				try
				{
					if(connections.get(0).socket.isConnected())
					{
						connections.get(0).send("send " + commandScanner.nextLine().substring(1));
					}
					
					else
					{
						System.out.println(Utility.getTimestamp() + "Not connected!");
					}
				}
				catch(Exception e)
				{
					System.out.println(Utility.getTimestamp() + "Not connected!");
				}
			}
			
			else if(commandString.equalsIgnoreCase("connect"))
			{
				connect(commandScanner.next());
			}
		}		
	}
	
	
	public void serverCommand(String command)
	{
		commandScanner = new Scanner(command);
		
		if(commandScanner.hasNext())
		{
			commandString = commandScanner.next();
			
			if(commandString.equalsIgnoreCase("send"))
			{
				System.out.println(commandScanner.nextLine().substring(1));
				System.out.print(Utility.getTimestamp());
			}
		}
	}
		
	public void connect(String address)
	{
		System.out.println(Utility.getTimestamp() + "Attempting connection at: " + address + " Port: " + portNumber);
		
		try
		{
			serverCommunicator = new ServerCommunicator(address, portNumber, this);
			serverCommunicator.start();
			
			connections.add(serverCommunicator);
			
			System.out.println(Utility.getTimestamp() + "Connected at: " + address + " Port: " + portNumber);
		}
		catch(Exception e)
		{
			System.out.println(Utility.getTimestamp() + "Connection failed at: " + address + " Port: " + portNumber);
		}
		
		try
		{
			Thread.sleep(500);
		}
		catch(Exception e)
		{
			System.out.println(Utility.getTimestamp() + "Sleep failed");
		}
	}
}

class UserCommunicator extends Thread
{
	Chat userCommunicatorChat;
	
	BufferedReader userInput;
	String stringUserInput;
	
	
	public UserCommunicator(Chat chatObject)
	{
		userInput = new BufferedReader(new InputStreamReader(System.in));
		userCommunicatorChat = chatObject;
	}
	
	public void run()
	{
		try
		{
			System.out.print(Utility.getTimestamp());
			
			while((stringUserInput = userInput.readLine()) != null)
			{
				userCommunicatorChat.userCommand(stringUserInput);
				
				System.out.print(Utility.getTimestamp());
			}
		}
		catch(Exception e)
		{
		}
	}
}

class ServerCommunicator extends Thread
{
	Chat serverCommunicatorChat;
	
	Socket socket;
	
	PrintWriter output;
	BufferedReader input;
	String stringInput;
	
	public ServerCommunicator(String address, int port, Chat passedChat) throws Exception
	{
		socket = new Socket(address, port);
		serverCommunicatorChat = passedChat;
		
		try
		{
			output = new PrintWriter(socket.getOutputStream(), true);
			input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		}
		catch(Exception e)
		{
			System.out.println(Utility.getTimestamp() + "ServerCommunicator constructor failed.");
		}
	}
	
	public void send(String toSend)
	{
		output.println(toSend);
	}
	
	public void run()
	{
		try
		{
			while((stringInput = input.readLine()) != null)
			{
				serverCommunicatorChat.serverCommand(stringInput);
			}
		}
		catch(Exception e)
		{
		}
	}	
}

Site Stats

Snippets: 1791
Codebases: 571