JInterface Can not connect to Peer Node

I ran into a nasty issue with jinterface when making a rapid succession of calls at the start of the application. I need to move this to a caching solution of the connections today but here is a quick solution for anyone else hitting their head against the wall with this. Jinterface swallows exception causes and it is very hard to hunt down the true underlying error. You will see the same error message for n-number of things that can go wrong farther down the stack.

First I’ve not added a boolean setter for if you want to use unique each time or not, so that’s something you can add. Also there’s a connection monitor for shared connections. I’ve left out all error handling for brevity. And finally, also for brevity, just used one constructor using the cookie to keep this shorter.

public class ConnectionFactory {

    private String cookie;
    private String selfNodeName;
    private String peerNodeName;
    private OtpSelf otpSelf;
    private OtpPeer otpPeer;
    private OtpConnection otpConnection;
    private final Object connectionMonitor = new Object();

    public ConnectionFactory(String selfNodeName, String cookie, String peerNodeName) {
		this.selfNodeName = selfNodeName;
		this.cookie = cookie;
		this.peerNodeName = peerNodeName;
                initialize();
    }

    public void initialize() {
		// assertions...
		String selfName = this.selfNodeName + "-" + UUID.randomUUID().toString();

		try {
			this.otpSelf = new OtpSelf(selfName.trim(), this.cookie);
		} catch (IOException e) {
			//
		}
		this.otpPeer = new OtpPeer(this.peerNodeName.trim());
	}

	public OtpConnection createConnection() {
		synchronized (this.connectionMonitor) {
			if (this.otpConnection == null) {
				try {
					doCreateConnection();
				} catch (IOException e) {
					//
				} catch (OtpAuthException e) {
                                 //
                         }
                  }
		return this.otpConnection;
		}
	}

	public void doCreateConnection() throws IOException, OtpAuthException {
		synchronized (this.connectionMonitor) {
			if (this.otpConnection != null) {
                             otpConnection.close();
			}

                       this.otpConnection = otpSelf.connect(otpPeer);
		}
	}

	public void destroy() {
		resetConnection();
	}

	public void resetConnection() {
		synchronized (this.connectionMonitor) {
			if (this.otpConnection != null) {
				otpConnection.close();
			}
			this.otpConnection = null;
		}
	}
}

Share/Save

Leave a Reply