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 getAcceptor() 092 { 093 if( ( acceptor != null ) && acceptor.isDisposed() ) 094 { 095 acceptor = createAcceptor( getAddress(), getPort() ); 096 } 097 098 return acceptor == null ? null : (DatagramAcceptor)acceptor; 099 } 100 101 102 /** 103 * Helper method to create an IoAcceptor 104 */ 105 private IoAcceptor createAcceptor( String address, int port ) 106 { 107 NioDatagramAcceptor acceptor = new NioDatagramAcceptor(); 108 109 InetSocketAddress socketAddress = null; 110 111 // The address can be null here, if one want to connect using the wildcard address 112 if ( address == null ) 113 { 114 // Create a socket listening on the wildcard address 115 socketAddress = new InetSocketAddress( port ); 116 } 117 else 118 { 119 socketAddress = new InetSocketAddress( address, port ); 120 } 121 122 acceptor.setDefaultLocalAddress( socketAddress ); 123 124 return acceptor; 125 } 126 127 128 /** 129 * @see Object#toString() 130 */ 131 public String toString() 132 { 133 return "UdpTransport" + super.toString(); 134 } 135 }