From 443d06f894751062da6d69238ce09f6fbfc27577 Mon Sep 17 00:00:00 2001 From: Martin Sustrik Date: Sun, 15 Apr 2012 13:10:41 +0200 Subject: "Survey" pattern implemented Survey pattern is "multicast with reply". There are two roles: surveyor and respondent. Surveyor publishes a survey which gets delivered to all connected respondents. Each repondent can send a response to the survey. All the responses are delivered to the original surveyor. Once the surveyor decides that the survey is over (e.g. deadline was reached) it can send initiate survey. Late responses from old surveys are automatically discarded by the surveyor socket. Socket types: SURVEYOR, XSURVEYOR, RESPONDENT, XRESPONDENT Patch also includes a test program with surveoyr, two respondents and an intermediary device. Signed-off-by: Martin Sustrik --- tests/Makefile.am | 4 +- tests/survey.cpp | 135 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/tests.cpp | 7 ++- 3 files changed, 144 insertions(+), 2 deletions(-) create mode 100644 tests/survey.cpp (limited to 'tests') diff --git a/tests/Makefile.am b/tests/Makefile.am index af826af..ba76260 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -23,7 +23,8 @@ noinst_PROGRAMS = pair_inproc \ polltimeo \ wireformat \ libzmq21 \ - resubscribe + resubscribe \ + survey pair_inproc_SOURCES = pair_inproc.cpp testutil.hpp pair_tcp_SOURCES = pair_tcp.cpp testutil.hpp @@ -46,5 +47,6 @@ polltimeo_SOURCES = polltimeo.cpp testutil.hpp wireformat_SOURCES = wireformat.cpp libzmq21_SOURCES = libzmq21.cpp resubscribe_SOURCES = resubscribe.cpp +survey_SOURCES = survey.cpp TESTS = $(noinst_PROGRAMS) diff --git a/tests/survey.cpp b/tests/survey.cpp new file mode 100644 index 0000000..41f39aa --- /dev/null +++ b/tests/survey.cpp @@ -0,0 +1,135 @@ +/* + Copyright (c) 2012 250bpm s.r.o. + Copyright (c) 2012 Other contributors as noted in the AUTHORS file + + This file is part of Crossroads I/O project. + + Crossroads I/O is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + Crossroads is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . +*/ + +#include "testutil.hpp" + +int XS_TEST_MAIN () +{ + int rc; + char buf [32]; + + fprintf (stderr, "survey test running...\n"); + + // Create the basic infrastructure. + void *ctx = xs_init (); + assert (ctx); + void *xsurveyor = xs_socket (ctx, XS_XSURVEYOR); + assert (xsurveyor); + rc = xs_bind (xsurveyor, "inproc://a"); + assert (rc == 0); + void *xrespondent = xs_socket (ctx, XS_XRESPONDENT); + assert (xrespondent); + rc = xs_bind (xrespondent, "inproc://b"); + assert (rc == 0); + void *surveyor = xs_socket (ctx, XS_SURVEYOR); + assert (surveyor); + rc = xs_connect (surveyor, "inproc://b"); + assert (rc == 0); + void *respondent1 = xs_socket (ctx, XS_RESPONDENT); + assert (respondent1); + rc = xs_connect (respondent1, "inproc://a"); + assert (rc == 0); + void *respondent2 = xs_socket (ctx, XS_RESPONDENT); + assert (respondent2); + rc = xs_connect (respondent2, "inproc://a"); + assert (rc == 0); + + // Send the survey. + rc = xs_send (surveyor, "ABC", 3, 0); + assert (rc == 3); + + // Forward the survey through the intermediate device. + // Survey consist of identity (4 bytes), survey ID (4 bytes) and the body. + rc = xs_recv (xrespondent, buf, sizeof (buf), 0); + assert (rc == 4); + rc = xs_send (xsurveyor, buf, 4, XS_SNDMORE); + assert (rc == 4); + rc = xs_recv (xrespondent, buf, sizeof (buf), 0); + assert (rc == 4); + rc = xs_send (xsurveyor, buf, 4, XS_SNDMORE); + assert (rc == 4); + rc = xs_recv (xrespondent, buf, sizeof (buf), 0); + assert (rc == 3); + rc = xs_send (xsurveyor, buf, 3, 0); + assert (rc == 3); + + // Respondent 1 responds to the survey. + rc = xs_recv (respondent1, buf, sizeof (buf), 0); + assert (rc == 3); + rc = xs_send (respondent1, "DE", 2, 0); + assert (rc == 2); + + // Forward the response through the intermediate device. + rc = xs_recv (xsurveyor, buf, sizeof (buf), 0); + assert (rc == 4); + rc = xs_send (xrespondent, buf, 4, XS_SNDMORE); + assert (rc == 4); + rc = xs_recv (xsurveyor, buf, sizeof (buf), 0); + assert (rc == 4); + rc = xs_send (xrespondent, buf, 4, XS_SNDMORE); + assert (rc == 4); + rc = xs_recv (xsurveyor, buf, sizeof (buf), 0); + assert (rc == 2); + rc = xs_send (xrespondent, buf, 2, 0); + assert (rc == 2); + + // Surveyor gets the response. + rc = xs_recv (surveyor, buf, sizeof (buf), 0); + assert (rc == 2); + + // Respondent 2 responds to the survey. + rc = xs_recv (respondent2, buf, sizeof (buf), 0); + assert (rc == 3); + rc = xs_send (respondent2, "FGHI", 4, 0); + assert (rc == 4); + + // Forward the response through the intermediate device. + rc = xs_recv (xsurveyor, buf, sizeof (buf), 0); + assert (rc == 4); + rc = xs_send (xrespondent, buf, 4, XS_SNDMORE); + assert (rc == 4); + rc = xs_recv (xsurveyor, buf, sizeof (buf), 0); + assert (rc == 4); + rc = xs_send (xrespondent, buf, 4, XS_SNDMORE); + assert (rc == 4); + rc = xs_recv (xsurveyor, buf, sizeof (buf), 0); + assert (rc == 4); + rc = xs_send (xrespondent, buf, 4, 0); + assert (rc == 4); + + // Surveyor gets the response. + rc = xs_recv (surveyor, buf, sizeof (buf), 0); + assert (rc == 4); + + rc = xs_close (respondent2); + assert (rc == 0); + rc = xs_close (respondent1); + assert (rc == 0); + rc = xs_close (surveyor); + assert (rc == 0); + rc = xs_close (xrespondent); + assert (rc == 0); + rc = xs_close (xsurveyor); + assert (rc == 0); + rc = xs_term (ctx); + assert (rc == 0); + + return 0 ; +} diff --git a/tests/tests.cpp b/tests/tests.cpp index 21d10e3..3d7951c 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -111,6 +111,10 @@ #include "resubscribe.cpp" #undef XS_TEST_MAIN +#define XS_TEST_MAIN survey +#include "survey.cpp" +#undef XS_TEST_MAIN + int main () { int rc; @@ -155,7 +159,8 @@ int main () assert (rc == 0); rc = resubscribe (); assert (rc == 0); - + rc = survey (); + assert (rc == 0); fprintf (stderr, "SUCCESS\n"); sleep (1); -- cgit v1.2.3