Search in sources :

Example 76 with Socket

use of org.zeromq.ZMQ.Socket in project jeromq by zeromq.

the class TestZPoller method testPollerPollout.

@Test
public void testPollerPollout() throws IOException, InterruptedException {
    final int port = Utils.findOpenPort();
    final ZContext context = new ZContext();
    final ZPoller poller = new ZPoller(context.createSelector());
    final ZMQ.Socket receiver = context.createSocket(ZMQ.PULL);
    final Server client = new Server(context, port);
    client.start();
    try {
        receiver.connect("tcp://127.0.0.1:" + port);
        final AtomicReference<ZMsg> msg = new AtomicReference<>();
        poller.register(receiver, new EventsHandlerAdapter() {

            @Override
            public boolean events(Socket s, int events) {
                if (receiver.equals(s)) {
                    msg.set(ZMsg.recvMsg(receiver));
                    return false;
                } else {
                    return true;
                }
            }
        }, ZPoller.IN);
        int maxAttempts = 100;
        while (!Thread.currentThread().isInterrupted() && maxAttempts-- > 0) {
            int rc = poller.poll(1000);
            if (rc < 0) {
                break;
            }
        }
        client.join();
        Assert.assertNotNull("unable to receive msg after several cycles", msg);
    } finally {
        receiver.close();
        context.close();
        poller.close();
    }
}
Also used : Socket(org.zeromq.ZMQ.Socket) AtomicReference(java.util.concurrent.atomic.AtomicReference) Socket(org.zeromq.ZMQ.Socket) Test(org.junit.Test)

Example 77 with Socket

use of org.zeromq.ZMQ.Socket in project jeromq by zeromq.

the class TestZStar method testNoStar.

@Test
public void testNoStar() {
    System.out.print("No star: ");
    ZStar.Fortune fortune = new BlackHole();
    ZStar.Entourage entourage = new ZStar.Entourage() {

        @Override
        public void breakaleg(ZContext ctx, Fortune fortune, Socket phone, Object[] bags) {
        // Crepi il lupo!
        }

        @Override
        public void party(ZContext ctx) {
            // right now there are some random closing issues
            ZStar.party(30, TimeUnit.MILLISECONDS);
        // waited a bit here seems to arrange that.
        // no user penalty cost, the show is over.
        }
    };
    ZStar star = new ZStar(fortune, "motdelafin", Arrays.asList("TEST", entourage).toArray());
    ZMsg msg = star.recv();
    Assert.assertNull("Able to receive a message from a black hole", msg);
    boolean rc = star.sign();
    Assert.assertFalse("Able to detect the presence of a black hole", rc);
    rc = star.send("whatever");
    Assert.assertFalse("Able to send a command to a black hole", rc);
    // don't try it
    // rc = star.pipe().send("boom ?!");
    //        star.retire();
    System.out.println(".");
}
Also used : Fortune(org.zeromq.ZStar.Fortune) Fortune(org.zeromq.ZStar.Fortune) Socket(org.zeromq.ZMQ.Socket) Test(org.junit.Test)

Example 78 with Socket

use of org.zeromq.ZMQ.Socket in project jeromq by zeromq.

the class TestZThread method testDetached.

@Test
public void testDetached() {
    final CountDownLatch stopped = new CountDownLatch(1);
    ZThread.IDetachedRunnable detached = new ZThread.IDetachedRunnable() {

        @Override
        public void run(Object[] args) {
            ZContext ctx = new ZContext();
            assert (ctx != null);
            Socket push = ctx.createSocket(ZMQ.PUSH);
            assert (push != null);
            ctx.close();
            stopped.countDown();
        }
    };
    ZThread.start(detached);
    try {
        stopped.await();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        e.printStackTrace();
    }
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) Socket(org.zeromq.ZMQ.Socket) Test(org.junit.Test)

Example 79 with Socket

use of org.zeromq.ZMQ.Socket in project jeromq by zeromq.

the class msgqueue method main.

public static void main(String[] args) {
    //  Prepare our context and sockets
    Context context = ZMQ.context(1);
    //  Socket facing clients
    Socket frontend = context.socket(ZMQ.ROUTER);
    frontend.bind("tcp://*:5559");
    //  Socket facing services
    Socket backend = context.socket(ZMQ.DEALER);
    backend.bind("tcp://*:5560");
    //  Start the proxy
    ZMQ.proxy(frontend, backend, null);
    //  We never get here but clean up anyhow
    frontend.close();
    backend.close();
    context.term();
}
Also used : Context(org.zeromq.ZMQ.Context) Socket(org.zeromq.ZMQ.Socket)

Example 80 with Socket

use of org.zeromq.ZMQ.Socket in project jeromq by zeromq.

the class mtrelay method main.

public static void main(String[] args) {
    Context context = ZMQ.context(1);
    //  Bind to inproc: endpoint, then start upstream thread
    Socket receiver = context.socket(ZMQ.PAIR);
    receiver.bind("inproc://step3");
    //  Step 2 relays the signal to step 3
    Thread step2 = new Step2(context);
    step2.start();
    //  Wait for signal
    receiver.recv(0);
    receiver.close();
    System.out.println("Test successful!");
    context.term();
}
Also used : Context(org.zeromq.ZMQ.Context) Socket(org.zeromq.ZMQ.Socket)

Aggregations

Socket (org.zeromq.ZMQ.Socket)84 Context (org.zeromq.ZMQ.Context)32 ZContext (org.zeromq.ZContext)30 Test (org.junit.Test)26 Poller (org.zeromq.ZMQ.Poller)20 ZMsg (org.zeromq.ZMsg)14 Random (java.util.Random)13 ZFrame (org.zeromq.ZFrame)13 PollItem (org.zeromq.ZMQ.PollItem)6 ByteBuffer (java.nio.ByteBuffer)4 ArrayList (java.util.ArrayList)4 SelectableChannel (java.nio.channels.SelectableChannel)3 HashMap (java.util.HashMap)3 IOException (java.io.IOException)2 LinkedList (java.util.LinkedList)2 Entry (java.util.Map.Entry)2 Ignore (org.junit.Ignore)2 Actor (org.zeromq.ZActor.Actor)2 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1