Java Code Examples for java.net.ProtocolFamily

The following are top voted examples for showing how to use java.net.ProtocolFamily. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: jdk8u-jdk   File: JdpBroadcaster.java   Source Code and License 8 votes vote down vote up
/**
 * Create a new broadcaster
 *
 * @param address - multicast group address
 * @param srcAddress - address of interface we should use to broadcast.
 * @param port - udp port to use
 * @param ttl - packet ttl
 * @throws IOException
 */
public JdpBroadcaster(InetAddress address, InetAddress srcAddress, int port, int ttl)
        throws IOException, JdpException {
    this.addr = address;
    this.port = port;

    ProtocolFamily family = (address instanceof Inet6Address)
            ? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;

    channel = DatagramChannel.open(family);
    channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, ttl);

    // with srcAddress equal to null, this constructor do exactly the same as
    // if srcAddress is not passed
    if (srcAddress != null) {
        // User requests particular interface to bind to
        NetworkInterface interf = NetworkInterface.getByInetAddress(srcAddress);
        try {
            channel.bind(new InetSocketAddress(srcAddress, 0));
        } catch (UnsupportedAddressTypeException ex) {
            throw new JdpException("Unable to bind to source address");
        }
        channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf);
    }
}
 
Example 2
Project: OpenJSharp   File: JdpBroadcaster.java   Source Code and License 6 votes vote down vote up
/**
 * Create a new broadcaster
 *
 * @param address - multicast group address
 * @param srcAddress - address of interface we should use to broadcast.
 * @param port - udp port to use
 * @param ttl - packet ttl
 * @throws IOException
 */
public JdpBroadcaster(InetAddress address, InetAddress srcAddress, int port, int ttl)
        throws IOException, JdpException {
    this.addr = address;
    this.port = port;

    ProtocolFamily family = (address instanceof Inet6Address)
            ? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;

    channel = DatagramChannel.open(family);
    channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, ttl);

    // with srcAddress equal to null, this constructor do exactly the same as
    // if srcAddress is not passed
    if (srcAddress != null) {
        // User requests particular interface to bind to
        NetworkInterface interf = NetworkInterface.getByInetAddress(srcAddress);
        try {
            channel.bind(new InetSocketAddress(srcAddress, 0));
        } catch (UnsupportedAddressTypeException ex) {
            throw new JdpException("Unable to bind to source address");
        }
        channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf);
    }
}
 
Example 3
Project: bt   File: AddressUtils.java   Source Code and License 6 votes vote down vote up
public static InetAddress getDefaultRoute(Class<? extends InetAddress> type) {
	InetAddress target = null;
	
	ProtocolFamily family = type == Inet6Address.class ? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;
	
	try(DatagramChannel chan=DatagramChannel.open(family)) {
		if(type == Inet4Address.class)
			target = InetAddress.getByAddress(new byte[] {8,8,8,8});
		if(type == Inet6Address.class)
			target = InetAddress.getByName("2001:4860:4860::8888");
		
		chan.connect(new InetSocketAddress(target,63));
		
		InetSocketAddress soa = (InetSocketAddress) chan.getLocalAddress();
		InetAddress local = soa.getAddress();
		
		if(type.isInstance(local) && !local.isAnyLocalAddress())
			return local;
		return null;
	} catch (IOException e) {
		e.printStackTrace();
		return null;
	}
}
 
Example 4
Project: bt   File: LocalServiceDiscoveryInfo.java   Source Code and License 6 votes vote down vote up
public LocalServiceDiscoveryInfo(
        Set<SocketChannelConnectionAcceptor> socketAcceptors,
        Collection<AnnounceGroup> announceGroups) {

    this.localPorts = unmodifiableSet(collectLocalPorts(socketAcceptors));

    Collection<NetworkInterface> networkInterfaces = new HashSet<>();
    boolean acceptIP4 = false;
    boolean acceptIP6 = false;
    for (SocketChannelConnectionAcceptor acceptor : socketAcceptors) {
        networkInterfaces.add(acceptor.getNetworkInterface());
        InetSocketAddress address = acceptor.getLocalAddress();
        ProtocolFamily protocolFamily = InternetProtocolUtils.getProtocolFamily(address.getAddress());
        if (protocolFamily == StandardProtocolFamily.INET) {
            acceptIP4 = true;
        } else {
            acceptIP6 = true;
        }
        if (acceptIP4 && acceptIP6) {
            break; // no need to look further
        }
    }

    this.compatibleGroups = unmodifiableCollection(collectCompatibleGroups(announceGroups, acceptIP4, acceptIP6));
    this.networkInterfaces = unmodifiableCollection(networkInterfaces);
}
 
Example 5
Project: jdk8u_jdk   File: JdpBroadcaster.java   Source Code and License 6 votes vote down vote up
/**
 * Create a new broadcaster
 *
 * @param address - multicast group address
 * @param srcAddress - address of interface we should use to broadcast.
 * @param port - udp port to use
 * @param ttl - packet ttl
 * @throws IOException
 */
public JdpBroadcaster(InetAddress address, InetAddress srcAddress, int port, int ttl)
        throws IOException, JdpException {
    this.addr = address;
    this.port = port;

    ProtocolFamily family = (address instanceof Inet6Address)
            ? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;

    channel = DatagramChannel.open(family);
    channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, ttl);

    // with srcAddress equal to null, this constructor do exactly the same as
    // if srcAddress is not passed
    if (srcAddress != null) {
        // User requests particular interface to bind to
        NetworkInterface interf = NetworkInterface.getByInetAddress(srcAddress);
        try {
            channel.bind(new InetSocketAddress(srcAddress, 0));
        } catch (UnsupportedAddressTypeException ex) {
            throw new JdpException("Unable to bind to source address");
        }
        channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf);
    }
}
 
Example 6
Project: lookaside_java-1.8.0-openjdk   File: JdpBroadcaster.java   Source Code and License 6 votes vote down vote up
/**
 * Create a new broadcaster
 *
 * @param address - multicast group address
 * @param srcAddress - address of interface we should use to broadcast.
 * @param port - udp port to use
 * @param ttl - packet ttl
 * @throws IOException
 */
public JdpBroadcaster(InetAddress address, InetAddress srcAddress, int port, int ttl)
        throws IOException, JdpException {
    this.addr = address;
    this.port = port;

    ProtocolFamily family = (address instanceof Inet6Address)
            ? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;

    channel = DatagramChannel.open(family);
    channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, ttl);

    // with srcAddress equal to null, this constructor do exactly the same as
    // if srcAddress is not passed
    if (srcAddress != null) {
        // User requests particular interface to bind to
        NetworkInterface interf = NetworkInterface.getByInetAddress(srcAddress);
        try {
            channel.bind(new InetSocketAddress(srcAddress, 0));
        } catch (UnsupportedAddressTypeException ex) {
            throw new JdpException("Unable to bind to source address");
        }
        channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf);
    }
}
 
Example 7
Project: mldht   File: AddressUtils.java   Source Code and License 6 votes vote down vote up
public static InetAddress getDefaultRoute(Class<? extends InetAddress> type) {
	InetAddress target = null;
	
	ProtocolFamily family = type == Inet6Address.class ? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;
	
	try(DatagramChannel chan=DatagramChannel.open(family)) {
		if(type == Inet4Address.class)
			target = InetAddress.getByAddress(new byte[] {8,8,8,8});
		if(type == Inet6Address.class)
			target = InetAddress.getByName("2001:4860:4860::8888");
		
		chan.connect(new InetSocketAddress(target,63));
		
		InetSocketAddress soa = (InetSocketAddress) chan.getLocalAddress();
		InetAddress local = soa.getAddress();
		
		if(type.isInstance(local) && !local.isAnyLocalAddress())
			return local;
		return null;
	} catch (IOException e) {
		e.printStackTrace();
		return null;
	}
}
 
Example 8
Project: infobip-open-jdk-8   File: JdpBroadcaster.java   Source Code and License 6 votes vote down vote up
/**
 * Create a new broadcaster
 *
 * @param address - multicast group address
 * @param srcAddress - address of interface we should use to broadcast.
 * @param port - udp port to use
 * @param ttl - packet ttl
 * @throws IOException
 */
public JdpBroadcaster(InetAddress address, InetAddress srcAddress, int port, int ttl)
        throws IOException, JdpException {
    this.addr = address;
    this.port = port;

    ProtocolFamily family = (address instanceof Inet6Address)
            ? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;

    channel = DatagramChannel.open(family);
    channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, ttl);

    // with srcAddress equal to null, this constructor do exactly the same as
    // if srcAddress is not passed
    if (srcAddress != null) {
        // User requests particular interface to bind to
        NetworkInterface interf = NetworkInterface.getByInetAddress(srcAddress);
        try {
            channel.bind(new InetSocketAddress(srcAddress, 0));
        } catch (UnsupportedAddressTypeException ex) {
            throw new JdpException("Unable to bind to source address");
        }
        channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf);
    }
}
 
Example 9
Project: jdk8u-dev-jdk   File: JdpBroadcaster.java   Source Code and License 6 votes vote down vote up
/**
 * Create a new broadcaster
 *
 * @param address - multicast group address
 * @param srcAddress - address of interface we should use to broadcast.
 * @param port - udp port to use
 * @param ttl - packet ttl
 * @throws IOException
 */
public JdpBroadcaster(InetAddress address, InetAddress srcAddress, int port, int ttl)
        throws IOException, JdpException {
    this.addr = address;
    this.port = port;

    ProtocolFamily family = (address instanceof Inet6Address)
            ? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;

    channel = DatagramChannel.open(family);
    channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, ttl);

    // with srcAddress equal to null, this constructor do exactly the same as
    // if srcAddress is not passed
    if (srcAddress != null) {
        // User requests particular interface to bind to
        NetworkInterface interf = NetworkInterface.getByInetAddress(srcAddress);
        try {
            channel.bind(new InetSocketAddress(srcAddress, 0));
        } catch (UnsupportedAddressTypeException ex) {
            throw new JdpException("Unable to bind to source address");
        }
        channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf);
    }
}
 
Example 10
Project: OLD-OpenJDK8   File: JdpBroadcaster.java   Source Code and License 6 votes vote down vote up
/**
 * Create a new broadcaster
 *
 * @param address - multicast group address
 * @param srcAddress - address of interface we should use to broadcast.
 * @param port - udp port to use
 * @param ttl - packet ttl
 * @throws IOException
 */
public JdpBroadcaster(InetAddress address, InetAddress srcAddress, int port, int ttl)
        throws IOException, JdpException {
    this.addr = address;
    this.port = port;

    ProtocolFamily family = (address instanceof Inet6Address)
            ? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;

    channel = DatagramChannel.open(family);
    channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, ttl);

    // with srcAddress equal to null, this constructor do exactly the same as
    // if srcAddress is not passed
    if (srcAddress != null) {
        // User requests particular interface to bind to
        NetworkInterface interf = NetworkInterface.getByInetAddress(srcAddress);
        try {
            channel.bind(new InetSocketAddress(srcAddress, 0));
        } catch (UnsupportedAddressTypeException ex) {
            throw new JdpException("Unable to bind to source address");
        }
        channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf);
    }
}
 
Example 11
Project: openjdk-jdk7u-jdk   File: JdpBroadcaster.java   Source Code and License 6 votes vote down vote up
/**
 * Create a new broadcaster
 *
 * @param address - multicast group address
 * @param srcAddress - address of interface we should use to broadcast.
 * @param port - udp port to use
 * @param ttl - packet ttl
 * @throws IOException
 */
public JdpBroadcaster(InetAddress address, InetAddress srcAddress, int port, int ttl)
        throws IOException, JdpException {
    this.addr = address;
    this.port = port;

    ProtocolFamily family = (address instanceof Inet6Address)
            ? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;

    channel = DatagramChannel.open(family);
    channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, ttl);

    // with srcAddress equal to null, this constructor do exactly the same as
    // if srcAddress is not passed
    if (srcAddress != null) {
        // User requests particular interface to bind to
        NetworkInterface interf = NetworkInterface.getByInetAddress(srcAddress);
        try {
            channel.bind(new InetSocketAddress(srcAddress, 0));
        } catch (UnsupportedAddressTypeException ex) {
            throw new JdpException("Unable to bind to source address");
        }
        channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf);
    }
}
 
Example 12
Project: openjdk-icedtea7   File: JdpBroadcaster.java   Source Code and License 6 votes vote down vote up
/**
 * Create a new broadcaster
 *
 * @param address - multicast group address
 * @param srcAddress - address of interface we should use to broadcast.
 * @param port - udp port to use
 * @param ttl - packet ttl
 * @throws IOException
 */
public JdpBroadcaster(InetAddress address, InetAddress srcAddress, int port, int ttl)
        throws IOException, JdpException {
    this.addr = address;
    this.port = port;

    ProtocolFamily family = (address instanceof Inet6Address)
            ? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;

    channel = DatagramChannel.open(family);
    channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, ttl);

    // with srcAddress equal to null, this constructor do exactly the same as
    // if srcAddress is not passed
    if (srcAddress != null) {
        // User requests particular interface to bind to
        NetworkInterface interf = NetworkInterface.getByInetAddress(srcAddress);
        try {
            channel.bind(new InetSocketAddress(srcAddress, 0));
        } catch (UnsupportedAddressTypeException ex) {
            throw new JdpException("Unable to bind to source address");
        }
        channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf);
    }
}
 
Example 13
Project: javapgm   File: SourcePathMessage.java   Source Code and License 6 votes vote down vote up
public static SocketBuffer create (ProtocolFamily family, int flags) {
                int tpdu_length = Packet.SIZEOF_PGM_HEADER;
                if (StandardProtocolFamily.INET6 == family)
                        tpdu_length += SIZEOF_SPM6_HEADER;
                else
                        tpdu_length += SIZEOF_SPM_HEADER;
                if (Packet.PGM_OPT_FIN == flags)
                {
                        tpdu_length += Packet.SIZEOF_PGM_OPT_LENGTH;
/* End of session */
                        if (Packet.PGM_OPT_FIN == flags)
                                tpdu_length += Packet.SIZEOF_PGM_OPT_HEADER + Packet.SIZEOF_PGM_OPT_FIN;
                }
                SocketBuffer skb = new SocketBuffer (tpdu_length);
		skb.setHeaderOffset (0);
		skb.getHeader().setType (Packet.PGM_SPM);
		skb.reserve (Packet.SIZEOF_PGM_HEADER);
                return skb;
        }
 
Example 14
Project: javapgm   File: NetworkInterface.java   Source Code and License 6 votes vote down vote up
public static InetAddress getMulticastEnabledNodeAddress (ProtocolFamily family) throws UnknownHostException, SocketException {
                InetAddress res[] = getNodeAddress (family);
            
/* iff one address return that independent of multicast support */
                if (res.length == 1)
                        return res[0];
                
                for (InetAddress addr : res) {
/* For each node address find matching interface and test flags */                
                        java.net.NetworkInterface ni = java.net.NetworkInterface.getByInetAddress (addr);
                        if (ni.supportsMulticast())
                                return addr;
                }
                
/* Use last address as fallback */
                return res[res.length - 1];
        }
 
Example 15
Project: javapgm   File: Socket.java   Source Code and License 6 votes vote down vote up
public Socket (ProtocolFamily family) throws IOException
{
               LOG.debug ("Socket (family: {})", family);
               
               this.family = family;
               this.canSendData = true;
               this.canSendNak = true;
               this.canReceiveData = true;
               this.dataDestinationPort = Packet.DEFAULT_DATA_DESTINATION_PORT;
               this.tsi = new TransportSessionId (null, Packet.DEFAULT_DATA_SOURCE_PORT);

               LOG.trace (NETWORK_MARKER, "Opening UDP encapsulated sockets.");
	this.recv_sock = DatagramChannel.open (this.family);
               LOG.trace (NETWORK_MARKER, "Set socket sharing.");
               this.recv_sock.setOption (StandardSocketOptions.SO_REUSEADDR, true);
               this.recv_sock.configureBlocking (false);

	this.send_sock = new MulticastSocket ();
               
               LOG.debug ("PGM socket successfully created.");
       }
 
Example 16
Project: openjdk-jdk10   File: JdpBroadcaster.java   Source Code and License 5 votes vote down vote up
/**
 * Create a new broadcaster
 *
 * @param address - multicast group address
 * @param srcAddress - address of interface we should use to broadcast.
 * @param port - udp port to use
 * @param ttl - packet ttl
 * @throws IOException
 */
public JdpBroadcaster(InetAddress address, InetAddress srcAddress, int port, int ttl)
        throws IOException, JdpException {
    this.addr = address;
    this.port = port;

    ProtocolFamily family = (address instanceof Inet6Address)
            ? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;

    channel = DatagramChannel.open(family);
    channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, ttl);

    // with srcAddress equal to null, this constructor do exactly the same as
    // if srcAddress is not passed
    if (srcAddress != null) {
        // User requests particular interface to bind to
        NetworkInterface interf = NetworkInterface.getByInetAddress(srcAddress);

        if (interf == null) {
            throw new JdpException("Unable to get network interface for " + srcAddress.toString());
        }

        if (!interf.isUp()) {
            throw new JdpException(interf.getName() + " is not up.");
        }

        if (!interf.supportsMulticast()) {
            throw new JdpException(interf.getName() + " does not support multicast.");
        }

        try {
            channel.bind(new InetSocketAddress(srcAddress, 0));
        } catch (UnsupportedAddressTypeException ex) {
            throw new JdpException("Unable to bind to source address");
        }
        channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf);
    }
}
 
Example 17
Project: openjdk9   File: JdpBroadcaster.java   Source Code and License 5 votes vote down vote up
/**
 * Create a new broadcaster
 *
 * @param address - multicast group address
 * @param srcAddress - address of interface we should use to broadcast.
 * @param port - udp port to use
 * @param ttl - packet ttl
 * @throws IOException
 */
public JdpBroadcaster(InetAddress address, InetAddress srcAddress, int port, int ttl)
        throws IOException, JdpException {
    this.addr = address;
    this.port = port;

    ProtocolFamily family = (address instanceof Inet6Address)
            ? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;

    channel = DatagramChannel.open(family);
    channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, ttl);

    // with srcAddress equal to null, this constructor do exactly the same as
    // if srcAddress is not passed
    if (srcAddress != null) {
        // User requests particular interface to bind to
        NetworkInterface interf = NetworkInterface.getByInetAddress(srcAddress);

        if (interf == null) {
            throw new JdpException("Unable to get network interface for " + srcAddress.toString());
        }

        if (!interf.isUp()) {
            throw new JdpException(interf.getName() + " is not up.");
        }

        if (!interf.supportsMulticast()) {
            throw new JdpException(interf.getName() + " does not support multicast.");
        }

        try {
            channel.bind(new InetSocketAddress(srcAddress, 0));
        } catch (UnsupportedAddressTypeException ex) {
            throw new JdpException("Unable to bind to source address");
        }
        channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf);
    }
}
 
Example 18
Project: bt   File: DHT.java   Source Code and License 5 votes vote down vote up
private DHTtype(String shortName, int nodeslength, int addresslength, Class<? extends InetAddress> addresstype, int header, int maxSize, ProtocolFamily family) {
	
	this.shortName = shortName;
	this.NODES_ENTRY_LENGTH = nodeslength;
	this.PREFERRED_ADDRESS_TYPE = addresstype;
	this.ADDRESS_ENTRY_LENGTH = addresslength;
	this.HEADER_LENGTH = header;
	this.MAX_PACKET_SIZE = maxSize;
	this.PROTO_FAMILY = family;
}
 
Example 19
Project: bt   File: AnnounceGroupChannel.java   Source Code and License 5 votes vote down vote up
private synchronized DatagramChannel getChannel() throws IOException {
    if (channel == null || !channel.isOpen()) {
        if (shutdown.get()) {
            throw new IllegalStateException("Channel has been shut down");
        }
        ProtocolFamily protocolFamily = InternetProtocolUtils.getProtocolFamily(group.getAddress().getAddress());
        DatagramChannel _channel = selector.provider().openDatagramChannel(protocolFamily);
        _channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
        // bind to any-local before setting TTL
        int port = group.getAddress().getPort();
        if (protocolFamily == StandardProtocolFamily.INET) {
            _channel.bind(new InetSocketAddress(Inet4Address.getByName("0.0.0.0"), port));
        } else {
            _channel.bind(new InetSocketAddress(Inet6Address.getByName("[::]"), port));
        }
        int timeToLive = group.getTimeToLive();
        if (timeToLive != 1) {
            _channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, timeToLive);
        }

        for (NetworkInterface iface : networkInterfaces) {
            _channel.join(group.getAddress().getAddress(), iface);
        }

        _channel.configureBlocking(false);
        channel = _channel;
    }
    return channel;
}
 
Example 20
Project: bt   File: InternetProtocolUtils.java   Source Code and License 5 votes vote down vote up
/**
 * @return {@link StandardProtocolFamily#INET} for IPv4 address or {@link StandardProtocolFamily#INET6} for IPv6 address
 * @throws IllegalArgumentException if the address is neither IPv4 or IPv6
 * @since 1.6
 */
public static ProtocolFamily getProtocolFamily(InetAddress address) {
    if (address.getAddress().length == IP4_BYTES) {
        return StandardProtocolFamily.INET;
    } else if (address.getAddress().length == IP6_BYTES) {
        return StandardProtocolFamily.INET6;
    } else {
        throw new IllegalArgumentException("Can't determine protocol family for address: " + address);
    }
}
 
Example 21
Project: mldht   File: DHT.java   Source Code and License 5 votes vote down vote up
private DHTtype(String shortName, int nodeslength, int addresslength, Class<? extends InetAddress> addresstype, int header, int maxSize, ProtocolFamily family) {
	
	this.shortName = shortName;
	this.NODES_ENTRY_LENGTH = nodeslength;
	this.PREFERRED_ADDRESS_TYPE = addresstype;
	this.ADDRESS_ENTRY_LENGTH = addresslength;
	this.HEADER_LENGTH = header;
	this.MAX_PACKET_SIZE = maxSize;
	this.PROTO_FAMILY = family;
}
 
Example 22
Project: netty4.0.27Learn   File: ProtocolFamilyConverter.java   Source Code and License 5 votes vote down vote up
/**
 * Convert the {@link InternetProtocolFamily}. This MUST only be called on jdk version >= 7.
 */
public static ProtocolFamily convert(InternetProtocolFamily family) {
    switch (family) {
    case IPv4:
        return StandardProtocolFamily.INET;
    case IPv6:
        return StandardProtocolFamily.INET6;
    default:
        throw new IllegalArgumentException();
    }
}
 
Example 23
Project: netty4study   File: ProtocolFamilyConverter.java   Source Code and License 5 votes vote down vote up
/**
 * Convert the {@link InternetProtocolFamily}. This MUST only be called on jdk version >= 7.
 */
public static ProtocolFamily convert(InternetProtocolFamily family) {
    switch (family) {
    case IPv4:
        return StandardProtocolFamily.INET;
    case IPv6:
        return StandardProtocolFamily.INET6;
    default:
        throw new IllegalArgumentException();
    }
}
 
Example 24
Project: simple-netty-source   File: ProtocolFamilyConverter.java   Source Code and License 5 votes vote down vote up
/**
 * Convert the {@link InternetProtocolFamily}. This MUST only be called on jdk version >= 7.
 */
public static ProtocolFamily convert(InternetProtocolFamily family) {
    switch (family) {
    case IPv4:
        return StandardProtocolFamily.INET;

    case IPv6:
        return StandardProtocolFamily.INET6;
    default:
        throw new IllegalArgumentException();
    }
}
 
Example 25
Project: netty-netty-5.0.0.Alpha1   File: ProtocolFamilyConverter.java   Source Code and License 5 votes vote down vote up
/**
 * Convert the {@link InternetProtocolFamily}. This MUST only be called on jdk version >= 7.
 */
public static ProtocolFamily convert(InternetProtocolFamily family) {
    switch (family) {
    case IPv4:
        return StandardProtocolFamily.INET;
    case IPv6:
        return StandardProtocolFamily.INET6;
    default:
        throw new IllegalArgumentException();
    }
}
 
Example 26
Project: cohorte-remote-services   File: MulticastHandler.java   Source Code and License 5 votes vote down vote up
/**
 * Sets up the multicast channel
 * 
 * @throws IOException
 *             Something wrong occurred (bad address, bad port, ...)
 */
private void setupMulticast() throws IOException {

    // Compute the address family
    final ProtocolFamily family;
    if (pAddress instanceof Inet4Address) {
        // IPv4
        family = StandardProtocolFamily.INET;

    } else if (pAddress instanceof Inet6Address) {
        // IPv6
        family = StandardProtocolFamily.INET6;

    } else {
        // Unknown
        throw new SocketException("Unknown multicast group family");
    }

    // Create the UDP channel
    pChannel = DatagramChannel.open(family);
    pChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    pChannel.bind(new InetSocketAddress(pPort));

    try {
        // Join the group on all interfaces
        for (final NetworkInterface itf : getMulticastInterfaces()) {
            pJoinedGroups.add(pChannel.join(pAddress, itf));
        }

    } catch (final SocketException ex) {
        // Be nice...
        pChannel.close();
        throw ex;
    }
}
 
Example 27
Project: javapgm   File: NetworkInterface.java   Source Code and License 5 votes vote down vote up
public static InetAddress[] getNodeAddress (ProtocolFamily family) throws UnknownHostException {
        LinkedList<InetAddress> list = new LinkedList<> ();
        String nodename = InetAddress.getLocalHost().getHostName();
        for (InetAddress addr : InetAddress.getAllByName (nodename)) {
                if (StandardProtocolFamily.INET == family && addr instanceof Inet4Address)
                        list.add (addr);
                else if (StandardProtocolFamily.INET6 == family && addr instanceof Inet6Address)
                        list.add (addr);
        }
        return list.toArray (new InetAddress[list.size()]);
}
 
Example 28
Project: javapgm   File: OriginalData.java   Source Code and License 5 votes vote down vote up
public static SocketBuffer create (ProtocolFamily family, int tsdu_length) {
              int tpdu_length = Packet.calculateOffset (false, null) + tsdu_length;
              SocketBuffer skb = new SocketBuffer (tpdu_length);
skb.setHeaderOffset (0);
skb.getHeader().setType (Packet.PGM_ODATA);
              skb.getHeader().setTsduLength (tsdu_length);
skb.reserve (Packet.calculateOffset (false, null));
              skb.put (tsdu_length);
              skb.setOriginalDataOffset (Packet.SIZEOF_PGM_HEADER);
              return skb;
      }
 
Example 29
Project: javapgm   File: Packet.java   Source Code and License 5 votes vote down vote up
public static int calculateOffset (boolean canFragment, @Nullable ProtocolFamily pgmcc_family) {
        int data_size = SIZEOF_PGM_HEADER + SIZEOF_PGM_DATA;
        int pkt_size = data_size;
        if (canFragment || (null != pgmcc_family))
                pkt_size += SIZEOF_PGM_OPT_LENGTH + SIZEOF_PGM_OPT_HEADER;
        if (canFragment)
                pkt_size += SIZEOF_PGM_OPT_FRAGMENT;
        if (StandardProtocolFamily.INET == pgmcc_family)
                pkt_size += SIZEOF_PGM_OPT_PGMCC_DATA;
        else if (StandardProtocolFamily.INET6 == pgmcc_family)
                pkt_size += SIZEOF_PGM_OPT6_PGMCC_DATA;
        return pkt_size;
}
 
Example 30
Project: javapgm   File: channelrecv.java   Source Code and License 5 votes vote down vote up
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 + "" +
				" }");
		}
	}
}
 
Example 31
Project: OpenJSharp   File: SocketOptionRegistry.java   Source Code and License 4 votes vote down vote up
RegistryKey(SocketOption<?> name, ProtocolFamily family) {             
    this.name = name;                                                  
    this.family = family;                                              
}
 
Example 32
Project: OpenJSharp   File: SocketOptionRegistry.java   Source Code and License 4 votes vote down vote up
public static OptionKey findOption(SocketOption<?> name, ProtocolFamily family) { 
    RegistryKey key = new RegistryKey(name, family);                       
    return LazyInitialization.options.get(key);                            
}
 
Example 33
Project: OpenJSharp   File: SelectorProviderImpl.java   Source Code and License 4 votes vote down vote up
public DatagramChannel openDatagramChannel(ProtocolFamily family) throws IOException {
    return new DatagramChannelImpl(this, family);
}
 
Example 34
Project: OpenJSharp   File: SocketOptionRegistry.java   Source Code and License 4 votes vote down vote up
RegistryKey(SocketOption<?> name, ProtocolFamily family) {             
    this.name = name;                                                  
    this.family = family;                                              
}
 
Example 35
Project: OpenJSharp   File: SocketOptionRegistry.java   Source Code and License 4 votes vote down vote up
public static OptionKey findOption(SocketOption<?> name, ProtocolFamily family) { 
    RegistryKey key = new RegistryKey(name, family);                       
    return LazyInitialization.options.get(key);                            
}
 
Example 36
Project: jdk8u-jdk   File: SelectorProviderImpl.java   Source Code and License 4 votes vote down vote up
public DatagramChannel openDatagramChannel(ProtocolFamily family) throws IOException {
    return new DatagramChannelImpl(this, family);
}
 
Example 37
Project: jdk8u-jdk   File: RmidViaInheritedChannel.java   Source Code and License 4 votes vote down vote up
public DatagramChannel openDatagramChannel(ProtocolFamily family)
    throws IOException
{
    return provider.openDatagramChannel(family);
}
 
Example 38
Project: jdk8u-jdk   File: InheritedChannelNotServerSocket.java   Source Code and License 4 votes vote down vote up
public DatagramChannel openDatagramChannel(ProtocolFamily family)
    throws IOException
{
    return provider.openDatagramChannel(family);
}
 
Example 39
Project: openjdk-jdk10   File: SelectorProviderImpl.java   Source Code and License 4 votes vote down vote up
public DatagramChannel openDatagramChannel(ProtocolFamily family) throws IOException {
    return new DatagramChannelImpl(this, family);
}
 
Example 40
Project: openjdk-jdk10   File: InheritedChannelNotServerSocket.java   Source Code and License 4 votes vote down vote up
public DatagramChannel openDatagramChannel(ProtocolFamily family)
    throws IOException
{
    return provider.openDatagramChannel(family);
}
 
Example 41
Project: openjdk-jdk10   File: RMIDSelectorProvider.java   Source Code and License 4 votes vote down vote up
public DatagramChannel openDatagramChannel(ProtocolFamily family)
    throws IOException
{
    return provider.openDatagramChannel(family);
}
 
Example 42
Project: openjdk9   File: SelectorProviderImpl.java   Source Code and License 4 votes vote down vote up
public DatagramChannel openDatagramChannel(ProtocolFamily family) throws IOException {
    return new DatagramChannelImpl(this, family);
}
 
Example 43
Project: openjdk9   File: RmidViaInheritedChannel.java   Source Code and License 4 votes vote down vote up
public DatagramChannel openDatagramChannel(ProtocolFamily family)
    throws IOException
{
    return provider.openDatagramChannel(family);
}
 
Example 44
Project: openjdk9   File: InheritedChannelNotServerSocket.java   Source Code and License 4 votes vote down vote up
public DatagramChannel openDatagramChannel(ProtocolFamily family)
    throws IOException
{
    return provider.openDatagramChannel(family);
}
 
Example 45
Project: netcrusher-java   File: DatagramCrusherSocketOptions.java   Source Code and License 4 votes vote down vote up
public ProtocolFamily getProtocolFamily() {
    return protocolFamily;
}
 
Example 46
Project: netcrusher-java   File: DatagramCrusherSocketOptions.java   Source Code and License 4 votes vote down vote up
public void setProtocolFamily(ProtocolFamily protocolFamily) {
    this.protocolFamily = protocolFamily;
}
 
Example 47
Project: jdk8u_jdk   File: SelectorProviderImpl.java   Source Code and License 4 votes vote down vote up
public DatagramChannel openDatagramChannel(ProtocolFamily family) throws IOException {
    return new DatagramChannelImpl(this, family);
}
 
Example 48
Project: jdk8u_jdk   File: RmidViaInheritedChannel.java   Source Code and License 4 votes vote down vote up
public DatagramChannel openDatagramChannel(ProtocolFamily family)
    throws IOException
{
    return provider.openDatagramChannel(family);
}
 
Example 49
Project: jdk8u_jdk   File: InheritedChannelNotServerSocket.java   Source Code and License 4 votes vote down vote up
public DatagramChannel openDatagramChannel(ProtocolFamily family)
    throws IOException
{
    return provider.openDatagramChannel(family);
}
 
Example 50
Project: lookaside_java-1.8.0-openjdk   File: SelectorProviderImpl.java   Source Code and License 4 votes vote down vote up
public DatagramChannel openDatagramChannel(ProtocolFamily family) throws IOException {
    return new DatagramChannelImpl(this, family);
}
 
Example 51
Project: lookaside_java-1.8.0-openjdk   File: RmidViaInheritedChannel.java   Source Code and License 4 votes vote down vote up
public DatagramChannel openDatagramChannel(ProtocolFamily family)
    throws IOException
{
    return provider.openDatagramChannel(family);
}
 
Example 52
Project: lookaside_java-1.8.0-openjdk   File: InheritedChannelNotServerSocket.java   Source Code and License 4 votes vote down vote up
public DatagramChannel openDatagramChannel(ProtocolFamily family)
    throws IOException
{
    return provider.openDatagramChannel(family);
}
 
Example 53
Project: jtransc   File: DatagramChannel.java   Source Code and License 4 votes vote down vote up
public static DatagramChannel open(ProtocolFamily family) throws IOException {
	return SelectorProvider.provider().openDatagramChannel(family);
}
 
Example 54
Project: infobip-open-jdk-8   File: SelectorProviderImpl.java   Source Code and License 4 votes vote down vote up
public DatagramChannel openDatagramChannel(ProtocolFamily family) throws IOException {
    return new DatagramChannelImpl(this, family);
}
 
Example 55
Project: infobip-open-jdk-8   File: RmidViaInheritedChannel.java   Source Code and License 4 votes vote down vote up
public DatagramChannel openDatagramChannel(ProtocolFamily family)
    throws IOException
{
    return provider.openDatagramChannel(family);
}
 
Example 56
Project: infobip-open-jdk-8   File: InheritedChannelNotServerSocket.java   Source Code and License 4 votes vote down vote up
public DatagramChannel openDatagramChannel(ProtocolFamily family)
    throws IOException
{
    return provider.openDatagramChannel(family);
}
 
Example 57
Project: jdk8u-dev-jdk   File: SelectorProviderImpl.java   Source Code and License 4 votes vote down vote up
public DatagramChannel openDatagramChannel(ProtocolFamily family) throws IOException {
    return new DatagramChannelImpl(this, family);
}
 
Example 58
Project: jdk8u-dev-jdk   File: RmidViaInheritedChannel.java   Source Code and License 4 votes vote down vote up
public DatagramChannel openDatagramChannel(ProtocolFamily family)
    throws IOException
{
    return provider.openDatagramChannel(family);
}
 
Example 59
Project: jdk8u-dev-jdk   File: InheritedChannelNotServerSocket.java   Source Code and License 4 votes vote down vote up
public DatagramChannel openDatagramChannel(ProtocolFamily family)
    throws IOException
{
    return provider.openDatagramChannel(family);
}
 
Example 60
Project: jdk7-jdk   File: SelectorProviderImpl.java   Source Code and License 4 votes vote down vote up
public DatagramChannel openDatagramChannel(ProtocolFamily family) throws IOException {
    return new DatagramChannelImpl(this, family);
}