diff --git a/src/chat/ClientWindow.java b/src/chat/ClientWindow.java index 7f5d5a6..165bbe6 100644 --- a/src/chat/ClientWindow.java +++ b/src/chat/ClientWindow.java @@ -1,18 +1,16 @@ package chat; + import java.net.*; + import javax.swing.*; import java.awt.*; import java.awt.event.*; +import java.io.*; +import java.util.*; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.PrintWriter; -import java.net.ServerSocket; -import java.net.Socket; -import java.net.DatagramSocket; +import chat.User; class SendThread extends Thread { String username; @@ -88,57 +86,76 @@ class ReceiveThread extends Thread { } } -/*class PopupWindow implements ActionListener { - JFrame popupWindow; - JPanel popupPanel; - JButton submitButton; - JPanel fieldLine[] = new JPanel[4]; - JTextField inputField[] = new JTextField[4]; - JLabel fieldLabel[] = new JLabel[4]; +class ConnectionListenerThread extends Thread { - PopupWindow() + int port; + JTextArea displayArea; + User user; + + ConnectionListenerThread(User in_user) { - fieldLabel[0] = new JLabel("Destination IP address"); - fieldLabel[1] = new JLabel("Destination port"); - fieldLabel[2] = new JLabel("Source port"); - fieldLabel[3] = new JLabel("Username"); + 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; - 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(); - fieldLine[i] = new JPanel(new GridLayout(1,2)); - fieldLine[i].add(fieldLabel[i]); - fieldLine[i].add(inputField[i]); - popupPanel.add(fieldLine[i]); + 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(); + + 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; - JPanel chatPanel; JTextArea chatText; JScrollPane textScroll; @@ -147,14 +164,66 @@ public class ClientWindow implements ActionListener { JTextField messageField; JButton sendButton; - String address; - int sport; - int dport; - String username; + User user; 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 userList; + ArrayList 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) userListStream.readObject(); + hostList = (ArrayList) 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)); chatText = new JTextArea(1,1); @@ -182,25 +251,8 @@ public class ClientWindow implements ActionListener { chatWindow.getContentPane().add(chatPanel, BorderLayout.CENTER); - //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); + //Display the window. chatWindow.pack(); chatWindow.setVisible(true); diff --git a/src/chat/User.java b/src/chat/User.java index 7165cf6..dbead23 100644 --- a/src/chat/User.java +++ b/src/chat/User.java @@ -1,8 +1,26 @@ package chat; +import java.io.*; +import java.util.*; + public class User { - private String pseudo = null; + private String pseudo; + private ArrayList knownUsers; + private ArrayList knownHosts; + + User(String in_pseudo) + { + pseudo = in_pseudo; + knownUsers = new ArrayList(); + knownHosts = new ArrayList(); + } + + public void add_to_known_users(String name, String address) + { + knownUsers.add(name); + knownHosts.add(address); + } public void setpseudo(String pseudo) { this.pseudo = pseudo; @@ -11,4 +29,16 @@ public class User { { return pseudo; } + public Boolean findUser(String username) + { + return (knownUsers.indexOf(username) != -1); + } + public ArrayList getUsers() + { + return knownUsers; + } + public ArrayList getHosts() + { + return knownHosts; + } }