Advertisement
Guest User

Untitled

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