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