1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package pcuserveur.lib;
21
22 import java.io.BufferedReader;
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.InputStreamReader;
29 import java.io.UnsupportedEncodingException;
30 import java.net.HttpURLConnection;
31 import java.net.InetAddress;
32 import java.net.MalformedURLException;
33 import java.net.NetworkInterface;
34 import java.net.ServerSocket;
35 import java.net.Socket;
36 import java.net.SocketException;
37 import java.net.URL;
38 import java.security.MessageDigest;
39 import java.security.NoSuchAlgorithmException;
40 import java.sql.Connection;
41 import java.sql.DriverManager;
42 import java.sql.PreparedStatement;
43 import java.sql.ResultSet;
44 import java.sql.SQLException;
45 import java.text.DateFormat;
46 import java.text.SimpleDateFormat;
47 import java.util.ArrayList;
48 import java.util.Date;
49 import java.util.Enumeration;
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89 public class Serveur {
90
91
92
93
94
95
96
97
98
99 private static int CANAUX_MAX;
100
101
102
103
104
105
106
107
108
109 private static int CONNEXION_MAX;
110
111
112
113
114
115 public static MonLog logCanaux;
116
117
118
119
120
121 public static MonLog logCoDeco;
122
123
124
125
126
127 public static MonLog logServeur;
128
129
130
131
132
133
134
135
136 private static String MDP_BDD;
137
138
139
140
141
142
143
144
145 public static String MOTD;
146
147
148
149
150
151
152
153
154
155 private static String MSG_CONNEXION_MAX;
156
157
158
159
160
161
162
163
164 private static String MSG_UTL_BANNI;
165
166
167
168
169
170
171
172
173 private static String NOM_BDD;
174
175
176
177
178
179
180
181
182 private static int PORT_DEFAUT;
183
184
185
186
187
188
189
190
191 private static int PORT_SERVEUR_BDD;
192
193
194
195
196
197
198
199
200 private static String SERVEUR_BDD;
201
202
203
204
205
206
207
208
209 private static String USER_BDD;
210
211
212
213
214
215
216
217
218
219 private Connection conn;
220
221
222
223
224
225
226
227
228
229 private boolean lance = false;
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247 private ArrayList<Canal> listeCanaux = new ArrayList<Canal> ();
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264 private ArrayList<GestionnaireConnexion> listeGestionnaire = new ArrayList<GestionnaireConnexion> ();
265
266
267
268
269
270
271
272
273
274
275 private int nbCanaux;
276
277
278
279
280
281
282
283
284
285
286
287
288 private int nbGC;
289
290
291
292
293
294
295
296
297
298
299 private final ServerSocket socket;
300
301
302
303
304
305
306
307
308
309
310
311 public Serveur () throws IOException, RuntimeException {
312 Serveur.configuration (this.nbGC, this.nbCanaux);
313 this.socket = new ServerSocket (Serveur.PORT_DEFAUT);
314
315 for (int i = 0;i < Serveur.CONNEXION_MAX;i++) {
316 this.listeGestionnaire.add (new GestionnaireConnexion ());
317 }
318 for (int i = 0;i < Serveur.CANAUX_MAX;i++) {
319 this.listeCanaux.add (new Canal ());
320 }
321 }
322
323
324
325
326
327
328
329
330
331
332 public void ajouterGC (final Socket socket1) {
333 this.nbGC++;
334 final int i = this.premierNull (this.listeGestionnaire);
335
336 if (i != -1) {
337 this.listeGestionnaire.get (i).initialisationGestionnaire (socket1, this, i);
338 new Thread (this.listeGestionnaire.get (i)).start ();
339 }
340 }
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357 public void bannirUtilisateur (final String login) throws LoginIntrouvableException, IOException, NonConnecteException {
358 final int place = this.chercherUtilisateur (login);
359
360 if (place != -1) {
361 PreparedStatement state;
362
363 try {
364 state = this.conn.prepareStatement ("UPDATE utilisateurs SET active = '0' WHERE identifiant = ?");
365
366 state.setString (1, login);
367 state.executeUpdate ();
368 this.listeGestionnaire.get (place).stopper (Serveur.MSG_UTL_BANNI);
369 this.listeCanaux.get (place).notifier (Evenement.BANNI, login, "");
370 this.listeCanaux.get (place).enleverGC (login);
371 Serveur.logCoDeco.logMessage ("Le client " + login + " a été banni");
372 state.close ();
373 }
374 catch (final SQLException e) {
375 Serveur.logServeur.logException ("Serveur", "bannirUtilisateur", e);
376 }
377 }
378 else {
379 throw new LoginIntrouvableException (login + " n'est pas connecté");
380 }
381 }
382
383
384
385
386
387
388
389
390
391
392 public int canalExistant (final String nom) {
393 for (int i = 0;i < this.listeCanaux.size ();i++) {
394 if ((this.listeCanaux.get (i).getNom () != null) && this.listeCanaux.get (i).getNom ().equals (nom)) {
395 return i;
396 }
397 }
398
399 return -1;
400 }
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416 public void changerSujetCanal (final String nom, final String sujet) throws CanalInexistantException, IOException {
417 final int tmp = this.canalExistant (nom);
418
419 if (tmp != -1) {
420 this.listeCanaux.get (tmp).changerSujet (sujet);
421 Serveur.logCanaux.logMessage ("Le sujet du canal " + nom + " a été changé en " + sujet);
422 }
423 else {
424 throw new CanalInexistantException ();
425 }
426 }
427
428
429
430
431
432
433
434
435
436
437 private int chercherUtilisateur (final String login) {
438 boolean trouve = false;
439 int i = 0;
440
441 while (i < this.listeGestionnaire.size ()) {
442 if (!this.listeGestionnaire.get (i).getClient ().estVide ()) {
443 if (this.listeGestionnaire.get (i).getClient ().getLogin ().equals (login)) {
444 trouve = true;
445 break;
446 }
447 }
448 i++;
449 }
450 if (trouve) {
451 return i;
452 }
453
454 return -1;
455 }
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470 public static void configuration (int nbCo, int nbC) throws RuntimeException {
471 Serveur.logCanaux = new MonLog ("Canaux");
472 Serveur.logServeur = new MonLog ("Serveur");
473 Serveur.logCoDeco = new MonLog ("CoDeco");
474 final File file = new File ("." + File.separator + "conf" + File.separator + "serveur.conf");
475 try {
476 final BufferedReader r = new BufferedReader (new InputStreamReader (new FileInputStream (file), "UTF-8"));
477 String contenu = r.readLine ();
478 int i = 0;
479
480 while (contenu != null) {
481 if (contenu.charAt (0) == '#') {
482 continue;
483 }
484
485 final String[] tab = contenu.split ("=");
486
487 if (tab[0].equals ("PORT_ECOUTE")) {
488 Serveur.PORT_DEFAUT = Integer.parseInt (tab[1]);
489 i++;
490 }
491 else if (tab[0].equals ("CONNEXION_MAX")) {
492 Serveur.CONNEXION_MAX = Integer.parseInt (tab[1]);
493 i++;
494 }
495 else if (tab[0].equals ("CANAUX_MAX")) {
496 Serveur.CANAUX_MAX = Integer.parseInt (tab[1]);
497 i++;
498 }
499 else if (tab[0].equals ("MOTD")) {
500 Serveur.MOTD = "";
501
502 if (tab[1].contains ("\\\\")) {
503 String phrase = tab[1];
504
505 while (phrase.contains ("\\\\")) {
506 if (phrase.contains ("%d")) {
507 Date aujourdui = new Date ();
508 DateFormat dateFormat = new SimpleDateFormat ("dd/MM/yyyy");
509
510 phrase = phrase.replace ("%d", dateFormat.format (aujourdui));
511 }
512 if (phrase.contains ("%ip")) {
513 phrase = phrase.replace ("%ip", Serveur.getIp ());
514 }
515 if (phrase.contains ("%nc")) {
516 phrase = phrase.replace ("%nc", Integer.toString (nbC));
517 }
518 if (phrase.contains ("%nu")) {
519 phrase = phrase.replace ("%nu", Integer.toString (nbCo));
520 }
521 Serveur.MOTD += phrase.substring (1, phrase.length () - 2);
522 phrase = r.readLine ();
523 }
524 if (phrase.contains ("%d")) {
525 Date aujourdui = new Date ();
526 DateFormat dateFormat = new SimpleDateFormat ("dd/MM/yyyy");
527
528 phrase = phrase.replace ("%d", dateFormat.format (aujourdui));
529 }
530 if (phrase.contains ("%ip")) {
531 phrase = phrase.replace ("%ip", Serveur.getIp ());
532 }
533 if (phrase.contains ("%nc")) {
534 phrase = phrase.replace ("%nc", Integer.toString (nbC));
535 }
536 if (phrase.contains ("%nu")) {
537 phrase = phrase.replace ("%nu", Integer.toString (nbCo));
538 }
539 Serveur.MOTD += phrase.substring (0, phrase.length () - 1);
540 }
541 else {
542 Serveur.MOTD = tab[1].substring (1, tab[1].length () - 1);
543 }
544
545 i++;
546 }
547 else if (tab[0].equals ("MSG_CONNEXION_MAX")) {
548 Serveur.MSG_CONNEXION_MAX = tab[1].substring (1, tab[1].length () - 1);
549 i++;
550 }
551 else if (tab[0].equals ("MSG_UTL_BANNI")) {
552 Serveur.MSG_UTL_BANNI = tab[1].substring (1, tab[1].length () - 1);
553 i++;
554 }
555 else if (tab[0].equals ("SERVEUR_BDD")) {
556 Serveur.SERVEUR_BDD = tab[1].substring (1, tab[1].length () - 1);
557 i++;
558 }
559 else if (tab[0].equals ("PORT_SERVEUR_BDD")) {
560 Serveur.PORT_SERVEUR_BDD = Integer.parseInt (tab[1]);
561 i++;
562 }
563 else if (tab[0].equals ("NOM_BDD")) {
564 Serveur.NOM_BDD = tab[1].substring (1, tab[1].length () - 1);
565 i++;
566 }
567 else if (tab[0].equals ("USER_BDD")) {
568 Serveur.USER_BDD = tab[1].substring (1, tab[1].length () - 1);
569 i++;
570 }
571 else if (tab[0].equals ("MDP_BDD")) {
572 Serveur.MDP_BDD = tab[1].substring (1, tab[1].length () - 1);
573 i++;
574 }
575 contenu = r.readLine ();
576 }
577 r.close ();
578 if (i != 11) {
579 Serveur.logServeur.logMessage ("Une erreur est survenu avec le fichier de configuration, tout les paramètres n'ont pas été trouvés.");
580 Serveur.logServeur.logException ("Serveur", "configuration", new RuntimeException ());
581 throw new RuntimeException ();
582 }
583 }
584 catch (final UnsupportedEncodingException e) {
585 Serveur.logServeur.logMessage ("Le fichier de configuration est introuvable.");
586 Serveur.logServeur.logException ("Serveur", "configuration", new RuntimeException ());
587 throw new RuntimeException ();
588 }
589 catch (final FileNotFoundException e1) {
590 Serveur.logServeur.logException ("Serveur", "configuration", e1);
591 }
592 catch (final IOException e2) {
593 Serveur.logServeur.logException ("Serveur", "configuration", e2);
594 }
595 }
596
597
598
599
600
601
602
603
604
605 public void connexion () throws ClassNotFoundException {
606 Class.forName ("com.mysql.jdbc.Driver");
607 final String url = "jdbc:mysql://" + Serveur.SERVEUR_BDD + ":" + Serveur.PORT_SERVEUR_BDD + "/" + Serveur.NOM_BDD; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
608 final String utilisateur = Serveur.USER_BDD;
609 final String mdp = Serveur.MDP_BDD;
610
611 try {
612 this.conn = DriverManager.getConnection (url, utilisateur, mdp);
613
614 Serveur.logServeur.logMessage ("Connection bdd effective !");
615 }
616 catch (final SQLException e) {
617 Serveur.logServeur.logMessage ("Connection bdd echoué");
618 Serveur.logServeur.logException ("Serveur", "connexion", e);
619 throw new RuntimeException ();
620 }
621 }
622
623
624
625
626
627
628
629
630
631
632
633
634 public GestionnaireConnexion connexion_directe (final String login) throws LoginIntrouvableException {
635 final int position = this.chercherUtilisateur (login);
636
637 if (position != -1) {
638 return this.listeGestionnaire.get (position);
639 }
640 throw new LoginIntrouvableException ();
641 }
642
643
644
645
646
647
648
649
650
651
652
653
654 public GestionnaireConnexion connexion_directe_accepte (final String login) throws LoginIntrouvableException {
655 final int position = this.chercherUtilisateur (login);
656
657 if (position != -1) {
658 return this.listeGestionnaire.get (position);
659 }
660 throw new LoginIntrouvableException ();
661 }
662
663
664
665
666
667
668
669
670
671
672
673
674 public GestionnaireConnexion connexion_directe_refuse (final String login) throws LoginIntrouvableException {
675 final int position = this.chercherUtilisateur (login);
676
677 if (position != -1) {
678 return this.listeGestionnaire.get (position);
679 }
680 throw new LoginIntrouvableException ();
681 }
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697 public void creerCanal (final String nom, final String sujet) throws CanalExistantException, NbCanauxMaxException {
698 if (this.canalExistant (nom) != -1) {
699 throw new CanalExistantException ();
700 }
701 if (this.nbCanaux >= Serveur.CANAUX_MAX) {
702 throw new NbCanauxMaxException ();
703 }
704 final int i = this.premierNull (this.listeCanaux);
705
706 this.listeCanaux.get (i).initialiser (nom, sujet);
707 this.nbCanaux++;
708 Serveur.logServeur.logMessage ("Nouveau canal créé s'appellant " + nom + " et ayant pour sujet " + sujet);
709 }
710
711
712
713
714
715
716
717
718
719 public void deconnexion (final int num_gc) {
720 Serveur.logCoDeco.logMessage ("Déconnexion de " + this.listeGestionnaire.get (num_gc).getClient ().getLogin ());
721 this.listeGestionnaire.get (num_gc).vider ();
722 this.nbGC--;
723 }
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743 public void ejecterUtilisateur (final String login, final String nom_canal) throws CanalInexistantException, LoginIntrouvableException, NonConnecteException, IOException {
744 final int place = this.chercherUtilisateur (login);
745
746 if (place != -1) {
747 final int tmp = this.canalExistant (nom_canal);
748
749 if (tmp != -1) {
750 this.listeCanaux.get (tmp).notifier (Evenement.EJECTE, login, "");
751 this.listeCanaux.get (tmp).enleverGC (login);
752 this.listeGestionnaire.get (place).enleverCanal (nom_canal);
753 Serveur.logCoDeco.logMessage (login + " a été éjecté du canal " + nom_canal);
754 }
755 else {
756 throw new CanalInexistantException ();
757 }
758 }
759 else {
760 throw new LoginIntrouvableException (login + " n'est pas connecté");
761 }
762 }
763
764
765
766
767
768
769
770
771
772 public static String getIp () {
773 String s = "";
774
775 if (Serveur.isConnecteInternet ()) {
776 URL u = null;
777 InputStream i = null;
778
779 try {
780 u = new URL ("http://mon-ip.com/"); //$NON-NLS-1$
781 i = u.openStream();
782
783 BufferedReader b = new BufferedReader (new InputStreamReader (i));
784
785 s = b.readLine();
786 while (s != null) {
787 if (s.contains ("Votre adresse IP est")) {
788 s = s.substring (65, 77);
789 break;
790 }
791 s = b.readLine();
792 }
793 }
794 catch (MalformedURLException e) {
795 Serveur.logServeur.logMessage ("URL mal formée");
796 Serveur.logServeur.logException ("Serveur", "getIp", e);
797 }
798 catch (IOException e) {
799 Serveur.logServeur.logMessage ("Problème d'entrée sortie");
800 Serveur.logServeur.logException ("Serveur", "getIp", e);
801 }
802
803 return s;
804 }
805
806 Enumeration<NetworkInterface> interfaces = null;
807
808 try {
809 interfaces = NetworkInterface.getNetworkInterfaces();
810 int i = 0;
811
812 while (interfaces.hasMoreElements ()) {
813 NetworkInterface currentInterface = interfaces.nextElement ();
814 Enumeration<InetAddress> addresses = currentInterface.getInetAddresses ();
815
816 while (addresses.hasMoreElements ()) {
817 i++;
818 InetAddress currentAddress = addresses.nextElement ();
819
820 if (i == 3) {
821 s = currentAddress.getHostAddress ().toString ();
822 }
823 }
824 }
825 }
826 catch (SocketException e) {
827 Serveur.logServeur.logMessage ("Problème d'entrée sortie");
828 Serveur.logServeur.logException ("Serveur", "getIp", e);
829 }
830
831 return s;
832 }
833
834
835
836
837
838
839
840
841 public ArrayList<Canal> getListeCanaux () {
842 return this.listeCanaux;
843 }
844
845
846
847
848
849
850
851
852
853
854
855
856 public String informationMembre (final String login) throws LoginIntrouvableException {
857 final int i = this.chercherUtilisateur (login);
858
859 if (i != -1) {
860 return login + GestionnaireConnexion.CR + this.listeGestionnaire.get (i).infoCanaux ();
861 }
862 throw new LoginIntrouvableException (login + " n'est pas connecté");
863 }
864
865
866
867
868
869
870
871
872
873 public void initialiser () throws IOException {
874 this.socket.setReuseAddress (true);
875 this.nbGC = 0;
876 this.nbCanaux = 0;
877 this.listeGestionnaire = null;
878 this.listeCanaux = null;
879 this.listeGestionnaire = new ArrayList<GestionnaireConnexion> (Serveur.CONNEXION_MAX);
880 this.listeCanaux = new ArrayList<Canal> (Serveur.CANAUX_MAX);
881 this.conn = null;
882 this.lance = false;
883
884 for (int i = 0;i < Serveur.CONNEXION_MAX;i++) {
885 this.listeGestionnaire.add (new GestionnaireConnexion ());
886 }
887 for (int i = 0;i < Serveur.CANAUX_MAX;i++) {
888 this.listeCanaux.add (new Canal ());
889 }
890 }
891
892
893
894
895
896
897
898
899 public static boolean isConnecteInternet ()
900 {
901 try {
902 URL url = new URL ("http://www.google.com"); //$NON-NLS-1$
903 HttpURLConnection urlConnect = (HttpURLConnection)url.openConnection ();
904 Object objData = urlConnect.getContent ();
905 objData.toString ();
906 }
907 catch (MalformedURLException e) {
908 Serveur.logServeur.logMessage ("URL mal formée");
909 Serveur.logServeur.logException ("Serveur", "isConnecteInternet", e);
910
911 return false;
912 }
913 catch (IOException e) {
914 Serveur.logServeur.logMessage ("Non connecté à internet");
915 Serveur.logServeur.logException ("Serveur", "isConnecteInternet", e);
916
917 return false;
918 }
919
920 return true;
921 }
922
923
924
925
926
927
928
929
930
931
932
933
934 public void lancer () throws ClassNotFoundException, RuntimeException {
935 this.lance = true;
936 Serveur.logServeur.logMessage ("Lancement du serveur");
937
938 try {
939 this.connexion ();
940 }
941 catch (final ClassNotFoundException e) {
942 this.lance = false;
943 throw e;
944 }
945 while (this.lance) {
946 Socket clientSocket = null;
947
948 try {
949 clientSocket = this.socket.accept ();
950 Serveur.configuration (this.nbGC, this.nbCanaux);
951
952 Serveur.logCoDeco.logMessage ("Connexion de " + clientSocket.getInetAddress ().toString ().substring (1));
953 }
954 catch (final IOException e) {
955 if (this.lance) {
956 Serveur.logServeur.logMessage ("Connexion echoue sur le port: 8888");
957 Serveur.logServeur.logException ("Serveur", "lancer", e);
958 }
959 }
960 if (this.lance) {
961 if (this.nbGC == Serveur.CONNEXION_MAX) {
962 GestionnaireConnexion gc = new GestionnaireConnexion ();
963 gc.initialisationGestionnaire (clientSocket, this, 0);
964 final Thread t = new Thread (gc);
965 if (t.isAlive ()) {
966 Serveur.logCoDeco.logMessage ("Déconnexion de " + gc.getIp () + " à cause d'un trop grand nombre de connectés");
967 gc.stopper (Serveur.MSG_CONNEXION_MAX);
968 gc = null;
969 }
970 }
971 else {
972 this.ajouterGC (clientSocket);
973 }
974 }
975 }
976 }
977
978
979
980
981
982
983
984
985
986
987
988
989
990 public String listerCanaux (final String nomCanal) throws CanalInexistantException {
991 final StringBuffer res = new StringBuffer ();
992 boolean trouve = false;
993 int i = 0;
994
995 if (!nomCanal.equals ("")) {
996 while (i < this.listeCanaux.size ()) {
997 if (!this.listeCanaux.get (i).estVide ()) {
998 if (this.listeCanaux.get (i).getNom ().equals (nomCanal)) {
999 trouve = true;
1000 break;
1001 }
1002 }
1003 i++;
1004 }
1005 if (trouve) {
1006 res.append (this.listeCanaux.get (i).getSignature ());
1007 }
1008 else {
1009 throw new CanalInexistantException ();
1010 }
1011 }
1012 else {
1013 int k = 0;
1014
1015 for (int j = 0;j < this.listeCanaux.size ();j++) {
1016 if (!this.listeCanaux.get (j).estVide ()) {
1017 res.append (this.listeCanaux.get (j).getSignature ());
1018 if (k != this.nbCanaux - 1) {
1019 res.append (GestionnaireConnexion.CR);
1020 }
1021 k++;
1022 }
1023 }
1024 }
1025
1026 return res.toString ();
1027 }
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038 public String listerMembres (final String nom_canal) {
1039 final Canal canal = this.listeCanaux.get (this.canalExistant (nom_canal));
1040
1041 return canal.listerMembre ();
1042 }
1043
1044
1045
1046
1047
1048
1049
1050
1051 public String listerServeur () {
1052 final StringBuffer res = new StringBuffer ();
1053 int k = 0;
1054 boolean isNull = false;
1055
1056 for (int i = 0;i < this.listeGestionnaire.size ();i++) {
1057 if (!this.listeGestionnaire.get (i).estVide ()) {
1058 if (this.listeGestionnaire.get (i).getClient ().getLogin () != null) {
1059 isNull = true;
1060 if ((k != this.nbGC) && isNull && (k != 0)) {
1061 res.append (GestionnaireConnexion.CR);
1062 }
1063 res.append (this.listeGestionnaire.get (i).getClient ().getLogin () + " " + this.listeGestionnaire.get (i).getIp ());
1064 }
1065
1066 k++;
1067 }
1068 }
1069
1070 return res.toString ();
1071 }
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086 private String md5 (final String key) throws NoSuchAlgorithmException {
1087 final byte[] uniqueKey = key.getBytes ();
1088 byte[] hash = null;
1089 hash = MessageDigest.getInstance ("MD5").digest (uniqueKey);
1090 final StringBuffer hashString = new StringBuffer ();
1091
1092 for (int i = 0;i < hash.length;++i) {
1093 final String hex = Integer.toHexString (hash[i]);
1094
1095 if (hex.length () == 1) {
1096 hashString.append ('0');
1097 hashString.append (hex.charAt (hex.length () - 1));
1098 }
1099 else {
1100 hashString.append (hex.substring (hex.length () - 2));
1101 }
1102 }
1103
1104 return hashString.toString ();
1105 }
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116 private int premierNull (final ArrayList<? extends Vide> liste) {
1117 int res = -1;
1118
1119 for (int i = 0;i < liste.size ();i++) {
1120 if (liste.get (i).estVide ()) {
1121 res = i;
1122 break;
1123 }
1124 }
1125
1126 return res;
1127 }
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142 public void relancerServeur (final String message) throws IOException, RuntimeException {
1143 for (int i = 0;i < this.listeGestionnaire.size ();i++) {
1144 if (!this.listeGestionnaire.get (i).estVide ()) {
1145 this.listeGestionnaire.get (i).setLance (false);
1146 this.listeGestionnaire.get (i).stopper (message);
1147 }
1148 this.listeGestionnaire.remove (i);
1149 }
1150 for (int i = 0;i < this.listeCanaux.size ();i++) {
1151 if (!this.listeCanaux.get (i).estVide ()) {
1152 this.listeCanaux.get (i).vider ();
1153 }
1154 this.listeCanaux.remove (i);
1155 }
1156 Serveur.logServeur.logMessage ("Relancement du serveur");
1157 this.initialiser ();
1158 try {
1159 this.lancer ();
1160 }
1161 catch (final ClassNotFoundException e) {
1162 Serveur.logServeur.logException ("Serveur", "relancerServeur", e);
1163 throw new RuntimeException ();
1164 }
1165 }
1166
1167
1168
1169
1170
1171
1172
1173
1174 public String requeteMOTD () {
1175 Serveur.configuration (this.nbGC, this.nbCanaux);
1176
1177 return Serveur.MOTD;
1178 }
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191 public void stopperServeur (final String message) throws IOException {
1192 for (int i = 0;i < this.listeGestionnaire.size ();i++) {
1193 if (!this.listeGestionnaire.get (i).estVide ()) {
1194 this.listeGestionnaire.get (i).setLance (false);
1195 this.listeGestionnaire.get (i).stopper (message);
1196 }
1197 else {
1198 this.listeGestionnaire.remove (i);
1199 }
1200 }
1201 for (int i = 0;i < this.listeCanaux.size ();i++) {
1202 if (!this.listeCanaux.get (i).estVide ()) {
1203 this.listeCanaux.get (i).vider ();
1204 }
1205 this.listeCanaux.remove (i);
1206 }
1207
1208 this.nbGC = 0;
1209 this.nbCanaux = 0;
1210 this.listeCanaux = null;
1211 this.listeGestionnaire = null;
1212 this.lance = false;
1213
1214 try {
1215 this.conn.close ();
1216 }
1217 catch (final SQLException e) {
1218 Serveur.logServeur.logException ("Serveur", "stopperServeur", e);
1219 }
1220 finally {
1221 this.socket.close ();
1222 Serveur.logServeur.logMessage ("Arrêt du serveur");
1223 Serveur.logCanaux.fermer ();
1224 Serveur.logCoDeco.fermer ();
1225 Serveur.logServeur.fermer ();
1226 }
1227 }
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240 public void supprimerCanal (final String nom) throws CanalInexistantException {
1241 final int tmp = this.canalExistant (nom);
1242
1243 if (tmp != -1) {
1244 try {
1245 this.listeCanaux.get (tmp).notifier (Evenement.SUPPRIME, null, null);
1246 }
1247 catch (final IOException e) {
1248 Serveur.logServeur.logMessage ("Erreur d'entrée sortie");
1249 Serveur.logServeur.logException ("Serveur", "supprimerCanal", e);
1250 }
1251 this.listeCanaux.get (tmp).vider ();
1252 this.listeCanaux.remove (tmp);
1253 this.nbCanaux--;
1254 Serveur.logCanaux.logMessage ("Le canal " + nom + " a été supprimé");
1255 }
1256 else {
1257 throw new CanalInexistantException ();
1258 }
1259 }
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280 public void verifierLoginMdp (final String login, final String mdp, final Utilisateur client) throws SQLException, IdentifierException, NoSuchAlgorithmException {
1281 boolean bool = false;
1282
1283 for (int i = 0;i < this.listeGestionnaire.size ();i++) {
1284 if (login.equals (this.listeGestionnaire.get (i).getClient ().getLogin ())) {
1285 bool = true;
1286 }
1287 }
1288 if (!bool) {
1289
1290 final PreparedStatement state = this.conn.prepareStatement ("SELECT * FROM utilisateurs WHERE identifiant = ? AND active = '1'");
1291 state.setString (1, login);
1292
1293 final ResultSet result = state.executeQuery ();
1294
1295 if (!result.next () || !result.getString ("mdp").equals (this.md5 (mdp))) {
1296 result.close ();
1297 state.close ();
1298 throw new IdentifierException ();
1299 }
1300 Serveur.logCoDeco.logMessage ("Identification " + login + " reussie");
1301 client.setLogin (login);
1302 client.setAdministrateur (result.getInt ("droits") == 0);
1303 result.close ();
1304 state.close ();
1305 }
1306 else {
1307 throw new IdentifierException ();
1308 }
1309 }
1310 }