Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2009-2015 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package org.glassfish.grizzly.nio.transport;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketAddress;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectionKey;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.glassfish.grizzly.Buffer;
import org.glassfish.grizzly.ConnectionProbe;
import org.glassfish.grizzly.Grizzly;
import org.glassfish.grizzly.WriteHandler;
import org.glassfish.grizzly.asyncqueue.AsyncQueueWriter;
import org.glassfish.grizzly.localization.LogMessages;
import org.glassfish.grizzly.nio.NIOConnection;
import org.glassfish.grizzly.nio.SelectorRunner;
import org.glassfish.grizzly.utils.Exceptions;
import org.glassfish.grizzly.utils.Holder;
import org.glassfish.grizzly.utils.JdkVersion;
import org.glassfish.grizzly.utils.NullaryFunction;
/**
* {@link org.glassfish.grizzly.Connection} implementation
* for the {@link UDPNIOTransport}
*
* @author Alexey Stashok
*/
@SuppressWarnings("unchecked")
public class UDPNIOConnection extends NIOConnection {
private static final Logger LOGGER = Grizzly.logger(UDPNIOConnection.class);
private static final boolean IS_MULTICAST_SUPPORTED;
private static final Method JOIN_METHOD;
private static final Method JOIN_WITH_SOURCE_METHOD;
private static final Method MK_GET_NETWORK_INTERFACE_METHOD;
private static final Method MK_GET_SOURCE_ADDRESS_METHOD;
private static final Method MK_DROP_METHOD;
private static final Method MK_BLOCK_METHOD;
private static final Method MK_UNBLOCK_METHOD;
static {
JdkVersion jdkVersion = JdkVersion.getJdkVersion();
JdkVersion minimumVersion = JdkVersion.parseVersion("1.7.0");
boolean isInitialized = false;
Method join = null, joinWithSource = null,
mkGetNetworkInterface = null, mkGetSourceAddress = null,
mkDrop = null, mkBlock = null, mkUnblock = null;
if (minimumVersion.compareTo(jdkVersion) <= 0) { // If JDK version is >= 1.7
try {
join = DatagramChannel.class.getMethod("join",
InetAddress.class, NetworkInterface.class);
joinWithSource = DatagramChannel.class.getMethod("join",
InetAddress.class, NetworkInterface.class, InetAddress.class);
final Class membershipKeyClass = loadClass("java.nio.channels.MembershipKey");
mkGetNetworkInterface = membershipKeyClass.getDeclaredMethod("networkInterface");
mkGetSourceAddress = membershipKeyClass.getDeclaredMethod("sourceAddress");
mkDrop = membershipKeyClass.getDeclaredMethod("drop");
mkBlock = membershipKeyClass.getDeclaredMethod("block", InetAddress.class);
mkUnblock = membershipKeyClass.getDeclaredMethod("unblock", InetAddress.class);
isInitialized = true;
} catch (Throwable t) {
LOGGER.log(Level.WARNING, LogMessages.WARNING_GRIZZLY_CONNECTION_UDPMULTICASTING_EXCEPTIONE(), t);
}
}
if (isInitialized) {
IS_MULTICAST_SUPPORTED = true;
JOIN_METHOD = join;
JOIN_WITH_SOURCE_METHOD = joinWithSource;
MK_GET_NETWORK_INTERFACE_METHOD = mkGetNetworkInterface;
MK_GET_SOURCE_ADDRESS_METHOD = mkGetSourceAddress;
MK_DROP_METHOD = mkDrop;
MK_BLOCK_METHOD = mkBlock;
MK_UNBLOCK_METHOD = mkUnblock;
} else {
IS_MULTICAST_SUPPORTED = false;
JOIN_METHOD = JOIN_WITH_SOURCE_METHOD =
MK_GET_NETWORK_INTERFACE_METHOD =
MK_GET_SOURCE_ADDRESS_METHOD = MK_DROP_METHOD =
MK_BLOCK_METHOD = MK_UNBLOCK_METHOD = null;
}
}
private final Object multicastSync;
private Map> membershipKeysMap;
Holder localSocketAddressHolder;
Holder peerSocketAddressHolder;
private int readBufferSize = -1;
private int writeBufferSize = -1;
public UDPNIOConnection(UDPNIOTransport transport,
DatagramChannel channel) {
super(transport);
this.channel = channel;
resetProperties();
multicastSync = IS_MULTICAST_SUPPORTED ? new Object() : null;
}
public boolean isConnected() {
return channel != null && ((DatagramChannel) channel).isConnected();
}
/**
* Joins a multicast group to begin receiving all datagrams sent to the
* group. If this connection is currently a member of the group on the given
* interface to receive all datagrams then this method call has no effect.
* Otherwise this connection joins the requested group and channel's
* membership in not source-specific.
*
* A multicast connection may join several multicast groups, including the
* same group on more than one interface. An implementation may impose a
* limit on the number of groups that may be joined at the same time.
*
* @param group The multicast address to join
* @param networkInterface The network interface on which to join the group
*
* @throws IOException
*/
public void join(final InetAddress group,
final NetworkInterface networkInterface) throws IOException {
join(group, networkInterface, null);
}
/**
* Joins a multicast group to begin receiving datagrams sent to the group
* from a given source address. If this connection is currently a member of
* the group on the given interface to receive datagrams from the given
* source address then this method call has no effect. Otherwise
* this connection joins the group and depending on the source parameter
* value (whether it's not null or null value) the connection's membership
* is or is not source-specific.
*
* Membership is cumulative and this method may be invoked again with the
* same group and interface to allow receiving datagrams sent by other
* source addresses to the group.
*
* @param group The multicast address to join
* @param networkInterface The network interface on which to join the group
* @param source The source address
*
* @throws java.io.IOException
*/
public void join(final InetAddress group,
final NetworkInterface networkInterface, final InetAddress source)
throws IOException {
if (!IS_MULTICAST_SUPPORTED) {
throw new UnsupportedOperationException("JDK 1.7+ required");
}
if (group == null) {
throw new IllegalArgumentException("group parameter can't be null");
}
if (networkInterface == null) {
throw new IllegalArgumentException("networkInterface parameter can't be null");
}
synchronized (multicastSync) {
Object membershipKey = join0((DatagramChannel) channel,
group, networkInterface, source);
if (membershipKeysMap == null) {
membershipKeysMap = new HashMap>();
}
Set