import java.io.*; import java.net.*; import java.util.*; public class Vastus6 { public Vastus6(int port) { ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(port); SingleServer.kirjuta_log_faili("aeg "+new Date().toGMTString()+" Uus server"); while (true) { try { Socket newSocket = serverSocket.accept(); SingleServer newServer = new SingleServer(newSocket); Thread newThread = new Thread(newServer); newThread.start(); //System.out.println("juures"); } 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]); Vastus6 manyServer = new Vastus6(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; static String pilt=null; String nimi=" xxx "; public SingleServer( Socket _mySocket ) { mySocket = _mySocket; if ( teated == null ) { teated = new Vector(); } } synchronized void addtoHistory(String mess) { if ( teated.size() >= MAX_HISTORY ) { teated.removeElementAt(0); } teated.addElement(new HistoryEvent(mess)); } synchronized void dumpHistory(String nimi) { this.nimi=mySocket.getInetAddress().getHostName(); while(this.nimi.length()<20)this.nimi+=" "; for(int ik=1; ik<5; ik++){if(kasutaja[ik]!=null){kasutaja[ik].write("tuuu");}} if(nimi.substring(1, 2).equals("t")){ for(ik=1;(ise<0)&&(ik<5); ik++){ if(kasutaja[ik]!=null)if (kasutaja[ik].nimi.compareTo(this.nimi)==0) { kasutaja[ik]=this; ise=ik;}} for(ik=1;(ise<0)&&(ik<5); ik++){ if (kasutaja[ik]==null){kasutaja[ik]=this; ise=ik;}} } else {kasutaja[0]=this; ise=0; // System.out.println("Opetaja olemas"); } HistoryEvent historyEvent; // manyCast("k"+ise+"an"+nimi.substring(4)); //this.nimi=nimi.substring(4); kirjuta_log_faili("aeg "+new Date().toGMTString()+" Uus kasutaja "+ this.nimi+" nr "+ise); for(int i=0; i<5; i++){ if(kasutaja[i]!=null)manyCast("k"+i+"an"+kasutaja[i].nimi); } this.write("knr"+ise); // this.write("k0"+ise+"u"); if (kasutaja[0]!=null){ SingleServer oneserver=(SingleServer)kasutaja[0]; oneserver.write("k"+ise+"0o");} if(ise==0){for(int i=1; i<5; i++) if(kasutaja[i]!=null)this.write("k"+i+"0o");} 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 { //System.out.println("Veel pole kuljes "+mySocket.toString()); inputs = new DataInputStream(mySocket.getInputStream()); outputs = new DataOutputStream(mySocket.getOutputStream()); //System.out.println("Kuljes "+mySocket.getInetAddress().getHostName()); //dumpHistory(); boolean go = true; while (go) { message = inputs.readLine(); //System.out.println("tuli"+message); if ( message == null ) { go = false; System.out.println("Tuhikiri"); } else { if(message.length()<1)message=" "; // System.out.println(message); if (message.startsWith("ktan")| message.startsWith("koan")) {dumpHistory(message); //System.out.println("Ajalugu valjas"); } else if (message.startsWith(".lopp")){ System.exit(1);} else if (message.startsWith(".kadu")){ this.kinni();} else if (message.charAt(0)=='a'){ if (message.startsWith("a0up"))loo_pildiarhiiv(); if (message.startsWith("a0sp"))kirjuta_pildiarhiivi(message.substring(4)); if (message.startsWith("a0pv"))kirjuta_pildiarhiiv(); } else if((message.charAt(0)=='t')|| (message.charAt(0)=='p')||(message.charAt(0)=='k')){ manyCast(message); if (message.charAt(0)=='p'){pilt=message; //System.out.println("Tuli pilt"); } else if(message.charAt(0)=='k'){ if((message.charAt(3)=='x')|(message.charAt(3)=='k')){ for(int ik=1; ik<5; ik++) if((message.charAt(2)=='a') || (message.substring(2, 3).compareTo(Integer.toString(ik))==0)) {kasutaja[ik].kinni(); kirjuta_log_faili(ik+" kinnipanek");} if(message.startsWith("k00k")) {manyCast("t0aoSession finished"); for(ik=1; ik<5; ik++)if(kasutaja[ik]!=null)kasutaja[ik].kinni(); System.exit(1); } }} else{addtoHistory(message);} } } } inputs.close(); outputs.close(); mySocket.close(); } catch (Exception e) { System.out.println("Viga"+e.toString()); // kasutaja[ise] = null; this.kinni(); } } public synchronized void kinni(){ try{ SingleServer oneserver=this; if(ise>=0){ manyCast("k"+ise+"ax"); kasutaja[ise]=null; } oneserver.inputs.close(); oneserver.outputs.close(); oneserver.mySocket.close(); }catch(Exception e){ System.out.println("Kinnipanekujama: "+e.getMessage());} //System.out.println(ise+" kinni"); kirjuta_log_faili("aeg "+new Date().toGMTString()+" "+nimi+" nr "+ ise+ " kinni"); } public synchronized void write ( String mess ) { try { outputs.writeBytes(mess+"\n"); outputs.flush(); //System.out.println(ise+" "+mess); } catch (Exception e) { if(ise>0){kasutaja[ise]=null; this.kinni();} /*else { valja(); //System.exit(1); if(ise==0) for(int ik=1; ik<5;ik++){if(kasutaja[ik]!=null) {kasutaja[ik].write("t0aoTeacher left, connection closed.");}} System.exit(1); }*/ // kasutaja[ise] = null; //System.out.println((kasutaja[ise]==null)+" "+ise); } } public void manyCast( String mess ) { SingleServer oneServer; // System.out.println("manyCast to " + mess + " servers."); try{ kirjuta_log_faili(mess.substring(0, 4)+ kasutaja[Integer.valueOf(mess.substring(1, 2)).intValue()].nimi+": "+ mess.substring(4));} catch(Exception e){} 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+mess); }}} } } public static void kirjuta_log_faili(String teade){ try{ String s="chat.log"; RandomAccessFile rf=new RandomAccessFile(s, "rw"); rf.seek(rf.length()); rf.writeBytes(teade+"\n"); rf.close(); }catch(Exception e){System.out.println("Failijama: "+e.getMessage());} } public static void kirjuta_pildiarhiivi(String pilt){ try{ String s="pildid.dat"; RandomAccessFile rf=new RandomAccessFile(s, "rw"); rf.seek(rf.length()); rf.writeBytes(pilt+"\n"); rf.close(); }catch(Exception e){System.out.println("Failijama: "+e.getMessage());} } public static void loo_pildiarhiiv(){ try{ String alus="pildid.dat"; String siht="pildid.da~"; File rf=new File(alus); rf.renameTo(new File(siht)); }catch(Exception e){System.out.println("Failijama: "+e.getMessage());} } public void kirjuta_pildiarhiiv(){ String failinimi="pildid.dat"; String s; write("au0p"); try{ RandomAccessFile rf=new RandomAccessFile(failinimi, "rw"); while((s=rf.readLine())!=null)write("as0p"+s); }catch(Exception e){System.out.println("Failijama: "+e.getMessage());} } } class HistoryEvent { String message; public HistoryEvent(String _message) { message = _message; } }