/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package aQute.shell.telnet;

import java.io.*;
import java.net.*;
import java.util.*;

import org.osgi.service.command.*;
import org.osgi.service.component.*;

public class TelnetShell extends Thread {
    boolean          quit;
    CommandProcessor processor;
    ServerSocket     server;
    int              port     = 2019;
    List<Handler>    handlers = new ArrayList<Handler>();

    protected void activate(ComponentContext context) {
        String s = (String) context.getProperties().get("port");
        if (s != null)
            port = Integer.parseInt(s);
        System.out.println("Telnet Listener at port " + port);
        start();
    }

    protected void deactivate(ComponentContext ctx) throws Exception {
        try {
            quit = true;
            server.close();
            interrupt();
        } catch (Exception e) {
            // Ignore
        }
    }

    public void run() {
        int delay = 0;
        try {
            while (!quit)
                try {
                    server = new ServerSocket(port);
                    delay = 5;
                    while (!quit) {
                        Socket socket = server.accept();
                        CommandSession session = processor.createSession(socket
                                .getInputStream(), new PrintStream(socket
                                .getOutputStream()), System.err);
                        Handler handler = new Handler(this, session, socket);
                        handlers.add(handler);
                        handler.start();
                    }
                } catch (BindException be) {
                    delay += 5;
                    System.err.println("Can not bind to port " + port);
                    try {
                        Thread.sleep(delay * 1000);
                    } catch (InterruptedException e) {
                        // who cares?
                    }
                } catch (Exception e) {
                    if (!quit)
                        e.printStackTrace();
                } finally {
                    try {
                        server.close();
                        Thread.sleep(2000);
                    } catch (Exception ie) {
                        //
                    }
                }

        } finally {
            try {
                if (server != null)
                    server.close();
            } catch (IOException e) {
                //
            }
            for (Handler handler : handlers)
                handler.close();
        }
    }

    public void setProcessor(CommandProcessor processor) {
        this.processor = processor;
    }
}
