001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 package org.apache.directory.server.protocol.shared.transport; 020 021 import java.net.InetSocketAddress; 022 023 import org.apache.mina.core.service.IoAcceptor; 024 import org.apache.mina.transport.socket.SocketAcceptor; 025 import org.apache.mina.transport.socket.nio.NioSocketAcceptor; 026 import org.slf4j.Logger; 027 import org.slf4j.LoggerFactory; 028 029 /** 030 * @org.apache.xbean.XBean 031 * 032 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 033 * @version $Rev$, $Date$ 034 */ 035 public class TcpTransport extends AbstractTransport 036 { 037 /** A logger for this class */ 038 private static final Logger LOG = LoggerFactory.getLogger( TcpTransport.class ); 039 040 /** 041 * Creates an instance of the TcpTransport class 042 */ 043 public TcpTransport() 044 { 045 super(); 046 } 047 048 049 /** 050 * Creates an instance of the TcpTransport class on localhost 051 * @param tcpPort The port 052 */ 053 public TcpTransport( int tcpPort ) 054 { 055 super( null, tcpPort, DEFAULT_NB_THREADS, DEFAULT_BACKLOG_NB ); 056 057 this.acceptor = createAcceptor( null, tcpPort, DEFAULT_NB_THREADS, DEFAULT_BACKLOG_NB ); 058 059 LOG.debug( "TCP Transport created : <*:{},>", tcpPort ); 060 } 061 062 063 /** 064 * Creates an instance of the TcpTransport class on localhost 065 * @param tcpPort The port 066 * @param nbThreads The number of threads to create in the acceptor 067 */ 068 public TcpTransport( int tcpPort, int nbThreads ) 069 { 070 super( null, tcpPort, nbThreads, DEFAULT_BACKLOG_NB ); 071 072 this.acceptor = createAcceptor( null, tcpPort, nbThreads, DEFAULT_BACKLOG_NB ); 073 074 LOG.debug( "TCP Transport created : <*:{},>", tcpPort ); 075 } 076 077 078 /** 079 * Creates an instance of the TcpTransport class 080 * @param address The address 081 * @param port The port 082 */ 083 public TcpTransport( String address, int tcpPort ) 084 { 085 super( address, tcpPort, DEFAULT_NB_THREADS, DEFAULT_BACKLOG_NB ); 086 this.acceptor = createAcceptor( address, tcpPort, DEFAULT_NB_THREADS, DEFAULT_BACKLOG_NB ); 087 088 LOG.debug( "TCP Transport created : <{}:{}>", address, tcpPort ); 089 } 090 091 092 /** 093 * Creates an instance of the TcpTransport class on localhost 094 * @param tcpPort The port 095 * @param nbThreads The number of threads to create in the acceptor 096 * @param backlog The queue size for incoming messages, waiting for the 097 * acceptor to be ready 098 */ 099 public TcpTransport( int tcpPort, int nbThreads, int backLog ) 100 { 101 super( LOCAL_HOST, tcpPort, nbThreads, backLog ); 102 this.acceptor = createAcceptor( null, tcpPort, nbThreads, backLog ); 103 104 LOG.debug( "TCP Transport created : <*:{},>", tcpPort ); 105 } 106 107 108 /** 109 * Creates an instance of the TcpTransport class 110 * @param address The address 111 * @param tcpPort The port 112 * @param nbThreads The number of threads to create in the acceptor 113 * @param backlog The queue size for incoming messages, waiting for the 114 * acceptor to be ready 115 */ 116 public TcpTransport( String address, int tcpPort, int nbThreads, int backLog ) 117 { 118 super( address, tcpPort, nbThreads, backLog ); 119 this.acceptor = createAcceptor( address, tcpPort, nbThreads, backLog ); 120 121 LOG.debug( "TCP Transport created : <{}:{},>", address, tcpPort ); 122 } 123 124 125 /** 126 * Initialize the Acceptor if needed 127 */ 128 public void init() 129 { 130 acceptor = createAcceptor( getAddress(), getPort(), getNbThreads(), getBackLog() ); 131 } 132 133 134 /** 135 * Helper method to create an IoAcceptor 136 */ 137 private IoAcceptor createAcceptor( String address, int port, int nbThreads, int backLog ) 138 { 139 NioSocketAcceptor acceptor = new NioSocketAcceptor( nbThreads ); 140 acceptor.setReuseAddress( true ); 141 acceptor.setBacklog( backLog ); 142 143 InetSocketAddress socketAddress = null; 144 145 // The address can be null here, if one want to connect using the wildcard address 146 if ( address == null ) 147 { 148 // Create a socket listening on the wildcard address 149 socketAddress = new InetSocketAddress( port ); 150 } 151 else 152 { 153 socketAddress = new InetSocketAddress( address, port ); 154 } 155 156 acceptor.setDefaultLocalAddress( socketAddress ); 157 158 return acceptor; 159 } 160 161 162 /** 163 * @return The associated SocketAcceptor 164 */ 165 public SocketAcceptor getAcceptor() 166 { 167 if( ( acceptor != null ) && acceptor.isDisposed() ) 168 { 169 acceptor = createAcceptor( getAddress(), getPort(), getNbThreads(), getBackLog() ); 170 } 171 172 return acceptor == null ? null : (SocketAcceptor)acceptor; 173 } 174 175 176 /** 177 * @see Object#toString() 178 */ 179 public String toString() 180 { 181 return "TcpTransport" + super.toString(); 182 } 183 }