net.sf.eBus.client.ETCPServer Maven / Gradle / Ivy
The newest version!
//
// Copyright 2020 Charles W. Rapp
//
// Licensed 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 net.sf.eBus.client;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SocketChannel;
import net.sf.eBus.config.EConfigure.Service;
import net.sf.eBus.net.AsyncServerSocket;
import net.sf.eBus.net.ServerSocketListener;
/**
* Accepts new TCP (both secure and plain text) connections.
* The accepted TCP socket is used to create an
* {@link ETCPConnection} and configured according to the
* {@link Service} instance.
*
* @author Charles W. Rapp
*/
public final class ETCPServer
extends EServer
implements ServerSocketListener
{
//---------------------------------------------------------------
// Member data.
//
//-----------------------------------------------------------
// Locals.
//
/**
* The actual service socket.
*/
private AsyncServerSocket mAsyncServer;
//---------------------------------------------------------------
// Member methods.
//
//-----------------------------------------------------------
// Constructors.
//
/**
* Creates a new, closed TCP server instance.
* @param config TCP server configuration.
*/
public ETCPServer(final Service config)
{
super (config);
mAsyncServer = null;
} // end of ETCPServer(Service)
//
// end of Constructors.
//-----------------------------------------------------------
//-----------------------------------------------------------
// Abstract Method Implementations.
//
/**
* Creates a new {@code AsyncServerSocket} and opens that
* socket on the configured address.
* @throws IOException
* if there is an I/O failure creating and opening the server
* socket.
*/
@Override
protected void doOpen()
throws IOException
{
// Create the TCP server if it does not exist.
if (mAsyncServer == null)
{
final AsyncServerSocket.ServerBuilder builder =
AsyncServerSocket.builder();
mAsyncServer =
builder.selector(mConfiguration.serviceSelector())
.listener(this)
.build();
}
// Open this TCP server only if not already open.
if (!mAsyncServer.isOpen())
{
mAsyncServer.open(mAddress);
}
} // end of doOpen()
/**
* Closes and drops the open server socket.
*/
@Override
protected void doClose()
{
final AsyncServerSocket asyncServer = mAsyncServer;
if (asyncServer != null && asyncServer.isOpen())
{
mAsyncServer = null;
asyncServer.close();
}
} // end of doClose()
/**
* Closes the accept TCP channel.
* @param text explains the connect attempt failure.
* @param channel accepted TCP connection.
*/
@Override
protected void doAcceptFailed(String text,
SelectableChannel channel)
{
try
{
channel.close();
}
catch (IOException ioex)
{
// Ignore exception when closing.
}
} // end of doAcceptFailed(String, SelectableChannel)
//
// end of Abstract Method Implementations.
//-----------------------------------------------------------
//-----------------------------------------------------------
// ServerSocketListener Interface Implementation
//
/**
* Creates a {@link ERemoteApp} to handle this new
* connection if the connection passes the positive filter.
* If not, the accepted client connection is immediately
* closed.
* @param socket a {@code Socket} connection
* @param aserver an
* {@link net.sf.eBus.net.AsyncServerSocket}
*/
@Override
public void handleAccept(final SocketChannel socket,
final AsyncServerSocket aserver)
{
final InetSocketAddress iAddress =
(InetSocketAddress)
(socket.socket()).getRemoteSocketAddress();
acceptConnection(iAddress, socket);
} // end of handleAccept(SocketChannel, AsyncServerSocket)
/**
* The eBus service has unexpectedly closed. Re-open it.
* @param jex an {@code Exception} value
* @param aserver an
* {@link net.sf.eBus.net.AsyncServerSocket}
*/
@Override
public void handleClose(final Throwable jex,
final AsyncServerSocket aserver)
{
// Before doing anything else, drop the server socket
// reference and close the notification feed.
mAsyncServer = null;
serverClosed(jex);
} // end of handleClose(Throwable, AsyncServerSocket)
//
// ServerSocketListener Interface Implementation
//-----------------------------------------------------------
} // end of class ETCPServer