use of org.apache.sshd.common.Property in project mina-sshd by apache.
the class Nio2Service method setSocketOptions.
@SuppressWarnings("unchecked")
protected <S extends NetworkChannel> S setSocketOptions(S socket) throws IOException {
Collection<? extends SocketOption<?>> supported = socket.supportedOptions();
if (GenericUtils.isEmpty(supported)) {
return socket;
}
for (Map.Entry<Property<?>, ? extends Map.Entry<SocketOption<?>, ?>> ce : CONFIGURABLE_OPTIONS.entrySet()) {
Property<?> property = ce.getKey();
Map.Entry<SocketOption<?>, ?> defConfig = ce.getValue();
@SuppressWarnings("rawtypes") SocketOption option = defConfig.getKey();
setOption(socket, property, option, defConfig.getValue());
}
return socket;
}
use of org.apache.sshd.common.Property in project mina-sshd by apache.
the class Nio2ServiceTest method testSetSocketOptions.
// see SSHD-554, SSHD-722
@Test
public void testSetSocketOptions() throws Exception {
try (SshServer sshd = setupTestServer()) {
Map<Property<?>, Object> expectedOptions = new LinkedHashMap<>();
expectedOptions.put(CoreModuleProperties.SOCKET_KEEPALIVE, true);
expectedOptions.put(CoreModuleProperties.SOCKET_LINGER, 5);
expectedOptions.put(CoreModuleProperties.SOCKET_RCVBUF, 1024);
expectedOptions.put(CoreModuleProperties.SOCKET_REUSEADDR, true);
expectedOptions.put(CoreModuleProperties.SOCKET_SNDBUF, 1024);
expectedOptions.put(CoreModuleProperties.TCP_NODELAY, true);
for (Map.Entry<Property<?>, ?> oe : expectedOptions.entrySet()) {
PropertyResolverUtils.updateProperty(sshd, oe.getKey().getName(), oe.getValue());
}
Semaphore sigSem = new Semaphore(0, true);
Map<SocketOption<?>, Map.Entry<?, ?>> actualOptionValues = new HashMap<>(expectedOptions.size());
sshd.setSessionFactory(new SessionFactory(sshd) {
@Override
protected ServerSessionImpl doCreateSession(IoSession ioSession) throws Exception {
validateSocketOptions(ioSession);
sigSem.release();
return super.doCreateSession(ioSession);
}
private void validateSocketOptions(IoSession ioSession) throws Exception {
if (!(ioSession instanceof Nio2Session)) {
return;
}
AsynchronousSocketChannel socket = ((Nio2Session) ioSession).getSocket();
Collection<? extends SocketOption<?>> supported = socket.supportedOptions();
if (GenericUtils.isEmpty(supported)) {
return;
}
for (Map.Entry<Property<?>, ?> oe : expectedOptions.entrySet()) {
Property<?> prop = oe.getKey();
Object expValue = oe.getValue();
Map.Entry<SocketOption<?>, ?> optionEntry = Nio2Service.CONFIGURABLE_OPTIONS.get(prop);
SocketOption<?> option = optionEntry.getKey();
if (!supported.contains(option)) {
continue;
}
Object actValue = socket.getOption(option);
actualOptionValues.put(option, new SimpleImmutableEntry<>(expValue, actValue));
}
}
});
sshd.start();
int port = sshd.getPort();
long startTime = System.nanoTime();
try (Socket s = new Socket(TEST_LOCALHOST, port)) {
long endTime = System.nanoTime();
long duration = endTime - startTime;
assertTrue("Connect duration is too high: " + duration, duration <= TimeUnit.SECONDS.toNanos(15L));
assertTrue("Validation not completed on time", sigSem.tryAcquire(15L, TimeUnit.SECONDS));
} finally {
sshd.stop();
}
// they might ignore it
for (Map.Entry<SocketOption<?>, ? extends Map.Entry<?, ?>> mme : actualOptionValues.entrySet()) {
SocketOption<?> option = mme.getKey();
Map.Entry<?, ?> vp = mme.getValue();
Object expValue = vp.getKey();
Object actValue = vp.getValue();
Appendable output = Objects.equals(expValue, actValue) ? System.out : System.err;
output.append('\t').append(option.name()).append(": expected=").append(Objects.toString(expValue)).append(", actual=").append(Objects.toString(actValue)).append(System.lineSeparator());
if (output instanceof Flushable) {
((Flushable) output).flush();
}
}
}
}
Aggregations