Java 类java.nio.channels.MembershipKey 实例源码

项目:cohorte-herald    文件:MulticastHandler.java   
/**
 * Leaves the group and closes the multicast socket.
 *
 * @throws IOException
 *             Error reading the address or leaving the group
 */
private void closeMulticast() throws IOException {

    if (pChannel == null) {
        // Nothing to do
        return;
    }

    try {
        // Leave the group, on all interfaces
        for (final MembershipKey key : pJoinedGroups) {
            key.drop();
        }

    } finally {
        // Close the socket
        pChannel.close();
    }
}
项目:cohorte-remote-services    文件:MulticastHandler.java   
/**
 * Leaves the group and closes the multicast socket.
 * 
 * @throws IOException
 *             Error reading the address or leaving the group
 */
private void closeMulticast() throws IOException {

    if (pChannel == null) {
        // Nothing to do
        return;
    }

    try {
        // Leave the group, on all interfaces
        for (final MembershipKey key : pJoinedGroups) {
            key.drop();
        }

    } finally {
        // Close the socket
        pChannel.close();
    }
}
项目:netty4.0.27Learn    文件:NioDatagramChannel.java   
@Override
public ChannelFuture leaveGroup(
        InetAddress multicastAddress, NetworkInterface networkInterface, InetAddress source,
        ChannelPromise promise) {
    checkJavaVersion();

    if (multicastAddress == null) {
        throw new NullPointerException("multicastAddress");
    }
    if (networkInterface == null) {
        throw new NullPointerException("networkInterface");
    }

    synchronized (this) {
        if (memberships != null) {
            List<MembershipKey> keys = memberships.get(multicastAddress);
            if (keys != null) {
                Iterator<MembershipKey> keyIt = keys.iterator();

                while (keyIt.hasNext()) {
                    MembershipKey key = keyIt.next();
                    if (networkInterface.equals(key.networkInterface())) {
                       if (source == null && key.sourceAddress() == null ||
                           source != null && source.equals(key.sourceAddress())) {
                           key.drop();
                           keyIt.remove();
                       }
                    }
                }
                if (keys.isEmpty()) {
                    memberships.remove(multicastAddress);
                }
            }
        }
    }

    promise.setSuccess();
    return promise;
}
项目:netty4.0.27Learn    文件:NioDatagramChannel.java   
/**
 * Block the given sourceToBlock address for the given multicastAddress on the given networkInterface
 */
@Override
public ChannelFuture block(
        InetAddress multicastAddress, NetworkInterface networkInterface,
        InetAddress sourceToBlock, ChannelPromise promise) {
    checkJavaVersion();

    if (multicastAddress == null) {
        throw new NullPointerException("multicastAddress");
    }
    if (sourceToBlock == null) {
        throw new NullPointerException("sourceToBlock");
    }

    if (networkInterface == null) {
        throw new NullPointerException("networkInterface");
    }
    synchronized (this) {
        if (memberships != null) {
            List<MembershipKey> keys = memberships.get(multicastAddress);
            for (MembershipKey key: keys) {
                if (networkInterface.equals(key.networkInterface())) {
                    try {
                        key.block(sourceToBlock);
                    } catch (IOException e) {
                        promise.setFailure(e);
                    }
                }
            }
        }
    }
    promise.setSuccess();
    return promise;
}
项目:netty4study    文件:NioDatagramChannel.java   
@Override
public ChannelFuture joinGroup(
        InetAddress multicastAddress, NetworkInterface networkInterface,
        InetAddress source, ChannelPromise promise) {
    if (PlatformDependent.javaVersion() >= 7) {
        if (multicastAddress == null) {
            throw new NullPointerException("multicastAddress");
        }

        if (networkInterface == null) {
            throw new NullPointerException("networkInterface");
        }

        try {
            MembershipKey key;
            if (source == null) {
                key = javaChannel().join(multicastAddress, networkInterface);
            } else {
                key = javaChannel().join(multicastAddress, networkInterface, source);
            }

            synchronized (this) {
                List<MembershipKey> keys = memberships.get(multicastAddress);
                if (keys == null) {
                    keys = new ArrayList<MembershipKey>();
                    memberships.put(multicastAddress, keys);
                }
                keys.add(key);
            }

            promise.setSuccess();
        } catch (Throwable e) {
            promise.setFailure(e);
        }
    } else {
        throw new UnsupportedOperationException();
    }
    return promise;
}
项目:netty4study    文件:NioDatagramChannel.java   
@Override
public ChannelFuture leaveGroup(
        InetAddress multicastAddress, NetworkInterface networkInterface, InetAddress source,
        ChannelPromise promise) {
    if (PlatformDependent.javaVersion() < 7) {
        throw new UnsupportedOperationException();
    }
    if (multicastAddress == null) {
        throw new NullPointerException("multicastAddress");
    }
    if (networkInterface == null) {
        throw new NullPointerException("networkInterface");
    }

    synchronized (this) {
        if (memberships != null) {
            List<MembershipKey> keys = memberships.get(multicastAddress);
            if (keys != null) {
                Iterator<MembershipKey> keyIt = keys.iterator();

                while (keyIt.hasNext()) {
                    MembershipKey key = keyIt.next();
                    if (networkInterface.equals(key.networkInterface())) {
                       if (source == null && key.sourceAddress() == null ||
                           source != null && source.equals(key.sourceAddress())) {
                           key.drop();
                           keyIt.remove();
                       }
                    }
                }
                if (keys.isEmpty()) {
                    memberships.remove(multicastAddress);
                }
            }
        }
    }

    promise.setSuccess();
    return promise;
}
项目:netty4study    文件:NioDatagramChannel.java   
/**
 * Block the given sourceToBlock address for the given multicastAddress on the given networkInterface
 */
@Override
public ChannelFuture block(
        InetAddress multicastAddress, NetworkInterface networkInterface,
        InetAddress sourceToBlock, ChannelPromise promise) {
    if (PlatformDependent.javaVersion() < 7) {
        throw new UnsupportedOperationException();
    } else {
        if (multicastAddress == null) {
            throw new NullPointerException("multicastAddress");
        }
        if (sourceToBlock == null) {
            throw new NullPointerException("sourceToBlock");
        }

        if (networkInterface == null) {
            throw new NullPointerException("networkInterface");
        }
        synchronized (this) {
            if (memberships != null) {
                List<MembershipKey> keys = memberships.get(multicastAddress);
                for (MembershipKey key: keys) {
                    if (networkInterface.equals(key.networkInterface())) {
                        try {
                            key.block(sourceToBlock);
                        } catch (IOException e) {
                            promise.setFailure(e);
                        }
                    }
                }
            }
        }
        promise.setSuccess();
        return promise;
    }
}
项目:netty-netty-5.0.0.Alpha1    文件:NioDatagramChannel.java   
@Override
public ChannelFuture joinGroup(
        InetAddress multicastAddress, NetworkInterface networkInterface,
        InetAddress source, ChannelPromise promise) {
    if (PlatformDependent.javaVersion() >= 7) {
        if (multicastAddress == null) {
            throw new NullPointerException("multicastAddress");
        }

        if (networkInterface == null) {
            throw new NullPointerException("networkInterface");
        }

        try {
            MembershipKey key;
            if (source == null) {
                key = javaChannel().join(multicastAddress, networkInterface);
            } else {
                key = javaChannel().join(multicastAddress, networkInterface, source);
            }

            synchronized (this) {
                List<MembershipKey> keys = memberships.get(multicastAddress);
                if (keys == null) {
                    keys = new ArrayList<MembershipKey>();
                    memberships.put(multicastAddress, keys);
                }
                keys.add(key);
            }

            promise.setSuccess();
        } catch (Throwable e) {
            promise.setFailure(e);
        }
    } else {
        throw new UnsupportedOperationException();
    }
    return promise;
}
项目:netty-netty-5.0.0.Alpha1    文件:NioDatagramChannel.java   
@Override
public ChannelFuture leaveGroup(
        InetAddress multicastAddress, NetworkInterface networkInterface, InetAddress source,
        ChannelPromise promise) {
    if (PlatformDependent.javaVersion() < 7) {
        throw new UnsupportedOperationException();
    }
    if (multicastAddress == null) {
        throw new NullPointerException("multicastAddress");
    }
    if (networkInterface == null) {
        throw new NullPointerException("networkInterface");
    }

    synchronized (this) {
        if (memberships != null) {
            List<MembershipKey> keys = memberships.get(multicastAddress);
            if (keys != null) {
                Iterator<MembershipKey> keyIt = keys.iterator();

                while (keyIt.hasNext()) {
                    MembershipKey key = keyIt.next();
                    if (networkInterface.equals(key.networkInterface())) {
                       if (source == null && key.sourceAddress() == null ||
                           source != null && source.equals(key.sourceAddress())) {
                           key.drop();
                           keyIt.remove();
                       }
                    }
                }
                if (keys.isEmpty()) {
                    memberships.remove(multicastAddress);
                }
            }
        }
    }

    promise.setSuccess();
    return promise;
}
项目:netty-netty-5.0.0.Alpha1    文件:NioDatagramChannel.java   
/**
 * Block the given sourceToBlock address for the given multicastAddress on the given networkInterface
 */
@Override
public ChannelFuture block(
        InetAddress multicastAddress, NetworkInterface networkInterface,
        InetAddress sourceToBlock, ChannelPromise promise) {
    if (PlatformDependent.javaVersion() < 7) {
        throw new UnsupportedOperationException();
    } else {
        if (multicastAddress == null) {
            throw new NullPointerException("multicastAddress");
        }
        if (sourceToBlock == null) {
            throw new NullPointerException("sourceToBlock");
        }

        if (networkInterface == null) {
            throw new NullPointerException("networkInterface");
        }
        synchronized (this) {
            if (memberships != null) {
                List<MembershipKey> keys = memberships.get(multicastAddress);
                for (MembershipKey key: keys) {
                    if (networkInterface.equals(key.networkInterface())) {
                        try {
                            key.block(sourceToBlock);
                        } catch (IOException e) {
                            promise.setFailure(e);
                        }
                    }
                }
            }
        }
        promise.setSuccess();
        return promise;
    }
}
项目:javapgm    文件:channelrecv.java   
public channelrecv (String[] args) throws IOException
{
    InetAddress group = InetAddress.getByName (this.group);
    ProtocolFamily pf = group instanceof Inet4Address ? StandardProtocolFamily.INET : StandardProtocolFamily.INET6;
    NetworkInterface ni = NetworkInterface.getByName (this.adapter);
    if (null == ni) ni = NetworkInterface.getByInetAddress (InetAddress.getByName (this.adapter));
    DatagramChannel dc = DatagramChannel.open (pf)
        .setOption (StandardSocketOptions.SO_REUSEADDR, true)
        .bind (new InetSocketAddress (this.port))
        .setOption (StandardSocketOptions.IP_MULTICAST_IF, ni);
    dc.configureBlocking (false);
    @SuppressWarnings("unused")
    MembershipKey key = dc.join (group, ni);
    ByteBuffer buffer = ByteBuffer.allocateDirect (this.max_tpdu);
    Selector selector = Selector.open();
    @SuppressWarnings("unused")
    SelectionKey sk = dc.register (selector, SelectionKey.OP_READ);

    while (true) {
        int keyCount = selector.select (1000);
        if (keyCount > 0) {
            selector.selectedKeys().clear();
            SocketAddress source = dc.receive (buffer);
            buffer.flip();
            byte[] bytes = new byte[buffer.remaining()];
            buffer.get (bytes, 0, bytes.length);
            buffer.clear();
            System.out.println ("packet: { " +
                  "\"src\": \"" + source + "\"" +
                ", \"data\": \"" + new String (bytes, 0, bytes.length) + "\"" +
                ", \"length\": " + bytes.length + "" +
                " }");
        }
    }
}
项目:netty4.0.27Learn    文件:NioDatagramChannel.java   
@Override
public ChannelFuture joinGroup(
        InetAddress multicastAddress, NetworkInterface networkInterface,
        InetAddress source, ChannelPromise promise) {

    checkJavaVersion();

    if (multicastAddress == null) {
        throw new NullPointerException("multicastAddress");
    }

    if (networkInterface == null) {
        throw new NullPointerException("networkInterface");
    }

    try {
        MembershipKey key;
        if (source == null) {
            key = javaChannel().join(multicastAddress, networkInterface);
        } else {
            key = javaChannel().join(multicastAddress, networkInterface, source);
        }

        synchronized (this) {
            List<MembershipKey> keys = null;
            if (memberships == null) {
                memberships = new HashMap<InetAddress, List<MembershipKey>>();
            } else {
                keys = memberships.get(multicastAddress);
            }
            if (keys == null) {
                keys = new ArrayList<MembershipKey>();
                memberships.put(multicastAddress, keys);
            }
            keys.add(key);
        }

        promise.setSuccess();
    } catch (Throwable e) {
        promise.setFailure(e);
    }

    return promise;
}
项目:rawsocketspi    文件:RSDatagramChannel.java   
@Override
public MembershipKey join(InetAddress arg0, NetworkInterface arg1)
        throws IOException {
    throw new UnsupportedOperationException();
}
项目:rawsocketspi    文件:RSDatagramChannel.java   
@Override
public MembershipKey join(InetAddress arg0, NetworkInterface arg1,
        InetAddress arg2) throws IOException {
    throw new UnsupportedOperationException();
}