Vue principale, liste utilisateurs, sélection, debut udp
This commit is contained in:
parent
e7a2297077
commit
1660e85004
7 changed files with 253 additions and 1 deletions
9
POO/src/main/Communication.java
Normal file
9
POO/src/main/Communication.java
Normal file
|
@ -0,0 +1,9 @@
|
|||
package main;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Communication {
|
||||
protected static ArrayList<Utilisateur> users;
|
||||
|
||||
|
||||
}
|
64
POO/src/main/CommunicationUDP.java
Normal file
64
POO/src/main/CommunicationUDP.java
Normal file
|
@ -0,0 +1,64 @@
|
|||
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();
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
31
POO/src/main/ControleurStandard.java
Normal file
31
POO/src/main/ControleurStandard.java
Normal file
|
@ -0,0 +1,31 @@
|
|||
package main;
|
||||
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
import javax.swing.JList;
|
||||
import javax.swing.event.ListSelectionEvent;
|
||||
import javax.swing.event.ListSelectionListener;
|
||||
|
||||
public class ControleurStandard implements ActionListener, ListSelectionListener {
|
||||
|
||||
private VueStandard vue;
|
||||
|
||||
public ControleurStandard(VueStandard vue) {
|
||||
this.vue = vue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void valueChanged(ListSelectionEvent e) {
|
||||
if (!e.getValueIsAdjusting()) {
|
||||
JList<String> list = vue.getActiveUsersList();
|
||||
System.out.println(list.getSelectedValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -1,9 +1,35 @@
|
|||
package main;
|
||||
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO Auto-generated method stub
|
||||
JFrame frame = new JFrame("Application");
|
||||
VueStandard vueStd = new VueStandard();
|
||||
frame.add(vueStd);
|
||||
|
||||
frame.setSize(300,300);
|
||||
frame.setVisible(true);
|
||||
|
||||
try {
|
||||
Utilisateur.setSelf("Raijila", "Raijila", "localhost");
|
||||
} catch (UnknownHostException e1) {
|
||||
System.out.println("hote inexistant");
|
||||
}
|
||||
|
||||
VueStandard.userList.addElement("Mirasio");
|
||||
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
VueStandard.userList.addElement("Semtexx");
|
||||
} catch (InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
|
51
POO/src/main/Utilisateur.java
Normal file
51
POO/src/main/Utilisateur.java
Normal file
|
@ -0,0 +1,51 @@
|
|||
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.getByName(host);
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
15
POO/src/main/Vue.java
Normal file
15
POO/src/main/Vue.java
Normal file
|
@ -0,0 +1,15 @@
|
|||
package main;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
|
||||
public class Vue extends JPanel{
|
||||
|
||||
public Vue() {
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
public void reduireAgent() {}
|
||||
|
||||
public void fermerAgent() {}
|
||||
|
||||
}
|
56
POO/src/main/VueStandard.java
Normal file
56
POO/src/main/VueStandard.java
Normal file
|
@ -0,0 +1,56 @@
|
|||
package main;
|
||||
|
||||
import java.awt.Dimension;
|
||||
import java.awt.GridLayout;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Vector;
|
||||
|
||||
import javax.swing.BorderFactory;
|
||||
import javax.swing.DefaultListModel;
|
||||
import javax.swing.JList;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.ListSelectionModel;
|
||||
import javax.swing.ScrollPaneConstants;
|
||||
|
||||
public class VueStandard extends Vue {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private JList<String> activeUsersList;
|
||||
private ControleurStandard c;
|
||||
public static DefaultListModel<String> userList = new DefaultListModel<String>();
|
||||
|
||||
|
||||
public VueStandard() {
|
||||
super();
|
||||
|
||||
this.c = new ControleurStandard(this);
|
||||
this.setLayout(new GridLayout(2, 1));
|
||||
|
||||
|
||||
this.activeUsersList = new JList<String>(VueStandard.userList);
|
||||
this.activeUsersList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
||||
this.activeUsersList.setLayoutOrientation(JList.VERTICAL);
|
||||
this.activeUsersList.addListSelectionListener(this.c);
|
||||
|
||||
JScrollPane listScroller = new JScrollPane(this.activeUsersList);
|
||||
listScroller.setPreferredSize(new Dimension(50,50));
|
||||
listScroller.setAlignmentX(LEFT_ALIGNMENT);
|
||||
listScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
|
||||
listScroller.setBorder(BorderFactory.createCompoundBorder(
|
||||
BorderFactory.createTitledBorder("Utilisateurs Actifs"),
|
||||
BorderFactory.createEmptyBorder(5,2,2,2)));
|
||||
|
||||
this.add(listScroller);
|
||||
|
||||
}
|
||||
|
||||
public JList<String> getActiveUsersList(){
|
||||
return this.activeUsersList;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue