Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getopt broken for nn_sub #51

Closed
nickdesaulniers opened this issue Feb 24, 2015 · 6 comments
Closed

getopt broken for nn_sub #51

nickdesaulniers opened this issue Feb 24, 2015 · 6 comments
Labels

Comments

@nickdesaulniers
Copy link
Owner

See the discussion here. This needs to be fixed: #46 (comment)

http://nanomsg.org/v0.2/nn_setsockopt.3.html
http://nanomsg.org/v0.2/nn_getsockopt.3.html

@reqshark
Copy link
Collaborator

reqshark commented Mar 2, 2015

right now we're subscribing to all msgs, i.e., the subscription key is an empty string.

should that choice be determined by the user?

@reqshark
Copy link
Collaborator

reqshark commented Mar 3, 2015

@nickdesaulniers, we need to implement any socket subscription tracking at the JavaScript level.

Nanomsg was not designed to keep track of that.

And it looks like this was a deliberate choice: nn_getsockopt() does not return subscription lists, or anything but an err for that matter, see when this was discussed nanomsg/nanomsg#203

@nickdesaulniers
Copy link
Owner Author

Also related is PR #37 . I'm getting a really bad feeling about this API. Apparently, you set a socket option on the subscriber, which allows it to ignore the first word in a message. I would have expected you to set the same topic on the publisher, but this is not the case. I'll need to spend more time figuring this out, but I really think this feature is broken upstream. At the bare minimum, the API makes it ambiguous for language binding maintainers to pass an int vs char[].

test_case.c:

// clang test_case.c -lnanomsg -o test_case && ./test_case
#include <pthread.h> // pthread_t, pthread_create, pthread_join
#include <stdio.h> // printf
#include <assert.h> // assert
#include <string.h> // strnlen, memset
#include <unistd.h> // usleep
#include "nanomsg/nn.h"
#include "nanomsg/pubsub.h"

// Shared memory
const char* addr = "tcp://127.0.0.1:7777";
//

void* publisher (void* data) {
  printf("hello from publisher thread\n");

  int sock = nn_socket(AF_SP, NN_PUB);
  assert(sock >= 0);

  assert(nn_bind(sock, addr) >= 0);

  const char* msg1 = "topic.A hello world";
  const char* msg2 = "topic.B goodbye world";

  while (1) {
    int bytes = nn_send(sock, msg1, strnlen(msg1, 200), 0);
    printf("sent: %s\n", msg1);
    bytes = nn_send(sock, msg2, strnlen(msg2, 200), 0);
    printf("sent: %s\n", msg2);
    /*usleep(10000); // 10ms*/
    sleep(1);
  }

  nn_shutdown(sock, 0);

  return NULL;
}

void* subscriber (void* data) {
  printf("hello from subscriber thread\n");

  int sock = nn_socket(AF_SP, NN_SUB);
  assert(sock >= 0);

  assert(nn_setsockopt(sock, NN_SUB, NN_SUB_SUBSCRIBE, "topic.A", 0) >= 0);
  assert(nn_connect(sock, addr) >= 0);

  char buf [200];
  while (1) {
    int bytes = nn_recv(sock, &buf, 200, 0);

    printf("received: %s\n", buf);
    /*nn_freemsg(&buf);*/
    memset(&buf, 0, 200);
  }

  nn_shutdown(sock, 0);

  return NULL;
}

int main () {
  pthread_t p, s;
  assert(pthread_create(&p, NULL, publisher, NULL) == 0);
  assert(pthread_create(&s, NULL, subscriber, NULL) == 0);
  assert(pthread_join(p, NULL) == 0);
  assert(pthread_join(s, NULL) == 0);
}

I would have expected the topic.B messages to be filtered out, but this is not the case.

@reqshark
Copy link
Collaborator

reqshark commented Mar 8, 2015

wait a minute, so with that code you're not able to receive msg2 are you?

@nickdesaulniers
Copy link
Owner Author

With that code, I do receive msg2, so either filtering does not work, or does not work as I'm expecting.

@nickdesaulniers
Copy link
Owner Author

dupe #31

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants