package pad.prac2; import java.io.IOException; public class Connection extends Thread { private MySocket socket; private Server serv; private boolean sleep, kill; public Connection(Server s) { serv = s; kill = false; sleep = true; } public void sendMessage(String message) throws IOException { socket.sendMsg(message); } public void awake() { synchronized(this) { sleep = false; this.notify(); } } public void finish() { kill = true; sleep = false; socket.close(); this.notify(); } public void setSock(MySocket s) { socket = s; } public void run() { while(!kill) { while(sleep) { try { synchronized(this) { wait(); } } catch (InterruptedException e) { e.printStackTrace(); } } String str = socket.recvMsg(); serv.addToChatroom(this, str); if(!kill) { while(true) { str = socket.recvMsg(); if(str != null) { try { if(str.equals("/disconnect")) { socket.sendMsg("DISC_OK"); kill = true; System.out.println(serv.getNickname(this) + " disconnected"); break; } else if(str.equals("/exit")) { socket.sendMsg("EXIT_OK"); kill = true; System.out.println(serv.getNickname(this) + " disconnected"); break; } else { System.out.println("FROM " + serv.getNickname(this) + ": " + str); serv.sendToChat(this,str); } } catch(IOException ioExc) { System.out.println("TCP: Error writing to socket"); break; } } else { break; } } sleep = true; } serv.finishWorker(this); } } }