Merge "MD-SAL StatisticsManager- Stopping statistics thread until i fix all the relev...
[controller.git] / opendaylight / netconf / netconf-ssh / src / main / java / org / opendaylight / controller / netconf / ssh / NetconfSSHServer.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.netconf.ssh;
9
10 import java.io.IOException;
11 import java.net.InetSocketAddress;
12 import java.net.ServerSocket;
13 import java.util.concurrent.atomic.AtomicLong;
14 import javax.annotation.concurrent.ThreadSafe;
15 import org.opendaylight.controller.netconf.ssh.threads.SocketThread;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 @ThreadSafe
20 public class NetconfSSHServer implements Runnable {
21
22     private static boolean acceptMore = true;
23     private ServerSocket ss = null;
24     private static final Logger logger =  LoggerFactory.getLogger(NetconfSSHServer.class);
25     private static final AtomicLong sesssionId = new AtomicLong();
26     private final InetSocketAddress clientAddress;
27
28     private NetconfSSHServer(int serverPort,InetSocketAddress clientAddress) throws Exception{
29
30         logger.trace("Creating SSH server socket on port {}",serverPort);
31         this.ss = new ServerSocket(serverPort);
32         if (!ss.isBound()){
33             throw new Exception("Socket can't be bound to requested port :"+serverPort);
34         }
35         logger.trace("Server socket created.");
36         this.clientAddress = clientAddress;
37
38     }
39
40
41     public static NetconfSSHServer start(int serverPort, InetSocketAddress clientAddress) throws Exception {
42         return new NetconfSSHServer(serverPort, clientAddress);
43     }
44
45     public void stop() throws Exception {
46         acceptMore = false;
47         logger.trace("Closing SSH server socket.");
48         ss.close();
49         logger.trace("SSH server socket closed.");
50     }
51
52     @Override
53     public void run() {
54         while (acceptMore) {
55             logger.trace("Starting new socket thread.");
56             try {
57                SocketThread.start(ss.accept(), clientAddress, sesssionId.incrementAndGet());
58             } catch (IOException e) {
59                 e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
60             }
61         }
62     }
63 }