1:  
2:   /**
3:    * class ClassicServer.
4:    * dans le Strategy pattern c'est un serveur qui cr les
5:    * thread au fur et  mesure des besoins..
6:    
7:    @author Laurent Dehoey I177416
8:    @version 23/01/2003
9:    */
10:  
11:   //import java.io.*;
12:   //import java.net.*;
13:  
14:   public class ClassicServer extends  AbstractServer{
15:  
16:      /**
17:       * on est pas fort avec jdb et les threads
18:       * donc on se compte et on s'affiche ;-)
19:       */
20:      /**
21:       * comptabilisateur de ClassicServer crs
22:       */
23:      private static int counterCSS;
24:      /**
25:       * Numero d'indentification du Serveur
26:       */
27:      private int counterCS;
28:      /**
29:       * comptabilisateur de threadedConnection
30:       */
31:      
32:       private static int counterTCS;
33:      /**
34:       * marff pour le moment on appele
35:       * juste notre constructeur pere
36:       */
37:  
38:      public ClassicServer(Protocol protocolint portthrows java.io.IOException
39:      {
40:         //....
41:           //marff pour le moment ca ira bien
42:           super(protocol,port);
43:  
44:           /**
45:            * on compte nos petits
46:            */
47:           counterCSS++;
48:           counterCS = counterCSS;
49:       /**
50:        * on remets nos stats partielles
51:        *  zero
52:        */
53:       globalInstanceConnections=0;
54:  
55:  
56:      }
57:      //....
58:      /**
59:       * methode appel par notre classe mere Abstractserver
60:       * on cree un thread  ds qu'on en a besoin...
61:       @param s java.net.Socket une "prise" de connection client/server
62:       @return void
63:       */
64:      
65:      public void executeConnection(java.net.Socket s){
66:       System.out.println("Hoho on appel l'execution effective par un classic server n"+counterCS+"/"+counterCSS);
67:        Thread t = new Thread(new ThreadConnection(s),s.toString()+"/Id:"+(int)(1000*java.lang.Math.random()));
68:        //on appelle directement notre thread pour rendre
69:        //la main  celui qui vient de nous appeler car
70:        //on est sympa et qu'on dteste faire attendre
71:        t.start();
72:        System.out.println("Classic serveur has threaded a new protocol thread"+counterCS+"/"+counterCSS);
73:      }
74:  
75:      private class ThreadConnection implements Runnable{
76:       
77:      // private static int counterTCS;
78:       private int counterTC;
79:       
80:       protected java.io.BufferedReader in;
81:       protected java.io.BufferedWriter out;
82:  
83:       protected java.net.Socket client;
84:  
85:       /**
86:        * tentative d'identifier les threads au milieu
87:        * des serveurs
88:        */
89:        public String getIdThread(){
90:           return("Thread : "+Thread.currentThread().getName());
91:       }
92:       public ThreadConnection(java.net.Socket s){
93:        this.client=s;
94:        counterTCS++;
95:        counterTC=counterTCS;
96:        System.out.println(getIdThread());
97:       }
98:       public void run(){
99:       try{
100:           if(getVerbose())
101:               System.out.println("Ok on essaye"+getIdThread()+"\n");
102:           in=new java.io.BufferedReader(new java.io.InputStreamReader(client.getInputStream()));
103:           out=new java.io.BufferedWriter(new java.io.PrintWriter(client.getOutputStream()));
104:  
105:       }catch(java.io.IOException e){
106:           System.out.println(getIdThread()+":"e);
107:           try{client.close();}
108:           catch(java.io.IOException e2){System.out.println("AHamupf ... "e2);}
109:        }
110:        try{
111:        String line;
112:        line=in.readLine();
113:        System.out.println("Trying to say something to me ("+getIdThread()+"? : "+line+"\n");
114:        /**
115:         * on va faire du random pour faire semblant de traiter
116:         * diffrentes longeurs de requetes
117:         */
118:        try{
119:        int sleepingTime (int)(20000*java.lang.Math.random());
120:        System.out.println(getIdThread()+" is Trying to sleep "sleepingTime+"ms");
121:        Thread.currentThread().sleep(sleepingTime);
122:        System.out.println(getIdThread()+" has tryed to sleep "sleepingTime+"ms");
123:        out.write(getIdThread()+" has tryed to sleep "sleepingTime+"ms");
124:        out.newLine();
125:        out.write("Did u Say ("+getIdThread()+"): "+line+" ?\r\n");
126:        out.flush();
127:        in.close();
128:        out.close();
129:   //     super.interrupt();
130:   //     client.close();
131:        }catch(InterruptedException ie){
132:           System.out.println("arrff on peut plus randomizer tranquille  la fin ?!!");
133:        }
134:        System.out.println(getIdThread()+" is finaly Closed");
135:        globalInstanceConnections--;
136:        connections--;
137:        }catch(java.io.IOException e){System.out.println("AHamupf ... "e);}
138:       }
139:      }
140:  
141:   }
142:  

This page was automatically generated by SharpDevelop.