outdoor users(5)
This commit is contained in:
parent
77a9c46bcc
commit
7971f14ad5
1 changed files with 75 additions and 57 deletions
|
@ -82,7 +82,7 @@ class OutdoorListenerThread extends Thread {
|
||||||
{
|
{
|
||||||
portNumber++;
|
portNumber++;
|
||||||
Socket s = (new ServerSocket(portNumber)).accept();
|
Socket s = (new ServerSocket(portNumber)).accept();
|
||||||
(new ReceiveThread(user, s, displayArea, known_users, knownUsersPanel, dest_sockets, outdoor_dest_sockets, false)).start();
|
(new ReceiveThread(user, s, displayArea, known_users, knownUsersPanel, dest_sockets, outdoor_dest_sockets, true, false)).start();
|
||||||
outdoor_dest_sockets.add(s);
|
outdoor_dest_sockets.add(s);
|
||||||
|
|
||||||
known_users.add(new User(username));
|
known_users.add(new User(username));
|
||||||
|
@ -118,12 +118,13 @@ class ReceiveThread extends Thread {
|
||||||
JTextArea displayArea;
|
JTextArea displayArea;
|
||||||
JTextArea knownUsersPanel;
|
JTextArea knownUsersPanel;
|
||||||
Boolean isOutdoor;
|
Boolean isOutdoor;
|
||||||
|
Boolean self;
|
||||||
List<User> known_users;
|
List<User> known_users;
|
||||||
List<Socket> dest_sockets;
|
List<Socket> dest_sockets;
|
||||||
List<Socket> outdoor_dest_sockets;
|
List<Socket> outdoor_dest_sockets;
|
||||||
|
|
||||||
ReceiveThread(User in_user, Socket in_socket, JTextArea in_displayArea, List<User> in_known_users, JTextArea in_knownUsersPanel,
|
ReceiveThread(User in_user, Socket in_socket, JTextArea in_displayArea, List<User> in_known_users, JTextArea in_knownUsersPanel,
|
||||||
List<Socket> in_dest_sockets, List<Socket> in_outdoor_dest_sockets, Boolean in_isOutdoor)
|
List<Socket> in_dest_sockets, List<Socket> in_outdoor_dest_sockets, Boolean in_isOutdoor, Boolean in_self)
|
||||||
{
|
{
|
||||||
user = in_user;
|
user = in_user;
|
||||||
socket = in_socket;
|
socket = in_socket;
|
||||||
|
@ -133,6 +134,7 @@ class ReceiveThread extends Thread {
|
||||||
dest_sockets = in_dest_sockets;
|
dest_sockets = in_dest_sockets;
|
||||||
outdoor_dest_sockets = in_outdoor_dest_sockets;
|
outdoor_dest_sockets = in_outdoor_dest_sockets;
|
||||||
isOutdoor = in_isOutdoor;
|
isOutdoor = in_isOutdoor;
|
||||||
|
self = in_self;
|
||||||
}
|
}
|
||||||
public void run()
|
public void run()
|
||||||
{
|
{
|
||||||
|
@ -141,45 +143,9 @@ class ReceiveThread extends Thread {
|
||||||
{
|
{
|
||||||
while(!exit)
|
while(!exit)
|
||||||
{
|
{
|
||||||
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
|
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
|
||||||
Message message = (Message) in.readObject();
|
Message message = (Message) in.readObject();
|
||||||
if(message.getText() != null)
|
if(isOutdoor)
|
||||||
{
|
|
||||||
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);
|
|
||||||
break;
|
|
||||||
//System.out.println("Removing " + message.getAuthor());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
knownUsersPanel.setText("");
|
|
||||||
knownUsersPanel.append("Online:\n");
|
|
||||||
for(User u:known_users)
|
|
||||||
{
|
|
||||||
knownUsersPanel.append(" " + u.getName() + " \n");
|
|
||||||
}
|
|
||||||
if(!message.getAuthor().equals(user.getName()))
|
|
||||||
{
|
|
||||||
socket.close();
|
|
||||||
if(!isOutdoor)
|
|
||||||
dest_sockets.remove(socket);
|
|
||||||
else
|
|
||||||
outdoor_dest_sockets.remove(socket);
|
|
||||||
}
|
|
||||||
exit = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
displayArea.setCaretPosition(displayArea.getDocument().getLength());
|
|
||||||
if(isOutdoor)
|
|
||||||
{
|
{
|
||||||
for(Socket s:dest_sockets)
|
for(Socket s:dest_sockets)
|
||||||
{
|
{
|
||||||
|
@ -191,19 +157,59 @@ class ReceiveThread extends Thread {
|
||||||
catch(IOException e){}
|
catch(IOException e){}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if(message.getText() != null)
|
||||||
|
{
|
||||||
|
displayArea.append(message.getAuthor() + " : " + message.getText() + "\n");
|
||||||
|
|
||||||
for(Socket s:outdoor_dest_sockets)
|
}
|
||||||
{
|
else
|
||||||
try
|
{
|
||||||
{
|
displayArea.append(message.getAuthor() + " has left the chat.\n");
|
||||||
ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
|
for(int i = 0;i < known_users.size();i ++)
|
||||||
out.writeObject(message);
|
{
|
||||||
}
|
|
||||||
catch(IOException e){}
|
if (known_users.get(i).getName().equals(message.getAuthor()))
|
||||||
}
|
{
|
||||||
|
known_users.remove(i);
|
||||||
|
break;
|
||||||
|
//System.out.println("Removing " + message.getAuthor());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
knownUsersPanel.setText("");
|
||||||
|
knownUsersPanel.append("Online:\n");
|
||||||
|
for(User u:known_users)
|
||||||
|
{
|
||||||
|
knownUsersPanel.append(" " + u.getName() + " \n");
|
||||||
|
}
|
||||||
|
if(!self)
|
||||||
|
{
|
||||||
|
socket.close();
|
||||||
|
if(!isOutdoor)
|
||||||
|
dest_sockets.remove(socket);
|
||||||
|
else
|
||||||
|
outdoor_dest_sockets.remove(socket);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
displayArea.setCaretPosition(displayArea.getDocument().getLength());
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!self)
|
||||||
|
{
|
||||||
|
for(Socket s:outdoor_dest_sockets)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
|
||||||
|
out.writeObject(message);
|
||||||
|
}
|
||||||
|
catch(IOException e){}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
catch(EOFException e) {}
|
catch(EOFException e) {}
|
||||||
catch(Exception e)
|
catch(Exception e)
|
||||||
|
@ -292,7 +298,7 @@ class ConnectionListenerThread extends Thread {
|
||||||
{
|
{
|
||||||
portNumber++;
|
portNumber++;
|
||||||
Socket s = (new ServerSocket(portNumber)).accept();
|
Socket s = (new ServerSocket(portNumber)).accept();
|
||||||
(new ReceiveThread(user, s, displayArea, known_users, knownUsersPanel, dest_sockets, outdoor_dest_sockets, false)).start();
|
(new ReceiveThread(user, s, displayArea, known_users, knownUsersPanel, dest_sockets, outdoor_dest_sockets, false, false)).start();
|
||||||
dest_sockets.add(s);
|
dest_sockets.add(s);
|
||||||
|
|
||||||
|
|
||||||
|
@ -425,7 +431,7 @@ public class NetworkClient {
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Socket requestSocket = new Socket(destinationIP, 1234);
|
Socket requestSocket = new Socket(destinationIP, 1233);
|
||||||
BufferedReader in = new BufferedReader(new InputStreamReader(requestSocket.getInputStream()));
|
BufferedReader in = new BufferedReader(new InputStreamReader(requestSocket.getInputStream()));
|
||||||
PrintWriter out = new PrintWriter(requestSocket.getOutputStream(), true);
|
PrintWriter out = new PrintWriter(requestSocket.getOutputStream(), true);
|
||||||
out.println(username);
|
out.println(username);
|
||||||
|
@ -450,7 +456,7 @@ public class NetworkClient {
|
||||||
{
|
{
|
||||||
Socket s = new Socket(a, portNumber);
|
Socket s = new Socket(a, portNumber);
|
||||||
dest_sockets.add(s);
|
dest_sockets.add(s);
|
||||||
(new ReceiveThread(user, s, chatText, known_users, knownUsersPanel, dest_sockets, outdoor_dest_sockets, false)).start();
|
(new ReceiveThread(user, s, chatText, known_users, knownUsersPanel, dest_sockets, outdoor_dest_sockets, false, false)).start();
|
||||||
}
|
}
|
||||||
Collections.sort(known_users);
|
Collections.sort(known_users);
|
||||||
connected = true;
|
connected = true;
|
||||||
|
@ -489,7 +495,7 @@ public class NetworkClient {
|
||||||
/*Attention, getLocalIP ne marche que sur un même réseau physique
|
/*Attention, getLocalIP ne marche que sur un même réseau physique
|
||||||
* pour tester avec Hamachi, il faut hardcoder les IP
|
* pour tester avec Hamachi, il faut hardcoder les IP
|
||||||
*/
|
*/
|
||||||
(new ReceiveThread(user, s2, chatText, known_users, knownUsersPanel, dest_sockets, outdoor_dest_sockets, false)).start();
|
(new ReceiveThread(user, s2, chatText, known_users, knownUsersPanel, dest_sockets, outdoor_dest_sockets, false, true)).start();
|
||||||
}
|
}
|
||||||
catch(IOException e)
|
catch(IOException e)
|
||||||
{
|
{
|
||||||
|
@ -502,7 +508,7 @@ public class NetworkClient {
|
||||||
{
|
{
|
||||||
Socket s = new Socket(destinationIP, portNumber);
|
Socket s = new Socket(destinationIP, portNumber);
|
||||||
dest_sockets.add(s);
|
dest_sockets.add(s);
|
||||||
(new ReceiveThread(user, s, chatText, known_users, knownUsersPanel, dest_sockets, outdoor_dest_sockets, false)).start();
|
(new ReceiveThread(user, s, chatText, known_users, knownUsersPanel, dest_sockets, outdoor_dest_sockets, false, false)).start();
|
||||||
}
|
}
|
||||||
catch(IOException e)
|
catch(IOException e)
|
||||||
{
|
{
|
||||||
|
@ -532,6 +538,7 @@ public class NetworkClient {
|
||||||
{
|
{
|
||||||
for(Socket s:dest_sockets)
|
for(Socket s:dest_sockets)
|
||||||
{
|
{
|
||||||
|
System.out.println("Indoor users:");
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
System.out.println(s.getInetAddress().getHostAddress() + ": local port " + s.getLocalPort() + ", remote port " + s.getPort());
|
System.out.println(s.getInetAddress().getHostAddress() + ": local port " + s.getLocalPort() + ", remote port " + s.getPort());
|
||||||
|
@ -540,6 +547,17 @@ public class NetworkClient {
|
||||||
}
|
}
|
||||||
catch(IOException e){}
|
catch(IOException e){}
|
||||||
}
|
}
|
||||||
|
System.out.println("Outdoor users:");
|
||||||
|
for(Socket s:outdoor_dest_sockets)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
System.out.println(s.getInetAddress().getHostAddress() + ": local port " + s.getLocalPort() + ", remote port " + s.getPort());
|
||||||
|
ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
|
||||||
|
out.writeObject(new Message(user.getName(), message));
|
||||||
|
}
|
||||||
|
catch(IOException e){}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void disconnect()
|
void disconnect()
|
||||||
|
|
Loading…
Reference in a new issue