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

How to detect successful connection? #121

Open
birnbuazn opened this issue Dec 2, 2020 · 0 comments
Open

How to detect successful connection? #121

birnbuazn opened this issue Dec 2, 2020 · 0 comments

Comments

@birnbuazn
Copy link

birnbuazn commented Dec 2, 2020

Hi Pavlov,

I try to open a connection right before sending a message, but didn't find a way to get hold of a Future which will be completed, once the connection to a server has been established, so had to resort to busy waiting:

  void send(IsoMessage message) throws InterruptedException {
      reconnectIfNecessary();
      client.send(message);
  }  

 private void reconnectIfNecessary() throws InterruptedException {
    if (!client.isConnected()) {
      client.connect();
    }
    waitForConnection();
  }

  @SuppressWarnings("BusyWait")
  private void waitForConnection() throws InterruptedException {
    int retry = 0;
    while (!client.isConnected()) {
      checkState(retry < 10, "Client channel is not connected after %s ms", retry * 100);
      Thread.sleep(100);
      retry++;
    }
  }

There sure must be a better way of handling this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant