refactoring: ajout des fonctionnalités réseaux dans une nouvelle classe NetworkClient séparée
This commit is contained in:
parent
9d8032a0cd
commit
8ac8cdd27e
2 changed files with 332 additions and 282 deletions
|
@ -12,171 +12,8 @@ import java.util.*;
|
|||
|
||||
import chat.User;
|
||||
|
||||
|
||||
class SendThread extends Thread {
|
||||
String username;
|
||||
String address;
|
||||
String message;
|
||||
int port;
|
||||
|
||||
SendThread(String in_username, String in_address, String in_message, int in_port)
|
||||
{
|
||||
username = in_username;
|
||||
address = in_address;
|
||||
message = in_message;
|
||||
port = in_port;
|
||||
}
|
||||
public void run()
|
||||
{
|
||||
Socket link;
|
||||
Boolean connected = false;
|
||||
PrintWriter out;
|
||||
|
||||
while(!connected)
|
||||
{
|
||||
try
|
||||
{
|
||||
link = new Socket(address, port);
|
||||
out = new PrintWriter(link.getOutputStream(),true);
|
||||
out.println(username + " : " + message);
|
||||
|
||||
connected = true;
|
||||
}
|
||||
catch(IOException e)
|
||||
{
|
||||
System.out.println("nik1!");
|
||||
connected = false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class ReceiveThread extends Thread {
|
||||
|
||||
int port;
|
||||
JTextArea displayArea;
|
||||
ReceiveThread(int in_port, JTextArea in_displayArea)
|
||||
{
|
||||
port = in_port;
|
||||
displayArea = in_displayArea;
|
||||
}
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
ServerSocket servSocket = new ServerSocket(port);
|
||||
Boolean exit = false;
|
||||
|
||||
while(!exit)
|
||||
{
|
||||
Socket link = servSocket.accept();
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(link.getInputStream()));
|
||||
String message = in.readLine();
|
||||
displayArea.append(message + "\n");
|
||||
displayArea.setCaretPosition(displayArea.getDocument().getLength());
|
||||
link.close();
|
||||
}
|
||||
|
||||
}
|
||||
catch(IOException e)
|
||||
{
|
||||
System.out.println("nik2 !");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ConnectionListenerThread extends Thread {
|
||||
|
||||
int port;
|
||||
JTextArea displayArea;
|
||||
User user;
|
||||
|
||||
ConnectionListenerThread(User in_user)
|
||||
{
|
||||
user = in_user;
|
||||
}
|
||||
public void run()
|
||||
{
|
||||
Boolean exit = false;
|
||||
byte[] buffer = new byte[100];
|
||||
DatagramPacket request = new DatagramPacket(buffer, buffer.length);
|
||||
DatagramPacket responsePacket1;
|
||||
DatagramPacket responsePacket2;
|
||||
|
||||
DatagramSocket requestSocket;
|
||||
DatagramSocket responseSocket;
|
||||
|
||||
ByteArrayOutputStream bStream1;
|
||||
ByteArrayOutputStream bStream2;
|
||||
ObjectOutput oo1;
|
||||
ObjectOutput oo2;
|
||||
|
||||
String username;
|
||||
|
||||
byte[] response1, response2;
|
||||
try
|
||||
{
|
||||
requestSocket = new DatagramSocket(1234);
|
||||
responseSocket = new DatagramSocket();
|
||||
|
||||
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);
|
||||
username = new String(request.getData(), 0, request.getLength());
|
||||
InetAddress clientAddress= request.getAddress();
|
||||
System.out.println("Received a request from " + username + "@" + clientAddress.getHostAddress());
|
||||
|
||||
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());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
catch(SocketTimeoutException e) {}
|
||||
}
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
System.out.println("nik!!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class ClientWindow implements ActionListener {
|
||||
String getLocalIP() {
|
||||
String ip = "";
|
||||
try(final DatagramSocket socket = new DatagramSocket()){
|
||||
socket.connect(InetAddress.getByName("8.8.8.8"), 10002);
|
||||
ip = socket.getLocalAddress().getHostAddress();
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
System.out.println("niiiiiiiikkk");
|
||||
}
|
||||
return ip;
|
||||
}
|
||||
|
||||
|
||||
JFrame chatWindow;
|
||||
|
||||
|
@ -188,100 +25,15 @@ public class ClientWindow implements ActionListener {
|
|||
JTextField messageField;
|
||||
JButton sendButton;
|
||||
|
||||
User user = new User("");
|
||||
NetworkClient network;
|
||||
|
||||
|
||||
ClientWindow()
|
||||
{
|
||||
String username = "";
|
||||
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;
|
||||
|
||||
byte[] buffer1 = new byte[20000];
|
||||
byte[] buffer2 = new byte[20000];
|
||||
|
||||
|
||||
while (!connected)
|
||||
{
|
||||
username = JOptionPane.showInputDialog(chatWindow,
|
||||
"Enter a username",
|
||||
"POPUPOPOPUPUPOPOPUP",
|
||||
JOptionPane.PLAIN_MESSAGE);
|
||||
try
|
||||
{
|
||||
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);
|
||||
System.out.println("Sending connection request");
|
||||
connectionSocket.send(connectionRequest);
|
||||
|
||||
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();
|
||||
|
||||
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(userList.get(i) + " : " + hostList.get(i));
|
||||
user.add_to_known_users(userList.get(i), hostList.get(i));
|
||||
}
|
||||
user.add_to_known_users(username, getLocalIP());
|
||||
connected = true;
|
||||
}
|
||||
}
|
||||
catch(SocketTimeoutException e)
|
||||
{
|
||||
user.add_to_known_users(username, getLocalIP());
|
||||
System.out.println("Reply timed out");
|
||||
connected = true;
|
||||
}
|
||||
}
|
||||
catch (SocketException e2)
|
||||
{
|
||||
System.out.println(e2.getMessage());
|
||||
}
|
||||
catch (Exception e3)
|
||||
{
|
||||
System.out.println(e3.getMessage());
|
||||
}
|
||||
}
|
||||
user.setpseudo(username);
|
||||
Boolean connected = false;
|
||||
String username = "";
|
||||
|
||||
chatWindow = new JFrame("Système de clavardage 2.0.1");
|
||||
|
||||
chatPanel = new JPanel(new BorderLayout(2, 2));
|
||||
chatText = new JTextArea(1,1);
|
||||
textScroll = new JScrollPane(chatText);
|
||||
|
@ -289,34 +41,44 @@ public class ClientWindow implements ActionListener {
|
|||
sendPanel = new JPanel(new BorderLayout(1, 2));
|
||||
messageField = new JTextField();
|
||||
sendButton = new JButton("Envoyer");
|
||||
sendButton.addActionListener(this);
|
||||
messageField.addActionListener(this);
|
||||
|
||||
|
||||
sendPanel.add(messageField);
|
||||
sendPanel.add(sendButton, BorderLayout.EAST);
|
||||
network = new NetworkClient(chatText);
|
||||
|
||||
while(!connected && username != null)
|
||||
{
|
||||
username = JOptionPane.showInputDialog(chatWindow,
|
||||
"Enter a username",
|
||||
"POPUPOPOPUPUPOPOPUP",
|
||||
JOptionPane.PLAIN_MESSAGE);
|
||||
|
||||
connected = network.connect(username);
|
||||
}
|
||||
|
||||
chatWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
chatText.setLineWrap(true);
|
||||
chatText.setEditable(false);
|
||||
|
||||
chatPanel.add(textScroll);
|
||||
chatPanel.add(sendPanel, BorderLayout.SOUTH);
|
||||
|
||||
chatWindow.getContentPane().add(chatPanel, BorderLayout.CENTER);
|
||||
|
||||
if(username != null)
|
||||
{
|
||||
sendButton.addActionListener(this);
|
||||
messageField.addActionListener(this);
|
||||
|
||||
|
||||
//Display the window.
|
||||
chatWindow.pack();
|
||||
chatWindow.setVisible(true);
|
||||
chatWindow.setSize(new Dimension(1024, 768));
|
||||
|
||||
sendPanel.add(messageField);
|
||||
sendPanel.add(sendButton, BorderLayout.EAST);
|
||||
|
||||
chatWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
ReceiveThread t2 = new ReceiveThread(1237, chatText);
|
||||
ConnectionListenerThread t3 = new ConnectionListenerThread(user);
|
||||
t2.start();
|
||||
t3.start();
|
||||
chatText.setLineWrap(true);
|
||||
chatText.setEditable(false);
|
||||
|
||||
chatPanel.add(textScroll);
|
||||
chatPanel.add(sendPanel, BorderLayout.SOUTH);
|
||||
|
||||
chatWindow.getContentPane().add(chatPanel, BorderLayout.CENTER);
|
||||
|
||||
//Display the window.
|
||||
chatWindow.pack();
|
||||
chatWindow.setVisible(true);
|
||||
chatWindow.setSize(new Dimension(1024, 768));
|
||||
}
|
||||
|
||||
}
|
||||
public static void main (String [] args)
|
||||
{
|
||||
|
@ -327,12 +89,10 @@ public class ClientWindow implements ActionListener {
|
|||
|
||||
public void actionPerformed(ActionEvent event) {
|
||||
String message = messageField.getText();
|
||||
messageField.setText("");
|
||||
|
||||
for(String address:user.getHosts())
|
||||
if(message.compareTo("") != 0)
|
||||
{
|
||||
SendThread t1 = new SendThread(user.getpseudo(), address, message, 1237);
|
||||
t1.start();
|
||||
messageField.setText("");
|
||||
network.send(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
290
src/chat/NetworkClient.java
Normal file
290
src/chat/NetworkClient.java
Normal file
|
@ -0,0 +1,290 @@
|
|||
package chat;
|
||||
|
||||
import java.net.*;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
import chat.User;
|
||||
|
||||
class SendThread extends Thread {
|
||||
String username;
|
||||
String address;
|
||||
String message;
|
||||
int port;
|
||||
|
||||
SendThread(String in_username, String in_address, String in_message, int in_port)
|
||||
{
|
||||
username = in_username;
|
||||
address = in_address;
|
||||
message = in_message;
|
||||
port = in_port;
|
||||
}
|
||||
public void run()
|
||||
{
|
||||
Socket link;
|
||||
Boolean connected = false;
|
||||
PrintWriter out;
|
||||
|
||||
while(!connected)
|
||||
{
|
||||
try
|
||||
{
|
||||
link = new Socket(address, port);
|
||||
out = new PrintWriter(link.getOutputStream(),true);
|
||||
out.println(username + " : " + message);
|
||||
|
||||
connected = true;
|
||||
}
|
||||
catch(IOException e)
|
||||
{
|
||||
System.out.println("nik1!");
|
||||
connected = false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class ReceiveThread extends Thread {
|
||||
|
||||
int port;
|
||||
JTextArea displayArea;
|
||||
ReceiveThread(int in_port, JTextArea in_displayArea)
|
||||
{
|
||||
port = in_port;
|
||||
displayArea = in_displayArea;
|
||||
}
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
ServerSocket servSocket = new ServerSocket(port);
|
||||
Boolean exit = false;
|
||||
|
||||
while(!exit)
|
||||
{
|
||||
Socket link = servSocket.accept();
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(link.getInputStream()));
|
||||
String message = in.readLine();
|
||||
displayArea.append(message + "\n");
|
||||
displayArea.setCaretPosition(displayArea.getDocument().getLength());
|
||||
link.close();
|
||||
}
|
||||
|
||||
}
|
||||
catch(IOException e)
|
||||
{
|
||||
System.out.println("nik2 !");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ConnectionListenerThread extends Thread {
|
||||
|
||||
int port;
|
||||
JTextArea displayArea;
|
||||
User user;
|
||||
|
||||
ConnectionListenerThread(User in_user)
|
||||
{
|
||||
user = in_user;
|
||||
}
|
||||
public void run()
|
||||
{
|
||||
Boolean exit = false;
|
||||
byte[] buffer = new byte[100];
|
||||
DatagramPacket request = new DatagramPacket(buffer, buffer.length);
|
||||
DatagramPacket responsePacket1;
|
||||
DatagramPacket responsePacket2;
|
||||
|
||||
DatagramSocket requestSocket;
|
||||
DatagramSocket responseSocket;
|
||||
|
||||
ByteArrayOutputStream bStream1;
|
||||
ByteArrayOutputStream bStream2;
|
||||
ObjectOutput oo1;
|
||||
ObjectOutput oo2;
|
||||
|
||||
String username;
|
||||
|
||||
byte[] response1, response2;
|
||||
try
|
||||
{
|
||||
requestSocket = new DatagramSocket(1234);
|
||||
responseSocket = new DatagramSocket();
|
||||
|
||||
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);
|
||||
username = new String(request.getData(), 0, request.getLength());
|
||||
InetAddress clientAddress= request.getAddress();
|
||||
System.out.println("Received a request from " + username + "@" + clientAddress.getHostAddress());
|
||||
|
||||
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());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
catch(SocketTimeoutException e) {}
|
||||
}
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
System.out.println("nik!!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class NetworkClient {
|
||||
static String getLocalIP() {
|
||||
String ip = "";
|
||||
try(final DatagramSocket socket = new DatagramSocket()){
|
||||
socket.connect(InetAddress.getByName("8.8.8.8"), 10002);
|
||||
ip = socket.getLocalAddress().getHostAddress();
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
System.out.println("niiiiiiiikkk");
|
||||
}
|
||||
return ip;
|
||||
}
|
||||
|
||||
User user;
|
||||
JTextArea chatText;
|
||||
|
||||
NetworkClient(JTextArea in_chatText)
|
||||
{
|
||||
user = new User("");
|
||||
chatText = in_chatText;
|
||||
}
|
||||
Boolean connect(String username)
|
||||
{
|
||||
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;
|
||||
|
||||
byte[] buffer1 = new byte[20000];
|
||||
byte[] buffer2 = new byte[20000];
|
||||
|
||||
if(username == null || username.compareTo("") == 0)
|
||||
return false;
|
||||
|
||||
try
|
||||
{
|
||||
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);
|
||||
System.out.println("Sending connection request");
|
||||
connectionSocket.send(connectionRequest);
|
||||
|
||||
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();
|
||||
|
||||
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(userList.get(i) + " : " + hostList.get(i));
|
||||
user.add_to_known_users(userList.get(i), hostList.get(i));
|
||||
}
|
||||
user.add_to_known_users(username, getLocalIP());
|
||||
connected = true;
|
||||
}
|
||||
}
|
||||
catch(SocketTimeoutException e)
|
||||
{
|
||||
user.add_to_known_users(username, getLocalIP());
|
||||
System.out.println("Reply timed out");
|
||||
connected = true;
|
||||
}
|
||||
}
|
||||
catch (SocketException e2)
|
||||
{
|
||||
System.out.println(e2.getMessage());
|
||||
}
|
||||
catch (Exception e3)
|
||||
{
|
||||
System.out.println(e3.getMessage());
|
||||
}
|
||||
|
||||
if(connected)
|
||||
{
|
||||
user.setpseudo(username);
|
||||
ReceiveThread t2 = new ReceiveThread(1237, chatText);
|
||||
ConnectionListenerThread t3 = new ConnectionListenerThread(user);
|
||||
t2.start();
|
||||
t3.start();
|
||||
}
|
||||
|
||||
return connected;
|
||||
}
|
||||
|
||||
void send (String message)
|
||||
{
|
||||
for(String address:user.getHosts())
|
||||
{
|
||||
SendThread t1 = new SendThread(user.getpseudo(), address, message, 1237);
|
||||
t1.start();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue