diff --git a/POO/src/communication/Communication.java b/POO/src/communication/Communication.java new file mode 100644 index 0000000..a1a63e6 --- /dev/null +++ b/POO/src/communication/Communication.java @@ -0,0 +1,82 @@ +package communication; + +import java.net.UnknownHostException; +import java.util.ArrayList; +import java.util.List; + +import main.Utilisateur; +import main.VueSession; + +public class Communication extends Thread{ + protected static ArrayList users = new ArrayList(); + + 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; + } + + //TODO + //Combiner add et change + protected static synchronized void addUser(List 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)); + + } + } + + protected static synchronized void changePseudoUser(List 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); + + } + } + + protected static synchronized void removeUser(List datas) { + String idClient = datas.get(0); + int index = Communication.getIndexFromID(idClient); + //System.out.println(index); + if( index != -1) { + Communication.users.remove(index); + } + } + + public static void removeAll(){ + int oSize = Communication.users.size(); + for(int i=0; i portOthers; + + public CommunicationUDP(int portClient, int portServer, int[] portsOther) throws IOException { + this.portServer = portServer; + this.portOthers = this.getArrayListFromArray(portsOther); + new UDPServer(portServer, this).start(); + this.client = new UDPClient(portClient); + } + + private ArrayList getArrayListFromArray(int ports[]) { + ArrayList tmp = new ArrayList(); + for (int port : ports) { + tmp.add(port); + } + tmp.remove(Integer.valueOf(portServer)); + + return tmp; + } + + + public void sendMessageConnecte() throws UnknownHostException, IOException { + for(int port : this.portOthers) { + this.client.sendMessageUDP_local("first_connection", port, InetAddress.getLocalHost()); + } + } + + + // Send the message "add,id,pseudo" to localhost on all the ports in + // "portOthers" + // This allows the receivers' agent (portOthers) to create an entry with the + // data of this agent + public void sendMessageAdd() throws UnknownHostException, IOException { + this.sendIDPseudo_local("add"); + } + + public void sendMessageAdd(ArrayList portServers) throws UnknownHostException, IOException { + this.sendIDPseudo_local("add", portServers); + } + + // Send the message "modify,id,pseudo" to localhost on all the ports in + // "portOthers" + // This allows the receivers' agent (portOthers) to update the entry + // corresponding to this agent + public void sendMessageModify() throws UnknownHostException, IOException { + this.sendIDPseudo_local("modify"); + } + + // Send the message "del,id,pseudo" to localhost on all the ports in + // "portOthers" + // This allows the receivers' agent (portOthers) to delete the entry + // corresponding to this agent + public void sendMessageDelete() throws UnknownHostException, IOException { + this.sendIDPseudo_local("del"); + } + + // Private function to create the message "[prefix],id,pseudo" + // and send it to localhost on all the ports in "portOthers" + private void sendIDPseudo_local(String prefixe, ArrayList portServers) throws UnknownHostException, IOException { + Utilisateur self = Utilisateur.getSelf(); + String idSelf = self.getId(); + String pseudoSelf = self.getPseudo(); + + if (!pseudoSelf.equals("")) { + + String message = prefixe + "," + idSelf + "," + pseudoSelf; + // A modifier pour créer un objet de type Message + // + // + + for (int port : portServers) { + this.client.sendMessageUDP_local(message, port, InetAddress.getLocalHost()); + } + } + + } + + private void sendIDPseudo_local(String prefixe) throws UnknownHostException, IOException { + this.sendIDPseudo_local(prefixe, this.portOthers); + } + +// private void sendIDPseudo_broadcast(String prefixe) throws UnknownHostException, IOException { +// Utilisateur self = Utilisateur.getSelf(); +// String idSelf = self.getId(); +// String pseudoSelf = self.getPseudo(); +// +// String message = prefixe+","+idSelf + "," + pseudoSelf; +// +// +// this.client.sendMessageUDP_broadcast(message, this.portServer); +// +// } + +// public synchronized void createSenderUDP(int port, Mode mode) throws SocketException { +// new SenderUDP(mode, port).start(); +// } + +} diff --git a/POO/src/communication/UDPClient.java b/POO/src/communication/UDPClient.java new file mode 100644 index 0000000..85c6c5c --- /dev/null +++ b/POO/src/communication/UDPClient.java @@ -0,0 +1,41 @@ +package communication; + +import java.io.IOException; +import java.net.DatagramPacket; +import java.net.DatagramSocket; +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.net.UnknownHostException; + +public class UDPClient { + + private DatagramSocket sockUDP; + private InetAddress broadcast; + + public UDPClient(int port) throws SocketException, UnknownHostException { + this.sockUDP = new DatagramSocket(port); + + InetAddress localHost = InetAddress.getLocalHost(); + NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost); + this.broadcast = networkInterface.getInterfaceAddresses().get(0).getBroadcast(); + } + + + //Send a string message to the specified port on localhost + protected void sendMessageUDP_local(String message, int port, InetAddress clientAddress) throws IOException { + + //A modifier, faire passer un type Message en paramètre + //puis écrire les instructions pour envoyer un Message à traver la socket + + DatagramPacket outpacket = new DatagramPacket(message.getBytes(), message.length(), clientAddress, port); + this.sockUDP.send(outpacket); + + } + +// protected void sendMessageUDP_broadcast(String message, int port) throws IOException{ +// DatagramPacket outpacket = new DatagramPacket(message.getBytes(), message.length(), this.broadcast, port); +// this.sockUDP.send(outpacket); +// } + +} diff --git a/POO/src/communication/UDPServer.java b/POO/src/communication/UDPServer.java new file mode 100644 index 0000000..f91b693 --- /dev/null +++ b/POO/src/communication/UDPServer.java @@ -0,0 +1,77 @@ +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"); + ArrayList portClient = new ArrayList(); + portClient.add(inPacket.getPort()+1); + this.commUDP.sendMessageAdd(portClient); + + } else if (msg.contains("add,")) { + //System.out.println("add"); + ArrayList datas = this.getDatas(inPacket); + Communication.addUser(datas); + + } else if (msg.contains("modify,")) { + ArrayList datas = this.getDatas(inPacket); + Communication.changePseudoUser(datas); + + } else if (msg.contains("del,")) { + ArrayList datas = this.getDatas(inPacket); + Communication.removeUser(datas); + } + + } catch (IOException e) { + System.out.println("receive exception"); + + } + + } + } + + protected ArrayList getDatas(DatagramPacket inPacket) { + //Message + // + + String msg = new String(inPacket.getData(), 0, inPacket.getLength()); + String tmp[] = msg.split(","); + + + + ArrayList datas = new ArrayList(Arrays.asList(tmp)); + datas.remove(0); + datas.add(inPacket.getAddress().toString()); + + return datas; + } + +} diff --git a/POO/src/main/ControleurSession.java b/POO/src/main/ControleurSession.java new file mode 100644 index 0000000..d91f418 --- /dev/null +++ b/POO/src/main/ControleurSession.java @@ -0,0 +1,36 @@ +package main; + +import java.awt.event.ActionEvent; +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.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; + +import communication.Communication; +import communication.CommunicationUDP; + +public class ControleurSession implements ActionListener{ + + + private VueSession vue; + + public ControleurSession(VueSession vue) throws IOException { + this.vue = vue; + } + + + + //---------- ACTION LISTENER OPERATIONS ----------// + @Override + public void actionPerformed(ActionEvent e) { + if ((JButton) e.getSource() == this.vue.getButtonEnvoyer()) { + + } + + } +} + \ No newline at end of file diff --git a/POO/src/main/Main.java b/POO/src/main/Main.java index f3ec08e..111a408 100644 --- a/POO/src/main/Main.java +++ b/POO/src/main/Main.java @@ -1,10 +1,54 @@ package main; +import java.io.IOException; + + + 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) { - // TODO Auto-generated method stub + + switch(args[0]) { + case "0": + Main.createApp(0); + break; + case "1": + 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("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 VueSession("Application"); + } catch (IOException e) { + System.out.println(e.toString()); + } } } diff --git a/POO/src/main/Utilisateur.java b/POO/src/main/Utilisateur.java new file mode 100644 index 0000000..41943a7 --- /dev/null +++ b/POO/src/main/Utilisateur.java @@ -0,0 +1,52 @@ +package main; +import java.io.Serializable; +import java.net.*; + +public class Utilisateur implements Serializable{ + + /** + * + */ + private static final long serialVersionUID = 1L; + + private String id; + private String pseudo; + private InetAddress ip; + + private static Utilisateur self; + + public Utilisateur(String id, String pseudo, String host) throws UnknownHostException { + this.id = id; + this.pseudo = pseudo; + this.ip = InetAddress.getLocalHost(); + //System.out.println(InetAddress.getLocalHost()); + } + + + public String getId() { + return id; + } + + public String getPseudo() { + return pseudo; + } + + public void setPseudo(String pseudo) { + this.pseudo = pseudo; + } + + public InetAddress getIp() { + return ip; + } + + public static void setSelf(String id, String pseudo,String host) throws UnknownHostException { + if(Utilisateur.self == null) { + Utilisateur.self = new Utilisateur(id, pseudo, host); + } + + } + + public static Utilisateur getSelf() { + return Utilisateur.self; + } +} diff --git a/POO/src/main/Vue.java b/POO/src/main/Vue.java new file mode 100644 index 0000000..9e7832d --- /dev/null +++ b/POO/src/main/Vue.java @@ -0,0 +1,15 @@ +package main; + +import javax.swing.JFrame; + +public class Vue extends JFrame{ + + public Vue(String title) { + super(title); + } + + public void reduireAgent() {} + + public void fermerAgent() {} + +} diff --git a/POO/src/main/VueSession.java b/POO/src/main/VueSession.java new file mode 100644 index 0000000..ac8d622 --- /dev/null +++ b/POO/src/main/VueSession.java @@ -0,0 +1,110 @@ +package main; + +import java.awt.*; +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.List; +import java.util.Vector; + +import javax.swing.BorderFactory; +import javax.swing.DefaultListModel; +import javax.swing.JButton; +import javax.swing.JLabel; +import javax.swing.JList; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTabbedPane; +import javax.swing.JTextArea; +import javax.swing.JTextField; +import javax.swing.ListSelectionModel; +import javax.swing.ScrollPaneConstants; + +public class VueSession extends Vue { + + /** + * + */ + private static final long serialVersionUID = 1L; + + + private JButton envoyerMessage; + private ControleurSession c; + + public VueSession(String title) throws IOException { + + super(title); + + JPanel main = new JPanel(new BorderLayout()); + main.setBackground(Color.green); + + + JTextArea chatWindow = new JTextArea(); + + + JScrollPane chatScroll = new JScrollPane(); + chatScroll.setPreferredSize(new Dimension(575, 600)); + chatScroll.setBackground(Color.blue); + + JTextField chatInput = new JTextField("Entrez votre message"); + chatInput.setPreferredSize(new Dimension(575, 150)); + + + + + + + this.c = new ControleurSession(this); + + + + + GridBagConstraints gridBagConstraint = new GridBagConstraints(); + + gridBagConstraint.fill = GridBagConstraints.BOTH; + gridBagConstraint.gridx = 0; + gridBagConstraint.gridy = 0; + gridBagConstraint.gridwidth = 1; + gridBagConstraint.gridheight = 5; + gridBagConstraint.weightx = 0.33; + gridBagConstraint.weighty = 1; + + //main.add(left,gridBagConstraint); + + gridBagConstraint.fill = GridBagConstraints.BOTH; + gridBagConstraint.gridx = 1; + gridBagConstraint.gridy = 0; + gridBagConstraint.gridwidth = 2; + gridBagConstraint.gridheight = 3; + gridBagConstraint.weightx = 0.66; + gridBagConstraint.weighty = 0.66; + + //main.add(chat,gridBagConstraint); + + gridBagConstraint.fill = GridBagConstraints.BOTH; + gridBagConstraint.gridx = 1; + gridBagConstraint.gridy = 3; + gridBagConstraint.gridwidth = 2; + gridBagConstraint.gridheight = 1; + gridBagConstraint.weightx = 0.66; + gridBagConstraint.weighty = 0.33; + + + //main.add(bottom,gridBagConstraint); + + this.add(main); + + this.setSize(900,900); + this.setVisible(true); + + } + + + protected JButton getButtonEnvoyer() { + return this.envoyerMessage; + } + +} diff --git a/POO/src/main/VueTest.java b/POO/src/main/VueTest.java new file mode 100644 index 0000000..ceda8e7 --- /dev/null +++ b/POO/src/main/VueTest.java @@ -0,0 +1,70 @@ +package main; + +import java.awt.BorderLayout; +import java.awt.Dimension; +import java.awt.EventQueue; + +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.border.EmptyBorder; +import java.awt.FlowLayout; +import javax.swing.JTextField; +import javax.swing.JButton; +import java.awt.ScrollPane; +import javax.swing.JTextPane; +import javax.swing.JTabbedPane; + +public class VueTest extends JFrame { + + private JPanel contentPane; + private JTextField txtEntrezUnMessage; + + /** + * Launch the application. + */ + public static void main(String[] args) { + EventQueue.invokeLater(new Runnable() { + public void run() { + try { + VueTest frame = new VueTest(); + frame.setVisible(true); + } catch (Exception e) { + e.printStackTrace(); + } + } + }); + } + + /** + * Create the frame. + */ + public VueTest() { + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setBounds(100, 100, 600, 600); + contentPane = new JPanel(); + contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); + setContentPane(contentPane); + contentPane.setLayout(new BorderLayout(0, 0)); + + JPanel panel = new JPanel(); + contentPane.add(panel, BorderLayout.SOUTH); + panel.setLayout(new BorderLayout(0, 0)); + + txtEntrezUnMessage = new JTextField(); + txtEntrezUnMessage.setText("Entrez un message"); + //textField.setPreferredSize(new Dimension(300, 50)); + panel.add(txtEntrezUnMessage); + txtEntrezUnMessage.setColumns(10); + + JButton btnNewButton = new JButton("Envoyer"); + panel.add(btnNewButton, BorderLayout.EAST); + + JTextPane textPane = new JTextPane(); + + ScrollPane scrollPane = new ScrollPane(); + contentPane.add(scrollPane, BorderLayout.CENTER); + + scrollPane.add(textPane); + } + +}