- netconf SSH bridge bundle
[controller.git] / opendaylight / netconf / netconf-ssh / src / main / java / org / opendaylight / controller / netconf / ssh / NetconfSSHServer.java
index dad149fd60b762704fbf4c5f4d79882c51b8b676..72135cc7dcdd39acddfd8d69d826f2683ee9b921 100644 (file)
@@ -1,32 +1,63 @@
-/*\r
- * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.\r
- *\r
- * This program and the accompanying materials are made available under the\r
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
- * and is available at http://www.eclipse.org/legal/epl-v10.html\r
+/*
+ * 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;
 
  */
 package org.opendaylight.controller.netconf.ssh;
 
+import java.io.IOException;
+import java.net.InetSocketAddress;
 import java.net.ServerSocket;
 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 boolean acceptMore = true;
-    private static final int SERVER_PORT = 830;
     private ServerSocket ss = null;
     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 {
     }
 
     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.
+            }
+        }
+    }
 }
 }