Advertisement
Guest User

Untitled

a guest
Apr 27th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.49 KB | None | 0 0
  1. package com.usthb.modeles;
  2. /*
  3. import java.io.IOException;
  4. import java.net.ServerSocket;
  5. import java.net.Socket;*/
  6. import java.io.IOException;
  7. import java.io.ObjectInputStream;
  8. import java.io.ObjectOutputStream;
  9. import java.net.ServerSocket;
  10. import java.net.Socket;
  11. import java.util.ArrayList;
  12. import java.util.HashMap;
  13.  
  14. public class ServeurMiniFacebook {
  15.  
  16.  
  17. //TOUS LES ABONNES
  18. private static HashMap<String,Abonne> baseAbonnes= new HashMap<String,Abonne>(); //la cl� est le username de l'abonn�
  19.  
  20. //TOUS LES GROUPES
  21. private static HashMap<String,Groupe> baseGroupes= new HashMap<String,Groupe>(); //la cl� est le nom du groupe
  22.  
  23. //TOUS LES POSTS
  24. private static HashMap<Integer,Post> basePosts= new HashMap<Integer,Post>();//la clé est la le codePost
  25.  
  26. //methode qui retourne l'abonné à partir d'un username
  27. public static Abonne getAbonne(String username) {
  28. return baseAbonnes.get(username);
  29. }
  30.  
  31. //methode qui retourne le groupe à partir d'un groupName
  32. public static Groupe getGroupe(String groupName) {
  33. return baseGroupes.get(groupName);
  34. }
  35.  
  36. //methode qui retourne le poste à partir du code post
  37.  
  38. public static Post getPost(Integer codePost) {
  39. return basePosts.get(codePost);
  40. }
  41.  
  42.  
  43. //SOME TESTS
  44. public static boolean userExiste(String username) {
  45. return baseAbonnes.containsKey(username);
  46.  
  47. }
  48.  
  49. public static boolean groupeExiste(String groupName) {
  50. return baseGroupes.containsKey(groupName);
  51. }
  52.  
  53. public static boolean postExiste(int codePost) {
  54. return baseGroupes.containsKey(codePost);
  55. }
  56.  
  57.  
  58.  
  59.  
  60.  
  61. /***************REQUETES*****************/
  62.  
  63.  
  64.  
  65. /************ABONNES***********/
  66.  
  67. public static Abonne inscrireAbonne(Abonne abo){ //MAJ : ENVOYER PARAM ABONNE ET LE CREER ICI
  68. baseAbonnes.put(abo.getUsername(),abo);
  69. return abo;
  70. }
  71.  
  72.  
  73.  
  74. public static void supprimerAbonne(String user) {//d�sactiver le compte
  75.  
  76. // TROUVER SOLUTION
  77. // supprimerTousLesAmis(abo);
  78. baseAbonnes.remove(user);
  79. }
  80.  
  81.  
  82.  
  83.  
  84. public static void monMur(String user) { //afficher le mur
  85. getAbonne(user).afficherMur("privé");
  86. }
  87.  
  88.  
  89.  
  90.  
  91. public static void mesNotifications(String user) {//afficher les notifications
  92. getAbonne(user).afficherNotifications();
  93. }
  94.  
  95.  
  96.  
  97. public static void mesInvitations(String user) { //afficher les invitations
  98. getAbonne(user).afficherInvitations();
  99. }
  100.  
  101. public static void quitterGroupe(String user,String grp){
  102. Abonne abo=getAbonne(user);
  103. if(groupeExiste(grp)) {
  104. getGroupe(grp).supprimerMembre(abo);
  105. abo.quitterGroupe(grp);
  106. }
  107. }
  108.  
  109. public static void rejoindreGroupe(String user,String grp){
  110. Abonne abo=getAbonne(user);
  111. if(groupeExiste(grp)) {
  112. getGroupe(grp).ajouterMembre(abo);
  113. abo.ajouterGroupe(grp);
  114. }
  115. }
  116.  
  117.  
  118.  
  119. public static void ajouter(String user,String ami,String message) {//ajouter qlqn
  120. Abonne abo=getAbonne(ami);
  121. if(abo!=null){
  122. abo.ajouterInvitation(new Invitation(user,message) );
  123. }
  124. }
  125.  
  126. public static void refuserInvitation(String user1,String user2){
  127. getAbonne(user1).refuserInvitation(user2);
  128. }
  129.  
  130.  
  131. public static void accepterInvitation(String user1,String user2){
  132.  
  133. Abonne abo1=getAbonne(user1),
  134. abo2=getAbonne(user2);
  135. abo1.accepterInvitation(user2);
  136. abo1.ajouterAmi(abo2);
  137. abo2.ajouterAmi(abo1);
  138. notifier(abo2,user1," votre invitation.","invitation");
  139.  
  140.  
  141. }
  142. /*
  143. public static void supprimerInvitation(String user,Invitation inv){
  144. Abonne abo=getAbonne(user);
  145. ArrayList invits;
  146. invits = inv.getRecepteur().getInvitations();
  147. invits.remove(inv);
  148.  
  149. }
  150. */
  151.  
  152.  
  153. public static void supprimerAmi(String abo,String ami){//supprimer qlqn de sa liste d'ami
  154. getAbonne(abo).supprimerAmi(ami);
  155. }
  156.  
  157.  
  158. public static void supprimerTousLesAmis(String user){
  159. getAbonne(user).supprimerTousLesAmis();
  160. }
  161.  
  162.  
  163.  
  164. /************GROUPES************/
  165.  
  166. public static Groupe creerGroupe(String createur,String groupName){
  167. Groupe grp=new Groupe(createur,groupName);
  168. baseGroupes.put(groupName, grp);
  169. return grp;
  170. }
  171.  
  172.  
  173. public static void supprimerGroupe(String grp) {
  174. baseGroupes.remove(grp);
  175. }
  176.  
  177.  
  178.  
  179.  
  180. public static void afficherGroupe(String user) {//affiche tous les groupes o� l'abonn� est membre
  181. getAbonne(user).afficherGroupes();
  182. }
  183.  
  184.  
  185.  
  186.  
  187. public static void afficherMur(String user,Groupe grp) {
  188.  
  189. if(grp.membreExiste(user))
  190. grp.afficherMur("groupe");
  191. else
  192. grp.afficherMur("publique");
  193. }
  194.  
  195.  
  196. /*************POSTES*************/
  197. public static Post creerPost(String poster,String contenu, String visibilite){
  198. Post p=new Post(getAbonne(poster), contenu, visibilite);
  199. basePosts.put(p.getCodePost(), p);
  200. return p;
  201. }
  202.  
  203. //connexion & déconnexion
  204.  
  205. public static boolean checkPassword(String user,String password){
  206. return getAbonne(user).getPassword().equals(password);
  207.  
  208. }
  209.  
  210. public static void seConnecter(String user) {
  211. getAbonne(user).setOnline(true);
  212. }
  213.  
  214. public static void seDeconnecter(String user) {
  215. getAbonne(user).setOnline(false);
  216. }
  217.  
  218. public static void notifier(Abonne recepteur ,String emetteur,String information,String type){
  219. recepteur.ajouterNotification(new Notification(emetteur,new StringBuilder(information),type));
  220. }
  221.  
  222. public static void alerterAmis(Abonne abo){
  223. HashMap<String,Abonne> listeAmis = abo.getListeAmis();
  224. for(Abonne receveur: listeAmis.values()) {
  225. notifier(receveur,abo.getUsername()," un nouveau statut.","publication");
  226. }
  227. }
  228.  
  229. //PUBLIER SUR MUR
  230.  
  231. public static void publier(String user,String contenu,String visibilite) {
  232. Abonne abo=getAbonne(user);
  233. Post p=creerPost(user,contenu,visibilite);
  234. abo.ajouterPost(p); //ajouter le post au mur de celui qui l'a post�
  235. if(!visibilite.toLowerCase().equals("privé")) {
  236. alerterAmis(abo); //alerter les amis d'abo de la publication d'un certain contenu
  237. abo.ajouterActuAmis(p);
  238. }
  239. }
  240.  
  241. //PUBLIER DANS UN GROUPE
  242.  
  243. public static void publier(String user,String contenu, String grp,String visibilite) {//publier dans groupe
  244.  
  245. Groupe g=getGroupe(grp);
  246. if(g!=null && g.membreExiste(user))
  247. {
  248. Post p =creerPost(user,contenu,visibilite);
  249. g.ajouterPost(p);
  250.  
  251. g.alerterMembres(getAbonne(user),contenu,"publication"); //alerter les membres du groupe
  252. }
  253. }
  254.  
  255. //METHODES EPINGLER (EN CHOISIR 2)
  256.  
  257.  
  258. public static void epingler(int codePost) {
  259. getPost(codePost).setEpinglee(true);
  260. //ça peut servir à l'afficher en premier
  261. }
  262. public static void desepingler(int codePost) {
  263. getPost(codePost).setEpinglee(false);
  264. }
  265.  
  266. public static void rechercher(String chercheur,String recherche) {
  267. Abonne abo1=getAbonne(chercheur),
  268. abo2=getAbonne(recherche);
  269. String visibilite= "publique";
  270. if(abo1 != null) {
  271. // POUR SAVOIR QUI RECHERCHE QUI POUR POUVOIR CONNAITRE LA VISIBILITE ACCORDEE
  272. if(chercheur.equals(recherche)) visibilite= "privé";
  273. else if(abo2.sontAmis(abo1)) visibilite= "amis";
  274. else if(abo2.ontAmisCommuns(abo1)) visibilite= "listeamis";
  275. }
  276. abo2.afficherMur(visibilite);
  277.  
  278. }
  279.  
  280. public static void rechercherGroupe(String user,String recherche){
  281. Groupe grp = getGroupe(recherche);
  282. if(grp!=null){
  283. if(grp.membreExiste(user)){
  284. //ICI ON DEVRAIT AFFICHER TOUTES LES PUBLICATIONS
  285. afficherMur(user,grp);
  286. }
  287. }
  288. }
  289.  
  290. public static void reagir(String user,String react,int codePost) {//abo => abonné ayant r�agi, poster=>abonné ayant posté le post p
  291. Post p = getPost(codePost);
  292. Abonne abo=getAbonne(user);
  293. p.ajouterReaction(abo.getUsername(),react); //ajouter la réaction à l'ensemble des réactions au post
  294. if(!abo.getUsername().equals(p.getPoster().getUsername())) //if(abo==poster) il réagi à sa propre publication (habes)
  295. notifier(p.getPoster(),abo.getUsername()," à votre publication.","reaction");//alerter le poster seulement de la réaction d'abo à son post
  296.  
  297. }
  298.  
  299.  
  300. //METHODES COMMENTAIRES
  301.  
  302. public static void commenter(String user,int codePost,String comment) { //abo => abonn� qui va commenter, poster=>abonn� qui a post� le post p
  303. Abonne abo=getAbonne(user);
  304. Post p=getPost(codePost);
  305. if(p!=null){
  306. p.ajouterCommentaire(new Commentaire(abo.getUsername(),new StringBuilder(comment)));
  307.  
  308. //alerter le poster si ce n'est pas lui qui a commenté a publication
  309. if(!p.getPoster().getUsername().equals(abo.getUsername())) {
  310. notifier(p.getPoster(),abo.getUsername()," votre publication.","commentaire");
  311. }
  312. }
  313. }
  314.  
  315. //supprimer un commentaire
  316.  
  317. public static void supprimerCommentaire(String user,int codePost,Commentaire comment) { //commentaire de user
  318. Post p = getPost(codePost);
  319. if(user.equals(p.getPoster().getUsername()) || comment.getUser().equals(user))
  320. p.supprimerCommentaire(comment.getCode());
  321.  
  322. }
  323.  
  324. //partager dans un groupe
  325.  
  326. public static void partager(String user,int codePost,String grp,String visibilite) {
  327. Post p=getPost(codePost);
  328. Groupe g = getGroupe(grp);
  329. if(g!=null && g.membreExiste(user)) //abo doit etre membre du groupe
  330. publier(user,"a partagé la publication de "
  331. +p.getPoster().getUsername()+"\n"+p.getContenu(),grp,visibilite);
  332. }
  333. //partager un post
  334.  
  335.  
  336. public static void partager(String user,int codePost, String visibilite) {
  337. Post p = getPost(codePost);
  338. publier(user,"a partagé la publication de "
  339. +p.getPoster().getUsername()+"\n"+p.getContenu(),visibilite);
  340. }
  341.  
  342. public static void lireNotification(String user,int indice){
  343. ArrayList<Notification> notifs= getAbonne(user).getNotifications();
  344. System.out.println("l'indice est"+ indice);
  345. notifs.get(indice).setEtat(true);
  346. System.out.println(notifs.get(indice).getEtat());
  347. }
  348.  
  349.  
  350. //////////////////////////////////LE MAIN//////////////////////////////////////////////
  351.  
  352.  
  353. public static void main(String args[]) throws IOException, ClassNotFoundException{
  354. Abonne sfn= new Abonne("Zeghoud","Sofiane","20/05/1998","homme","sofman","imene","","","");
  355. Abonne imn= new Abonne("Kerboua","Imene","30/05/1999","femme","minette","sofiane","","","");
  356. inscrireAbonne(sfn);
  357. inscrireAbonne(imn);
  358. ajouter("sofman","minette","cc cv");
  359. accepterInvitation("minette","sofman");
  360. publier("minette","Ceci est pour le publique","publique");
  361. publier("minette","Ceci est pour les amis de mes amis","listeamis");
  362. publier("minette","Ceci est pour mes amis","amis");
  363. publier("minette","Ceci est pour moi-même","privé");
  364. reagir("sofman","jaime",2);
  365. reagir("sofman","jadore",1);
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379. ServerSocket socket=null;
  380. Socket serveur=null;
  381. ObjectInputStream in=null;
  382. ObjectOutputStream out=null;
  383.  
  384. String username,password,ami,message;
  385. int indice;
  386. boolean check;
  387.  
  388. while(true){
  389. try{
  390. socket=new ServerSocket(2018);
  391. serveur = socket.accept();
  392. out=new ObjectOutputStream(serveur.getOutputStream());
  393. in=new ObjectInputStream(serveur.getInputStream());
  394.  
  395. byte choix;
  396.  
  397. choix = in.readByte();
  398. switch(choix){
  399.  
  400. case 1 : //LA CONNEXION
  401. username=(String)in.readObject();
  402.  
  403. check=userExiste(username);
  404. out.writeBoolean(check);
  405. out.flush();
  406. if(!check) break;
  407.  
  408.  
  409. password=(String) in.readObject();
  410.  
  411. check=checkPassword(username,password);
  412. out.writeBoolean(check);
  413. out.flush();
  414. if(!check) break;
  415.  
  416. seConnecter(username);
  417. break;
  418.  
  419. case 2: //L'INSCRIPTION
  420. username=(String) in.readObject();
  421.  
  422. check = !userExiste(username);
  423. out.writeBoolean(check);
  424. out.flush();
  425. if(!check) break;
  426.  
  427. inscrireAbonne((Abonne) in.readObject());
  428. break;
  429.  
  430. case 3: //LA DECONNEXION
  431. username=(String) in.readObject();
  432. seDeconnecter(username);
  433. break;
  434.  
  435. case 4: //ENVOYER LA LISTE DE NOTIFICATIONS
  436. username=(String) in.readObject();
  437. out.writeObject(getAbonne(username).getNotifications());
  438. out.flush();
  439. break;
  440.  
  441. case 5: //ENVOYER LA LISTE D'INVITATIONS
  442. username=(String) in.readObject();
  443. out.writeObject(getAbonne(username).getInvitations());
  444. out.flush();
  445. break;
  446.  
  447. case 6: //AJOUTER
  448. username=(String) in.readObject();
  449. ami=(String) in.readObject();
  450. message=(String) in.readObject();
  451. ajouter(username,ami,message);
  452. break;
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462. }
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.  
  470.  
  471. }catch(IOException e){
  472. System.out.println(e+" Impossible de se connecter");
  473. }finally{
  474. in.close();
  475. out.close();
  476. socket.close();
  477. serveur.close();
  478.  
  479. }
  480.  
  481.  
  482.  
  483. }
  484.  
  485. }
  486.  
  487. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement