Advertisement
Guest User

Untitled

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