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.
/*
* Copyright (c) 2009, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
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.function.Supplier;
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;
/**
* {@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 {
boolean isInitialized = false;
Method join = null, joinWithSource = null, mkGetNetworkInterface = null, mkGetSourceAddress = null, mkDrop = null, mkBlock = null, mkUnblock = null;
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