WIP connection décentralisée

This commit is contained in:
Louis Farina 2020-12-10 12:20:44 +01:00
parent 55f7d26a0f
commit 5dd3f1d0da
2 changed files with 154 additions and 72 deletions

View file

@ -1,18 +1,16 @@
package chat; package chat;
import java.net.*; import java.net.*;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.*; import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.io.BufferedReader; import chat.User;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.DatagramSocket;
class SendThread extends Thread { class SendThread extends Thread {
String username; String username;
@ -88,57 +86,76 @@ class ReceiveThread extends Thread {
} }
} }
/*class PopupWindow implements ActionListener { class ConnectionListenerThread extends Thread {
JFrame popupWindow;
JPanel popupPanel;
JButton submitButton;
JPanel fieldLine[] = new JPanel[4];
JTextField inputField[] = new JTextField[4];
JLabel fieldLabel[] = new JLabel[4];
PopupWindow() int port;
JTextArea displayArea;
User user;
ConnectionListenerThread(User in_user)
{ {
fieldLabel[0] = new JLabel("Destination IP address"); user = in_user;
fieldLabel[1] = new JLabel("Destination port"); }
fieldLabel[2] = new JLabel("Source port"); public void run()
fieldLabel[3] = new JLabel("Username");
popupPanel = new JPanel(new GridLayout(5,1));
for(int i = 0;i < 4; i ++)
{ {
inputField[i] = new JTextField(); Boolean exit = false;
fieldLine[i] = new JPanel(new GridLayout(1,2)); byte[] buffer = new byte[100];
fieldLine[i].add(fieldLabel[i]); DatagramPacket request = new DatagramPacket(buffer, buffer.length);
fieldLine[i].add(inputField[i]); DatagramPacket responsePacket1;
popupPanel.add(fieldLine[i]); DatagramPacket responsePacket2;
}
submitButton = new JButton("OK"); DatagramSocket requestSocket;
DatagramSocket responseSocket;
popupPanel.add(submitButton); ByteArrayOutputStream bStream = new ByteArrayOutputStream();
ObjectOutput oo = new ObjectOutputStream(bStream);
popupWindow = new JFrame(); String username;
popupWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
popupWindow.setSize(new Dimension(640, 480));
popupWindow.getContentPane().add(popupPanel, BorderLayout.CENTER); byte[] response1, response2;
try
{
requestSocket = new DatagramSocket(1234);
responseSocket = new DatagramSocket();
while(!exit)
{
try
{
requestSocket.receive(request);
username = new String(request.getData(), 0, request.getLength());
InetAddress clientAddress= request.getAddress();
popupWindow.pack(); oo.writeObject(user.getUsers());
popupWindow.setVisible(true); response1 = bStream.toByteArray();
} responsePacket1 = new DatagramPacket(response1, response1.length, clientAddress, 1235);
responseSocket.send(responsePacket1);
public void actionPerformed(ActionEvent event) { oo.writeObject(user.getHosts());
response2 = bStream.toByteArray();
responsePacket2 = new DatagramPacket(response2, response2.length, clientAddress, 1236);
responseSocket.send(responsePacket2);
if(!user.findUser(username))
user.add_to_known_users(username, clientAddress.toString());
} }
}*/ catch(SocketTimeoutException e)
{
}
}
}
catch(Exception e)
{
System.out.println("nik!!");
}
}
}
public class ClientWindow implements ActionListener { public class ClientWindow implements ActionListener {
JFrame chatWindow; JFrame chatWindow;
JPanel chatPanel; JPanel chatPanel;
JTextArea chatText; JTextArea chatText;
JScrollPane textScroll; JScrollPane textScroll;
@ -147,13 +164,65 @@ public class ClientWindow implements ActionListener {
JTextField messageField; JTextField messageField;
JButton sendButton; JButton sendButton;
String address; User user;
int sport;
int dport;
String username;
ClientWindow() 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;
username = JOptionPane.showInputDialog(chatWindow,
"Enter a username",
"POPOPOPOPUPUPOPOPUP",
JOptionPane.PLAIN_MESSAGE);
while (!connected)
{
connectionSocket = new DatagramSocket();
userListSocket = new DatagramSocket(1235);
hostListSocket = new DatagramSocket(1236);
connectionRequest = new DatagramPacket(username.getBytes(), username.length(),
InetAddress.getByName("192.168.1.255"), 1234);
try
{
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)
{
connected = true;
}
}
chatWindow = new JFrame("Système de clavardage 2.0.1"); chatWindow = new JFrame("Système de clavardage 2.0.1");
chatPanel = new JPanel(new GridLayout(2, 2)); chatPanel = new JPanel(new GridLayout(2, 2));
@ -182,25 +251,8 @@ public class ClientWindow implements ActionListener {
chatWindow.getContentPane().add(chatPanel, BorderLayout.CENTER); chatWindow.getContentPane().add(chatPanel, BorderLayout.CENTER);
//Display the window. //Display the window.
address = JOptionPane.showInputDialog(chatWindow,
"Enter the destination address",
"POPOPOPOPUPUPOPOPUP",
JOptionPane.PLAIN_MESSAGE);
sport = Integer.parseInt(JOptionPane.showInputDialog(chatWindow,
"Enter the source port",
"POPOPOPOPUPUPOPOPUP",
JOptionPane.PLAIN_MESSAGE));
dport = Integer.parseInt(JOptionPane.showInputDialog(chatWindow,
"Enter the destination port",
"POPOPOPOPUPUPOPOPUP",
JOptionPane.PLAIN_MESSAGE));
username = JOptionPane.showInputDialog(chatWindow,
"Enter a username",
"POPOPOPOPUPUPOPOPUP",
JOptionPane.PLAIN_MESSAGE);
chatWindow.pack(); chatWindow.pack();
chatWindow.setVisible(true); chatWindow.setVisible(true);

View file

@ -1,8 +1,26 @@
package chat; package chat;
import java.io.*;
import java.util.*;
public class User { public class User {
private String pseudo = null; private String pseudo;
private ArrayList<String> knownUsers;
private ArrayList<String> knownHosts;
User(String in_pseudo)
{
pseudo = in_pseudo;
knownUsers = new ArrayList<String>();
knownHosts = new ArrayList<String>();
}
public void add_to_known_users(String name, String address)
{
knownUsers.add(name);
knownHosts.add(address);
}
public void setpseudo(String pseudo) public void setpseudo(String pseudo)
{ {
this.pseudo = pseudo; this.pseudo = pseudo;
@ -11,4 +29,16 @@ public class User {
{ {
return pseudo; return pseudo;
} }
public Boolean findUser(String username)
{
return (knownUsers.indexOf(username) != -1);
}
public ArrayList<String> getUsers()
{
return knownUsers;
}
public ArrayList<String> getHosts()
{
return knownHosts;
}
} }