No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Proxy.java 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package fr.insa.clavardator.server;
  2. import fr.insa.clavardator.lib.errors.UsernameTakenException;
  3. import fr.insa.clavardator.lib.message.Message;
  4. import fr.insa.clavardator.lib.network.TcpConnection;
  5. import fr.insa.clavardator.lib.network.TcpListener;
  6. import fr.insa.clavardator.lib.users.UserInformation;
  7. import fr.insa.clavardator.lib.util.Log;
  8. import java.io.EOFException;
  9. import java.io.Serializable;
  10. import java.util.HashMap;
  11. import java.util.Map;
  12. public class Proxy {
  13. private static final int PROXY_PORT = 35750;
  14. private final HashMap<String, TcpConnection> users = new HashMap<>();
  15. private final TcpListener proxyListener;
  16. public Proxy() {
  17. proxyListener = new TcpListener(PROXY_PORT);
  18. proxyListener.acceptConnection(clientSocket -> {
  19. Log.v(getClass().getSimpleName(), "Accepting a new user");
  20. TcpConnection connection = new TcpConnection(clientSocket);
  21. connection.receive(msg -> {
  22. if (msg instanceof Message) {
  23. Log.v(getClass().getSimpleName(), "Transmitting message: " + msg);
  24. transmitMessage((Serializable) msg, ((Message) msg).getRecipient().id);
  25. } else if (msg instanceof UsernameTakenException) {
  26. UsernameTakenException unameTaken = ((UsernameTakenException) msg);
  27. transmitMessage(unameTaken, unameTaken.recipient);
  28. } else if (msg instanceof UserInformation) {
  29. Log.v(getClass().getSimpleName(), "Registering new user: " + msg);
  30. users.put(((UserInformation) msg).id, connection);
  31. for (String userId : users.keySet()) {
  32. UserInformation userInfo = ((UserInformation) msg);
  33. if (!userId.equals(userInfo.id)) {
  34. transmitMessage((Serializable) msg, userId);
  35. }
  36. }
  37. }
  38. }, e -> {
  39. if (e instanceof EOFException) {
  40. for (Map.Entry<String, TcpConnection> user : users.entrySet()) {
  41. if (user.getValue().equals(connection)) {
  42. Log.v(getClass().getSimpleName(), "Disconnecting user " + user.getKey());
  43. users.remove(user.getKey());
  44. break;
  45. }
  46. }
  47. } else {
  48. Log.e(getClass().getSimpleName(), "Error while receiving message to transmit", e);
  49. }
  50. });
  51. },
  52. e -> Log.e(getClass().getSimpleName(), "Error while accepting a user", e));
  53. }
  54. void transmitMessage(Serializable msg, String recipientId) {
  55. TcpConnection user = users.get(recipientId);
  56. if (user == null) {
  57. Log.e(getClass().getSimpleName(), "Cannot find the recipient in the connected users");
  58. } else {
  59. user.send(msg, null, e -> Log.e(getClass().getSimpleName(), "Error while sending message", e));
  60. }
  61. }
  62. public void stop() {
  63. proxyListener.stopAccepting();
  64. for (TcpConnection connection : users.values()) {
  65. connection.close();
  66. }
  67. }
  68. }