User Tools

Site Tools


Nano-message next-generation framework sample

It is a good library. IMHO after some my small research, NNG better ZeroMQ (TLS, websockets, thread-safe, “unkillable” req-rep, etc)

See RATIONALE

PUB-SUB (publisher-subscriber) model sample

nng-pub.c

nng-pub.c
/*
 * Copyright 2004-2019 Oleg Borodin  <borodin@unix7.org>
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
 
#include <nng/nng.h>
#include <nng/protocol/pubsub0/pub.h>
#include <nng/protocol/pubsub0/sub.h>
 
char* date(void) {
    time_t now = time(&now);
    struct tm *info = localtime(&now);
    char *text = asctime(info);
    text[strlen(text)-1] = '\0';
    return text;
}
 
void fatal(const char *func, int rv) {
    fprintf(stderr, "%s: %s\n", func, nng_strerror(rv));
}
 
int main(int argc, char **argv) {
 
    nng_socket sock;
    int rv;
    char url[] = "tcp://127.0.0.1:1024";
 
    if ((rv = nng_pub0_open(&sock)) != 0) {
            fatal("nng_pub0_open", rv);
    }
    if ((rv = nng_listen(sock, url, NULL, 0)) < 0) {
            fatal("nng_listen", rv);
    }
    while(1) {
        char* ts = date();
        printf("nng send %s\n", ts);
        if ((rv = nng_send(sock, ts, strlen(ts) + 1, 0)) != 0) {
                fatal("nng_send", rv);
        }
        sleep(1);
    }
}

nng-sub.c

nng-sub.c
/*
 * Copyright 2004-2019 Oleg Borodin  <borodin@unix7.org>
 */
 
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
 
#include <nng/nng.h>
#include <nng/protocol/pubsub0/pub.h>
#include <nng/protocol/pubsub0/sub.h>
 
void fatal(const char *func, int rv) {
    fprintf(stderr, "%s: %s\n", func, nng_strerror(rv));
}
 
int main(int argc, char **argv) {
 
    char name[] = "client01";
    nng_socket sock;
    int rv;
    char url[] = "tcp://127.0.0.1:1024";
 
    if ((rv = nng_sub0_open(&sock)) != 0) {
        fatal("nng_sub0_open", rv);
    }
 
    if ((rv = nng_setopt(sock, NNG_OPT_SUB_SUBSCRIBE, "", 0)) != 0) {
        fatal("nng_setopt", rv);
    }
    if ((rv = nng_dial(sock, url, NULL, 0)) != 0) {
        fatal("nng_dial", rv);
    }
    while(1) {
        char *buffer = NULL;
        size_t size;
        if ((rv = nng_recv(sock, &buffer, &size, NNG_FLAG_ALLOC)) != 0) {
                fatal("nng_recv", rv);
        }
        printf("client (%s) received: %s\n", name, buffer);
        nng_free(buffer, size);
    }
}

REQ-REP (request-report) model sample

nng-rep.c

nng-rep.c
/*
 * Copyright 2004-2019 Oleg Borodin  <borodin@unix7.org>
 */
 
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
 
#include <nng/nng.h>
#include <nng/protocol/reqrep0/rep.h>
#include <nng/protocol/reqrep0/req.h>
 
#include <nng/protocol/pubsub0/pub.h>
#include <nng/protocol/pubsub0/sub.h>
 
char* date(void) {
    time_t now = time(&now);
    struct tm *info = localtime(&now);
    char *text = asctime(info);
    text[strlen(text)-1] = '\0';
    return text;
}
 
void fatal(const char *func, int rv) {
    fprintf(stderr, "%s: %s\n", func, nng_strerror(rv));
}
 
int main(int argc, char **argv) {
 
    nng_socket sock;
    int rv;
 
    char url[] = "tcp://127.0.0.1:1024";
 
    if ((rv = nng_rep0_open(&sock)) != 0) {
            fatal("nng_rep0_open", rv);
    }
    if ((rv = nng_listen(sock, url, NULL, 0)) != 0) {
            fatal("nng_listen", rv);
    }
    while(1) {
        char *buf = NULL;
        size_t sz;
        if ((rv = nng_recv(sock, &buf, &sz, NNG_FLAG_ALLOC)) != 0) {
                fatal("nng_recv", rv);
        }
        printf("received request\n");
 
        char *d = date();
        printf("sending %s\n", d);
        if ((rv = nng_send(sock, d, strlen(d) + 1, 0)) != 0) {
                fatal("nng_send", rv);
        }
        nng_free(buf, sz);
    }
}

nng-req.c

nng-req.c
/*
 * Copyright 2004-2019 Oleg Borodin  <borodin@unix7.org>
 */
 
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
 
#include <nng/nng.h>
#include <nng/protocol/reqrep0/rep.h>
#include <nng/protocol/reqrep0/req.h>
 
#include <nng/protocol/pubsub0/pub.h>
#include <nng/protocol/pubsub0/sub.h>
 
void fatal(const char *func, int rv) {
    fprintf(stderr, "%s: %s\n", func, nng_strerror(rv));
}
 
int main(int argc, char **argv) {
 
    nng_socket sock;
    int rv;
    char url[] = "tcp://127.0.0.1:1024";
 
    if ((rv = nng_req0_open(&sock)) != 0) {
            fatal("nng_socket", rv);
    }
    if ((rv = nng_dial(sock, url, NULL, 0)) != 0) {
            fatal("nng_dial", rv);
    }
 
    while(1) {
        char *buf = NULL;
        char data[] = "date";
        size_t sz = 0;
        printf("sending request\n");
        if ((rv = nng_send(sock, data, strlen(data), 0)) != 0) {
            fatal("nng_send", rv);
        }
 
        if ((rv = nng_recv(sock, &buf, &sz, NNG_FLAG_ALLOC)) != 0) {
            fatal("nng_recv", rv);
        }
        printf("received date %s\n", buf);
        nng_free(buf, sz);
 
        sleep(1);
    }
    nng_close(sock);
    return (0);
}

First PagePrevious PageBack to overviewNext PageLast Page