Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds WebSocketListener at WebSocketService #787

Merged
merged 1 commit into from
Feb 10, 2019

Conversation

zacscoding
Copy link
Contributor

What does this PR do?

added WebSocketListener at WebSocketService

Where should the reviewer start?

WebSocketService, WebSocketServiceTest

Why is it needed?

I want to handle websocket events such as onClose event.
I think that it will be better if WebSocketClient have multiple listeners and WebSocketSerivce`s listener
is added.

@snazha-blkio snazha-blkio changed the base branch from master to release/4.0 November 14, 2018 09:44
@snazha-blkio snazha-blkio self-requested a review November 14, 2018 10:05
Copy link
Contributor

@snazha-blkio snazha-blkio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @zacscoding

It would make sense to have something like this at the lower level within WebSocketClient.
In there perhaps we could have a List<WebSocketListener> that is traversed and notified of events from within WebSocketClient.

Then modify WebSocketService to include a addWebSocketListener() method that will add the argument to the webSocketClient member.

@snazha-blkio snazha-blkio added the needs-user-changes PR has been reviewed and changes are requested label Nov 14, 2018
Copy link
Contributor

@iikirilov iikirilov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can see checkstyle errors by running ./gradlew check locally.

I want to handle websocket events such as onClose event.

I think a cleaner approach is to overload setWebSocketListener() to allow it to take functions as parameters which are invoked in the handlers. Something like:

    private void setWebSocketListener(Consumer<String> onMessage, Consumer<Exception> onError, Runnable onClose) {
        webSocketClient.setListener(new WebSocketListener() {
            @Override
            public void onMessage(String message) throws IOException {
                onWebSocketMessage(message);
                onMessage.accept(message);
            }

            @Override
            public void onError(Exception e) {
                log.error("Received error from a WebSocket connection", e);
                onError.accept(e);
            }

            @Override
            public void onClose() {
                onWebSocketClose();
                onClose.run();
            }
        });
    }

I think that it will be better if WebSocketClient have multiple listeners and WebSocketSerivce`s listener
is added.

AFAIK WebSocketClient can only have 1 listener.

To get this functionality we need to maintain a list (probably a map) of WebSocketClient in WebSocketService

@zacscoding
Copy link
Contributor Author

how about this ?

    public void connect() throws ConnectException {
        connect(null, null, null);
    }

    public void connect(Consumer<String> onMessage, Consumer<Throwable> onError, Runnable onClose) throws ConnectException {
        try {
            connectToWebSocket();
            setWebSocketListener(onMessage, onError, onClose);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            log.warn("Interrupted while connecting via WebSocket protocol");
        }
    }

    private void connectToWebSocket() throws InterruptedException, ConnectException {
        boolean connected = webSocketClient.connectBlocking();
        if (!connected) {
            throw new ConnectException("Failed to connect to WebSocket");
        }
    }

    private void setWebSocketListener(Consumer<String> onMessage, Consumer<Throwable> onError, Runnable onClose) {
        webSocketClient.setListener(new WebSocketListener() {
            @Override
            public void onMessage(String message) throws IOException {
                onWebSocketMessage(message);
                if (onMessage != null) {
                    onMessage.accept(message);
                }
            }

            @Override
            public void onError(Exception e) {
                log.error("Received error from a WebSocket connection", e);
                if (onError != null) {
                    onError.accept(e);
                }
            }

            @Override
            public void onClose() {
                onWebSocketClose();
                if (onClose != null) {
                    onClose.run();
                }
            }
        });
    }

I don`t know how to maintain multiple WebSocketClient in WebSocketService TT

@snazha-blkio
Copy link
Contributor

Your proposed solution re: consumers would be less intrusive on the tests, so i am ok with it.
@iikirilov ?

@snazha-blkio snazha-blkio added this to the 4.1 milestone Nov 26, 2018
@snazha-blkio snazha-blkio changed the base branch from release/4.0 to release/4.1 November 26, 2018 10:29
@zacscoding
Copy link
Contributor Author

added functional listeners at WebSocketService.
I hope to be applied this commit because i am using this code by define again :(

@@ -106,21 +111,30 @@ private void connectToWebSocket() throws InterruptedException, ConnectException
}
}

private void setWebSocketListener() {
private void setWebSocketListener(Consumer<String> onMessage, Consumer<Throwable> onError, Runnable onClose) {
webSocketClient.setListener(new WebSocketListener() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zacscoding , use of Runnable can be confusing as it would imply the implementation is invoked by another thread. Could you instead use Consumer<Void> please?
@iikirilov , thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If use Consumer, then have to called like onClose.accept(null);
is ok ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed to consumer :)

@snazha-blkio
Copy link
Contributor

@zacscoding can you please resolve the travis-ci checks, as per Ivaylo's suggestion?

@zacscoding
Copy link
Contributor Author

appreciate ur kindness 👍

@zacscoding
Copy link
Contributor Author

@snazha-blkio I updated source with checkstyle. is any problem?

@snazha-blkio
Copy link
Contributor

@snazha-blkio I updated source with checkstyle. is any problem?

Could you please add some unit tests for your changes? We can then look at getting this into 5.0

@snazha-blkio snazha-blkio modified the milestones: 4.1.x, 5.0 Jan 18, 2019
@zacscoding
Copy link
Contributor Author

@snazha-blkio added some testing code !

@snazha-blkio snazha-blkio added awaiting-release On release branch, awaiting build and removed needs-user-changes PR has been reviewed and changes are requested labels Feb 10, 2019
@snazha-blkio
Copy link
Contributor

Thanks @zacscoding , this will go out in 5.0

@snazha-blkio snazha-blkio changed the base branch from release/4.1 to release/5.0 February 10, 2019 20:10
@snazha-blkio snazha-blkio merged commit a906256 into hyperledger:release/5.0 Feb 10, 2019
@olimsaidov
Copy link
Contributor

@snazha-blkio is there any reason why this pull request cannot be released on 4.* versions? There is no breaking changes in public interface.

We really need this feature. Thank you.

@iikirilov
Copy link
Contributor

@olimsaidov yes we can do this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-release On release branch, awaiting build
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants