WIP connection décentralisée
This commit is contained in:
parent
55f7d26a0f
commit
5dd3f1d0da
2 changed files with 154 additions and 72 deletions
|
@ -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");
|
{
|
||||||
|
Boolean exit = false;
|
||||||
|
byte[] buffer = new byte[100];
|
||||||
|
DatagramPacket request = new DatagramPacket(buffer, buffer.length);
|
||||||
|
DatagramPacket responsePacket1;
|
||||||
|
DatagramPacket responsePacket2;
|
||||||
|
|
||||||
popupPanel = new JPanel(new GridLayout(5,1));
|
DatagramSocket requestSocket;
|
||||||
|
DatagramSocket responseSocket;
|
||||||
|
|
||||||
for(int i = 0;i < 4; i ++)
|
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
|
||||||
|
ObjectOutput oo = new ObjectOutputStream(bStream);
|
||||||
|
|
||||||
|
String username;
|
||||||
|
|
||||||
|
byte[] response1, response2;
|
||||||
|
try
|
||||||
{
|
{
|
||||||
inputField[i] = new JTextField();
|
requestSocket = new DatagramSocket(1234);
|
||||||
fieldLine[i] = new JPanel(new GridLayout(1,2));
|
responseSocket = new DatagramSocket();
|
||||||
fieldLine[i].add(fieldLabel[i]);
|
|
||||||
fieldLine[i].add(inputField[i]);
|
while(!exit)
|
||||||
popupPanel.add(fieldLine[i]);
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
requestSocket.receive(request);
|
||||||
|
username = new String(request.getData(), 0, request.getLength());
|
||||||
|
InetAddress clientAddress= request.getAddress();
|
||||||
|
|
||||||
|
oo.writeObject(user.getUsers());
|
||||||
|
response1 = bStream.toByteArray();
|
||||||
|
responsePacket1 = new DatagramPacket(response1, response1.length, clientAddress, 1235);
|
||||||
|
responseSocket.send(responsePacket1);
|
||||||
|
|
||||||
|
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!!");
|
||||||
}
|
}
|
||||||
|
|
||||||
submitButton = new JButton("OK");
|
|
||||||
|
|
||||||
popupPanel.add(submitButton);
|
|
||||||
|
|
||||||
popupWindow = new JFrame();
|
|
||||||
popupWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
||||||
popupWindow.setSize(new Dimension(640, 480));
|
|
||||||
|
|
||||||
popupWindow.getContentPane().add(popupPanel, BorderLayout.CENTER);
|
|
||||||
|
|
||||||
|
|
||||||
popupWindow.pack();
|
|
||||||
popupWindow.setVisible(true);
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
public void actionPerformed(ActionEvent event) {
|
|
||||||
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
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,14 +164,66 @@ 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()
|
||||||
{
|
{
|
||||||
chatWindow = new JFrame("Système de clavardage 2.0.1");
|
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");
|
||||||
|
|
||||||
chatPanel = new JPanel(new GridLayout(2, 2));
|
chatPanel = new JPanel(new GridLayout(2, 2));
|
||||||
chatText = new JTextArea(1,1);
|
chatText = new JTextArea(1,1);
|
||||||
|
@ -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);
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue