X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-ssh%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Fssh%2FNetconfSSHServer.java;h=c6974d4982db051f1cb6a66d3f67394ee2d57817;hp=45807a83334f8e655b268c00bcb9c9e163ccf88f;hb=aefe82b158bc1694fe633053d04f2364bcbe67d9;hpb=1ff9939abc7a4072b07df6b79516fe344b1b42e3 diff --git a/opendaylight/netconf/netconf-ssh/src/main/java/org/opendaylight/controller/netconf/ssh/NetconfSSHServer.java b/opendaylight/netconf/netconf-ssh/src/main/java/org/opendaylight/controller/netconf/ssh/NetconfSSHServer.java index 45807a8333..c6974d4982 100644 --- a/opendaylight/netconf/netconf-ssh/src/main/java/org/opendaylight/controller/netconf/ssh/NetconfSSHServer.java +++ b/opendaylight/netconf/netconf-ssh/src/main/java/org/opendaylight/controller/netconf/ssh/NetconfSSHServer.java @@ -7,33 +7,34 @@ */ package org.opendaylight.controller.netconf.ssh; -import java.io.IOException; -import java.net.InetSocketAddress; -import java.net.ServerSocket; -import java.util.concurrent.atomic.AtomicLong; -import javax.annotation.concurrent.ThreadSafe; import org.opendaylight.controller.netconf.ssh.authentication.AuthProvider; import org.opendaylight.controller.netconf.ssh.threads.SocketThread; import org.opendaylight.controller.usermanager.IUserManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import javax.annotation.concurrent.ThreadSafe; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.ServerSocket; +import java.util.concurrent.atomic.AtomicLong; + @ThreadSafe -public class NetconfSSHServer implements Runnable { +public final class NetconfSSHServer implements Runnable { private ServerSocket ss = null; private static final Logger logger = LoggerFactory.getLogger(NetconfSSHServer.class); private static final AtomicLong sesssionId = new AtomicLong(); private final InetSocketAddress clientAddress; private final AuthProvider authProvider; - private boolean up = false; + private volatile boolean up = false; - private NetconfSSHServer(int serverPort,InetSocketAddress clientAddress, AuthProvider authProvider) throws Exception{ + private NetconfSSHServer(int serverPort,InetSocketAddress clientAddress, AuthProvider authProvider) throws IllegalStateException, IOException { logger.trace("Creating SSH server socket on port {}",serverPort); this.ss = new ServerSocket(serverPort); if (!ss.isBound()){ - throw new Exception("Socket can't be bound to requested port :"+serverPort); + throw new IllegalStateException("Socket can't be bound to requested port :"+serverPort); } logger.trace("Server socket created."); this.clientAddress = clientAddress; @@ -41,11 +42,11 @@ public class NetconfSSHServer implements Runnable { this.up = true; } - public static NetconfSSHServer start(int serverPort, InetSocketAddress clientAddress,AuthProvider authProvider) throws Exception { + public static NetconfSSHServer start(int serverPort, InetSocketAddress clientAddress,AuthProvider authProvider) throws IllegalStateException, IOException { return new NetconfSSHServer(serverPort, clientAddress,authProvider); } - public void stop() throws Exception { + public void stop() throws IOException { up = false; logger.trace("Closing SSH server socket."); ss.close(); @@ -67,9 +68,17 @@ public class NetconfSSHServer implements Runnable { while (up) { logger.trace("Starting new socket thread."); try { - SocketThread.start(ss.accept(), clientAddress, sesssionId.incrementAndGet(),authProvider); - } catch (IOException e) { - e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. + SocketThread.start(ss.accept(), clientAddress, sesssionId.incrementAndGet(), authProvider); + } + catch (IOException e) { + if( up ) { + logger.error("Exception occurred during socket thread initialization", e); + } + else { + // We're shutting down so an exception is expected as the socket's been closed. + // Log to debug. + logger.debug("Shutting down - got expected exception: " + e); + } } } }