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++;
|
||||
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);
|
||||
|
||||
known_users.add(new User(username));
|
||||
|
@ -118,12 +118,13 @@ class ReceiveThread extends Thread {
|
|||
JTextArea displayArea;
|
||||
JTextArea knownUsersPanel;
|
||||
Boolean isOutdoor;
|
||||
Boolean self;
|
||||
List<User> known_users;
|
||||
List<Socket> 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,
|
||||
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;
|
||||
socket = in_socket;
|
||||
|
@ -133,6 +134,7 @@ class ReceiveThread extends Thread {
|
|||
dest_sockets = in_dest_sockets;
|
||||
outdoor_dest_sockets = in_outdoor_dest_sockets;
|
||||
isOutdoor = in_isOutdoor;
|
||||
self = in_self;
|
||||
}
|
||||
public void run()
|
||||
{
|
||||
|
@ -141,45 +143,9 @@ class ReceiveThread extends Thread {
|
|||
{
|
||||
while(!exit)
|
||||
{
|
||||
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
|
||||
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
|
||||
Message message = (Message) in.readObject();
|
||||
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);
|
||||
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)
|
||||
if(isOutdoor)
|
||||
{
|
||||
for(Socket s:dest_sockets)
|
||||
{
|
||||
|
@ -191,19 +157,59 @@ class ReceiveThread extends Thread {
|
|||
catch(IOException e){}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(message.getText() != null)
|
||||
{
|
||||
displayArea.append(message.getAuthor() + " : " + message.getText() + "\n");
|
||||
|
||||
for(Socket s:outdoor_dest_sockets)
|
||||
{
|
||||
try
|
||||
{
|
||||
ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
|
||||
out.writeObject(message);
|
||||
}
|
||||
catch(IOException e){}
|
||||
}
|
||||
}
|
||||
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(!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(Exception e)
|
||||
|
@ -292,7 +298,7 @@ class ConnectionListenerThread extends Thread {
|
|||
{
|
||||
portNumber++;
|
||||
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);
|
||||
|
||||
|
||||
|
@ -425,7 +431,7 @@ public class NetworkClient {
|
|||
}
|
||||
else
|
||||
{
|
||||
Socket requestSocket = new Socket(destinationIP, 1234);
|
||||
Socket requestSocket = new Socket(destinationIP, 1233);
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(requestSocket.getInputStream()));
|
||||
PrintWriter out = new PrintWriter(requestSocket.getOutputStream(), true);
|
||||
out.println(username);
|
||||
|
@ -450,7 +456,7 @@ public class NetworkClient {
|
|||
{
|
||||
Socket s = new Socket(a, portNumber);
|
||||
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);
|
||||
connected = true;
|
||||
|
@ -489,7 +495,7 @@ public class NetworkClient {
|
|||
/*Attention, getLocalIP ne marche que sur un même réseau physique
|
||||
* 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)
|
||||
{
|
||||
|
@ -502,7 +508,7 @@ public class NetworkClient {
|
|||
{
|
||||
Socket s = new Socket(destinationIP, portNumber);
|
||||
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)
|
||||
{
|
||||
|
@ -532,6 +538,7 @@ public class NetworkClient {
|
|||
{
|
||||
for(Socket s:dest_sockets)
|
||||
{
|
||||
System.out.println("Indoor users:");
|
||||
try
|
||||
{
|
||||
System.out.println(s.getInetAddress().getHostAddress() + ": local port " + s.getLocalPort() + ", remote port " + s.getPort());
|
||||
|
@ -540,6 +547,17 @@ public class NetworkClient {
|
|||
}
|
||||
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()
|
||||
|
|
Loading…
Reference in a new issue