1

I have a server and several "clients" (servers actually because of callbacks). A client can send a message to another only through the server. For this, the server must:

  1. Identify the calling client.
  2. Hold the clients' information and exported object reference so it is able to lookup the recipient.

I have read on the Remote Session pattern (1, 2, 3) and here and here, but I couldn't find the answer I was looking for.

For (1), I see the following options:

  1. The client sends its exported object reference during the call to the server.
  2. The client sends some identification information during the call to the server.
  3. The client is identified with getClientHost.

The recipient must be sent as some identification information since clients do not hold a reference to each other.

public interface RemoteClient extends Remote {

    void message(String sender, String message);
}

public interface RemoteServer extends Remote {

    void relayMessage(String recipient,       RemoteClient sender,     String msg);
                      // or some identifier?  // or string/identifier?
}

public class RemoteServerImpl extends UnicastRemoteObject implements RemoteServer {

    void relayMessage(String recipient, RemoteClient sender, String msg) {

        RemoteClient recp = lookup(recipient); // See point 2 below
        String sndr = getRepresentation(sender); // See below...
        recp.message(sndr, msg);

       // OR using

       String sndr = getRepresentation(getClientHost());
       // Then sender parameter is not needed
    }
}

I'm pretty sure getClientHost is not a reliable way of identifying the caller because it can disconnect and reconnect with a different IP, and I'm not sure if there are 2 computer in the same LAN that this method will be able to distinguish between them.

For (2), the options I see are:

  1. Keep a Map of the identification information and the clients' exported objects (as mentioned, but not recommended, in one of the above answers).
  2. Keep a Set of client information objects where these objects hold the remote object reference and whatever relevant information.

These are updated during login (registration) and logout.

Then lookup takes the information and returns the remote object reference and getRepresentation is similar to a reverse lookup.

My problem is not to make it work (it's working), it's to make it work correctly. Is there any advantage or preferred way from the above or otherwise?

Community
  • 1
  • 1
user1803551
  • 12,965
  • 5
  • 47
  • 74

1 Answers1

-3

You don't appear to have understood the remote session pattern at all. The session object the client is calling remote methods on is unique to the client, so there is no necessity for it to further identify itself during subsequent calls to the session. Whatever information the client sent to the login object to obtain the session can be stored in the session object, or the server can assign a unique client ID itself. The session object should also contain the callback of course: it's the only sensible place to put it.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • I didn't try to do the remote session pattern in my question, I just mentioned that I read those answers that use it and another 2 which do not. My code is just an example implementation of the functionality I am trying to achieve. I was asking, out of those different options, if there is one which is preferred/advantageous. So, if you recommend remote session pattern over those other methods, would you care to note its advantage, or what's wrong with the other approaches? – user1803551 Mar 03 '16 at 22:40
  • It's the answer to your question because it solves the problem completely without requiring extra parameters on each call after the first, and it has further advantages like enforceable session expiry, dead client detection, etc. NB if your clients change their IP addresses after registering their callbacks the callbacks won't work anyway, so this is not a unique objection to `getClientHost()`. – user207421 Mar 03 '16 at 22:43
  • If you can expand on "*further advantages like enforceable session expiry, dead client detection, etc.*" in your answer I would be happy to accept it since it will answer my question by showing that it is the preferred way to solve the problem. NB is `getClientHost()` generally a reliable way to uniquely identify a caller, considering that 2 clients can connect through the same gateway or proxy? – user1803551 Mar 03 '16 at 23:07
  • These are all described in your references (1,2,3). `getClientHost()` identifies the peer via `Socket.getRemoteAddress()` and friends, so yes it is subject to that limitation. – user207421 Mar 03 '16 at 23:47
  • Excuse me for interrupting. I have a similar problem and I don't understand the part about the callback. When you say "The session object also contains the callback of course.", in what way does the server session object (the one which exposes server methods) contain the callback? Is there a different session object that exposes the client methods? Maybe you can add a code example please? – Mark Sep 17 '16 at 04:07
  • @Mark The server session object contains the client callback because that's where you should put it. It's the only sensible place. – user207421 Sep 22 '16 at 07:19