| 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 protocol, int port) throws 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: | ||
| 99: | public void run(){ | |
| 100: | try{ | |
| 101: | if(getVerbose()) | |
| 102: | System.out.println("Ok on essaye"+getIdThread()+"\n"); | |
| 103: | ||
| 104: | /** | |
| 105: | * on remplace | |
| 106: | * * | |
| 107: | * * in=new java.io.BufferedReader(new java.io.InputStreamReader(client.getInputStream())); | |
| 108: | * * out=new java.io.BufferedWriter(new java.io.PrintWriter(client.getOutputStream())); | |
| 109: | * * | |
| 110: | * * par | |
| 111: | */ | |
| 112: | protocol.service(client); | |
| 113: | ||
| 114: | ||
| 115: | }catch(java.io.IOException e){ | |
| 116: | System.out.println(getIdThread()+":"+ e); | |
| 117: | try{client.close();} | |
| 118: | catch(java.io.IOException e2){System.out.println("AHamupf ... "+ e2);} | |
| 119: | }catch(java.lang.Exception ex){ | |
| 120: | System.out.println(getIdThread()+":"+ ex); | |
| 121: | } | |
| 122: | // try{ | |
| 123: | // String line; | |
| 124: | // line=in.readLine(); | |
| 125: | // System.out.println("Trying to say something to me ("+getIdThread()+"? : "+line+"\n"); | |
| 126: | /** | |
| 127: | * on va faire du random pour faire semblant de traiter | |
| 128: | * diffrentes longeurs de requetes | |
| 129: | */ | |
| 130: | try{ | |
| 131: | int sleepingTime = (int)(20000*java.lang.Math.random()); | |
| 132: | System.out.println(getIdThread()+" is Trying to sleep "+ sleepingTime+"ms"); | |
| 133: | Thread.currentThread().sleep(sleepingTime); | |
| 134: | System.out.println(getIdThread()+" has tryed to sleep "+ sleepingTime+"ms"); | |
| 135: | // out.write(getIdThread()+" has tryed to sleep "+ sleepingTime+"ms"); | |
| 136: | // out.newLine(); | |
| 137: | // out.write("Did u Say ("+getIdThread()+"): "+line+" ?\r\n"); | |
| 138: | // out.flush(); | |
| 139: | // in.close(); | |
| 140: | // out.close(); | |
| 141: | // super.interrupt(); | |
| 142: | // client.close(); | |
| 143: | }catch(InterruptedException ie){ | |
| 144: | System.out.println("arrff on peut plus randomizer tranquille la fin ?!!"); | |
| 145: | } | |
| 146: | System.out.println(getIdThread()+" is finaly Closed"); | |
| 147: | globalInstanceConnections--; | |
| 148: | connections--; | |
| 149: | // }catch(java.io.IOException e){System.out.println("AHamupf ... "+ e);} | |
| 150: | // } | |
| 151: | } | |
| 152: | } | |
| 153: | } | |
| 154: |
This page was automatically generated by SharpDevelop.