implémentation de la phase de découverte/connexion finie
This commit is contained in:
parent
6904cf452e
commit
935125c083
2 changed files with 36 additions and 36 deletions
|
@ -120,39 +120,40 @@ class ConnectionListenerThread extends Thread {
|
|||
{
|
||||
requestSocket = new DatagramSocket(1234);
|
||||
responseSocket = new DatagramSocket();
|
||||
bStream1 = new ByteArrayOutputStream();
|
||||
bStream2 = new ByteArrayOutputStream();
|
||||
oo1 = new ObjectOutputStream(bStream1);
|
||||
oo2 = new ObjectOutputStream(bStream2);
|
||||
|
||||
|
||||
while(!exit)
|
||||
{
|
||||
try
|
||||
{
|
||||
bStream1 = new ByteArrayOutputStream();
|
||||
bStream2 = new ByteArrayOutputStream();
|
||||
oo1 = new ObjectOutputStream(bStream1);
|
||||
oo2 = new ObjectOutputStream(bStream2);
|
||||
|
||||
System.out.println("Waiting for connection request");
|
||||
requestSocket.receive(request);
|
||||
System.out.println("Received a request!");
|
||||
username = new String(request.getData(), 0, request.getLength());
|
||||
InetAddress clientAddress= request.getAddress();
|
||||
|
||||
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);
|
||||
|
||||
System.out.println("Received a request from " + username + "@" + clientAddress.getHostAddress());
|
||||
|
||||
if(!user.findUser(username))
|
||||
user.add_to_known_users(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);
|
||||
|
||||
user.add_to_known_users(username, clientAddress.getHostAddress());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
catch(SocketTimeoutException e)
|
||||
{
|
||||
|
||||
}
|
||||
catch(SocketTimeoutException e) {}
|
||||
}
|
||||
}
|
||||
catch(Exception e)
|
||||
|
@ -191,7 +192,6 @@ public class ClientWindow implements ActionListener {
|
|||
|
||||
ClientWindow()
|
||||
{
|
||||
|
||||
String username = "";
|
||||
Boolean connected = false;
|
||||
DatagramSocket connectionSocket;
|
||||
|
@ -228,7 +228,7 @@ public class ClientWindow implements ActionListener {
|
|||
hostListSocket.setSoTimeout(500);
|
||||
|
||||
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");
|
||||
connectionSocket.send(connectionRequest);
|
||||
|
||||
|
@ -240,25 +240,25 @@ public class ClientWindow implements ActionListener {
|
|||
System.out.println("Waiting for reply");
|
||||
userListSocket.receive(userListPacket);
|
||||
hostListSocket.receive(hostListPacket);
|
||||
System.out.println("Received a reply from " + userListPacket.getAddress().getHostAddress());
|
||||
|
||||
userListStream = new ObjectInputStream(new ByteArrayInputStream(userListPacket.getData()));
|
||||
hostListStream = new ObjectInputStream(new ByteArrayInputStream(hostListPacket.getData()));
|
||||
|
||||
userList = (ArrayList<String>) userListStream.readObject();
|
||||
hostList = (ArrayList<String>) hostListStream.readObject();
|
||||
|
||||
System.out.println(userList.size() + " users currently connected");
|
||||
hostList = (ArrayList<String>) hostListStream.readObject();
|
||||
|
||||
if (userList.indexOf(username) != -1)
|
||||
connected = false;
|
||||
else
|
||||
{
|
||||
System.out.println(userList.size() + " users currently connected");
|
||||
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(username, InetAddress.getLocalHost().getHostAddress());
|
||||
user.add_to_known_users(username, getLocalIP());
|
||||
connected = true;
|
||||
}
|
||||
}
|
||||
|
@ -282,8 +282,8 @@ public class ClientWindow implements ActionListener {
|
|||
|
||||
chatWindow = new JFrame("Système de clavardage 2.0.1");
|
||||
|
||||
chatPanel = new JPanel(new GridLayout(2, 2));
|
||||
chatText = new JTextArea(1,1);
|
||||
chatPanel = new JPanel(new BorderLayout(2, 2));
|
||||
chatText = new JTextArea(1,1);
|
||||
textScroll = new JScrollPane(chatText);
|
||||
|
||||
sendPanel = new JPanel(new GridLayout(1, 2));
|
||||
|
@ -297,7 +297,6 @@ public class ClientWindow implements ActionListener {
|
|||
sendPanel.add(sendButton);
|
||||
|
||||
chatWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
chatWindow.setSize(new Dimension(1024, 768));
|
||||
|
||||
chatText.setLineWrap(true);
|
||||
chatText.setEditable(false);
|
||||
|
@ -312,6 +311,7 @@ public class ClientWindow implements ActionListener {
|
|||
//Display the window.
|
||||
chatWindow.pack();
|
||||
chatWindow.setVisible(true);
|
||||
chatWindow.setSize(new Dimension(1024, 768));
|
||||
|
||||
ReceiveThread t2 = new ReceiveThread(1237, chatText);
|
||||
ConnectionListenerThread t3 = new ConnectionListenerThread(user);
|
||||
|
|
|
@ -6,8 +6,8 @@ import java.util.*;
|
|||
public class User {
|
||||
|
||||
private String pseudo;
|
||||
private ArrayList<String> knownUsers;
|
||||
private ArrayList<String> knownHosts;
|
||||
private List<String> knownUsers;
|
||||
private List<String> knownHosts;
|
||||
|
||||
public User(String in_pseudo)
|
||||
{
|
||||
|
@ -33,11 +33,11 @@ public class User {
|
|||
{
|
||||
return (knownUsers.indexOf(username) != -1);
|
||||
}
|
||||
public ArrayList<String> getUsers()
|
||||
public List<String> getUsers()
|
||||
{
|
||||
return knownUsers;
|
||||
}
|
||||
public ArrayList<String> getHosts()
|
||||
public List<String> getHosts()
|
||||
{
|
||||
return knownHosts;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue