RMI: determining the IP address of a remote object

I have a client-server RMI system. Server host information is known to customers so that they can view and make calls. On the first call to the server, clients send the remote link "to themselves" to the server so that the server can make callbacks. The server maintains a list of all connected clients (when a client disconnects it "removes" it from the server, and there is a thread that periodically checks whether the clients reach and supplant those that are not).

The server has a graphical interface (part of webapp), which allows users to visualize clients that are connected at any time. Now I am asked to display the IP address of the client in this interface.

My question is: in RMI, if you have a remote link to a remote object (stub), is there an easy way to determine the host (DNS name or IP address) on which this remote object really lives?

Note. I could use RemoteServer.getClientHost when the client first connects and stores the information, or I can implement a remote method on the client that returns the host information, but I want to know if there is a built-in RMI way to do this with the remote link.

+4
source share
2 answers

RMISecurityManager and UnicastRemoteObject say nothing about this.
You can use RemoteServer.getClientHost when the client connects.
I do not believe that this information can be obtained if it is not transmitted when the remote method is called.

+1
source

Note. Do not use this, see comments.

I have the same requirement when displaying the IP address of clients in the GUI. After debugging and analyzing the types of the remote object and its sub-objects, I came up with the following code:

  public static String getIpAddress(Object proxy) throws IllegalArgumentException, RemoteException, NotExpectedTypeException { InvocationHandler h = Proxy.getInvocationHandler(proxy); if (h instanceof RemoteObjectInvocationHandler) { RemoteRef remoteRef = ((RemoteObjectInvocationHandler) h).getRef(); if (remoteRef instanceof UnicastRef) { Endpoint ep = ((UnicastRef) remoteRef).getLiveRef().getChannel().getEndpoint(); if (ep instanceof TCPEndpoint) { return ((TCPEndpoint) ep).getHost(); } throw new NotExpectedTypeException(ep.getClass().getSimpleName(), "TCPEndpoint"); } throw new NotExpectedTypeException(remoteRef.getClass().getSimpleName(), "UnicastRef"); } throw new NotExpectedTypeException(h.getClass().getSimpleName(), "RemoteObjectInvocationHandler"); } 

where NotExpectedTypeException is a self- NotExpectedTypeException exception.

I'm not sure how reliable the method is, since it contains a lot of instanceof s. However, I successfully tested it on a local network where I used TCP connections.

-1
source

Source: https://habr.com/ru/post/1391644/


All Articles