How to properly identify and hold client references in RMI callback?

让人想犯罪 __ 提交于 2019-12-23 04:47:29

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/35781694/how-to-properly-identify-and-hold-client-references-in-rmi-callback

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!