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

Option to suppress connection info log during n2c connection #75

Merged
merged 2 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ public boolean isRunning() {
}

public void shutdown() {
log.info("Shutdown connection !!!");
if (showConnectionLog())
log.info("Shutdown connection !!!");

if (session != null) {
session.disableReconnection();
Expand Down Expand Up @@ -162,7 +163,8 @@ public SessionListenerAdapter() {

@Override
public void disconnected() {
log.info("Connection closed !!!");
if (showConnectionLog())
log.info("Connection closed !!!");
if (session != null) {
session.dispose();
}
Expand All @@ -182,7 +184,12 @@ public void disconnected() {

@Override
public void connected() {
log.info("Connected !!!");
if (showConnectionLog())
log.info("Connected !!!");
}
}

private boolean showConnectionLog() {
return log.isDebugEnabled() || (handshakeAgent != null && !handshakeAgent.isSuppressConnectionInfoLog());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bloxbean.cardano.yaci.core.network;

import com.bloxbean.cardano.yaci.core.protocol.Agent;
import com.bloxbean.cardano.yaci.core.protocol.handshake.HandshakeAgent;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
Expand All @@ -18,12 +19,12 @@ class Session implements Disposable {
private final Bootstrap clientBootstrap;
private Channel activeChannel;
private final AtomicBoolean shouldReconnect; //Not used currently
private final Agent handshakeAgent;
private final HandshakeAgent handshakeAgent;
private final Agent[] agents;

private SessionListener sessionListener;

public Session(SocketAddress socketAddress, Bootstrap clientBootstrap, Agent handshakeAgent, Agent[] agents) {
public Session(SocketAddress socketAddress, Bootstrap clientBootstrap, HandshakeAgent handshakeAgent, Agent[] agents) {
this.socketAddress = socketAddress;
this.clientBootstrap = clientBootstrap;
this.shouldReconnect = new AtomicBoolean(true);
Expand Down Expand Up @@ -67,7 +68,8 @@ public Disposable start() throws InterruptedException {

connectFuture.addListeners((ChannelFuture cf) -> {
if (cf.isSuccess()) {
log.info("Connection established");
if (showConnectionLog())
log.info("Connection established");
if (sessionListener != null)
sessionListener.connected();
//Listen to the channel closing
Expand Down Expand Up @@ -96,7 +98,8 @@ public Disposable start() throws InterruptedException {
*/
@Override
public void dispose() {
log.info("Disposing the session !!!");
if (showConnectionLog())
log.info("Disposing the session !!!");
// try {
if (activeChannel != null) {
activeChannel.close();
Expand Down Expand Up @@ -129,4 +132,8 @@ public void handshake() {
if (log.isDebugEnabled())
log.debug("Handshake successful");
}

private boolean showConnectionLog() {
return log.isDebugEnabled() || (handshakeAgent != null && !handshakeAgent.isSuppressConnectionInfoLog());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
@Slf4j
public class HandshakeAgent extends Agent<HandshakeAgentListener> {
private final VersionTable versionTable;
private boolean suppressConnectionInfoLog = false;

public HandshakeAgent(VersionTable versionTable) {
this.versionTable = versionTable;
Expand All @@ -38,7 +39,8 @@ public Message buildNextMessage() {
public void processResponse(Message message) {
if (message == null) return;
if (message instanceof AcceptVersion) {
log.info("Handshake Ok!!! {}", message);
if (log.isDebugEnabled() || !suppressConnectionInfoLog)
log.info("Handshake Ok!!! {}", message);
setProtocolVersion((AcceptVersion)message);
handshakeOk();
} else if (message instanceof VersionTable) {
Expand Down Expand Up @@ -68,4 +70,12 @@ public boolean isDone() {
public void reset() {
this.currenState = Propose;
}

public void setSuppressConnectionInfoLog(boolean suppressConnectionInfoLog) {
this.suppressConnectionInfoLog = suppressConnectionInfoLog;
}

public boolean isSuppressConnectionInfoLog() {
return suppressConnectionInfoLog;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,15 @@ public void setLocalClientProviderListener(LocalClientProviderListener listener)
this.localClientProviderListener = listener;
}

/**
* Suppress connection info log. Default is false
*
* @param flag
*/
public void suppressConnectionInfoLog(boolean flag) {
handshakeAgent.setSuppressConnectionInfoLog(flag);
}

@Override
public void close() throws Exception {
shutdown();
Expand Down
Loading