Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions tests/pubsub.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,35 @@ reader_http_request(struct cx *c, const char* buffer, const char *limit) {
}
}

/*
* prepare an event structure(s)
*/
void
cx_init(struct cx* c)
{
if(c->read_fun) {
event_set(&c->evr, c->fd, EV_READ, c->read_fun, c);
event_base_set(c->base, &c->evr);
}

if(c->write_fun) {
event_set(&c->evw, c->fd, EV_WRITE, c->write_fun, c);
event_base_set(c->base, &c->evw);
}
}

/**
* (re)install connection in the event loop.
*/
void
cx_install(struct cx *c) {

if(c->read_fun) { /* attach callback for read. */
event_set(&c->evr, c->fd, EV_READ, c->read_fun, c);
event_base_set(c->base, &c->evr);
event_add(&c->evr, NULL);
}
if(c->write_fun) { /* attach callback for write. */
event_set(&c->evw, c->fd, EV_WRITE, c->write_fun, c);
event_base_set(c->base, &c->evw);
event_add(&c->evw, NULL);
}

}

/**
Expand Down Expand Up @@ -160,6 +172,7 @@ reader_new(struct event_base *base, const char *host, short port, int total, int
reader_http_request(c, c->http_request, "{\"SUBSCRIBE\":[\"subscribe\"");

/* add to the event loop. */
cx_init(c);
cx_install(c);
}

Expand Down Expand Up @@ -209,6 +222,7 @@ writer_new(struct event_base *base, const char *host, short port, int chan) {
sprintf(c->http_request, "GET /PUBLISH/chan:%d/hi HTTP/1.1\r\n\r\n", chan);
reader_http_request(c, c->http_request, "{\"PUBLISH\":");

cx_init(c);
cx_install(c);
}

Expand Down