
/**
 * class ClassicServer.
 * dans le Strategy pattern c'est un serveur qui créé les
 * thread au fur et à mesure des besoins..
 * 
 * @author Laurent Dehoey I177416
 * @version 23/01/2003
 */

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

public class ClassicServer extends  AbstractServer{

   /**
    * on est pas fort avec jdb et les threads
    * donc on se compte et on s'affiche ;-)
    */
   /**
    * comptabilisateur de ClassicServer créés
    */
   private static int counterCSS;
   /**
    * Numero d'indentification du Serveur
    */
   private int counterCS;
   /**
    * comptabilisateur de threadedConnection
    */
   
    private static int counterTCS;
   /**
    * marff pour le moment on appele
    * juste notre constructeur pere
    */

   public ClassicServer(Protocol protocol, int port) throws java.io.IOException
   {
      //....
        //marff pour le moment ca ira bien
        super(protocol,port);

        /**
         * on compte nos petits
         */
        counterCSS++;
        counterCS = counterCSS;
    /**
     * on remets nos stats partielles
     * à zero
     */
    globalInstanceConnections=0;


   }
   //....
   /**
    * methode appelé par notre classe mere Abstractserver
    * on cree un thread  dès qu'on en a besoin...
    * @param s java.net.Socket une "prise" de connection client/server
    * @return void
    */
   
   public void executeConnection(java.net.Socket s){
    System.out.println("Hoho on appel l'execution effective par un classic server n°"+counterCS+"/"+counterCSS);
     Thread t = new Thread(new ThreadConnection(s),s.toString()+"/Id:"+(int)(1000*java.lang.Math.random()));
     //on appelle directement notre thread pour rendre
     //la main à celui qui vient de nous appeler car
     //on est sympa et qu'on déteste faire attendre
     t.start();
     System.out.println("Classic serveur has threaded a new protocol thread"+counterCS+"/"+counterCSS);
   }

   private class ThreadConnection implements Runnable{
    
   // private static int counterTCS;
    private int counterTC;
    
    protected java.io.BufferedReader in;
    protected java.io.BufferedWriter out;

    protected java.net.Socket client;

    /**
     * tentative d'identifier les threads au milieu
     * des serveurs
     */
     public String getIdThread(){
        return("Thread : "+Thread.currentThread().getName());
    }
    public ThreadConnection(java.net.Socket s){
     this.client=s;
     counterTCS++;
     counterTC=counterTCS;
     System.out.println(getIdThread());
    }
    public void run(){
    try{
        if(getVerbose())
            System.out.println("Ok on essaye"+getIdThread()+"\n");
        in=new java.io.BufferedReader(new java.io.InputStreamReader(client.getInputStream()));
        out=new java.io.BufferedWriter(new java.io.PrintWriter(client.getOutputStream()));

    }catch(java.io.IOException e){
        System.out.println(getIdThread()+":"+ e);
        try{client.close();}
        catch(java.io.IOException e2){System.out.println("AHamupf ... "+ e2);}
     }
     try{
     String line;
     line=in.readLine();
     System.out.println("Trying to say something to me ("+getIdThread()+"? : "+line+"\n");
     /**
      * on va faire du random pour faire semblant de traiter
      * différentes longeurs de requetes
      */
     try{
     int sleepingTime = (int)(20000*java.lang.Math.random());
     System.out.println(getIdThread()+" is Trying to sleep "+ sleepingTime+"ms");
     Thread.currentThread().sleep(sleepingTime);
     System.out.println(getIdThread()+" has tryed to sleep "+ sleepingTime+"ms");
     out.write(getIdThread()+" has tryed to sleep "+ sleepingTime+"ms");
     out.newLine();
     out.write("Did u Say ("+getIdThread()+"): "+line+" ?\r\n");
     out.flush();
     in.close();
     out.close();
//     super.interrupt();
//     client.close();
     }catch(InterruptedException ie){
        System.out.println("arrff on peut plus randomizer tranquille à la fin ?!!");
     }
     System.out.println(getIdThread()+" is finaly Closed");
     globalInstanceConnections--;
     connections--;
     }catch(java.io.IOException e){System.out.println("AHamupf ... "+ e);}
    }
   }

}

