import java.io.*; import java.net.*; import java.util.*; public class ManyServer { public ManyServer(int port) { ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(port); while (true) { try { Socket newSocket = serverSocket.accept(); SingleServer newServer = new SingleServer(newSocket); Thread newThread = new Thread(newServer); newThread.start(); } catch (Exception e) { System.out.println("Error with the new socket stuff : " + e); } } } catch (Exception e) { System.out.println("Error with the socket stuff : " + e); } finally { if (serverSocket != null) { try { serverSocket.close(); } catch ( IOException e ) { System.out.println("Error with socket close : " + e); } } System.exit(1); } } public static void main( String args[] ) { if ( args.length != 1 ) { System.out.println("Usage: java ManyServer port"); } int serverPort = Integer.parseInt(args[0]); ManyServer manyServer = new ManyServer(serverPort); } } class SingleServer implements Runnable { static SingleServer kasutaja[]=new SingleServer[5]; static Vector teated = null; static final int MAX_HISTORY = 200; Socket mySocket; int ise=-1; int ik; DataInputStream inputs; DataOutputStream outputs; String pilt=null; public SingleServer( Socket _mySocket ) { if ( teated == null ) { teated = new Vector(); } for(int ik=0; ik<5; ik++){if(kasutaja[ik]!=null){kasutaja[ik].write("tuuu ");}} for(ik=0;(ise<0)&&(ik<5); ik++){ if (kasutaja[ik]==null){kasutaja[ik]=this; ise=ik;} mySocket = _mySocket; }} synchronized void addtoHistory(String mess) { if ( teated.size() >= MAX_HISTORY ) { teated.removeElementAt(0); } teated.addElement(new HistoryEvent(mess)); } synchronized void dumpHistory() { HistoryEvent historyEvent; this.write("knr"+ise); if(pilt!=null){this.write(pilt);} for ( int i = 0; i < teated.size(); i++ ) { try { historyEvent = (HistoryEvent)teated.elementAt(i); if ( historyEvent != null ) { { if ( historyEvent.message != null ) { this.write( (String) historyEvent.message ); } } } } catch (Exception e) { } } } public void run () { String message; try { inputs = new DataInputStream(mySocket.getInputStream()); outputs = new DataOutputStream(mySocket.getOutputStream()); boolean go = true; while (go) { message = inputs.readLine(); if ( message == null ) { go = false; } else { System.out.println(message); if (message.startsWith("HISTORY")) { dumpHistory();} else if (message.startsWith(".lopp")){ System.exit(1);} else if (message.startsWith(".kadu")){ kasutaja[ise]=null;} else if((message.charAt(0)=='t')|| (message.charAt(0)=='p')){ manyCast(message); if (message.charAt(0)=='p'){pilt=message;} else{addtoHistory(message);} } } } inputs.close(); outputs.close(); mySocket.close(); } catch (Exception e) { System.out.println("Viga"); kasutaja[ise] = null; } } public synchronized void write ( String mess ) { try { outputs.writeBytes(mess+"\n"); outputs.flush(); System.out.println("Korras "+ise); } catch (Exception e) { kasutaja[ise] = null; System.out.println((kasutaja[ise]==null)+" "+ise); } } public void manyCast( String mess ) { SingleServer oneServer; System.out.println("manyCast to " + mess + " servers."); for ( int i = 0; i < 5; i++ ) { if(kasutaja[i]!=null){ oneServer = (SingleServer) kasutaja[i]; if(mess.length()>2){ if ((mess.substring(2, 3).compareTo(Integer.toString(i))==0) || (mess.charAt(2)=='a') || (mess.substring(1, 2).compareTo(Integer.toString(i))==0 )) { oneServer.write(mess); System.out.println("laks "+i); }}} } } } class HistoryEvent { String message; public HistoryEvent(String _message) { message = _message; } }