Unicité du nom d'utilisateur + WIP déconnexion
This commit is contained in:
parent
de1ffdb4a8
commit
6c0c3f8747
3 changed files with 89 additions and 41 deletions
|
@ -58,6 +58,11 @@ public class ClientWindow implements ActionListener {
|
||||||
{
|
{
|
||||||
sendButton.addActionListener(this);
|
sendButton.addActionListener(this);
|
||||||
messageField.addActionListener(this);
|
messageField.addActionListener(this);
|
||||||
|
chatWindow.addWindowListener(new WindowAdapter() {
|
||||||
|
public void windowClosing(WindowEvent e) {
|
||||||
|
network.send(null);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
sendPanel.add(messageField);
|
sendPanel.add(messageField);
|
||||||
|
|
|
@ -1,15 +1,29 @@
|
||||||
package chat;
|
package chat;
|
||||||
public class Message {
|
public class Message {
|
||||||
|
|
||||||
|
private String text;
|
||||||
|
private String author;
|
||||||
|
|
||||||
private String msg = null;
|
public Message(String in_text, String in_author)
|
||||||
public void setmsg(String msg)
|
|
||||||
{
|
{
|
||||||
this.msg = msg;
|
text = in_text;
|
||||||
|
author = in_author;
|
||||||
}
|
}
|
||||||
public String getmsg()
|
public void setText(String new_text)
|
||||||
{
|
{
|
||||||
return msg;
|
text = new_text;
|
||||||
|
}
|
||||||
|
public String getText()
|
||||||
|
{
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
public void setAuthor(String new_author)
|
||||||
|
{
|
||||||
|
author = new_author;
|
||||||
|
}
|
||||||
|
public String getAuthor()
|
||||||
|
{
|
||||||
|
return author;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,39 +12,37 @@ import java.io.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
import chat.User;
|
import chat.User;
|
||||||
|
import chat.Message;
|
||||||
|
|
||||||
class SendThread extends Thread {
|
class SendThread extends Thread {
|
||||||
String username;
|
|
||||||
String address;
|
String address;
|
||||||
String message;
|
Message message;
|
||||||
int port;
|
int port;
|
||||||
|
|
||||||
SendThread(String in_username, String in_address, String in_message, int in_port)
|
SendThread(Message in_message, String in_address, int in_port)
|
||||||
{
|
{
|
||||||
username = in_username;
|
|
||||||
address = in_address;
|
|
||||||
message = in_message;
|
message = in_message;
|
||||||
|
address = in_address;
|
||||||
port = in_port;
|
port = in_port;
|
||||||
}
|
}
|
||||||
public void run()
|
public void run()
|
||||||
{
|
{
|
||||||
Socket link;
|
Socket link;
|
||||||
boolean connected = false;
|
boolean connected = false;
|
||||||
PrintWriter out;
|
ObjectOutputStream out;
|
||||||
|
|
||||||
while(!connected)
|
while(!connected)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
link = new Socket(address, port);
|
link = new Socket(address, port);
|
||||||
out = new PrintWriter(link.getOutputStream(),true);
|
out = new ObjectOutputStream(link.getOutputStream());
|
||||||
out.println(username + " : " + message);
|
out.writeObject(message);
|
||||||
|
|
||||||
connected = true;
|
connected = true;
|
||||||
}
|
}
|
||||||
catch(IOException e)
|
catch(IOException e)
|
||||||
{
|
{
|
||||||
System.out.println("nik1!");
|
e.printStackTrace();
|
||||||
connected = false;
|
connected = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -56,10 +54,13 @@ class ReceiveThread extends Thread {
|
||||||
|
|
||||||
int port;
|
int port;
|
||||||
JTextArea displayArea;
|
JTextArea displayArea;
|
||||||
ReceiveThread(int in_port, JTextArea in_displayArea)
|
List<User> known_users;
|
||||||
|
|
||||||
|
ReceiveThread(int in_port, JTextArea in_displayArea, List<User> in_known_users)
|
||||||
{
|
{
|
||||||
port = in_port;
|
port = in_port;
|
||||||
displayArea = in_displayArea;
|
displayArea = in_displayArea;
|
||||||
|
known_users = in_known_users;
|
||||||
}
|
}
|
||||||
public void run()
|
public void run()
|
||||||
{
|
{
|
||||||
|
@ -69,14 +70,33 @@ class ReceiveThread extends Thread {
|
||||||
boolean exit = false;
|
boolean exit = false;
|
||||||
|
|
||||||
while(!exit)
|
while(!exit)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
Socket link = servSocket.accept();
|
Socket link = servSocket.accept();
|
||||||
BufferedReader in = new BufferedReader(new InputStreamReader(link.getInputStream()));
|
ObjectInputStream in = new ObjectInputStream(link.getInputStream());
|
||||||
String message = in.readLine();
|
Message message = (Message) in.readObject();
|
||||||
displayArea.append(message + "\n");
|
if(message.getText() != null)
|
||||||
|
{
|
||||||
|
displayArea.append(message.getAuthor() + " : " + message.getText() + "\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
displayArea.append(message.getAuthor() + " has left the chat.\n");
|
||||||
|
for(int i = 0;i < known_users.size();i ++)
|
||||||
|
{
|
||||||
|
if (known_users.get(i).getName().equals(message.getAuthor()))
|
||||||
|
known_users.remove(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
displayArea.setCaretPosition(displayArea.getDocument().getLength());
|
displayArea.setCaretPosition(displayArea.getDocument().getLength());
|
||||||
link.close();
|
link.close();
|
||||||
}
|
}
|
||||||
|
catch(Exception e)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
catch(IOException e)
|
catch(IOException e)
|
||||||
|
@ -99,6 +119,7 @@ class ConnectionListenerThread extends Thread {
|
||||||
public void run()
|
public void run()
|
||||||
{
|
{
|
||||||
boolean exit = false;
|
boolean exit = false;
|
||||||
|
boolean accepted = true;
|
||||||
byte[] buffer = new byte[100];
|
byte[] buffer = new byte[100];
|
||||||
DatagramPacket request = new DatagramPacket(buffer, buffer.length);
|
DatagramPacket request = new DatagramPacket(buffer, buffer.length);
|
||||||
DatagramPacket responsePacket;
|
DatagramPacket responsePacket;
|
||||||
|
@ -139,11 +160,20 @@ class ConnectionListenerThread extends Thread {
|
||||||
response1 = bStream.toByteArray();
|
response1 = bStream.toByteArray();
|
||||||
responsePacket = new DatagramPacket(response1, response1.length, clientAddress, 1337);
|
responsePacket = new DatagramPacket(response1, response1.length, clientAddress, 1337);
|
||||||
responseSocket.send(responsePacket);
|
responseSocket.send(responsePacket);
|
||||||
if(known_users.indexOf(new User(username, clientAddress.getHostAddress())) == -1)
|
|
||||||
|
accepted = true;
|
||||||
|
for(User a:known_users)
|
||||||
|
{
|
||||||
|
accepted = accepted && (!username.equals(a.getName()));
|
||||||
|
}
|
||||||
|
if(accepted)
|
||||||
{
|
{
|
||||||
known_users.add(new User(username, clientAddress.getHostAddress()));
|
known_users.add(new User(username, clientAddress.getHostAddress()));
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
System.out.println("Username already taken");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch(SocketTimeoutException e) {}
|
catch(SocketTimeoutException e) {}
|
||||||
}
|
}
|
||||||
|
@ -183,6 +213,7 @@ public class NetworkClient {
|
||||||
|
|
||||||
boolean connect(String username)
|
boolean connect(String username)
|
||||||
{
|
{
|
||||||
|
boolean nameAvailable = true;
|
||||||
boolean connected = false;
|
boolean connected = false;
|
||||||
DatagramSocket connectionSocket;
|
DatagramSocket connectionSocket;
|
||||||
DatagramSocket userListSocket;
|
DatagramSocket userListSocket;
|
||||||
|
@ -223,9 +254,11 @@ public class NetworkClient {
|
||||||
|
|
||||||
userList = (ArrayList<User>) userListStream.readObject();
|
userList = (ArrayList<User>) userListStream.readObject();
|
||||||
|
|
||||||
if (userList.indexOf(user) != -1)
|
for(User a:userList)
|
||||||
connected = false;
|
{
|
||||||
else
|
nameAvailable = nameAvailable && (!username.equals(a.getName()));
|
||||||
|
}
|
||||||
|
if(nameAvailable)
|
||||||
{
|
{
|
||||||
System.out.println(userList.size() + " users currently connected");
|
System.out.println(userList.size() + " users currently connected");
|
||||||
for(int i = 0;i < userList.size();i ++)
|
for(int i = 0;i < userList.size();i ++)
|
||||||
|
@ -233,16 +266,19 @@ public class NetworkClient {
|
||||||
System.out.println(userList.get(i).getName() + " : " + userList.get(i).getAddress());
|
System.out.println(userList.get(i).getName() + " : " + userList.get(i).getAddress());
|
||||||
known_users.add(userList.get(i));
|
known_users.add(userList.get(i));
|
||||||
}
|
}
|
||||||
known_users.add(new User(username, "25.67.234.235"));
|
known_users.add(new User(username, "25.68.185.103"));
|
||||||
connected = true;
|
connected = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch(SocketTimeoutException e)
|
catch(SocketTimeoutException e)
|
||||||
{
|
{
|
||||||
known_users.add(new User(username, "25.67.234.235"));
|
known_users.add(new User(username, "25.68.185.103"));
|
||||||
System.out.println("Reply timed out");
|
System.out.println("Reply timed out");
|
||||||
connected = true;
|
connected = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
connectionSocket.close();
|
||||||
|
userListSocket.close();
|
||||||
}
|
}
|
||||||
catch (SocketException e2)
|
catch (SocketException e2)
|
||||||
{
|
{
|
||||||
|
@ -256,11 +292,7 @@ public class NetworkClient {
|
||||||
if(connected)
|
if(connected)
|
||||||
{
|
{
|
||||||
user.setName(username);
|
user.setName(username);
|
||||||
ReceiveThread t2 = new ReceiveThread(1237, chatText);
|
ReceiveThread t2 = new ReceiveThread(1237, chatText, known_users);
|
||||||
for(User a:known_users)
|
|
||||||
{
|
|
||||||
System.out.println(a.getName() + ":" + a.getAddress());
|
|
||||||
}
|
|
||||||
|
|
||||||
ConnectionListenerThread t3 = new ConnectionListenerThread(known_users);
|
ConnectionListenerThread t3 = new ConnectionListenerThread(known_users);
|
||||||
t2.start();
|
t2.start();
|
||||||
|
@ -274,11 +306,8 @@ public class NetworkClient {
|
||||||
{
|
{
|
||||||
for(User recipient:known_users)
|
for(User recipient:known_users)
|
||||||
{
|
{
|
||||||
if(recipient != user)
|
SendThread t1 = new SendThread(new Message(user.getName(), message), recipient.getAddress(), 1237);
|
||||||
{
|
|
||||||
SendThread t1 = new SendThread(user.getName(), recipient.getAddress(), message, 1237);
|
|
||||||
t1.start();
|
t1.start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue