Search in sources :

Example 1 with SelectorProviderChooser

use of zmq.io.net.SelectorProviderChooser in project jeromq by zeromq.

the class OptionsTest method testSelectorObject.

@Test(timeout = 5000)
public void testSelectorObject() {
    try (ZContext ctx = new ZContext();
        Socket socket = ctx.createSocket(SocketType.PUB)) {
        SelectorProviderChooser chooser = new DefaultSelectorProviderChooser();
        socket.setSelectorChooser(chooser);
        Assert.assertEquals(chooser, socket.getSelectorProviderChooser());
    }
}
Also used : DefaultSelectorProviderChooser(org.zeromq.SelectorProviderTest.DefaultSelectorProviderChooser) ZContext(org.zeromq.ZContext) SelectorProviderChooser(zmq.io.net.SelectorProviderChooser) DefaultSelectorProviderChooser(org.zeromq.SelectorProviderTest.DefaultSelectorProviderChooser) Socket(org.zeromq.ZMQ.Socket) Test(org.junit.Test)

Example 2 with SelectorProviderChooser

use of zmq.io.net.SelectorProviderChooser in project jeromq by zeromq.

the class Options method setSocketOpt.

@SuppressWarnings("deprecation")
public boolean setSocketOpt(int option, Object optval) {
    final ValueReference<Boolean> result = new ValueReference<>(false);
    switch(option) {
        case ZMQ.ZMQ_SNDHWM:
            sendHwm = (Integer) optval;
            if (sendHwm < 0) {
                throw new IllegalArgumentException("sendHwm " + optval);
            }
            return true;
        case ZMQ.ZMQ_RCVHWM:
            recvHwm = (Integer) optval;
            if (recvHwm < 0) {
                throw new IllegalArgumentException("recvHwm " + optval);
            }
            return true;
        case ZMQ.ZMQ_AFFINITY:
            affinity = (Long) optval;
            return true;
        case ZMQ.ZMQ_IDENTITY:
            byte[] val = parseBytes(option, optval);
            if (val == null || val.length > 255) {
                throw new IllegalArgumentException("identity must not be null or less than 255 " + optval);
            }
            identity = Arrays.copyOf(val, val.length);
            identitySize = (short) identity.length;
            return true;
        case ZMQ.ZMQ_RATE:
            rate = (Integer) optval;
            return true;
        case ZMQ.ZMQ_RECOVERY_IVL:
            recoveryIvl = (Integer) optval;
            return true;
        case ZMQ.ZMQ_SNDBUF:
            sndbuf = (Integer) optval;
            return true;
        case ZMQ.ZMQ_RCVBUF:
            rcvbuf = (Integer) optval;
            return true;
        case ZMQ.ZMQ_TOS:
            tos = (Integer) optval;
            return true;
        case ZMQ.ZMQ_LINGER:
            linger = (Integer) optval;
            return true;
        case ZMQ.ZMQ_RECONNECT_IVL:
            reconnectIvl = (Integer) optval;
            if (reconnectIvl < -1) {
                throw new IllegalArgumentException("reconnectIvl " + optval);
            }
            return true;
        case ZMQ.ZMQ_RECONNECT_IVL_MAX:
            reconnectIvlMax = (Integer) optval;
            if (reconnectIvlMax < 0) {
                throw new IllegalArgumentException("reconnectIvlMax " + optval);
            }
            return true;
        case ZMQ.ZMQ_BACKLOG:
            backlog = (Integer) optval;
            return true;
        case ZMQ.ZMQ_MAXMSGSIZE:
            maxMsgSize = (Long) optval;
            return true;
        case ZMQ.ZMQ_MULTICAST_HOPS:
            multicastHops = (Integer) optval;
            return true;
        case ZMQ.ZMQ_RCVTIMEO:
            recvTimeout = (Integer) optval;
            return true;
        case ZMQ.ZMQ_SNDTIMEO:
            sendTimeout = (Integer) optval;
            return true;
        /*  Deprecated in favor of ZMQ_IPV6  */
        case ZMQ.ZMQ_IPV4ONLY:
            return setSocketOpt(ZMQ.ZMQ_IPV6, !parseBoolean(option, optval));
        /*  To replace the somewhat surprising IPV4ONLY */
        case ZMQ.ZMQ_IPV6:
            ipv6 = parseBoolean(option, optval);
            return true;
        case ZMQ.ZMQ_SOCKS_PROXY:
            socksProxyAddress = parseString(option, optval);
            return true;
        case ZMQ.ZMQ_TCP_KEEPALIVE:
            tcpKeepAlive = ((Number) optval).intValue();
            if (tcpKeepAlive != -1 && tcpKeepAlive != 0 && tcpKeepAlive != 1) {
                throw new IllegalArgumentException("tcpKeepAlive only accepts one of -1,0,1 " + optval);
            }
            return true;
        case ZMQ.ZMQ_TCP_KEEPALIVE_CNT:
        case ZMQ.ZMQ_TCP_KEEPALIVE_IDLE:
        case ZMQ.ZMQ_TCP_KEEPALIVE_INTVL:
            // not supported
            return false;
        case ZMQ.ZMQ_IMMEDIATE:
            immediate = parseBoolean(option, optval);
            return true;
        case ZMQ.ZMQ_DELAY_ATTACH_ON_CONNECT:
            immediate = !parseBoolean(option, optval);
            return true;
        case ZMQ.ZMQ_TCP_ACCEPT_FILTER:
            String filterStr = parseString(option, optval);
            if (filterStr == null) {
                tcpAcceptFilters.clear();
            } else if (filterStr.length() == 0 || filterStr.length() > 255) {
                throw new IllegalArgumentException("tcp_accept_filter " + optval);
            } else {
                TcpAddressMask filter = new TcpAddressMask(filterStr, ipv6);
                tcpAcceptFilters.add(filter);
            }
            return true;
        case ZMQ.ZMQ_PLAIN_SERVER:
            asServer = parseBoolean(option, optval);
            mechanism = (asServer ? Mechanisms.PLAIN : Mechanisms.NULL);
            return true;
        case ZMQ.ZMQ_PLAIN_USERNAME:
            if (optval == null) {
                mechanism = Mechanisms.NULL;
                asServer = false;
                return true;
            }
            plainUsername = parseString(option, optval);
            asServer = false;
            mechanism = Mechanisms.PLAIN;
            return true;
        case ZMQ.ZMQ_PLAIN_PASSWORD:
            if (optval == null) {
                mechanism = Mechanisms.NULL;
                asServer = false;
                return true;
            }
            plainPassword = parseString(option, optval);
            asServer = false;
            mechanism = Mechanisms.PLAIN;
            return true;
        case ZMQ.ZMQ_ZAP_DOMAIN:
            String domain = parseString(option, optval);
            if (domain != null && domain.length() < 256) {
                zapDomain = domain;
                return true;
            }
            throw new IllegalArgumentException("zap domain length shall be < 256 : " + optval);
        case ZMQ.ZMQ_CURVE_SERVER:
            asServer = parseBoolean(option, optval);
            mechanism = (asServer ? Mechanisms.CURVE : Mechanisms.NULL);
            return true;
        case ZMQ.ZMQ_CURVE_PUBLICKEY:
            curvePublicKey = setCurveKey(option, optval, result);
            return result.get();
        case ZMQ.ZMQ_CURVE_SECRETKEY:
            curveSecretKey = setCurveKey(option, optval, result);
            return result.get();
        case ZMQ.ZMQ_CURVE_SERVERKEY:
            curveServerKey = setCurveKey(option, optval, result);
            if (curveServerKey == null) {
                asServer = false;
            }
            return result.get();
        case ZMQ.ZMQ_CONFLATE:
            conflate = parseBoolean(option, optval);
            return true;
        case ZMQ.ZMQ_GSSAPI_SERVER:
            asServer = parseBoolean(option, optval);
            mechanism = Mechanisms.GSSAPI;
            return true;
        case ZMQ.ZMQ_GSSAPI_PRINCIPAL:
            gssPrincipal = parseString(option, optval);
            mechanism = Mechanisms.GSSAPI;
            return true;
        case ZMQ.ZMQ_GSSAPI_SERVICE_PRINCIPAL:
            gssServicePrincipal = parseString(option, optval);
            mechanism = Mechanisms.GSSAPI;
            return true;
        case ZMQ.ZMQ_GSSAPI_PLAINTEXT:
            gssPlaintext = parseBoolean(option, optval);
            return true;
        case ZMQ.ZMQ_HANDSHAKE_IVL:
            handshakeIvl = (Integer) optval;
            if (handshakeIvl < 0) {
                throw new IllegalArgumentException("handshakeIvl only accept positive values " + optval);
            }
            return true;
        case ZMQ.ZMQ_HEARTBEAT_IVL:
            heartbeatInterval = (Integer) optval;
            if (heartbeatInterval < 0) {
                throw new IllegalArgumentException("heartbeatInterval only accept positive values " + optval);
            }
            return true;
        case ZMQ.ZMQ_HEARTBEAT_TIMEOUT:
            heartbeatTimeout = (Integer) optval;
            if (heartbeatTimeout < 0) {
                throw new IllegalArgumentException("heartbeatTimeout only accept positive values " + optval);
            }
            return true;
        case ZMQ.ZMQ_HEARTBEAT_TTL:
            Integer value = (Integer) optval;
            // Convert this to deciseconds from milliseconds
            value /= 100;
            if (value >= 0 && value <= 6553) {
                heartbeatTtl = value;
            } else {
                throw new IllegalArgumentException("heartbeatTtl is out of range [0..655399]" + optval);
            }
            return true;
        case ZMQ.ZMQ_HEARTBEAT_CONTEXT:
            heartbeatContext = (byte[]) optval;
            if (heartbeatContext == null) {
                throw new IllegalArgumentException("heartbeatContext cannot be null");
            }
            return true;
        case ZMQ.ZMQ_DECODER:
            decoder = checkCustomCodec(optval, IDecoder.class);
            rawSocket = true;
            // if that line is reached, everything is fine
            return true;
        case ZMQ.ZMQ_ENCODER:
            encoder = checkCustomCodec(optval, IEncoder.class);
            rawSocket = true;
            // if that line is reached, everything is fine
            return true;
        case ZMQ.ZMQ_MSG_ALLOCATOR:
            if (optval instanceof String) {
                try {
                    allocator = allocator(Class.forName((String) optval));
                    return true;
                } catch (ClassNotFoundException e) {
                    throw new IllegalArgumentException(e);
                }
            } else if (optval instanceof Class) {
                allocator = allocator((Class<?>) optval);
                return true;
            } else if (optval instanceof MsgAllocator) {
                allocator = (MsgAllocator) optval;
                return true;
            }
            return false;
        case ZMQ.ZMQ_MSG_ALLOCATION_HEAP_THRESHOLD:
            Integer allocationHeapThreshold = (Integer) optval;
            allocator = new MsgAllocatorThreshold(allocationHeapThreshold);
            return true;
        case ZMQ.ZMQ_SELECTOR_PROVIDERCHOOSER:
            if (optval instanceof String) {
                try {
                    selectorChooser = chooser(Class.forName((String) optval));
                    return true;
                } catch (ClassNotFoundException e) {
                    throw new IllegalArgumentException(e);
                }
            } else if (optval instanceof Class) {
                selectorChooser = chooser((Class<?>) optval);
                return true;
            } else if (optval instanceof SelectorProviderChooser) {
                selectorChooser = (SelectorProviderChooser) optval;
                return true;
            }
            return false;
        case ZMQ.ZMQ_HELLO_MSG:
            if (optval == null) {
                helloMsg = null;
            } else {
                byte[] bytes = parseBytes(option, optval);
                if (bytes.length == 0) {
                    helloMsg = null;
                } else {
                    helloMsg = new Msg(Arrays.copyOf(bytes, bytes.length));
                }
            }
            return true;
        case ZMQ.ZMQ_DISCONNECT_MSG:
            if (optval == null) {
                disconnectMsg = null;
            } else {
                byte[] bytes = parseBytes(option, optval);
                if (bytes.length == 0) {
                    disconnectMsg = null;
                } else {
                    disconnectMsg = new Msg(Arrays.copyOf(bytes, bytes.length));
                }
            }
            return true;
        case ZMQ.ZMQ_HICCUP_MSG:
            if (optval == null) {
                hiccupMsg = null;
            } else {
                byte[] bytes = parseBytes(option, optval);
                if (bytes.length == 0) {
                    hiccupMsg = null;
                } else {
                    hiccupMsg = new Msg(Arrays.copyOf(bytes, bytes.length));
                }
            }
            return true;
        case ZMQ.ZMQ_AS_TYPE:
            this.asType = (Integer) optval;
            return true;
        default:
            throw new IllegalArgumentException("Unknown Option " + option);
    }
}
Also used : IDecoder(zmq.io.coder.IDecoder) TcpAddressMask(zmq.io.net.tcp.TcpAddress.TcpAddressMask) MsgAllocator(zmq.msg.MsgAllocator) IEncoder(zmq.io.coder.IEncoder) SelectorProviderChooser(zmq.io.net.SelectorProviderChooser) ValueReference(zmq.util.ValueReference) MsgAllocatorThreshold(zmq.msg.MsgAllocatorThreshold)

Aggregations

SelectorProviderChooser (zmq.io.net.SelectorProviderChooser)2 Test (org.junit.Test)1 DefaultSelectorProviderChooser (org.zeromq.SelectorProviderTest.DefaultSelectorProviderChooser)1 ZContext (org.zeromq.ZContext)1 Socket (org.zeromq.ZMQ.Socket)1 IDecoder (zmq.io.coder.IDecoder)1 IEncoder (zmq.io.coder.IEncoder)1 TcpAddressMask (zmq.io.net.tcp.TcpAddress.TcpAddressMask)1 MsgAllocator (zmq.msg.MsgAllocator)1 MsgAllocatorThreshold (zmq.msg.MsgAllocatorThreshold)1 ValueReference (zmq.util.ValueReference)1