View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.directory.server.protocol.shared.transport;
20  
21  
22  import java.net.InetSocketAddress;
23  
24  import org.apache.mina.core.service.IoAcceptor;
25  import org.apache.mina.transport.socket.DatagramAcceptor;
26  import org.apache.mina.transport.socket.nio.NioDatagramAcceptor;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  
31  /**
32   * The Transport instance for UDP based protocols.
33   * 
34   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
35   */
36  public class UdpTransport extends AbstractTransport
37  {
38      /** A logger for this class */
39      private static final Logger LOG = LoggerFactory.getLogger( UdpTransport.class );
40  
41  
42      /**
43       * Creates an instance of the UdpTransport class 
44       */
45      public UdpTransport()
46      {
47          super();
48      }
49  
50  
51      /**
52       * Creates an instance of the UdpTransport class on localhost
53       * @param udpPort The port
54       */
55      public UdpTransport( int udpPort )
56      {
57          super( udpPort );
58  
59          this.acceptor = createAcceptor( null, udpPort );
60  
61          LOG.debug( "UDP Transport created : <*:{},>", udpPort );
62      }
63  
64  
65      /**
66       * Creates an instance of the UdpTransport class 
67       * @param address The address
68       * @param udpPort The port
69       */
70      public UdpTransport( String address, int udpPort )
71      {
72          super( address, udpPort );
73  
74          this.acceptor = createAcceptor( address, udpPort );
75  
76          LOG.debug( "UDP Transport created : <{}:{},>", address, udpPort );
77      }
78  
79  
80      /**
81       * Initialize the Acceptor if needed
82       */
83      public void init()
84      {
85          acceptor = createAcceptor( getAddress(), getPort() );
86          LOG.debug( "UDP Transport created : <{}:{},>", getAddress(), getPort() );
87      }
88  
89  
90      /**
91       * @return The associated DatagramAcceptor
92       */
93      public DatagramAcceptor getAcceptor()
94      {
95          if ( ( acceptor != null ) && acceptor.isDisposed() )
96          {
97              acceptor = createAcceptor( getAddress(), getPort() );
98          }
99  
100         return acceptor == null ? null : ( DatagramAcceptor ) acceptor;
101     }
102 
103 
104     /**
105      * Helper method to create an IoAcceptor
106      */
107     private IoAcceptor createAcceptor( String address, int port )
108     {
109         NioDatagramAcceptor acceptor = new NioDatagramAcceptor();
110 
111         InetSocketAddress socketAddress = null;
112 
113         // The address can be null here, if one want to connect using the wildcard address
114         if ( address == null )
115         {
116             // Create a socket listening on the wildcard address
117             socketAddress = new InetSocketAddress( port );
118         }
119         else
120         {
121             socketAddress = new InetSocketAddress( address, port );
122         }
123 
124         acceptor.setDefaultLocalAddress( socketAddress );
125 
126         return acceptor;
127     }
128 
129 
130     /**
131      * @see Object#toString()
132      */
133     @Override
134     public String toString()
135     {
136         return "UdpTransport" + super.toString();
137     }
138 }