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());
}
}
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);
}
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);
}
Aggregations