choix pseudo, modif et déconnexion + comm udp

This commit is contained in:
Cavailles Kevin 2020-11-30 20:42:30 +01:00
parent 1660e85004
commit 539a0f0438
12 changed files with 450 additions and 108 deletions

View file

@ -0,0 +1,74 @@
package communication;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import main.Utilisateur;
import main.VueStandard;
public class Communication extends Thread{
protected static ArrayList<Utilisateur> users = new ArrayList<Utilisateur>();
protected static boolean containsUserFromID(String id) {
for(Utilisateur u : Communication.users) {
if(u.getId().equals(id) ) {
return true;
}
}
return false;
}
public static boolean containsUserFromPseudo(String pseudo) {
for(Utilisateur u : Communication.users) {
if(u.getPseudo().equals(pseudo) ) {
return true;
}
}
return false;
}
protected static int getIndexFromID(String id) {
for(int i=0; i < Communication.users.size() ; i++) {
if(Communication.users.get(i).getId().equals(id) ) {
return i;
}
}
return -1;
}
protected static synchronized void addUser(List<String> datas) throws UnknownHostException {
String idClient = datas.get(0);
String pseudoClient = datas.get(1);
String clientAddress = datas.get(2);
if (!Communication.containsUserFromID(idClient)) {
Communication.users.add(new Utilisateur(idClient, pseudoClient, clientAddress));
VueStandard.userList.addElement(pseudoClient);
}
}
protected static synchronized void changePseudoUser(List<String> datas) {
String idClient = datas.get(0);
String pseudoClient = datas.get(1);
int index = Communication.getIndexFromID(idClient);
System.out.println(index);
if( index != -1) {
Communication.users.get(index).setPseudo(pseudoClient);
VueStandard.userList.set(index, pseudoClient);
}
}
protected static synchronized void removeUser(List<String> datas) {
String idClient = datas.get(0);
int index = Communication.getIndexFromID(idClient);
System.out.println(index);
if( index != -1) {
Communication.users.remove(index);
VueStandard.userList.remove(index);
}
}
}

View file

@ -0,0 +1,67 @@
package communication;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import main.Utilisateur;
public class CommunicationUDP extends Communication {
//public enum Mode {PREMIERE_CONNEXION, CHANGEMENT_PSEUDO, DECONNEXION};
private UDPClient client;
private int portServer;
private ArrayList<Integer> portsOther;
public CommunicationUDP(int portClient, int portServer, int[] portsOther) throws IOException {
this.portServer = portServer;
this.portsOther = this.getArrayListFromArray(portsOther);
new UDPServer(portServer, this).start();
this.client = new UDPClient(portClient);
}
private ArrayList<Integer> getArrayListFromArray(int ports[]){
ArrayList<Integer> tmp = new ArrayList<Integer>();
for(int port : ports) {
tmp.add(port);
}
tmp.remove( Integer.valueOf(portServer) );
return tmp;
}
public void sendMessageConnecte() throws UnknownHostException, IOException {
for(int port : this.portsOther) {
this.client.sendMessageUDP("first_connection", port, InetAddress.getLocalHost());
}
}
public void sendMessageAdd() throws UnknownHostException, IOException { this.sendIDPseudo("add"); }
public void sendMessageModify() throws UnknownHostException, IOException{ this.sendIDPseudo("modify"); }
public void sendMessageDelete() throws UnknownHostException, IOException{ this.sendIDPseudo("del"); }
private void sendIDPseudo(String prefixe) throws UnknownHostException, IOException {
Utilisateur self = Utilisateur.getSelf();
String idSelf = self.getId();
String pseudoSelf = self.getPseudo();
String message = prefixe+","+idSelf + "," + pseudoSelf;
for(int port : this.portsOther) {
this.client.sendMessageUDP(message, port, InetAddress.getLocalHost());
}
}
// public synchronized void createSenderUDP(int port, Mode mode) throws SocketException {
// new SenderUDP(mode, port).start();
// }
}

View file

@ -0,0 +1,24 @@
package communication;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class UDPClient {
private DatagramSocket sockUDP;
private byte[] buffer;
public UDPClient(int port) throws SocketException {
this.sockUDP = new DatagramSocket(port);
this.buffer = new byte[256];
}
protected void sendMessageUDP(String message, int port, InetAddress clientAddress) throws IOException {
DatagramPacket outpacket = new DatagramPacket(message.getBytes(), message.length(), clientAddress, port);
this.sockUDP.send(outpacket);
}
}

View file

@ -0,0 +1,67 @@
package communication;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Arrays;
public class UDPServer extends Thread {
private DatagramSocket sockUDP;
private CommunicationUDP commUDP;
private byte[] buffer;
public UDPServer(int port, CommunicationUDP commUDP) throws SocketException {
this.commUDP = commUDP;
this.sockUDP = new DatagramSocket(port);
this.buffer = new byte[256];
}
@Override
public void run() {
while (true) {
try {
DatagramPacket inPacket = new DatagramPacket(buffer, buffer.length);
this.sockUDP.receive(inPacket);
String msg = new String(inPacket.getData(), 0, inPacket.getLength());
if (msg.equals("first_connection")) {
System.out.println("first co");
this.commUDP.sendMessageAdd();
//new ReceiverUDP(Mode.PREMIERE_CONNEXION, inPacket).start();
} else if (msg.contains("add,")) {
System.out.println("add");
ArrayList<String> datas = this.getDatas(inPacket);
Communication.addUser(datas);
} else if (msg.contains("modify,")) {
ArrayList<String> datas = this.getDatas(inPacket);
Communication.changePseudoUser(datas);
} else if (msg.contains("del,")) {
ArrayList<String> datas = this.getDatas(inPacket);
Communication.removeUser(datas);
}
} catch (IOException e) {
System.out.println("receive exception");
}
}
}
protected ArrayList<String> getDatas(DatagramPacket inPacket) {
String msg = new String(inPacket.getData(), 0, inPacket.getLength());
String tmp[] = msg.split(",");
ArrayList<String> datas = new ArrayList<String>(Arrays.asList(tmp));
datas.remove(0);
datas.add(inPacket.getAddress().toString());
return datas;
}
}

View file

@ -1,9 +0,0 @@
package main;
import java.util.ArrayList;
public class Communication {
protected static ArrayList<Utilisateur> users;
}

View file

@ -1,64 +0,0 @@
package main;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
public class CommunicationUDP extends Thread {
private int port;
private DatagramSocket sockUDP;
private byte[] buffer;
public CommunicationUDP(int port) throws SocketException, UnknownHostException {
this.sockUDP = new DatagramSocket(port);
this.buffer = new byte[256];
}
@Override
public void run() {
while(true) {
DatagramPacket inPacket = new DatagramPacket(buffer, buffer.length);
try {
this.sockUDP.receive(inPacket);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private class ReponseUDP extends Thread{
private DatagramSocket sockUDP;
private DatagramPacket inPacket;
public ReponseUDP(DatagramSocket sockUDP, DatagramPacket inPacket) {
this.sockUDP = sockUDP;
this.inPacket = inPacket;
}
@Override
public void run() {
String msg = new String(this.inPacket.getData(),0,this.inPacket.getLength());
if(msg == "Connecté") {
Utilisateur self = Utilisateur.getSelf();
String id = self.getId();
String pseudo = self.getPseudo();
InetAddress ip = self.getIp();
InetAddress clientAddress = this.inPacket.getAddress();
int clientPort = this.inPacket.getPort();
}
}
}
}

View file

@ -2,19 +2,38 @@ package main;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JList; import javax.swing.JList;
import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener; import javax.swing.event.ListSelectionListener;
public class ControleurStandard implements ActionListener, ListSelectionListener { import communication.Communication;
import communication.CommunicationUDP;
private VueStandard vue; public class ControleurStandard implements ActionListener, ListSelectionListener, WindowListener {
public ControleurStandard(VueStandard vue) { private enum EtatModif {
this.vue = vue; TERMINE, EN_COURS
} }
private EtatModif etatModif;
private VueStandard vue;
private CommunicationUDP commUDP;
private String lastPseudo;
private int clientPort;
public ControleurStandard(VueStandard vue, int portClient, int portServer, int[] portsOther) throws IOException {
this.vue = vue;
this.commUDP = new CommunicationUDP(portClient,portServer, portsOther);
this.commUDP.sendMessageConnecte();
this.commUDP.sendMessageAdd();
this.etatModif = EtatModif.TERMINE;
}
//---------- LISTSELECTION LISTENER OPERATIONS ----------//
@Override @Override
public void valueChanged(ListSelectionEvent e) { public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) { if (!e.getValueIsAdjusting()) {
@ -23,9 +42,92 @@ public class ControleurStandard implements ActionListener, ListSelectionListener
} }
} }
//---------- ACTION LISTENER OPERATIONS ----------//
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if ((JButton) e.getSource() == this.vue.getButtonModifierPseudo()) {
JButton modifierPseudo = (JButton) e.getSource();
if (this.etatModif == EtatModif.TERMINE) {
this.lastPseudo = Utilisateur.getSelf().getPseudo();
modifierPseudo.setText("OK");
this.etatModif = EtatModif.EN_COURS;
} else {
if (!Communication.containsUserFromPseudo(this.vue.getDisplayedPseudo())) {
Utilisateur.getSelf().setPseudo(this.vue.getDisplayedPseudo());
try {
this.commUDP.sendMessageModify();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else {
this.vue.setDisplayedPseudo(this.lastPseudo);
}
modifierPseudo.setText("Modifier");
this.etatModif = EtatModif.TERMINE;
}
this.vue.toggleEditPseudo();
}
}
//---------- WINDOW LISTENER OPERATIONS ----------//
@Override
public void windowClosing(WindowEvent e) {
try {
this.commUDP.sendMessageDelete();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
} }
} }

View file

@ -1,36 +1,54 @@
package main; package main;
import java.net.UnknownHostException; import java.io.IOException;
import javax.swing.JFrame;
public class Main { public class Main {
private static int portServers[] = {1526,1501,1551,1561};
private static String ids[] = {"Raijila", "titi33", "Semtexx", "Salam"};
private static String pseudo[] = {"Raijila", "Mirasio", "Semtexx", "Xaegon"};
public static void main(String[] args) { public static void main(String[] args) {
JFrame frame = new JFrame("Application");
VueStandard vueStd = new VueStandard();
frame.add(vueStd);
frame.setSize(300,300);
frame.setVisible(true); switch(args[0]) {
case "0":
try { Main.createApp(0);
Utilisateur.setSelf("Raijila", "Raijila", "localhost"); break;
} catch (UnknownHostException e1) { case "1":
System.out.println("hote inexistant"); Main.createApp(1);
break;
case "2":
Main.createApp(2);
break;
default:
Main.createApp(3);
} }
VueStandard.userList.addElement("Mirasio");
try {
Thread.sleep(2000); // VueStandard.userList.addElement("Mirasio");
VueStandard.userList.addElement("Semtexx"); //
} catch (InterruptedException e) { // try {
// TODO Auto-generated catch block // Thread.sleep(2000);
e.printStackTrace(); // VueStandard.userList.addElement("Semtexx");
} // } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
} }
private static void createApp(int i) {
try {
Utilisateur.setSelf(Main.ids[i], Main.pseudo[i], "localhost");
new VueStandard("Application", Main.portServers[i]-1, Main.portServers[i], Main.portServers);
} catch (IOException e) {
System.out.println(e.toString());
}
}
} }

13
POO/src/main/Message.java Normal file
View file

@ -0,0 +1,13 @@
package main;
import java.io.Serializable;
public class Message implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String contenu;
}

View file

@ -18,7 +18,8 @@ public class Utilisateur implements Serializable{
public Utilisateur(String id, String pseudo,String host) throws UnknownHostException { public Utilisateur(String id, String pseudo,String host) throws UnknownHostException {
this.id = id; this.id = id;
this.pseudo = pseudo; this.pseudo = pseudo;
this.ip = InetAddress.getByName(host); this.ip = InetAddress.getLocalHost();
System.out.println(InetAddress.getLocalHost());
} }

View file

@ -1,11 +1,11 @@
package main; package main;
import javax.swing.JPanel; import javax.swing.JFrame;
public class Vue extends JPanel{ public class Vue extends JFrame{
public Vue() { public Vue(String title) {
// TODO Auto-generated constructor stub super(title);
} }
public void reduireAgent() {} public void reduireAgent() {}

View file

@ -2,14 +2,23 @@ package main;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.GridLayout; import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Vector; import java.util.Vector;
import javax.swing.BorderFactory; import javax.swing.BorderFactory;
import javax.swing.DefaultListModel; import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList; import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane; import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel; import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants; import javax.swing.ScrollPaneConstants;
@ -21,15 +30,31 @@ public class VueStandard extends Vue {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private JList<String> activeUsersList; private JList<String> activeUsersList;
private JTextField pseudoSelf;
private JButton modifierPseudo;
private ControleurStandard c; private ControleurStandard c;
public static DefaultListModel<String> userList = new DefaultListModel<String>(); public static DefaultListModel<String> userList = new DefaultListModel<String>();
public VueStandard() { public VueStandard(String title, int port, int clientPort, int[] portsOther) throws IOException {
super(); super(title);
this.c = new ControleurStandard(this); JPanel main = new JPanel(new GridLayout(3, 1));
this.setLayout(new GridLayout(2, 1));
this.c = new ControleurStandard(this, port, clientPort, portsOther);
JPanel self = new JPanel(new GridLayout(1, 3));
this.pseudoSelf = new JTextField(Utilisateur.getSelf().getPseudo());
this.pseudoSelf.setEditable(false);
this.modifierPseudo = new JButton("Modifier");
this.modifierPseudo.addActionListener(c);
self.add(new JLabel("Moi : "));
self.add(this.pseudoSelf);
self.add(this.modifierPseudo);
this.activeUsersList = new JList<String>(VueStandard.userList); this.activeUsersList = new JList<String>(VueStandard.userList);
@ -45,12 +70,36 @@ public class VueStandard extends Vue {
BorderFactory.createTitledBorder("Utilisateurs Actifs"), BorderFactory.createTitledBorder("Utilisateurs Actifs"),
BorderFactory.createEmptyBorder(5,2,2,2))); BorderFactory.createEmptyBorder(5,2,2,2)));
this.add(listScroller); main.add(self);
main.add(listScroller);
this.add(main);
this.setSize(350,600);
this.setVisible(true);
this.addWindowListener(c);
} }
public JList<String> getActiveUsersList(){ public JList<String> getActiveUsersList(){
return this.activeUsersList; return this.activeUsersList;
} }
protected JButton getButtonModifierPseudo() {
return this.modifierPseudo;
}
protected String getDisplayedPseudo() {
return this.pseudoSelf.getText();
}
protected void setDisplayedPseudo(String pseudo) {
this.pseudoSelf.setText(pseudo);
}
protected void toggleEditPseudo() {
this.pseudoSelf.setEditable(!this.pseudoSelf.isEditable());
}
} }