/*
* pjftp FTP server.
* Copyright (C) 2012 Dmitriy Simbiriatin <dmitriy.simbiriatin@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package ds.pjftp.main;
import java.io.IOException;
import java.net.Socket;
import java.net.ServerSocket;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import ds.pjftp.log.LogWrapper;
import ds.pjftp.settings.SettingsHolder;
/**
* Main class, runs server at specified port and handles incoming connections.
*/
public class Server {
private static final LogWrapper LOGGER = LogWrapper.get(Server.class.getName());
/**
* Port which server listens for incoming connections.
*/
private final int port;
/**
* Constructs Server object.
* @param port server's port.
*/
public Server(final int port) {
this.port = port;
}
/**
* Starts the server and listens for income connections.
* @throws IOException if failed to connect to specified port.
*/
public void init() throws IOException {
final ServerSocket server = new ServerSocket(port);
final ExecutorService executor = Executors.newCachedThreadPool();
for (;;) {
final Socket client = server.accept();
executor.execute(new Interpreter(client));
}
}
public static void main(final String[] args) {
try {
final SettingsHolder loader = SettingsHolder.getInstance();
loader.loadSettings();
final Server server = new Server(loader.getSettings().getPort());
server.init();
} catch (IOException ex) {
LOGGER.error("Failed to initialize server: {0}", ex.getMessage());
}
}
}