ServerThread

ServerThread main file
mail@pastecode.io avatar
unknown
java
4 years ago
3.9 kB
5
Indexable
Never
package server;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.List;
import java.util.Scanner;


public class ServerThread implements Runnable {

	private Socket socket;
	private DataInputStream input;
	private DataOutputStream output;
	private Server server;
	private ServerThread serverThread = this;
	private Scanner scanner;
	private String clientName;
	private boolean quite = false;
	private List<String> messages;
	
	public ServerThread(Socket socket, Server server) throws Exception {
		
		this.scanner = new Scanner(System.in);
		this.server = server;
		this.socket = socket;
		this.input = new DataInputStream(socket.getInputStream());
		this.output = new DataOutputStream(socket.getOutputStream());
		this.messages = server.getMessages();
	}
	
	
	@Override
	public synchronized void run() {
		
		askName();
		
		sendLast10messages();
		
		Thread thread = new Thread() {
			@Override
			public void run() {
				try {
					
					String message;
					while (!quite) {
						try {
							
							message = input.readUTF();
//							System.out.println(message);
							if (message.equals("/exit")) {
								exitConnection();
								return;
							}
						
							server.sendToAllClients(clientName + ": " + message, serverThread);
							
						} catch (Exception e) {
							// TODO: handle exception
							socket.close();
							output.close();
							input.close();
						}
					}
					
				} catch (Exception e) {
					try {
						socket.close();
						output.close();
						input.close();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
		};
		thread.start();
		
		
		Thread thread2 = new Thread() {
			@Override
			public void run() {
				try {
				
					String message;
					while (true) {
						
						message = scanner.nextLine();
//						sendMessage(message);
						
					}
				} catch (Exception e) {
					// TODO: handle exception
					try {
						socket.close();
						output.close();
						input.close();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					
				}
			}
		};
		thread2.start();
	}
	
	
	
	public synchronized void askName() {
		try {
			
			output.writeUTF("Server: write your name");
			String name = input.readUTF();			
			while (true) {
				
				if (server.getNane(name)) {
					
					output.writeUTF("Server: This name is already taken! Choose another one.");
					name = input.readUTF();
					
				} else {
					
					output.writeUTF(" ");
					server.setClientName(name);
					clientName = name;
					break;
					
				}
				
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	
	public void exitConnection() {
		try {
			quite = true;
			closeConnection();
			return;
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	
public void closeConnection() throws Exception {
		
		socket.close();
		output.close();
		input.close();
	}
	
	public void sendMessage(String message) {
		
		try {
			
			output.writeUTF(message);
			server.setMessages(message);
			
		} catch (Exception e) {
			
			System.out.println("Writing error: ");
			
		}
		
	}
	
	
	public synchronized void sendLast10messages() {
		
		if (messages.size() > 10) {
			for (int i = messages.size() - 10; i < messages.size(); i++) {
				try {
					output.writeUTF(messages.get(i));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		} else {
			for (int i = 0; i < messages.size(); i++) {
				try {
					output.writeUTF(messages.get(i));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

}