Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "mdp_worker.h"
- #define SAFEFREE(x) \
- if (x) { \
- free(x); \
- (x) = NULL; \
- }
- int main() {
- char service[] = "bb-test";
- char endpoint[] = "ipc:///tmp/bbtest.ipc";
- mdp_worker_t *worker_session = NULL;
- zsock_t *worker_sock = NULL;
- zframe_t *address = NULL;
- char *cmd = NULL;
- char *request = NULL;
- char *reply = NULL;
- int rc = 0;
- /* create new worker and register the service with the broker */
- worker_session = mdp_worker_new(endpoint, service);
- assert(worker_session != NULL);
- //mdp_worker_set_verbose(worker_session);
- worker_sock = mdp_worker_msgpipe(worker_session);
- assert(worker_sock != NULL);
- while (1) {
- rc = zsock_recv(worker_sock, "sfs", &cmd, &address, &request);
- if (rc != 0) {
- fprintf(stderr, "Failed to receive message: %s\r\n",
- mdp_worker_reason(worker_session));
- continue;
- }
- fprintf(stdout, "Got message \"%s\"\r\n", request);
- reply = calloc(strlen(request) + 10, sizeof(char));
- assert(reply != NULL);
- snprintf(reply, strlen(request) + 10, "%s - reply", request);
- /* Create reply message */
- zmsg_t *msg_response = zmsg_new();
- assert(msg_response != NULL);
- /* Send */
- rc = zmsg_addstr(msg_response, reply);
- assert(rc == 0);
- rc = mdp_worker_send_final(worker_session, &address, &msg_response);
- fprintf(rc == 0 ? stdout : stderr, "Sending reply (\"%s\") was %s\r\n\r\n",
- reply, rc == 0 ? "successful" : "UNSUCCESSFUL");
- zmsg_destroy(&msg_response);
- SAFEFREE(cmd)
- SAFEFREE(request)
- SAFEFREE(reply)
- }
- mdp_worker_destroy(&worker_session);
- exit(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment