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 */ 019package org.apache.directory.server.protocol.shared.transport; 020 021 022import java.net.InetSocketAddress; 023 024import org.apache.mina.core.service.IoAcceptor; 025import org.apache.mina.transport.socket.DatagramAcceptor; 026import org.apache.mina.transport.socket.nio.NioDatagramAcceptor; 027import org.slf4j.Logger; 028import org.slf4j.LoggerFactory; 029 030 031/** 032 * The Transport instance for UDP based protocols. 033 * 034 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 035 */ 036public class UdpTransport extends AbstractTransport 037{ 038 /** A logger for this class */ 039 private static final Logger LOG = LoggerFactory.getLogger( UdpTransport.class ); 040 041 042 /** 043 * Creates an instance of the UdpTransport class 044 */ 045 public UdpTransport() 046 { 047 super(); 048 } 049 050 051 /** 052 * Creates an instance of the UdpTransport class on localhost 053 * @param udpPort The port 054 */ 055 public UdpTransport( int udpPort ) 056 { 057 super( udpPort ); 058 059 this.acceptor = createAcceptor( null, udpPort ); 060 061 LOG.debug( "UDP Transport created : <*:{},>", udpPort ); 062 } 063 064 065 /** 066 * Creates an instance of the UdpTransport class 067 * @param address The address 068 * @param udpPort The port 069 */ 070 public UdpTransport( String address, int udpPort ) 071 { 072 super( address, udpPort ); 073 074 this.acceptor = createAcceptor( address, udpPort ); 075 076 LOG.debug( "UDP Transport created : <{}:{},>", address, udpPort ); 077 } 078 079 080 /** 081 * Initialize the Acceptor if needed 082 */ 083 public void init() 084 { 085 acceptor = createAcceptor( getAddress(), getPort() ); 086 LOG.debug( "UDP Transport created : <{}:{},>", getAddress(), getPort() ); 087 } 088 089 090 /** 091 * @return The associated DatagramAcceptor 092 */ 093 public DatagramAcceptor getAcceptor() 094 { 095 if ( ( acceptor != null ) && acceptor.isDisposed() ) 096 { 097 acceptor = createAcceptor( getAddress(), getPort() ); 098 } 099 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}