Search in sources :

Example 6 with ZMQException

use of org.zeromq.ZMQException in project jeromq by zeromq.

the class TcpAddressTest method parsesIpv6AddressNotWanted.

@Test
public void parsesIpv6AddressNotWanted() throws IOException {
    try {
        String addressString = "2000::a1";
        int port = Utils.findOpenPort();
        Address addr = new Address(NetProtocol.tcp, addressString + ":" + port);
        addr.resolve(false);
        InetSocketAddress expected = new InetSocketAddress(addressString, port);
        Address.IZAddress resolved = addr.resolved();
        assertEquals(expected, resolved.address());
        InetSocketAddress sa = (InetSocketAddress) resolved.address();
        Assert.assertTrue(sa.getAddress() instanceof Inet6Address);
        Assert.assertEquals(port, sa.getPort());
        Assert.fail();
    } catch (ZMQException e) {
        Assert.assertEquals(ZError.EADDRNOTAVAIL, e.getErrorCode());
        Assert.assertEquals("2000::a1 not found matching IPv4/IPv6 settings", e.getMessage());
    }
}
Also used : Address(zmq.io.net.Address) Inet4Address(java.net.Inet4Address) InetSocketAddress(java.net.InetSocketAddress) Inet6Address(java.net.Inet6Address) InetSocketAddress(java.net.InetSocketAddress) Inet6Address(java.net.Inet6Address) ZMQException(org.zeromq.ZMQException) Test(org.junit.Test)

Example 7 with ZMQException

use of org.zeromq.ZMQException in project jeromq by zeromq.

the class TcpAddress method resolve.

/**
 * @param name
 * @param ipv6
 * @param local ignored
 * @return the resolved address
 * @see zmq.io.net.Address.IZAddress#resolve(java.lang.String, boolean, boolean)
 */
@Override
public InetSocketAddress resolve(String name, boolean ipv6, boolean local) {
    // Find the ':' at end that separates address from the port number.
    int delimiter = name.lastIndexOf(':');
    if (delimiter < 0) {
        throw new IllegalArgumentException(name);
    }
    // Separate the address/port.
    String addrStr = name.substring(0, delimiter);
    String portStr = name.substring(delimiter + 1);
    // Remove square brackets around the address, if any.
    if (addrStr.length() >= 2 && addrStr.charAt(0) == '[' && addrStr.charAt(addrStr.length() - 1) == ']') {
        addrStr = addrStr.substring(1, addrStr.length() - 1);
    }
    int port;
    // Allow 0 specifically, to detect invalid port error in atoi if not
    if (portStr.equals("*") || portStr.equals("0")) {
        // Resolve wildcard to 0 to allow autoselection of port
        port = 0;
    } else {
        // Parse the port number (0 is not a valid port).
        port = Integer.parseInt(portStr);
        if (port == 0) {
            throw new IllegalArgumentException(name);
        }
    }
    InetAddress addrNet = null;
    // '::' for IPv6 is accepted
    if (addrStr.equals("*")) {
        addrStr = ipv6 ? "::" : "0.0.0.0";
    }
    try {
        InetAddress[] addresses = InetAddress.getAllByName(addrStr);
        if (ipv6) {
            // prefer IPv6: return the first ipv6 or the first value if not found
            for (InetAddress addr : addresses) {
                if (addr instanceof Inet6Address) {
                    addrNet = addr;
                    break;
                }
            }
            if (addrNet == null) {
                addrNet = addresses[0];
            }
        } else {
            for (InetAddress addr : addresses) {
                if (addr instanceof Inet4Address) {
                    addrNet = addr;
                    break;
                }
            }
        }
    } catch (UnknownHostException e) {
        throw new ZMQException(e.getMessage(), ZError.EADDRNOTAVAIL, e);
    }
    if (addrNet == null) {
        throw new ZMQException(addrStr + " not found matching IPv4/IPv6 settings", ZError.EADDRNOTAVAIL);
    }
    return new InetSocketAddress(addrNet, port);
}
Also used : Inet4Address(java.net.Inet4Address) UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress) ZMQException(org.zeromq.ZMQException)

Example 8 with ZMQException

use of org.zeromq.ZMQException in project LogHub by fbacchella.

the class ZMQHelper method logZMQException.

public static void logZMQException(Logger l, String prefix, RuntimeException e) {
    ERRNO errno;
    Supplier<String> message;
    if (e instanceof ZError.IOException) {
        IOException cause = (java.io.IOException) e.getCause();
        errno = ERRNO.get(exccode(cause));
        message = () -> errno.toString(prefix, e);
    } else if (e instanceof ZError.CtxTerminatedException) {
        errno = ERRNO.ETERM;
        message = () -> errno.toString(prefix, e, new RuntimeException("Context terminated"));
    } else if (e instanceof ZError.InstantiationException) {
        errno = ERRNO.EOTHER;
        message = () -> errno.toString(prefix, e, e.getCause());
    } else if (e instanceof ZMQException) {
        errno = ERRNO.get(((ZMQException) e).getErrorCode());
        message = () -> errno.toString(prefix, e);
    } else {
        throw e;
    }
    l.log(errno.level, message);
}
Also used : ZError(zmq.ZError) IOException(java.io.IOException) ZMQException(org.zeromq.ZMQException)

Aggregations

ZMQException (org.zeromq.ZMQException)8 Inet4Address (java.net.Inet4Address)3 Inet6Address (java.net.Inet6Address)3 InetSocketAddress (java.net.InetSocketAddress)3 IOException (java.io.IOException)2 Test (org.junit.Test)2 ZFrame (org.zeromq.ZFrame)2 Address (zmq.io.net.Address)2 Draft (zmq.util.Draft)2 InetAddress (java.net.InetAddress)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1 FutureTask (java.util.concurrent.FutureTask)1 Helpers (loghub.Helpers)1 ZContext (org.zeromq.ZContext)1 ZMQ (org.zeromq.ZMQ)1 Socket (org.zeromq.ZMQ.Socket)1 ZMsg (org.zeromq.ZMsg)1 ZError (zmq.ZError)1