
org.simpleframework.http.connect.SocketConnection Maven / Gradle / Ivy
/*
* SocketConnection.java October 2002
*
* Copyright (C) 2002, Niall Gallagher
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation.
*
* This library 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
*/
package org.simpleframework.http.connect;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.List;
import javax.net.ssl.SSLContext;
import org.simpleframework.http.Server;
import org.simpleframework.http.core.Container;
import org.simpleframework.http.core.ContainerServer;
/**
* The SocketConnection
is used to manage connections
* from a server socket. In order to achieve this it spawns a task
* to listen for incoming connect requests. When a TCP connection
* request arrives it hands off the SocketChannel
to
* the Server
which processes the request.
*
* This handles connections from a ServerSocketChannel
* object so that features such as SSL can be used by a server that
* uses this package. The background acceptor process will terminate
* if the connection is closed.
*
* @author Niall Gallagher
*
* @see org.simpleframework.http.Server
*/
public class SocketConnection implements Connection {
/**
* This is used to maintain the active connection end points.
*/
private final List active;
/**
* The processor is used to process connected HTTP pipelines.
*/
private final Server server;
/**
* Constructor for the SocketConnection
object. This
* will create a new connection that accepts incoming connections
* and hands these connections as Pipeline
objects
* to the specified processor. This in turn will deliver request
* and response objects to the internal container.
*
* @param server this is the processor that receives requests
*/
public SocketConnection(Server server) throws Exception {
this.active = new ArrayList();
this.server = server;
}
/**
* Constructor for the SocketConnection
object. This
* will create a new connection that accepts incoming connections
* and hands these connections as Pipeline
objects
* to the specified processor. This in turn will deliver request
* and response objects to the internal container.
*
* @param container this is the container that receives requests
*/
public SocketConnection(Container container) throws Exception {
this(new ContainerServer(container, 20));
}
/**
* This creates a new background task that will listen to the
* specified ServerAddress
for incoming TCP connect
* requests. When an connection is accepted it is handed to the
* internal Server
implementation as a pipeline. The
* background task is a non daemon task to ensure the server is
* kept active, to terminate the connection this can be closed.
*
* @param address this is the address used to accept connections
*/
public void connect(SocketAddress address) throws Exception {
connect(address, null);
}
/**
* This creates a new background task that will listen to the
* specified ServerAddress
for incoming TCP connect
* requests. When an connection is accepted it is handed to the
* internal Server
implementation as a pipeline. The
* background task is a non daemon task to ensure the server is
* kept active, to terminate the connection this can be closed.
*
* @param address this is the address used to accept connections
* @param context this is used for secure SSL connections
*/
public void connect(SocketAddress address, SSLContext context) throws Exception {
active.add(new Notifier(address, context, server));
}
/**
* This is used to close the connection and the server socket
* used to accept connections. This will perform a close of all
* connected server sockets that have been created from using
* the connect
method. The connection can be
* reused after the existing server sockets have been closed.
*
* @throws Exception thrown if there is a problem closing
*/
public void close() throws Exception {
for(Notifier port : active) {
port.close();
}
active.clear();
}
}