Understanding Chat service in sim-eth-basic

Hi

Today i watched a few tutorials for java RMI API.
So to be able to share object and method invocation in RMI we should have an interface and an implementation of it.
Client only knows about interface.

So sorry to ask it here, i am a noob and I am a bit confused how this is done in chat service in sim-eth example.

Client has it’s own implementation of interface ChatSessionListener through class ChatSessionCallback and share it with server.
Server has it’s own implementation of ChatSessionListener through class ChatSessionImpl and share with client.

Can you please explain why? (with more details please)

And why should we have this object in client service :

/**
     *  Shared with the server over RMI so that it can notify us about account
     *  related stuff.
     */
    private class ChatSessionCallback implements ChatSessionListener {
 
        @Override   
        public void playerJoined( int clientId, String playerName ) {
            if( log.isTraceEnabled() ) {            
                log.trace("playerJoined(" + clientId + ", " + playerName + ")");
            }
            for( ChatSessionListener l : listeners ) {
                l.playerJoined(clientId, playerName);
            }
        }
 
        @Override   
        public void newMessage( int clientId, String playerName, String message ) {
            if( log.isTraceEnabled() ) {            
                log.trace("newMessage(" + clientId + ", " + playerName + ", " + message + ")");
            }
            for( ChatSessionListener l : listeners ) {
                l.newMessage(clientId, playerName, message);
            }
        }
    
        @Override   
        public void playerLeft( int clientId, String playerName ) {
            if( log.isTraceEnabled() ) {            
                log.trace("playerLeft(" + clientId + ", " + playerName + ")");
            }
            for( ChatSessionListener l : listeners ) {
                l.playerLeft(clientId, playerName);
            }
        }
    }

which tries to notify other clients and has access to private List<ChatSessionListener> listeners = new CopyOnWriteArrayList<>();

Seems i have some fundamental issues understanding logic here, will appreciate if you direct me to some resources that can help me understand logic and concept better.

No. It’s trying to notify other listeners on THIS client. Else, how would any application code do anything with the chat messages?

Shouldn’t this done by server ?

The server is delivering the message to the other clients.

But what does a client do when it receives the message? Throw it away? Drop it on the floor? How will it ever get to the UI to be displayed?

Because code in the UI registers a listener with the CLIENT-side service… which is the loops you’ve included.

The CLIENT (this client. The one you have logged in with) receives a message from the server. Then it delivers it to the listeners that the LOCAL CLIENT code has registered with THIS CLIENT. Server is already out of the picture because it did the job of delivering the message to THIS CLIENT.

Add a bunch of printlns and trace through the code.

Ah, I thought these listeners
private List<ChatSessionListener> listeners = new CopyOnWriteArrayList<>();
are pointing to clients are involved in chat !! :sweat_smile:
Sorry !!