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.DatagramAcceptor;
025    import org.apache.mina.transport.socket.nio.NioDatagramAcceptor;
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 UdpTransport extends AbstractTransport
036    {
037        /** A logger for this class */
038        private static final Logger LOG = LoggerFactory.getLogger( UdpTransport.class );
039    
040        /**
041         * Creates an instance of the UdpTransport class 
042         */
043        public UdpTransport()
044        {
045            super();
046        }
047        
048        
049        /**
050         * Creates an instance of the UdpTransport class on localhost
051         * @param udpPort The port
052         */
053        public UdpTransport( int udpPort )
054        {
055            super( udpPort );
056            
057            this.acceptor = createAcceptor( null, udpPort );
058            
059            LOG.debug( "UDP Transport created : <*:{},>", udpPort );
060        }
061        
062        
063        /**
064         * Creates an instance of the UdpTransport class 
065         * @param address The address
066         * @param udpPort The port
067         */
068        public UdpTransport( String address, int udpPort )
069        {
070            super( address, udpPort );
071            
072            this.acceptor = createAcceptor( address, udpPort );
073    
074            LOG.debug( "UDP Transport created : <{}:{},>", address, udpPort );
075        }
076        
077        
078        /**
079         * Initialize the Acceptor if needed
080         */
081        public void init()
082        {
083            acceptor = createAcceptor( getAddress(), getPort() );
084            LOG.debug( "UDP Transport created : <{}:{},>", getAddress(), getPort() );
085        }
086    
087        
088        /**
089         * @return The associated DatagramAcceptor
090         */
091        public DatagramAcceptor getDatagramAcceptor()
092        {
093            return acceptor == null ? null : (DatagramAcceptor)acceptor;
094        }
095        
096        
097        /**
098         * Helper method to create an IoAcceptor
099         */
100        private IoAcceptor createAcceptor( String address, int port )
101        {
102            NioDatagramAcceptor acceptor = new NioDatagramAcceptor();
103            
104            InetSocketAddress socketAddress =  null;
105            
106            // The address can be null here, if one want to connect using the wildcard address
107            if ( address == null )
108            {
109                // Create a socket listening on the wildcard address
110                socketAddress = new InetSocketAddress( port );
111            }
112            else
113            {
114                socketAddress = new InetSocketAddress( address, port );
115            }
116            
117            acceptor.setDefaultLocalAddress( socketAddress );
118            
119            return acceptor;
120        }
121        
122        
123        /**
124         * @see Object#toString()
125         */
126        public String toString()
127        {
128            return "UdpTransport" + super.toString();
129        }
130    }