Skip to content

Instantly share code, notes, and snippets.

@hintjens
Created November 6, 2013 12:51
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save hintjens/7335589 to your computer and use it in GitHub Desktop.
ZeroMQ TIPC load-balancing example
/*
* cli.c: TIPC reqrep client example
*
* Copyright (c) 2013 Ericsson AB
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the names of the copyright holders nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* Alternatively, this software may be distributed under the terms of the
* GNU General Public License ("GPL") version 2 as published by the Free
* Software Foundation.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <zmq.h>
#include <zmq_utils.h>
#include <assert.h>
int main (int argc, char **argv)
{
void *ctx;
void *client;
int rc;
char buff[255];
char c;
char *servname = NULL;
char hostname[128];
while ((c = getopt(argc, argv, "c:")) != -1)
{
switch (c) {
case 'c':
servname = optarg;
break;
default:
bail:
printf("usage: %s -s tipc://{type, instance}\n"\
"example: %s -s tipc://{1000,42}", argv[0], argv[0]);
return -1;
}
}
if (!servname)
goto bail;
gethostname(hostname, sizeof(hostname));
printf("%s sending message to: %s\n", hostname, servname);
ctx = zmq_init (1);
assert (ctx);
client = zmq_socket(ctx, ZMQ_REQ);
assert (client);
rc = zmq_connect(client, servname);
assert (rc == 0);
sleep(1); /* Yield, let it connect..*/
memset(buff,0, sizeof(buff));
sprintf(buff, "hello world from %s", hostname);
rc = zmq_send(client, buff, strlen(buff), 0);
assert (rc >= 0);
printf ("client sent message '%s', rc=%d\n", buff, rc);
rc = zmq_recv(client, buff, sizeof(buff), 0);
assert (rc >= 0);
printf("%s\n", buff);
rc = zmq_close (client);
assert (rc == 0);
rc = zmq_term (ctx);
assert (rc == 0);
return 0 ;
}
all: srv.c
g++ srv.c /home/eerihug/dev/BakkaLinux/root/usr/local/lib/libzmq.a -lpthread -lrt -o srv
g++ cli.c /home/eerihug/dev/BakkaLinux/root/usr/local/lib/libzmq.a -lpthread -lrt -o cli
clean:
rm cli
rm srv
/*
* srv.c: TIPC reqrep server example
*
* Copyright (c) 2013 Ericsson AB
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the names of the copyright holders nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* Alternatively, this software may be distributed under the terms of the
* GNU General Public License ("GPL") version 2 as published by the Free
* Software Foundation.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <zmq.h>
#include <zmq_utils.h>
#include <assert.h>
int main (int argc, char **argv)
{
void *ctx;
void *server;
int rc;
char buff[255];
char c;
char *servname = NULL;
char hostname[128];
while ((c = getopt(argc, argv, "s:")) != -1)
{
switch (c) {
case 's':
servname = optarg;
break;
default:
bail:
printf("usage: %s -s tipc://{type, lower, upper}\n"\
"example: %s -s tipc://{1000,0,10}", argv[0], argv[0]);
return -1;
}
}
if (!servname)
goto bail;
gethostname(hostname, sizeof(hostname));
printf("server %s listening on %s\n", hostname, servname);
ctx = zmq_init (1);
assert (ctx);
server = zmq_socket (ctx, ZMQ_REP);
rc = zmq_bind (server, servname);
assert (rc == 0);
again:
rc = zmq_recv(server, buff, sizeof(buff), 0);
assert (rc >= 0);
printf ("server %s received message:'%s'\n", hostname, buff);
sprintf(buff, "Your request have been served by %s\n", hostname);
rc = zmq_send(server, buff, sizeof(buff), 0);
assert (rc >= 0);
goto again; /* None shall pass */
rc = zmq_close (server);
assert (rc == 0);
rc = zmq_term (ctx);
assert (rc == 0);
return 0 ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment