Skip to content

Commit cab7731

Browse files
Added Thread naming based on remote socket address (#738) (#753)
- Added ThreadNameProvider to set name based on Thread Class and remote socket address - Added RemoteAddressProvider to abstract access to Remote Socket Address - Set Reader Thread name in TransportImpl - Set SFTP PacketReader Thread name in SFTPEngine - Set KeepAlive Thread name in SSHClient Co-authored-by: Jeroen van Erp <jeroen@hierynomus.com>
1 parent 50073db commit cab7731

File tree

11 files changed

+104
-13
lines changed

11 files changed

+104
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (C)2009 - SSHJ Contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.hierynomus.sshj.common;
17+
18+
import java.net.InetSocketAddress;
19+
20+
public interface RemoteAddressProvider {
21+
/**
22+
* Get Remote Socket Address associated with transport connection
23+
*
24+
* @return Remote Socket Address or null when not connected
25+
*/
26+
InetSocketAddress getRemoteSocketAddress();
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (C)2009 - SSHJ Contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.hierynomus.sshj.common;
17+
18+
import java.net.InetSocketAddress;
19+
20+
public class ThreadNameProvider {
21+
private static final String DISCONNECTED = "DISCONNECTED";
22+
23+
/**
24+
* Set Thread Name prefixed with sshj followed by class and remote address when connected
25+
*
26+
* @param thread Class of Thread being named
27+
* @param remoteAddressProvider Remote Address Provider associated with Thread
28+
*/
29+
public static void setThreadName(final Thread thread, final RemoteAddressProvider remoteAddressProvider) {
30+
final InetSocketAddress remoteSocketAddress = remoteAddressProvider.getRemoteSocketAddress();
31+
final String address = remoteSocketAddress == null ? DISCONNECTED : remoteSocketAddress.toString();
32+
final String threadName = String.format("sshj-%s-%s", thread.getClass().getSimpleName(), address);
33+
thread.setName(threadName);
34+
}
35+
}

src/main/java/net/schmizz/keepalive/Heartbeater.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ final class Heartbeater
2424
extends KeepAlive {
2525

2626
Heartbeater(ConnectionImpl conn) {
27-
super(conn, "heartbeater");
27+
super(conn, "sshj-Heartbeater");
2828
}
2929

3030
@Override

src/main/java/net/schmizz/keepalive/KeepAliveRunner.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class KeepAliveRunner extends KeepAlive {
3737
new LinkedList<Promise<SSHPacket, ConnectionException>>();
3838

3939
KeepAliveRunner(ConnectionImpl conn) {
40-
super(conn, "keep-alive");
40+
super(conn, "sshj-KeepAliveRunner");
4141
}
4242

4343
synchronized public int getMaxAliveCount() {

src/main/java/net/schmizz/sshj/SSHClient.java

+13
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package net.schmizz.sshj;
1717

1818
import net.schmizz.keepalive.KeepAlive;
19+
import com.hierynomus.sshj.common.ThreadNameProvider;
1920
import net.schmizz.sshj.common.*;
2021
import net.schmizz.sshj.connection.Connection;
2122
import net.schmizz.sshj.connection.ConnectionException;
@@ -56,6 +57,7 @@
5657
import java.io.Closeable;
5758
import java.io.File;
5859
import java.io.IOException;
60+
import java.net.InetSocketAddress;
5961
import java.net.ServerSocket;
6062
import java.nio.charset.Charset;
6163
import java.security.KeyPair;
@@ -443,6 +445,16 @@ public Connection getConnection() {
443445
return conn;
444446
}
445447

448+
/**
449+
* Get Remote Socket Address from Transport
450+
*
451+
* @return Remote Socket Address or null when not connected
452+
*/
453+
@Override
454+
public InetSocketAddress getRemoteSocketAddress() {
455+
return trans.getRemoteSocketAddress();
456+
}
457+
446458
/**
447459
* Returns the character set used to communicate with the remote machine for certain strings (like paths).
448460
*
@@ -795,6 +807,7 @@ protected void onConnect()
795807
trans.init(getRemoteHostname(), getRemotePort(), getInputStream(), getOutputStream());
796808
final KeepAlive keepAliveThread = conn.getKeepAlive();
797809
if (keepAliveThread.isEnabled()) {
810+
ThreadNameProvider.setThreadName(conn.getKeepAlive(), trans);
798811
keepAliveThread.start();
799812
}
800813
doKex();

src/main/java/net/schmizz/sshj/connection/channel/direct/SessionFactory.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
*/
1616
package net.schmizz.sshj.connection.channel.direct;
1717

18+
import com.hierynomus.sshj.common.RemoteAddressProvider;
1819
import net.schmizz.sshj.common.SSHException;
1920

2021
/** A factory interface for creating SSH {@link Session session channels}. */
21-
public interface SessionFactory {
22+
public interface SessionFactory extends RemoteAddressProvider {
2223

2324
/**
2425
* Opens a {@code session} channel. The returned {@link Session} instance allows {@link Session#exec(String)
@@ -27,7 +28,7 @@ public interface SessionFactory {
2728
*
2829
* @return the opened {@code session} channel
2930
*
30-
* @throws SSHException
31+
* @throws SSHException Thrown on session initialization failures
3132
* @see Session
3233
*/
3334
Session startSession()

src/main/java/net/schmizz/sshj/sftp/PacketReader.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public PacketReader(SFTPEngine engine) {
4141
this.engine = engine;
4242
log = engine.getLoggerFactory().getLogger(getClass());
4343
this.in = engine.getSubsystem().getInputStream();
44-
setName("sftp reader");
44+
setName("sshj-PacketReader");
4545
setDaemon(true);
4646
}
4747

src/main/java/net/schmizz/sshj/sftp/SFTPEngine.java

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package net.schmizz.sshj.sftp;
1717

18+
import com.hierynomus.sshj.common.ThreadNameProvider;
1819
import net.schmizz.concurrent.Promise;
1920
import net.schmizz.sshj.common.IOUtils;
2021
import net.schmizz.sshj.common.LoggerFactory;
@@ -68,6 +69,7 @@ public SFTPEngine(SessionFactory ssh, String pathSep)
6869
sub = session.startSubsystem("sftp");
6970
out = sub.getOutputStream();
7071
reader = new PacketReader(this);
72+
ThreadNameProvider.setThreadName(reader, ssh);
7173
pathHelper = new PathHelper(new PathHelper.Canonicalizer() {
7274
@Override
7375
public String canonicalize(String path)

src/main/java/net/schmizz/sshj/transport/Reader.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public final class Reader
2929
public Reader(TransportImpl trans) {
3030
this.trans = trans;
3131
log = trans.getConfig().getLoggerFactory().getLogger(getClass());
32-
setName("reader");
32+
setName("sshj-Reader");
3333
setDaemon(true);
3434
}
3535

src/main/java/net/schmizz/sshj/transport/Transport.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package net.schmizz.sshj.transport;
1717

18+
import com.hierynomus.sshj.common.RemoteAddressProvider;
1819
import com.hierynomus.sshj.key.KeyAlgorithm;
1920
import net.schmizz.sshj.Config;
2021
import net.schmizz.sshj.Service;
@@ -31,7 +32,7 @@
3132

3233
/** Transport layer of the SSH protocol. */
3334
public interface Transport
34-
extends SSHPacketHandler {
35+
extends SSHPacketHandler, RemoteAddressProvider {
3536

3637
/**
3738
* Sets the host information and the streams to be used by this transport. Identification information is exchanged
@@ -208,7 +209,7 @@ long write(SSHPacket payload)
208209
/**
209210
* Specify a {@code listener} that will be notified upon disconnection.
210211
*
211-
* @param listener
212+
* @param listener Disconnect Listener to be configured
212213
*/
213214
void setDisconnectListener(DisconnectListener listener);
214215

src/main/java/net/schmizz/sshj/transport/TransportImpl.java

+17-5
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
*/
1616
package net.schmizz.sshj.transport;
1717

18+
import com.hierynomus.sshj.common.ThreadNameProvider;
1819
import com.hierynomus.sshj.key.KeyAlgorithm;
1920
import com.hierynomus.sshj.key.KeyAlgorithms;
2021
import com.hierynomus.sshj.transport.IdentificationStringParser;
2122
import net.schmizz.concurrent.ErrorDeliveryUtil;
2223
import net.schmizz.concurrent.Event;
2324
import net.schmizz.sshj.AbstractService;
2425
import net.schmizz.sshj.Config;
25-
import net.schmizz.sshj.SSHClient;
2626
import net.schmizz.sshj.Service;
2727
import net.schmizz.sshj.common.*;
2828
import net.schmizz.sshj.transport.verification.AlgorithmsVerifier;
@@ -32,6 +32,7 @@
3232
import java.io.IOException;
3333
import java.io.InputStream;
3434
import java.io.OutputStream;
35+
import java.net.InetSocketAddress;
3536
import java.util.List;
3637
import java.util.concurrent.TimeUnit;
3738
import java.util.concurrent.locks.ReentrantLock;
@@ -128,8 +129,8 @@ static final class ConnInfo {
128129
public TransportImpl(Config config) {
129130
this.config = config;
130131
this.loggerFactory = config.getLoggerFactory();
131-
this.serviceAccept = new Event<TransportException>("service accept", TransportException.chainer, loggerFactory);
132-
this.close = new Event<TransportException>("transport close", TransportException.chainer, loggerFactory);
132+
this.serviceAccept = new Event<>("service accept", TransportException.chainer, loggerFactory);
133+
this.close = new Event<>("transport close", TransportException.chainer, loggerFactory);
133134
this.nullService = new NullService(this);
134135
this.service = nullService;
135136
this.log = loggerFactory.getLogger(getClass());
@@ -163,9 +164,20 @@ public void init(String remoteHost, int remotePort, InputStream in, OutputStream
163164
throw new TransportException(e);
164165
}
165166

167+
ThreadNameProvider.setThreadName(reader, this);
166168
reader.start();
167169
}
168170

171+
/**
172+
* Get Remote Socket Address using Connection Information
173+
*
174+
* @return Remote Socket Address or null when not connected
175+
*/
176+
@Override
177+
public InetSocketAddress getRemoteSocketAddress() {
178+
return connInfo == null ? null : new InetSocketAddress(getRemoteHost(), getRemotePort());
179+
}
180+
169181
/**
170182
* TransportImpl implements its own default DisconnectListener.
171183
*/
@@ -209,7 +221,7 @@ private void sendClientIdent() throws IOException {
209221
*
210222
* @param buffer The buffer to read from.
211223
* @return empty string if full ident string has not yet been received
212-
* @throws IOException
224+
* @throws IOException Thrown when protocol version is not supported
213225
*/
214226
private String readIdentification(Buffer.PlainBuffer buffer)
215227
throws IOException {
@@ -542,7 +554,7 @@ private void gotServiceAccept()
542554
* Got an SSH_MSG_UNIMPLEMENTED, so lets see where we're at and act accordingly.
543555
*
544556
* @param packet The 'unimplemented' packet received
545-
* @throws TransportException
557+
* @throws TransportException Thrown when key exchange is ongoing
546558
*/
547559
private void gotUnimplemented(SSHPacket packet)
548560
throws SSHException {

0 commit comments

Comments
 (0)