简体   繁体   中英

How to receive data from a python ZeroMQ PUB server on a Java SUB client (ZMQ)

I'm working with Pupil Labs , a huge open source for eye/pupil tracking. The entire code is written in Python. The so-called Pupil Remote is based on ZeroMQ.

If I start running the Filter Messages everything is fine. For my purposes I need to "translate" Filter Messages into Java because I created an Android app, which should call a client, which has the purpose to serve as the python client.

Here's what I've done so far:

import android.annotation.SuppressLint;
import org.zeromq.ZMQ;
import java.nio.charset.Charset;
import static java.lang.Thread.sleep;

public class ZeroMQClient {
    @SuppressLint("NewApi")
    public static void requestGazeData() {

        ZMQ.Context context = ZMQ.context(1);
        ZMQ.Socket subscriber = context.socket(ZMQ.SUB);

        System.out.println("Connecting to server...");

        subscriber.connect("tcp://xxx.x.x.x:50020");

        System.out.println("Connected");

        String gaze = "gaze";
        subscriber.subscribe(gaze.getBytes(Charset.forName("UTF-8")));

        while (true) {
            String msg = subscriber.recvStr();
            System.out.println(msg);

            subscriber.close();
            context.term();
        }
    }
}

Now as you can expect, why I'm asking you, nothing happens, I don't receive any data from the Pupil Labs server. I oriented myself on this post , but unfortunately, it didn't work out for me. Also the IP-Address and port are the same as on the server. It works neither locally nor remotely.

Happy about any answer, since I stuck at this.

Due to the correct establishment in terms of my implementation the actual issue was the firewall, which just blocked the connection. By posting my solution I'm hopefully able to help future visitors of this question.

The final solution, after having debugged the root-cause issue is below


Happy about having answer, you have to set a subscription Policy:

ZeroMQ expects each SUB -side to first explicitly say, what this SUB -side wants to receive from PUB ( Yes, what it to subscribes to ).

Like your mailbox will never get newspapers in, without first subscribing to any. :o)

So setup an empty string "" in the subscriber and you are done:

// String                filterPermitANY = "";        // WAS AN EXAMPLE TO TEST
// subscriber.subscribe( filterPermitANY.getBytes() );//     IF PUB.send()-s ANY
   String                gaze = "gaze";               // WAS ON TOPIC
   subscriber.subscribe( gaze.getBytes() );           //     

Voilá.

Having zero-warranty what python version is running on the opposite side, tweaking may take place for string-representation matching...

( Also recommended to setup LINGER to 1, that prevents from hanging terminations
and preferably it is the best time to turn the process
into using a non-blocking .poll() + .recv( ..., ZMQ_DONTWAIT ) in a soft-realtime maintained event-loop )


[ 1 ] We have got confirmed the Android/ZeroMQ side is working fine

if the PUB -side was mocked by a plain python- PUB infinite-sender and the Android- SUB was subscribed to String filterPermitANY ="";

This makes the above claim " It's an issue from the android side " actually void if not misleading.


[ 2 ] Next comes the question why it still does not work?

And the answer is : because the above designed code does not follow the published principles , how to connect and use the Pupil Labs API.

A careful reader will notice that the Pupil Labs API is not connected by the SUB -side ( be it an Android or python or whatever else implementation of such a peer ) on a port :50020 , but on another port, which is first asked about via another dialogue, held over an REQ/REP -formal communication archetype ( lines 13/14/15+19 ).


Epilogue

Knocking on a wrong door will never make the intended interview happen.

One first has to ask onto which door to knock next, so as to get the Pupil Labs API into the game.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM