Search in sources :

Example 1 with Address

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

the class SocketBase method connectInternal.

private boolean connectInternal(String addr) {
    if (ctxTerminated) {
        errno.set(ZError.ETERM);
        return false;
    }
    options.mechanism.check(options);
    // Process pending commands, if any.
    boolean brc = processCommands(0, false, null);
    if (!brc) {
        return false;
    }
    SimpleURI uri = SimpleURI.create(addr);
    String address = uri.getAddress();
    NetProtocol protocol = checkProtocol(uri.getProtocol());
    if (protocol == null || !protocol.valid) {
        return false;
    }
    if (protocol == NetProtocol.inproc) {
        // TODO: inproc connect is specific with respect to creating pipes
        // as there's no 'reconnect' functionality implemented. Once that
        // is in place we should follow generic pipe creation algorithm.
        // Find the peer endpoint.
        Ctx.Endpoint peer = findEndpoint(addr);
        // The total HWM for an inproc connection should be the sum of
        // the binder's HWM and the connector's HWM.
        int sndhwm = 0;
        if (peer.socket == null) {
            sndhwm = options.sendHwm;
        } else if (options.sendHwm != 0 && peer.options.recvHwm != 0) {
            sndhwm = options.sendHwm + peer.options.recvHwm;
        }
        int rcvhwm = 0;
        if (peer.socket == null) {
            rcvhwm = options.recvHwm;
        } else if (options.recvHwm != 0 && peer.options.sendHwm != 0) {
            rcvhwm = options.recvHwm + peer.options.sendHwm;
        }
        // Create a bi-directional pipe to connect the peers.
        ZObject[] parents = { this, peer.socket == null ? this : peer.socket };
        boolean conflate = options.conflate && (options.type == ZMQ.ZMQ_DEALER || options.type == ZMQ.ZMQ_PULL || options.type == ZMQ.ZMQ_PUSH || options.type == ZMQ.ZMQ_PUB || options.type == ZMQ.ZMQ_SUB);
        int[] hwms = { conflate ? -1 : sndhwm, conflate ? -1 : rcvhwm };
        boolean[] conflates = { conflate, conflate };
        Pipe[] pipes = Pipe.pair(parents, hwms, conflates);
        // Attach local end of the pipe to this socket object.
        attachPipe(pipes[0], true);
        if (peer.socket == null) {
            // The peer doesn't exist yet so we don't know whether
            // to send the identity message or not. To resolve this,
            // we always send our identity and drop it later if
            // the peer doesn't expect it.
            Msg id = new Msg(options.identitySize);
            id.put(options.identity, 0, options.identitySize);
            id.setFlags(Msg.IDENTITY);
            boolean written = pipes[0].write(id);
            assert (written);
            pipes[0].flush();
            // If set, send the hello msg of the local socket to the peer.
            if (options.canSendHelloMsg && options.helloMsg != null) {
                written = pipes[0].write(options.helloMsg);
                assert (written);
                pipes[0].flush();
            }
            pendConnection(addr, new Ctx.Endpoint(this, options), pipes);
        } else {
            // If required, send the identity of the peer to the local socket.
            if (peer.options.recvIdentity) {
                Msg id = new Msg(options.identitySize);
                id.put(options.identity, 0, options.identitySize);
                id.setFlags(Msg.IDENTITY);
                boolean written = pipes[0].write(id);
                assert (written);
                pipes[0].flush();
            }
            // If required, send the identity of the local socket to the peer.
            if (options.recvIdentity) {
                Msg id = new Msg(peer.options.identitySize);
                id.put(peer.options.identity, 0, peer.options.identitySize);
                id.setFlags(Msg.IDENTITY);
                boolean written = pipes[1].write(id);
                assert (written);
                pipes[1].flush();
            }
            // If set, send the hello msg of the local socket to the peer.
            if (options.canSendHelloMsg && options.helloMsg != null) {
                boolean written = pipes[0].write(options.helloMsg);
                assert (written);
                pipes[0].flush();
            }
            // If set, send the hello msg of the peer to the local socket.
            if (peer.options.canSendHelloMsg && peer.options.helloMsg != null) {
                boolean written = pipes[1].write(peer.options.helloMsg);
                assert (written);
                pipes[1].flush();
            }
            if (peer.options.canReceiveDisconnectMsg && peer.options.disconnectMsg != null) {
                pipes[0].setDisconnectMsg(peer.options.disconnectMsg);
            }
            // Attach remote end of the pipe to the peer socket. Note that peer's
            // seqnum was incremented in findEndpoint function. We don't need it
            // increased here.
            sendBind(peer.socket, pipes[1], false);
        }
        // Save last endpoint URI
        options.lastEndpoint = addr;
        // remember inproc connections for disconnect
        inprocs.insert(addr, pipes[0]);
        return true;
    }
    boolean isSingleConnect = options.type == ZMQ.ZMQ_DEALER || options.type == ZMQ.ZMQ_SUB || options.type == ZMQ.ZMQ_REQ;
    if (isSingleConnect) {
        if (endpoints.hasValues(addr)) {
            // nonsensical results.
            return true;
        }
    }
    // Choose the I/O thread to run the session in.
    IOThread ioThread = chooseIoThread(options.affinity);
    if (ioThread == null) {
        errno.set(ZError.EMTHREAD);
        return false;
    }
    Address paddr = new Address(protocol, address);
    // Resolve address (if needed by the protocol)
    protocol.resolve(paddr, options.ipv6);
    // Create session.
    SessionBase session = Sockets.createSession(ioThread, true, this, options, paddr);
    assert (session != null);
    // PGM does not support subscription forwarding; ask for all data to be
    // sent to this pipe. (same for NORM, currently?)
    boolean subscribe2all = protocol.subscribe2all;
    Pipe newpipe = null;
    if (options.immediate || subscribe2all) {
        // Create a bi-directional pipe.
        ZObject[] parents = { this, session };
        boolean conflate = options.conflate && (options.type == ZMQ.ZMQ_DEALER || options.type == ZMQ.ZMQ_PULL || options.type == ZMQ.ZMQ_PUSH || options.type == ZMQ.ZMQ_PUB || options.type == ZMQ.ZMQ_SUB);
        int[] hwms = { conflate ? -1 : options.sendHwm, conflate ? -1 : options.recvHwm };
        boolean[] conflates = { conflate, conflate };
        Pipe[] pipes = Pipe.pair(parents, hwms, conflates);
        // Attach local end of the pipe to the socket object.
        attachPipe(pipes[0], subscribe2all, true);
        newpipe = pipes[0];
        // Attach remote end of the pipe to the session object later on.
        session.attachPipe(pipes[1]);
    }
    // Save last endpoint URI
    options.lastEndpoint = paddr.toString();
    addEndpoint(addr, session, newpipe);
    return true;
}
Also used : Address(zmq.io.net.Address) IZAddress(zmq.io.net.Address.IZAddress) InetSocketAddress(java.net.InetSocketAddress) Pipe(zmq.pipe.Pipe) SessionBase(zmq.io.SessionBase) IOThread(zmq.io.IOThread) NetProtocol(zmq.io.net.NetProtocol)

Example 2 with Address

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

the class TcpAddressTest method testUnspecifiedIPv6DoubleColon.

@Test
public void testUnspecifiedIPv6DoubleColon() throws IOException {
    int port = Utils.findOpenPort();
    Address addr = new Address(NetProtocol.tcp, ":::" + port);
    addr.resolve(true);
    Address.IZAddress resolved = addr.resolved();
    InetSocketAddress sa = (InetSocketAddress) resolved.address();
    Assert.assertTrue(sa.getAddress() instanceof Inet6Address);
    String hostString = sa.getHostString();
    Assert.assertTrue("::".equals(hostString) || "0:0:0:0:0:0:0:0".equals(hostString));
    Assert.assertEquals(port, sa.getPort());
}
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) Test(org.junit.Test)

Example 3 with Address

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

the class TcpAddressTest method testGoodIP46Google.

@Test
public void testGoodIP46Google() {
    Address addr = new Address(NetProtocol.tcp, "www.google.com:80");
    addr.resolve(false);
    Address.IZAddress resolved = addr.resolved();
    InetSocketAddress sa = (InetSocketAddress) resolved.address();
    Assert.assertTrue(sa.getAddress() instanceof Inet4Address);
    Assert.assertEquals(80, sa.getPort());
}
Also used : Inet4Address(java.net.Inet4Address) Address(zmq.io.net.Address) Inet4Address(java.net.Inet4Address) InetSocketAddress(java.net.InetSocketAddress) Inet6Address(java.net.Inet6Address) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 4 with Address

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

the class TcpAddressTest method testUnspecifiedIPv4.

@Test
public void testUnspecifiedIPv4() throws IOException {
    int port = Utils.findOpenPort();
    Address addr = new Address(NetProtocol.tcp, "*:" + port);
    addr.resolve(false);
    Address.IZAddress resolved = addr.resolved();
    InetSocketAddress sa = (InetSocketAddress) resolved.address();
    Assert.assertTrue(sa.getAddress() instanceof Inet4Address);
    Assert.assertEquals("0.0.0.0", sa.getHostString());
    Assert.assertEquals(port, sa.getPort());
}
Also used : Inet4Address(java.net.Inet4Address) Address(zmq.io.net.Address) Inet4Address(java.net.Inet4Address) InetSocketAddress(java.net.InetSocketAddress) Inet6Address(java.net.Inet6Address) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 5 with Address

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

the class TcpAddressTest method testGoodIPv6Google.

// Fails on both Circleci and Travis
@Ignore
@Test
public void testGoodIPv6Google() {
    Address addr = new Address(NetProtocol.tcp, "www.google.com:80");
    addr.resolve(true);
    Address.IZAddress resolved = addr.resolved();
    InetSocketAddress sa = (InetSocketAddress) resolved.address();
    Assert.assertTrue(sa.getAddress() instanceof Inet6Address);
    Assert.assertEquals(80, sa.getPort());
}
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) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

Address (zmq.io.net.Address)12 InetSocketAddress (java.net.InetSocketAddress)10 Inet4Address (java.net.Inet4Address)9 Inet6Address (java.net.Inet6Address)9 Test (org.junit.Test)9 ZMQException (org.zeromq.ZMQException)2 NetProtocol (zmq.io.net.NetProtocol)2 IOException (java.io.IOException)1 SocketAddress (java.net.SocketAddress)1 Ignore (org.junit.Ignore)1 IOThread (zmq.io.IOThread)1 SessionBase (zmq.io.SessionBase)1 IZAddress (zmq.io.net.Address.IZAddress)1 IpcConnecter (zmq.io.net.ipc.IpcConnecter)1 NormEngine (zmq.io.net.norm.NormEngine)1 PgmReceiver (zmq.io.net.pgm.PgmReceiver)1 PgmSender (zmq.io.net.pgm.PgmSender)1 SocksConnecter (zmq.io.net.tcp.SocksConnecter)1 TcpConnecter (zmq.io.net.tcp.TcpConnecter)1 TipcConnecter (zmq.io.net.tipc.TipcConnecter)1