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=72135cc7dcdd39acddfd8d69d826f2683ee9b921;hp=dad149fd60b762704fbf4c5f4d79882c51b8b676;hb=25ba6b145406b98f8521bcf510bb85bf0167ef72;hpb=d82b96055d2c0d471c85c2b2b3cb30e52fff1c69 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 dad149fd60..72135cc7dc 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 @@ -1,32 +1,63 @@ -/* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html +/* + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html */ 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.threads.SocketThread; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; -public class NetconfSSHServer { +@ThreadSafe +public class NetconfSSHServer implements Runnable { private static boolean acceptMore = true; - private static final int SERVER_PORT = 830; 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 NetconfSSHServer() throws Exception{ - this.ss = new ServerSocket(SERVER_PORT); - while (acceptMore) { - SocketThread.start(ss.accept()); + private NetconfSSHServer(int serverPort,InetSocketAddress clientAddress) throws Exception{ + + 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); } + logger.trace("Server socket created."); + this.clientAddress = clientAddress; + } - public static NetconfSSHServer start() throws Exception { - return new NetconfSSHServer(); + + + public static NetconfSSHServer start(int serverPort, InetSocketAddress clientAddress) throws Exception { + return new NetconfSSHServer(serverPort, clientAddress); } public void stop() throws Exception { - ss.close(); + acceptMore = false; + logger.trace("Closing SSH server socket."); + ss.close(); + logger.trace("SSH server socket closed."); } + @Override + public void run() { + while (acceptMore) { + logger.trace("Starting new socket thread."); + try { + SocketThread.start(ss.accept(), clientAddress, sesssionId.incrementAndGet()); + } catch (IOException e) { + e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. + } + } + } }