Skip to content

Commit

Permalink
Reglage 45047 + ferme la com d'un coté ca marche pas d'erreur mais de…
Browse files Browse the repository at this point in the history
… l'autre la fiche reste ouverte et attend toujours (?)
  • Loading branch information
ElyesKhama committed Dec 23, 2017
1 parent 0233c8b commit e8b344a
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 141 deletions.
56 changes: 7 additions & 49 deletions src/GraphiquePackage/FichAccueil.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,24 @@
package GraphiquePackage;

import SourcePackage.Main;
import SourcePackage.ThreadReceive;
import SourcePackage.User;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashMap;

import static java.lang.Thread.sleep;

/**
*
* @author elyes
*/



public class FichAccueil extends javax.swing.JFrame {

/** * Creates new form FichMenu
*/

private static int i = 0;
private FichMenu menu;


public FichAccueil() {


initComponents();

}
Expand Down Expand Up @@ -105,46 +97,12 @@ private void PseudoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
String a = Pseudo.getText();

Main.user.sendMessageUDP("quiti",Main.user.getPort());
ThreadReceive threadReceive = new ThreadReceive("quiti",Main.user);
threadReceive.start();
System.out.println("J'ai lancé le thread");


System.out.println("hash dans le while");
System.out.println(Main.user.getListUser().toString());

if(i < 1) {
try {
sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
i++;
if(Main.user.getListUser().containsKey(a))
System.out.println("pareil gros");
else if (!Main.user.getListUser().containsKey(a)){
threadReceive = new ThreadReceive("thread",Main.user);
Main.user.removeUserList("quiti");
System.out.println("hash actuelle");
System.out.println(Main.user.getListUser().toString());
Main.user.setPseudo(a);
Main.user.startThread();
menu = new FichMenu(this, false);
this.setVisible(false);
menu.setVisible(true);
Main.setFichMenu(menu);
System.out.println("POOOOOOORT : >>>>>>>" + Main.user.getPort());
}
else if(!Main.user.getListUser().toString().contains("quiti")) {
System.out.println("mmmmh");
}


Main.launchUser(a);
menu = new FichMenu(this, false);
this.setVisible(false);
menu.setVisible(true);

Main.setFichMenu(menu);

}//GEN-LAST:event_jButton1ActionPerformed

Expand Down
1 change: 0 additions & 1 deletion src/GraphiquePackage/FichCom.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public void exitSystem(){
public void windowClosing(java.awt.event.WindowEvent e) {
Main.user.sendMessageUDP("quito",portDest);
System.out.println("jai send le msg" );
System.exit(0);
}
});
}
Expand Down
1 change: 1 addition & 0 deletions src/GraphiquePackage/ThreadPrintListUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public void run(){
try {
sleep(1000);
FichMenu.setListUser(Main.user.getListUser().toString());
System.out.println(Main.user.getListUser().toString());

} catch (InterruptedException ex) {
Logger.getLogger(ThreadPrintListUser.class.getName()).log(Level.SEVERE, null, ex);
Expand Down
11 changes: 4 additions & 7 deletions src/SourcePackage/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@ public class Main {
private static FichMenu fichMenu;

public static void main(String[] args){
user = new User();
fichAccueil = new FichAccueil();
fichAccueil.setVisible(true);
fichAccueil = new FichAccueil();
fichAccueil.setVisible(true);

}



public static void launchUser(User user1){
user = user1;
public static void launchUser(String pseudo){
user = new User(pseudo);
user.startThread();
}

Expand Down
71 changes: 12 additions & 59 deletions src/SourcePackage/ThreadReceive.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,63 +7,19 @@

public class ThreadReceive extends Thread {

private User user;
private static String nama;

public ThreadReceive(String name){
super(name);
}

public ThreadReceive(String name, User usr){
super(name);
this.user = usr;
this.nama = name;
}

public void run(){
try {
if(this.getName().equals("quiti"))
receiveMessage2(user);
else {
receiveMessage();
}
receiveMessage();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void receiveMessage2(User user) throws IOException { //UDP : boucle qui recoit les messages udp broadcast pour voir qui est en ligne

System.out.println("THREAD COURANT :::::: "+Thread.currentThread().toString());


while(Main.user.getPseudo().equals("quiti")){
// System.out.println("Jattends de recevoir un message <<< bloqué" + "temps : " + System.currentTimeMillis());

byte[] recvBuf = new byte[1024];
DatagramPacket recvPacket = new DatagramPacket(recvBuf, recvBuf.length);
try {
System.out.println("wsh tu vas recevoir");
user.getSocketEcoute().receive(recvPacket);
System.out.println("t'as reçu");
} catch (IOException e) {
e.printStackTrace();
}
// System.out.println("Jai recu le message >>> non bloqué ");

String recvStr = new String(recvPacket.getData(), 0, recvPacket.getLength());

InetAddress addr = recvPacket.getAddress();
int port = recvPacket.getPort();
System.out.println("je vais ajouter sur le port : : : : : " + port);

user.setListUser(recvStr,addr.toString().substring(1)+"-"+port);
}
}

public static void receiveMessage() throws IOException { //UDP : boucle qui recoit les messages udp broadcast pour voir qui est en ligne
System.out.println("THREAD COURANT :::::: "+Thread.currentThread().toString());

while(true){
// System.out.println("Jattends de recevoir un message <<< bloqué" + "temps : " + System.currentTimeMillis());

Expand All @@ -88,22 +44,19 @@ public static void receiveMessage() throws IOException { //UDP : boucle qui re
}

else if(recvStr.contains("quito")){
System.out.print("etat du socket : "+ User.getSocketEcoute());
Main.user.removeSocketList(recvPacket.getPort());
// Main.user.removeSocketList(recvPacket.getPort());
}

else {
if( !Main.user.belongList(recvStr)){
InetAddress addr = recvPacket.getAddress();
int port = recvPacket.getPort();
System.out.println("port >>>>>>>> " + port);
sendMessage(addr, port);
if(recvStr.contains("quiti")){}
else {
System.out.println("PPPPOOORRRTTT "+ port);
ajoutUserListe(recvStr,addr.toString().substring(1)+"-"+port);
}
}
if( !Main.user.belongList(recvStr)){
InetAddress addr = recvPacket.getAddress();
int port = recvPacket.getPort();
System.out.println("JE LAI RECU AVEC LE PORT : "+ port );
sendMessage(addr, port);
ajoutUserListe(recvStr,addr.toString().substring(1)+"-"+port);
}
}

}
}

Expand All @@ -119,7 +72,7 @@ public static void sendMessage (InetAddress address, int port) throws IOExceptio
DatagramPacket packet = new DatagramPacket(data.getBytes(),
data.getBytes().length, address, 45047);
//System.out.println("J'ai renvoyé mon paquet apres l'ecoute sur le port : " + 45047);
User.getSocketEcoute().send(packet);
User.getSocketEnvoi().send(packet);
}

public synchronized static void ajoutUserListe(String pseudo, String ip){
Expand Down
41 changes: 16 additions & 25 deletions src/SourcePackage/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,9 @@ public class User {
private HashMap<Integer,Socket> listSocket;
private ArrayList<FichCom> listFichCom;
private PrintWriter writer = null;
private HashMap<String, String> listUser ;
private HashMap<String, String> listUser ; //jai recu sur mon port d'udp mais je veux fermer celui de tcp ? comment recup ce port ??????

public User(int i) {
this.pseudo = String.valueOf(pseudo);
try {

listFichCom = new ArrayList<>();
listUser = new HashMap<>();
listSocket = new HashMap();
this.socketEnvoi = new DatagramSocket();

this.socketEcoute = new DatagramSocket(45047);
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public User(){
this.pseudo = "quiti";
public User(String pseudo){
listFichCom = new ArrayList<>();
listUser = new HashMap<>();
listSocket = new HashMap();
Expand All @@ -53,6 +36,7 @@ public User(){
catch (SocketException e) {
e.printStackTrace();
}
this.pseudo = pseudo;

System.out.println("J'ai crée un utilisateur ; son pseudo est : " + pseudo );
}
Expand All @@ -71,17 +55,24 @@ public void startThread(){
public void sendMessageUDP(String message,int port){
String data = "" ;

if(message != null && message.equals("quiti"))
data = message;
else if(message != null ){
if(message != null){



if(message.contains("quita") ){
data = message+"-"+pseudo;

}
else if(message.contains("quito")){
data = message+"-"+port;
listSocket.remove(port);
}
}

else {
data = Main.user.getPseudo();
}


System.out.println(Main.user);
try {
User.getSocketEnvoi().setBroadcast(true);
Expand Down Expand Up @@ -170,7 +161,7 @@ public HashMap getListUser(){
}

public void setListUser(String pseudo, String ipPort){
System.out.println("PSEUUUUDO : "+ pseudo +" \n IPPORTTT ::: " + ipPort);
System.out.println("JE PUT AVEC PSEUDO : " + pseudo + " ET IP-PORT : " + ipPort);
this.listUser.put(pseudo, ipPort);
}

Expand All @@ -182,6 +173,7 @@ public void removeUserList(String pseudo1){
public void removeSocketList(int port){
System.out.println("J'ai fermé la co !!! ");
try {
System.out.println("PORT QUE JE VAIS FERMER " + port);
listSocket.get(port).close();
} catch (IOException ex) {
Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
Expand Down Expand Up @@ -212,6 +204,5 @@ public void putListSocket(Socket socket){
listSocket.put(socket.getPort(), socket);
}



}

0 comments on commit e8b344a

Please sign in to comment.