Skip to content

Commit

Permalink
Updated KeepAlive and RemotePF examples (#791)
Browse files Browse the repository at this point in the history
- Set KeepAlive interval before connecting
  • Loading branch information
exceptionfactory authored Jul 12, 2022
1 parent 3de0302 commit c0f6000
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
4 changes: 2 additions & 2 deletions examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<groupId>com.hierynomus</groupId>
<artifactId>sshj-examples</artifactId>
<packaging>jar</packaging>
<version>0.19.1</version>
<version>0.33.0</version>

<name>sshj-examples</name>
<description>Examples for SSHv2 library for Java</description>
Expand Down Expand Up @@ -55,7 +55,7 @@
<dependency>
<groupId>com.hierynomus</groupId>
<artifactId>sshj</artifactId>
<version>0.31.0</version>
<version>0.33.0</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ public static void main(String... args)
final SSHClient ssh = new SSHClient(defaultConfig);
try {
ssh.addHostKeyVerifier(new PromiscuousVerifier());
// Set interval to enable keep-alive before connecting
ssh.getConnection().getKeepAlive().setKeepAliveInterval(5);
ssh.connect(args[0]);
ssh.getConnection().getKeepAlive().setKeepAliveInterval(5); //every 60sec
ssh.authPassword(args[1], args[2]);
Session session = ssh.startSession();
session.allocateDefaultPTY();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static void main(String... args)
client.loadKnownHosts();

client.connect("localhost");
client.getConnection().getKeepAlive().setKeepAliveInterval(5);
try {

client.authPublickey(System.getProperty("user.name"));
Expand All @@ -33,8 +34,6 @@ public static void main(String... args)
// what we do with incoming connections that are forwarded to us
new SocketForwardingConnectListener(new InetSocketAddress("google.com", 80)));

client.getTransport().setHeartbeatInterval(30);

// Something to hang on to so that the forwarding stays
client.getTransport().join();

Expand Down
15 changes: 15 additions & 0 deletions src/main/java/net/schmizz/keepalive/KeepAlive.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,29 @@ protected KeepAlive(ConnectionImpl conn, String name) {
setDaemon(true);
}

/**
* KeepAlive enabled based on KeepAlive interval
*
* @return Enabled when KeepInterval is greater than 0
*/
public boolean isEnabled() {
return keepAliveInterval > 0;
}

/**
* Get KeepAlive interval in seconds
*
* @return KeepAlive interval in seconds defaults to 0
*/
public synchronized int getKeepAliveInterval() {
return keepAliveInterval;
}

/**
* Set KeepAlive interval in seconds
*
* @param keepAliveInterval KeepAlive interval in seconds
*/
public synchronized void setKeepAliveInterval(int keepAliveInterval) {
this.keepAliveInterval = keepAliveInterval;
}
Expand Down

0 comments on commit c0f6000

Please sign in to comment.