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

class ConnexionTD extends Thread{
  protected Socket client;
  protected ServeurTD serveur;
  protected BufferedReader in;
  protected PrintStream out;
  protected Nettoyeur nettoyeur;
    
  public ConnexionTD(Socket client_soc, Nettoyeur n, ServeurTD s)
    {client=client_soc;
     nettoyeur=n;
     serveur=s;
     
     try
       {in =new BufferedReader(new InputStreamReader(client.getInputStream()));
	  out =new PrintStream(client.getOutputStream());}
     catch (IOException e)
       {try {client.close();} catch (IOException e1) {};
	    System.err.println(e.getMessage());
    	return;
       }
     this.start();
    }
    
  public void run()
  { String ligne;
    ConnexionTD c;
    try
      {while(true)
       {ligne=in.readLine();
	    // envoyer a tous les clients la ligne
        System.out.println(ligne);
	  synchronized(serveur.connexions)
	     {for (int i=0;i<serveur.connexions.size();i++)
	      {c=(ConnexionTD) serveur.connexions.elementAt(i);
	       c.out.println(ligne);}}
	    if (ligne.endsWith("FIN")) break;	    
       }}
    catch (IOException e){}
    finally
      {try {client.close();} catch (IOException e){};
       System.out.println("Fin de connexion...");	  
       synchronized(nettoyeur) {nettoyeur.notify();}      
      }
  }
}