implémentation de la phase de découverte/connexion finie

This commit is contained in:
Louis Farina 2020-12-14 16:42:27 +01:00
parent 6904cf452e
commit 935125c083
2 changed files with 36 additions and 36 deletions

View file

@ -120,39 +120,40 @@ class ConnectionListenerThread extends Thread {
{ {
requestSocket = new DatagramSocket(1234); requestSocket = new DatagramSocket(1234);
responseSocket = new DatagramSocket(); responseSocket = new DatagramSocket();
bStream1 = new ByteArrayOutputStream();
bStream2 = new ByteArrayOutputStream();
oo1 = new ObjectOutputStream(bStream1);
oo2 = new ObjectOutputStream(bStream2);
while(!exit) while(!exit)
{ {
try try
{ {
bStream1 = new ByteArrayOutputStream();
bStream2 = new ByteArrayOutputStream();
oo1 = new ObjectOutputStream(bStream1);
oo2 = new ObjectOutputStream(bStream2);
System.out.println("Waiting for connection request"); System.out.println("Waiting for connection request");
requestSocket.receive(request); requestSocket.receive(request);
System.out.println("Received a request!");
username = new String(request.getData(), 0, request.getLength()); username = new String(request.getData(), 0, request.getLength());
InetAddress clientAddress= request.getAddress(); InetAddress clientAddress= request.getAddress();
System.out.println("Received a request from " + username + "@" + clientAddress.getHostAddress());
oo1.writeObject(user.getUsers());
response1 = bStream1.toByteArray();
responsePacket1 = new DatagramPacket(response1, response1.length, clientAddress, 1337);
responseSocket.send(responsePacket1);
oo2.writeObject(user.getHosts());
response2 = bStream2.toByteArray();
responsePacket2 = new DatagramPacket(response2, response2.length, clientAddress, 1338);
responseSocket.send(responsePacket2);
if(!user.findUser(username)) if(!user.findUser(username))
{
oo1.writeObject(user.getUsers());
response1 = bStream1.toByteArray();
responsePacket1 = new DatagramPacket(response1, response1.length, clientAddress, 1337);
responseSocket.send(responsePacket1);
oo2.writeObject(user.getHosts());
response2 = bStream2.toByteArray();
responsePacket2 = new DatagramPacket(response2, response2.length, clientAddress, 1338);
responseSocket.send(responsePacket2);
user.add_to_known_users(username, clientAddress.getHostAddress()); user.add_to_known_users(username, clientAddress.getHostAddress());
} }
catch(SocketTimeoutException e)
{
} }
catch(SocketTimeoutException e) {}
} }
} }
catch(Exception e) catch(Exception e)
@ -191,7 +192,6 @@ public class ClientWindow implements ActionListener {
ClientWindow() ClientWindow()
{ {
String username = ""; String username = "";
Boolean connected = false; Boolean connected = false;
DatagramSocket connectionSocket; DatagramSocket connectionSocket;
@ -228,7 +228,7 @@ public class ClientWindow implements ActionListener {
hostListSocket.setSoTimeout(500); hostListSocket.setSoTimeout(500);
connectionRequest = new DatagramPacket(username.getBytes(), username.length(), connectionRequest = new DatagramPacket(username.getBytes(), username.length(),
InetAddress.getByName("192.168.1.255"), 1234); InetAddress.getByName("25.255.255.255"), 1234);
System.out.println("Sending connection request"); System.out.println("Sending connection request");
connectionSocket.send(connectionRequest); connectionSocket.send(connectionRequest);
@ -240,6 +240,7 @@ public class ClientWindow implements ActionListener {
System.out.println("Waiting for reply"); System.out.println("Waiting for reply");
userListSocket.receive(userListPacket); userListSocket.receive(userListPacket);
hostListSocket.receive(hostListPacket); hostListSocket.receive(hostListPacket);
System.out.println("Received a reply from " + userListPacket.getAddress().getHostAddress());
userListStream = new ObjectInputStream(new ByteArrayInputStream(userListPacket.getData())); userListStream = new ObjectInputStream(new ByteArrayInputStream(userListPacket.getData()));
hostListStream = new ObjectInputStream(new ByteArrayInputStream(hostListPacket.getData())); hostListStream = new ObjectInputStream(new ByteArrayInputStream(hostListPacket.getData()));
@ -247,18 +248,17 @@ public class ClientWindow implements ActionListener {
userList = (ArrayList<String>) userListStream.readObject(); userList = (ArrayList<String>) userListStream.readObject();
hostList = (ArrayList<String>) hostListStream.readObject(); hostList = (ArrayList<String>) hostListStream.readObject();
System.out.println(userList.size() + " users currently connected");
if (userList.indexOf(username) != -1) if (userList.indexOf(username) != -1)
connected = false; connected = false;
else else
{ {
System.out.println(userList.size() + " users currently connected");
for(int i = 0;i < userList.size();i ++) for(int i = 0;i < userList.size();i ++)
{ {
System.out.println(hostList.get(i)); System.out.println(userList.get(i) + " : " + hostList.get(i));
user.add_to_known_users(userList.get(i), hostList.get(i)); user.add_to_known_users(userList.get(i), hostList.get(i));
} }
user.add_to_known_users(username, InetAddress.getLocalHost().getHostAddress()); user.add_to_known_users(username, getLocalIP());
connected = true; connected = true;
} }
} }
@ -282,8 +282,8 @@ public class ClientWindow implements ActionListener {
chatWindow = new JFrame("Système de clavardage 2.0.1"); chatWindow = new JFrame("Système de clavardage 2.0.1");
chatPanel = new JPanel(new GridLayout(2, 2)); chatPanel = new JPanel(new BorderLayout(2, 2));
chatText = new JTextArea(1,1); chatText = new JTextArea(1,1);
textScroll = new JScrollPane(chatText); textScroll = new JScrollPane(chatText);
sendPanel = new JPanel(new GridLayout(1, 2)); sendPanel = new JPanel(new GridLayout(1, 2));
@ -297,7 +297,6 @@ public class ClientWindow implements ActionListener {
sendPanel.add(sendButton); sendPanel.add(sendButton);
chatWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); chatWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
chatWindow.setSize(new Dimension(1024, 768));
chatText.setLineWrap(true); chatText.setLineWrap(true);
chatText.setEditable(false); chatText.setEditable(false);
@ -312,6 +311,7 @@ public class ClientWindow implements ActionListener {
//Display the window. //Display the window.
chatWindow.pack(); chatWindow.pack();
chatWindow.setVisible(true); chatWindow.setVisible(true);
chatWindow.setSize(new Dimension(1024, 768));
ReceiveThread t2 = new ReceiveThread(1237, chatText); ReceiveThread t2 = new ReceiveThread(1237, chatText);
ConnectionListenerThread t3 = new ConnectionListenerThread(user); ConnectionListenerThread t3 = new ConnectionListenerThread(user);

View file

@ -6,8 +6,8 @@ import java.util.*;
public class User { public class User {
private String pseudo; private String pseudo;
private ArrayList<String> knownUsers; private List<String> knownUsers;
private ArrayList<String> knownHosts; private List<String> knownHosts;
public User(String in_pseudo) public User(String in_pseudo)
{ {
@ -33,11 +33,11 @@ public class User {
{ {
return (knownUsers.indexOf(username) != -1); return (knownUsers.indexOf(username) != -1);
} }
public ArrayList<String> getUsers() public List<String> getUsers()
{ {
return knownUsers; return knownUsers;
} }
public ArrayList<String> getHosts() public List<String> getHosts()
{ {
return knownHosts; return knownHosts;
} }