WIP système de connection à plusieurs utilisateurs
This commit is contained in:
parent
5dd3f1d0da
commit
a745f39e0c
1 changed files with 79 additions and 43 deletions
|
@ -107,8 +107,8 @@ class ConnectionListenerThread extends Thread {
|
||||||
DatagramSocket requestSocket;
|
DatagramSocket requestSocket;
|
||||||
DatagramSocket responseSocket;
|
DatagramSocket responseSocket;
|
||||||
|
|
||||||
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
|
ByteArrayOutputStream bStream;
|
||||||
ObjectOutput oo = new ObjectOutputStream(bStream);
|
ObjectOutput oo;
|
||||||
|
|
||||||
String username;
|
String username;
|
||||||
|
|
||||||
|
@ -117,6 +117,8 @@ class ConnectionListenerThread extends Thread {
|
||||||
{
|
{
|
||||||
requestSocket = new DatagramSocket(1234);
|
requestSocket = new DatagramSocket(1234);
|
||||||
responseSocket = new DatagramSocket();
|
responseSocket = new DatagramSocket();
|
||||||
|
bStream = new ByteArrayOutputStream();
|
||||||
|
oo = new ObjectOutputStream(bStream);
|
||||||
|
|
||||||
while(!exit)
|
while(!exit)
|
||||||
{
|
{
|
||||||
|
@ -128,12 +130,12 @@ class ConnectionListenerThread extends Thread {
|
||||||
|
|
||||||
oo.writeObject(user.getUsers());
|
oo.writeObject(user.getUsers());
|
||||||
response1 = bStream.toByteArray();
|
response1 = bStream.toByteArray();
|
||||||
responsePacket1 = new DatagramPacket(response1, response1.length, clientAddress, 1235);
|
responsePacket1 = new DatagramPacket(response1, response1.length, clientAddress, 1337);
|
||||||
responseSocket.send(responsePacket1);
|
responseSocket.send(responsePacket1);
|
||||||
|
|
||||||
oo.writeObject(user.getHosts());
|
oo.writeObject(user.getHosts());
|
||||||
response2 = bStream.toByteArray();
|
response2 = bStream.toByteArray();
|
||||||
responsePacket2 = new DatagramPacket(response2, response2.length, clientAddress, 1236);
|
responsePacket2 = new DatagramPacket(response2, response2.length, clientAddress, 1338);
|
||||||
responseSocket.send(responsePacket2);
|
responseSocket.send(responsePacket2);
|
||||||
if(!user.findUser(username))
|
if(!user.findUser(username))
|
||||||
user.add_to_known_users(username, clientAddress.toString());
|
user.add_to_known_users(username, clientAddress.toString());
|
||||||
|
@ -164,11 +166,11 @@ public class ClientWindow implements ActionListener {
|
||||||
JTextField messageField;
|
JTextField messageField;
|
||||||
JButton sendButton;
|
JButton sendButton;
|
||||||
|
|
||||||
User user;
|
User user = new User("");
|
||||||
|
|
||||||
ClientWindow()
|
ClientWindow()
|
||||||
{
|
{
|
||||||
String username;
|
String username = "";
|
||||||
Boolean connected = false;
|
Boolean connected = false;
|
||||||
DatagramSocket connectionSocket;
|
DatagramSocket connectionSocket;
|
||||||
DatagramSocket userListSocket;
|
DatagramSocket userListSocket;
|
||||||
|
@ -184,44 +186,72 @@ public class ClientWindow implements ActionListener {
|
||||||
ArrayList<String> userList;
|
ArrayList<String> userList;
|
||||||
ArrayList<String> hostList;
|
ArrayList<String> hostList;
|
||||||
|
|
||||||
username = JOptionPane.showInputDialog(chatWindow,
|
byte[] buffer1 = new byte[20000];
|
||||||
"Enter a username",
|
byte[] buffer2 = new byte[20000];
|
||||||
"POPOPOPOPUPUPOPOPUP",
|
|
||||||
JOptionPane.PLAIN_MESSAGE);
|
|
||||||
|
|
||||||
while (!connected)
|
while (!connected)
|
||||||
{
|
{
|
||||||
connectionSocket = new DatagramSocket();
|
username = JOptionPane.showInputDialog(chatWindow,
|
||||||
userListSocket = new DatagramSocket(1235);
|
"Enter a username",
|
||||||
hostListSocket = new DatagramSocket(1236);
|
"POPUPOPOPUPUPOPOPUP",
|
||||||
|
JOptionPane.PLAIN_MESSAGE);
|
||||||
connectionRequest = new DatagramPacket(username.getBytes(), username.length(),
|
|
||||||
InetAddress.getByName("192.168.1.255"), 1234);
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
userListStream = new ObjectInputStream(new ByteArrayInputStream(userListPacket.getData()));
|
connectionSocket = new DatagramSocket();
|
||||||
hostListStream = new ObjectInputStream(new ByteArrayInputStream(hostListPacket.getData()));
|
userListSocket = new DatagramSocket(1337);
|
||||||
|
hostListSocket = new DatagramSocket(1338);
|
||||||
|
|
||||||
userList = (ArrayList<String>) userListStream.readObject();
|
userListSocket.setSoTimeout(500);
|
||||||
hostList = (ArrayList<String>) hostListStream.readObject();
|
hostListSocket.setSoTimeout(500);
|
||||||
|
|
||||||
if (userList.indexOf(username) != -1)
|
connectionRequest = new DatagramPacket(username.getBytes(), username.length(),
|
||||||
connected = false;
|
InetAddress.getByName("192.168.1.255"), 1234);
|
||||||
else
|
System.out.println("Sending connection request");
|
||||||
{
|
connectionSocket.send(connectionRequest);
|
||||||
for(int i = 0;i < userList.size();i ++)
|
|
||||||
{
|
try
|
||||||
user.add_to_known_users(userList.get(i), hostList.get(i));
|
{
|
||||||
}
|
userListPacket = new DatagramPacket(buffer1, buffer1.length);
|
||||||
connected = true;
|
hostListPacket = new DatagramPacket(buffer2, buffer2.length);
|
||||||
}
|
|
||||||
|
System.out.println("Waiting for reply");
|
||||||
|
userListSocket.receive(userListPacket);
|
||||||
|
hostListSocket.receive(hostListPacket);
|
||||||
|
|
||||||
|
userListStream = new ObjectInputStream(new ByteArrayInputStream(userListPacket.getData()));
|
||||||
|
hostListStream = new ObjectInputStream(new ByteArrayInputStream(hostListPacket.getData()));
|
||||||
|
|
||||||
|
userList = (ArrayList<String>) userListStream.readObject();
|
||||||
|
hostList = (ArrayList<String>) hostListStream.readObject();
|
||||||
|
|
||||||
|
if (userList.indexOf(username) != -1)
|
||||||
|
connected = false;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for(int i = 0;i < userList.size();i ++)
|
||||||
|
{
|
||||||
|
user.add_to_known_users(userList.get(i), hostList.get(i));
|
||||||
|
}
|
||||||
|
connected = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch(SocketTimeoutException e)
|
||||||
|
{
|
||||||
|
System.out.println("Reply timed out");
|
||||||
|
connected = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch(SocketTimeoutException e)
|
catch (SocketException e2)
|
||||||
{
|
{
|
||||||
connected = true;
|
System.out.println(e2.getMessage());
|
||||||
|
}
|
||||||
|
catch (Exception e3)
|
||||||
|
{
|
||||||
|
System.out.println(e3.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
user.setpseudo(username);
|
||||||
|
|
||||||
chatWindow = new JFrame("Système de clavardage 2.0.1");
|
chatWindow = new JFrame("Système de clavardage 2.0.1");
|
||||||
|
|
||||||
|
@ -256,8 +286,10 @@ public class ClientWindow implements ActionListener {
|
||||||
chatWindow.pack();
|
chatWindow.pack();
|
||||||
chatWindow.setVisible(true);
|
chatWindow.setVisible(true);
|
||||||
|
|
||||||
ReceiveThread t2 = new ReceiveThread(sport, chatText);
|
ReceiveThread t2 = new ReceiveThread(1237, chatText);
|
||||||
|
ConnectionListenerThread t3 = new ConnectionListenerThread(user);
|
||||||
t2.start();
|
t2.start();
|
||||||
|
t3.start();
|
||||||
}
|
}
|
||||||
public static void main (String [] args)
|
public static void main (String [] args)
|
||||||
{
|
{
|
||||||
|
@ -269,8 +301,12 @@ public class ClientWindow implements ActionListener {
|
||||||
public void actionPerformed(ActionEvent event) {
|
public void actionPerformed(ActionEvent event) {
|
||||||
String message = messageField.getText();
|
String message = messageField.getText();
|
||||||
messageField.setText("");
|
messageField.setText("");
|
||||||
chatText.append(username + " : " + message + "\n");
|
chatText.append(user.getpseudo() + " : " + message + "\n");
|
||||||
SendThread t1 = new SendThread(username, address, message, dport);
|
|
||||||
t1.start();
|
for(String address:user.getHosts())
|
||||||
|
{
|
||||||
|
SendThread t1 = new SendThread(user.getpseudo(), address, message, 1237);
|
||||||
|
t1.start();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue