refactoring: modification de la classe User
This commit is contained in:
parent
8ac8cdd27e
commit
28d750584d
3 changed files with 62 additions and 82 deletions
|
@ -30,7 +30,7 @@ public class ClientWindow implements ActionListener {
|
||||||
|
|
||||||
ClientWindow()
|
ClientWindow()
|
||||||
{
|
{
|
||||||
Boolean connected = false;
|
boolean connected = false;
|
||||||
String username = "";
|
String username = "";
|
||||||
|
|
||||||
chatWindow = new JFrame("Système de clavardage 2.0.1");
|
chatWindow = new JFrame("Système de clavardage 2.0.1");
|
||||||
|
|
|
@ -5,6 +5,7 @@ import java.net.*;
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
import java.util.List;
|
||||||
import java.awt.event.*;
|
import java.awt.event.*;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
@ -28,7 +29,7 @@ class SendThread extends Thread {
|
||||||
public void run()
|
public void run()
|
||||||
{
|
{
|
||||||
Socket link;
|
Socket link;
|
||||||
Boolean connected = false;
|
boolean connected = false;
|
||||||
PrintWriter out;
|
PrintWriter out;
|
||||||
|
|
||||||
while(!connected)
|
while(!connected)
|
||||||
|
@ -51,7 +52,6 @@ class SendThread extends Thread {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class ReceiveThread extends Thread {
|
class ReceiveThread extends Thread {
|
||||||
|
|
||||||
int port;
|
int port;
|
||||||
|
@ -66,7 +66,7 @@ class ReceiveThread extends Thread {
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
ServerSocket servSocket = new ServerSocket(port);
|
ServerSocket servSocket = new ServerSocket(port);
|
||||||
Boolean exit = false;
|
boolean exit = false;
|
||||||
|
|
||||||
while(!exit)
|
while(!exit)
|
||||||
{
|
{
|
||||||
|
@ -90,31 +90,28 @@ class ConnectionListenerThread extends Thread {
|
||||||
|
|
||||||
int port;
|
int port;
|
||||||
JTextArea displayArea;
|
JTextArea displayArea;
|
||||||
User user;
|
List<User> known_users;
|
||||||
|
|
||||||
ConnectionListenerThread(User in_user)
|
ConnectionListenerThread(List<User> in_known_users)
|
||||||
{
|
{
|
||||||
user = in_user;
|
known_users = in_known_users;
|
||||||
}
|
}
|
||||||
public void run()
|
public void run()
|
||||||
{
|
{
|
||||||
Boolean exit = false;
|
boolean exit = false;
|
||||||
byte[] buffer = new byte[100];
|
byte[] buffer = new byte[100];
|
||||||
DatagramPacket request = new DatagramPacket(buffer, buffer.length);
|
DatagramPacket request = new DatagramPacket(buffer, buffer.length);
|
||||||
DatagramPacket responsePacket1;
|
DatagramPacket responsePacket;
|
||||||
DatagramPacket responsePacket2;
|
|
||||||
|
|
||||||
DatagramSocket requestSocket;
|
DatagramSocket requestSocket;
|
||||||
DatagramSocket responseSocket;
|
DatagramSocket responseSocket;
|
||||||
|
|
||||||
ByteArrayOutputStream bStream1;
|
ByteArrayOutputStream bStream;
|
||||||
ByteArrayOutputStream bStream2;
|
ObjectOutput oo;
|
||||||
ObjectOutput oo1;
|
|
||||||
ObjectOutput oo2;
|
|
||||||
|
|
||||||
String username;
|
String username;
|
||||||
|
|
||||||
byte[] response1, response2;
|
byte[] response1;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
requestSocket = new DatagramSocket(1234);
|
requestSocket = new DatagramSocket(1234);
|
||||||
|
@ -124,10 +121,8 @@ class ConnectionListenerThread extends Thread {
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
bStream1 = new ByteArrayOutputStream();
|
bStream = new ByteArrayOutputStream();
|
||||||
bStream2 = new ByteArrayOutputStream();
|
oo = new ObjectOutputStream(bStream);
|
||||||
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);
|
||||||
|
@ -135,20 +130,14 @@ class ConnectionListenerThread extends Thread {
|
||||||
InetAddress clientAddress= request.getAddress();
|
InetAddress clientAddress= request.getAddress();
|
||||||
System.out.println("Received a request from " + username + "@" + clientAddress.getHostAddress());
|
System.out.println("Received a request from " + username + "@" + clientAddress.getHostAddress());
|
||||||
|
|
||||||
if(!user.findUser(username))
|
if(known_users.indexOf(new User(username, clientAddress.getHostAddress())) != -1)
|
||||||
{
|
{
|
||||||
oo1.writeObject(user.getUsers());
|
oo.writeObject(known_users);
|
||||||
response1 = bStream1.toByteArray();
|
response1 = bStream.toByteArray();
|
||||||
responsePacket1 = new DatagramPacket(response1, response1.length, clientAddress, 1337);
|
responsePacket = new DatagramPacket(response1, response1.length, clientAddress, 1337);
|
||||||
responseSocket.send(responsePacket1);
|
responseSocket.send(responsePacket);
|
||||||
|
|
||||||
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());
|
|
||||||
|
|
||||||
|
known_users.add(new User(username, clientAddress.getHostAddress()));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -163,6 +152,10 @@ class ConnectionListenerThread extends Thread {
|
||||||
}
|
}
|
||||||
|
|
||||||
public class NetworkClient {
|
public class NetworkClient {
|
||||||
|
private User user;
|
||||||
|
private List<User> known_users;
|
||||||
|
private JTextArea chatText;
|
||||||
|
|
||||||
static String getLocalIP() {
|
static String getLocalIP() {
|
||||||
String ip = "";
|
String ip = "";
|
||||||
try(final DatagramSocket socket = new DatagramSocket()){
|
try(final DatagramSocket socket = new DatagramSocket()){
|
||||||
|
@ -176,33 +169,27 @@ public class NetworkClient {
|
||||||
return ip;
|
return ip;
|
||||||
}
|
}
|
||||||
|
|
||||||
User user;
|
|
||||||
JTextArea chatText;
|
|
||||||
|
|
||||||
NetworkClient(JTextArea in_chatText)
|
NetworkClient(JTextArea in_chatText)
|
||||||
{
|
{
|
||||||
user = new User("");
|
user = new User("", "");
|
||||||
chatText = in_chatText;
|
chatText = in_chatText;
|
||||||
|
known_users = new ArrayList<User>();
|
||||||
}
|
}
|
||||||
Boolean connect(String username)
|
|
||||||
|
boolean connect(String username)
|
||||||
{
|
{
|
||||||
Boolean connected = false;
|
boolean connected = false;
|
||||||
DatagramSocket connectionSocket;
|
DatagramSocket connectionSocket;
|
||||||
DatagramSocket userListSocket;
|
DatagramSocket userListSocket;
|
||||||
DatagramSocket hostListSocket;
|
|
||||||
|
|
||||||
DatagramPacket connectionRequest;
|
DatagramPacket connectionRequest;
|
||||||
DatagramPacket userListPacket;
|
DatagramPacket userListPacket;
|
||||||
DatagramPacket hostListPacket;
|
|
||||||
|
|
||||||
ObjectInputStream userListStream;
|
ObjectInputStream userListStream;
|
||||||
ObjectInputStream hostListStream;
|
|
||||||
|
|
||||||
ArrayList<String> userList;
|
ArrayList<User> userList;
|
||||||
ArrayList<String> hostList;
|
|
||||||
|
|
||||||
byte[] buffer1 = new byte[20000];
|
byte[] buffer1 = new byte[20000];
|
||||||
byte[] buffer2 = new byte[20000];
|
|
||||||
|
|
||||||
if(username == null || username.compareTo("") == 0)
|
if(username == null || username.compareTo("") == 0)
|
||||||
return false;
|
return false;
|
||||||
|
@ -211,10 +198,8 @@ public class NetworkClient {
|
||||||
{
|
{
|
||||||
connectionSocket = new DatagramSocket();
|
connectionSocket = new DatagramSocket();
|
||||||
userListSocket = new DatagramSocket(1337);
|
userListSocket = new DatagramSocket(1337);
|
||||||
hostListSocket = new DatagramSocket(1338);
|
|
||||||
|
|
||||||
userListSocket.setSoTimeout(500);
|
userListSocket.setSoTimeout(500);
|
||||||
hostListSocket.setSoTimeout(500);
|
|
||||||
|
|
||||||
connectionRequest = new DatagramPacket(username.getBytes(), username.length(),
|
connectionRequest = new DatagramPacket(username.getBytes(), username.length(),
|
||||||
InetAddress.getByName("25.255.255.255"), 1234);
|
InetAddress.getByName("25.255.255.255"), 1234);
|
||||||
|
@ -224,36 +209,32 @@ public class NetworkClient {
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
userListPacket = new DatagramPacket(buffer1, buffer1.length);
|
userListPacket = new DatagramPacket(buffer1, buffer1.length);
|
||||||
hostListPacket = new DatagramPacket(buffer2, buffer2.length);
|
|
||||||
|
|
||||||
System.out.println("Waiting for reply");
|
System.out.println("Waiting for reply");
|
||||||
userListSocket.receive(userListPacket);
|
userListSocket.receive(userListPacket);
|
||||||
hostListSocket.receive(hostListPacket);
|
|
||||||
System.out.println("Received a reply from " + userListPacket.getAddress().getHostAddress());
|
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()));
|
|
||||||
|
|
||||||
userList = (ArrayList<String>) userListStream.readObject();
|
userList = (ArrayList<User>) userListStream.readObject();
|
||||||
hostList = (ArrayList<String>) hostListStream.readObject();
|
|
||||||
|
|
||||||
if (userList.indexOf(username) != -1)
|
if (userList.indexOf(user) != -1)
|
||||||
connected = false;
|
connected = false;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
System.out.println(userList.size() + " users currently connected");
|
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(userList.get(i) + " : " + hostList.get(i));
|
System.out.println(userList.get(i).getName() + " : " + userList.get(i).getAddress());
|
||||||
user.add_to_known_users(userList.get(i), hostList.get(i));
|
known_users.add(userList.get(i));
|
||||||
}
|
}
|
||||||
user.add_to_known_users(username, getLocalIP());
|
known_users.add(new User(username, getLocalIP()));
|
||||||
connected = true;
|
connected = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch(SocketTimeoutException e)
|
catch(SocketTimeoutException e)
|
||||||
{
|
{
|
||||||
user.add_to_known_users(username, getLocalIP());
|
known_users.add(new User(username, getLocalIP()));
|
||||||
System.out.println("Reply timed out");
|
System.out.println("Reply timed out");
|
||||||
connected = true;
|
connected = true;
|
||||||
}
|
}
|
||||||
|
@ -269,9 +250,9 @@ public class NetworkClient {
|
||||||
|
|
||||||
if(connected)
|
if(connected)
|
||||||
{
|
{
|
||||||
user.setpseudo(username);
|
user.setName(username);
|
||||||
ReceiveThread t2 = new ReceiveThread(1237, chatText);
|
ReceiveThread t2 = new ReceiveThread(1237, chatText);
|
||||||
ConnectionListenerThread t3 = new ConnectionListenerThread(user);
|
ConnectionListenerThread t3 = new ConnectionListenerThread(known_users);
|
||||||
t2.start();
|
t2.start();
|
||||||
t3.start();
|
t3.start();
|
||||||
}
|
}
|
||||||
|
@ -281,10 +262,13 @@ public class NetworkClient {
|
||||||
|
|
||||||
void send (String message)
|
void send (String message)
|
||||||
{
|
{
|
||||||
for(String address:user.getHosts())
|
for(User recipient:known_users)
|
||||||
{
|
{
|
||||||
SendThread t1 = new SendThread(user.getpseudo(), address, message, 1237);
|
if(recipient != user)
|
||||||
t1.start();
|
{
|
||||||
|
SendThread t1 = new SendThread(user.getName(), recipient.getAddress(), message, 1237);
|
||||||
|
t1.start();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,40 +5,36 @@ import java.util.*;
|
||||||
|
|
||||||
public class User {
|
public class User {
|
||||||
|
|
||||||
private String pseudo;
|
private String name;
|
||||||
private List<String> knownUsers;
|
private String address;
|
||||||
private List<String> knownHosts;
|
|
||||||
|
|
||||||
public User(String in_pseudo)
|
|
||||||
|
public User(String in_name, String in_address)
|
||||||
{
|
{
|
||||||
pseudo = in_pseudo;
|
name = in_name;
|
||||||
knownUsers = new ArrayList<String>();
|
address = in_address;
|
||||||
knownHosts = new ArrayList<String>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void add_to_known_users(String name, String address)
|
public void setName(String new_name)
|
||||||
{
|
{
|
||||||
knownUsers.add(name);
|
name = new_name;
|
||||||
knownHosts.add(address);
|
|
||||||
}
|
}
|
||||||
public void setpseudo(String pseudo)
|
public String getName()
|
||||||
{
|
{
|
||||||
this.pseudo = pseudo;
|
return name;
|
||||||
}
|
}
|
||||||
public String getpseudo()
|
public void setAddress(String new_address)
|
||||||
{
|
{
|
||||||
return pseudo;
|
name = new_address;
|
||||||
}
|
}
|
||||||
public Boolean findUser(String username)
|
public String getAddress()
|
||||||
{
|
{
|
||||||
return (knownUsers.indexOf(username) != -1);
|
return address;
|
||||||
}
|
}
|
||||||
public List<String> getUsers()
|
|
||||||
|
boolean equals(User b)
|
||||||
{
|
{
|
||||||
return knownUsers;
|
return (b.getName() == name);
|
||||||
}
|
|
||||||
public List<String> getHosts()
|
|
||||||
{
|
|
||||||
return knownHosts;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue