Discussion:
[capnproto] Trying to send C-CapnProto messages over C-ZeromMQ
Carol Rice
2018-07-24 17:31:55 UTC
Permalink
Can someone please help? I've worked through several compile time errors
and run-time errors, and I'm still stuck. I'm trying to send CapnProto
messages over ZeroMQ Pub-Sub all in C. When I run ./subscriber_test and
./publisher_test, the subscriber code errors out saying
Initialization returned 0
Expected ID is 17 Got back: 0
Expected Name is Firstname Lastname Got back: (null)
Expected Emails is ***@domain.com Got back: (null)
I: 18-07-24 10:29:27 Recv on TOPIC
I: 18-07-24 10:29:27 > MESSAGE PART
I: 18-07-24 10:29:27 > ANOTHER
Invalid argument (src/mutex.hpp:142)
Aborted (core dumped)


I think I have a problem initializing with capn_init_mem. Can someone help
explain how to do this properly?


Here is my subscriber code which is compiled with this command: gcc -
Ic-capnproto/ -Wall -g publisher_test.c c-capnproto/addressbook.capnp.c -
lzmq -lczmq -lcapnp -lcapnp_c -o publisher_test

#include <stdio.h>
#include <czmq.h>
#include "c-capnproto/capnp_c.h"
#include "c-capnproto/addressbook.capnp.h"


int main(int argc, char ** argv) {
const char *name = "Firstname Lastname";
const char *email = "***@domain.com";
//const char *school = "of life";
Person_ptr rroot;
struct Person rp;




zmq_msg_t msg;
int rc = zmq_msg_init(&msg);
printf("Initialization returned %d\n", rc);


/**********************************
*C-CapnProto
***********************************/
void *data = zmq_msg_data (&msg);
size_t size = zmq_msg_size (&msg);
struct capn ctx;
int init_mem_ret = capn_init_mem(&ctx, data, size, 0);
//assert(init_mem_ret == 0);

/**********************************
*C-ZMQ
***********************************/
zsock_t *socket = zsock_new_sub("ipc://example.sock", "TOPIC");
assert(socket);
char *topic;
char *frame;
zmsg_t *msg_c;
rc = zsock_recv(socket, "sm", &topic, &msg_c);
assert(rc==0);

/**********************************
*C-CapnProto
***********************************/
rroot.p = capn_getp(capn_root(&ctx), 0 /* off */, 1 /* resolve */);
read_Person(&rp, rroot);




// Assert deserialized values in `rp`
//EXPECT_EQ(rp.id, (uint32_t) 17);
//EXPECT_CAPN_TEXT_EQ(name, rp.name);
//EXPECT_CAPN_TEXT_EQ(email, rp.email);
printf("Expected ID is %d Got back: %d\n", 17, rp.id);
printf("Expected Name is %s Got back: %s\n", name, rp.name);
printf("Expected Emails is %s Got back: %s\n", email, rp.email);




/**********************************
*C-ZMQ
***********************************/
zsys_info("Recv on %s", topic);
while(frame = zmsg_popstr(msg_c)) {
zsys_info("> %s", frame);
free(frame);
}




Here is my publisher code which is compiled with this command: gcc -
Ic-capnproto/ -Wall -g publisher_test.c c-capnproto/addressbook.capnp.c -
lzmq -lczmq -lcapnp -lcapnp_c -o publisher_test

#include <stdio.h>
#include <czmq.h>
#include "c-capnproto/capnp_c.h"
#include "c-capnproto/addressbook.capnp.h"


static capn_text chars_to_text(const char *chars) {
return (capn_text) {
.len = (int) strlen(chars),
.str = chars,
.seg = NULL,
};
}




int main(int argc, char ** argv) {
uint8_t buf[4096];
ssize_t sz = 0;


const char *name = "Firstname Lastname";
const char *email = "***@domain.com";
const char *school = "of life";





struct capn c;
capn_init_malloc(&c);
capn_ptr cr = capn_root(&c);
struct capn_segment *cs = cr.seg;


// Set initial object in `p`.
struct Person p = {
.id = 17,
.name = chars_to_text(name),
.email = chars_to_text(email),
};


/* p.employment_which = Person_employment_school;
p.employment.school = chars_to_text(school);


p.phones = new_Person_PhoneNumber_list(cs, 2);
struct Person_PhoneNumber pn0 = {
.number = chars_to_text("123"),
.type = Person_PhoneNumber_Type_work,
};
set_Person_PhoneNumber(&pn0, p.phones, 0);
struct Person_PhoneNumber pn1 = {
.number = chars_to_text("234"),
.type = Person_PhoneNumber_Type_home,
};
set_Person_PhoneNumber(&pn1, p.phones, 1);
*/
Person_ptr pp = new_Person(cs);
write_Person(&p, pp);
int setp_ret = capn_setp(capn_root(&c), 0, pp.p);
//ASSERT_EQ(0, setp_ret);
sz = capn_write_mem(&c, buf, sizeof(buf), 0 /* packed */);
capn_free(&c);


zsock_t *socket = zsock_new_pub("ipc://example.sock");
assert(socket);


int x;
while(!zsys_interrupted) {
zsys_info("Publishing");
x = zsock_send(socket, "sss", "TOPIC", "MESSAGE PART", "ANOTHER");
//zsock_send(socket, buf);
printf("Zsock_send returned %d\n", x);
zclock_sleep(1000);
}


zsock_destroy(&socket);
return 0;
}
--
You received this message because you are subscribed to the Google Groups "Cap'n Proto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to capnproto+***@googlegroups.com.
Visit this group at https://groups.google.com/group/capnproto.
'Kenton Varda' via Cap'n Proto
2018-07-28 20:17:18 UTC
Permalink
Hi Carol,

Unfortunately I'm not sure if the maintainers of C-CapnProto are active on
this list. You may have more success filing an issue on their Github
repository, or e-mailing the maintainers personally.

-Kenton
Post by Carol Rice
Can someone please help? I've worked through several compile time errors
and run-time errors, and I'm still stuck. I'm trying to send CapnProto
messages over ZeroMQ Pub-Sub all in C. When I run ./subscriber_test and
./publisher_test, the subscriber code errors out saying
Initialization returned 0
Expected ID is 17 Got back: 0
Expected Name is Firstname Lastname Got back: (null)
I: 18-07-24 10:29:27 Recv on TOPIC
I: 18-07-24 10:29:27 > MESSAGE PART
I: 18-07-24 10:29:27 > ANOTHER
Invalid argument (src/mutex.hpp:142)
Aborted (core dumped)
I think I have a problem initializing with capn_init_mem. Can someone help
explain how to do this properly?
Here is my subscriber code which is compiled with this command: gcc -
Ic-capnproto/ -Wall -g publisher_test.c c-capnproto/addressbook.capnp.c -
lzmq -lczmq -lcapnp -lcapnp_c -o publisher_test
#include <stdio.h>
#include <czmq.h>
#include "c-capnproto/capnp_c.h"
#include "c-capnproto/addressbook.capnp.h"
int main(int argc, char ** argv) {
const char *name = "Firstname Lastname";
//const char *school = "of life";
Person_ptr rroot;
struct Person rp;
zmq_msg_t msg;
int rc = zmq_msg_init(&msg);
printf("Initialization returned %d\n", rc);
/**********************************
*C-CapnProto
***********************************/
void *data = zmq_msg_data (&msg);
size_t size = zmq_msg_size (&msg);
struct capn ctx;
int init_mem_ret = capn_init_mem(&ctx, data, size, 0);
//assert(init_mem_ret == 0);
/**********************************
*C-ZMQ
***********************************/
zsock_t *socket = zsock_new_sub("ipc://example.sock", "TOPIC");
assert(socket);
char *topic;
char *frame;
zmsg_t *msg_c;
rc = zsock_recv(socket, "sm", &topic, &msg_c);
assert(rc==0);
/**********************************
*C-CapnProto
***********************************/
rroot.p = capn_getp(capn_root(&ctx), 0 /* off */, 1 /* resolve */);
read_Person(&rp, rroot);
// Assert deserialized values in `rp`
//EXPECT_EQ(rp.id, (uint32_t) 17);
//EXPECT_CAPN_TEXT_EQ(name, rp.name);
//EXPECT_CAPN_TEXT_EQ(email, rp.email);
printf("Expected ID is %d Got back: %d\n", 17, rp.id);
printf("Expected Name is %s Got back: %s\n", name, rp.name);
printf("Expected Emails is %s Got back: %s\n", email, rp.email);
/**********************************
*C-ZMQ
***********************************/
zsys_info("Recv on %s", topic);
while(frame = zmsg_popstr(msg_c)) {
zsys_info("> %s", frame);
free(frame);
}
Here is my publisher code which is compiled with this command: gcc -
Ic-capnproto/ -Wall -g publisher_test.c c-capnproto/addressbook.capnp.c -
lzmq -lczmq -lcapnp -lcapnp_c -o publisher_test
#include <stdio.h>
#include <czmq.h>
#include "c-capnproto/capnp_c.h"
#include "c-capnproto/addressbook.capnp.h"
static capn_text chars_to_text(const char *chars) {
return (capn_text) {
.len = (int) strlen(chars),
.str = chars,
.seg = NULL,
};
}
int main(int argc, char ** argv) {
uint8_t buf[4096];
ssize_t sz = 0;
const char *name = "Firstname Lastname";
const char *school = "of life";
struct capn c;
capn_init_malloc(&c);
capn_ptr cr = capn_root(&c);
struct capn_segment *cs = cr.seg;
// Set initial object in `p`.
struct Person p = {
.id = 17,
.name = chars_to_text(name),
.email = chars_to_text(email),
};
/* p.employment_which = Person_employment_school;
p.employment.school = chars_to_text(school);
p.phones = new_Person_PhoneNumber_list(cs, 2);
struct Person_PhoneNumber pn0 = {
.number = chars_to_text("123"),
.type = Person_PhoneNumber_Type_work,
};
set_Person_PhoneNumber(&pn0, p.phones, 0);
struct Person_PhoneNumber pn1 = {
.number = chars_to_text("234"),
.type = Person_PhoneNumber_Type_home,
};
set_Person_PhoneNumber(&pn1, p.phones, 1);
*/
Person_ptr pp = new_Person(cs);
write_Person(&p, pp);
int setp_ret = capn_setp(capn_root(&c), 0, pp.p);
//ASSERT_EQ(0, setp_ret);
sz = capn_write_mem(&c, buf, sizeof(buf), 0 /* packed */);
capn_free(&c);
zsock_t *socket = zsock_new_pub("ipc://example.sock");
assert(socket);
int x;
while(!zsys_interrupted) {
zsys_info("Publishing");
x = zsock_send(socket, "sss", "TOPIC", "MESSAGE PART", "ANOTHER");
//zsock_send(socket, buf);
printf("Zsock_send returned %d\n", x);
zclock_sleep(1000);
}
zsock_destroy(&socket);
return 0;
}
--
You received this message because you are subscribed to the Google Groups
"Cap'n Proto" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at https://groups.google.com/group/capnproto.
--
You received this message because you are subscribed to the Google Groups "Cap'n Proto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to capnproto+***@googlegroups.com.
Visit this group at https://groups.google.com/group/capnproto.
Loading...