-
-
Notifications
You must be signed in to change notification settings - Fork 487
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add forwarder demo #1669
Merged
Merged
Add forwarder demo #1669
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# This software is supplied under the terms of the MIT License, a | ||
# copy of which should be located in the distribution where this | ||
# file was obtained (LICENSE.txt). A copy of the license may also be | ||
# found online at https://opensource.org/licenses/MIT. | ||
|
||
cmake_minimum_required(VERSION 3.10) | ||
project(pubsub_forwarder C) | ||
|
||
# Find the nng library | ||
find_package(nng REQUIRED) | ||
|
||
# Add the executable target | ||
add_executable(pubsub_forwarder pubsub_forwarder.c) | ||
|
||
target_compile_options(pubsub_forwarder PRIVATE -Wall -Wextra -Wpedantic -Werror -O2) | ||
|
||
# Link against the nng library | ||
target_link_libraries(pubsub_forwarder PRIVATE nng) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
= PubSub Forwarder | ||
|
||
This is a trivial example of a forwarder/proxy for the pub/sub pattern. | ||
|
||
The concept is as follows: the forwarder will listen for connections on | ||
both a front-end port and a back-end port. The front-end will act as a | ||
subscriber so that publishers can publish to it. The back-end will act | ||
as a publisher so that subscribers can subscribe to it. The front-end | ||
then forwards to the back end. | ||
|
||
== Compiling | ||
|
||
CMake with ninja-build is simplest: | ||
|
||
[source, bash] | ||
---- | ||
cmake -GNinja -B build | ||
cd build | ||
ninja | ||
---- | ||
|
||
Or if you prefer a traditional approach, | ||
the following is an example typical of UNIX and similar systems like | ||
Linux and macOS may appeal: | ||
|
||
[source, bash] | ||
---- | ||
export CPPFLAGS="-I /usr/local/include" | ||
export LDFLAGS="-L /usr/local/lib -lnng" | ||
export CC="cc" | ||
${CC} ${CPPFLAGS} pubsub_forwarder.c -o pubsub_forwarder ${LDFLAGS} | ||
---- | ||
|
||
== Running | ||
|
||
An example setup for running this example would involve the following: | ||
|
||
. Step 1: Run this example binary (in the background or a terminal, etc) | ||
. Step 2: In a new terminal, run the following | ||
|
||
[source, bash] | ||
---- | ||
nngcat --sub --dial "tcp://localhost:3328" --quoted | ||
---- | ||
|
||
. Step 3: In a second terminal, run the same command again to give us two subscribers | ||
|
||
[source, bash] | ||
---- | ||
nngcat --sub --dial "tcp://localhost:3328" --quoted | ||
---- | ||
|
||
|
||
. In a third terminal, run the following to publish a counter | ||
|
||
[source, bash] | ||
---- | ||
for n in $(seq 0 99); do nngcat --pub --dial "tcp://localhost:3327" --data "$n"; done | ||
---- | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// | ||
// This software is supplied under the terms of the MIT License, a | ||
// copy of which should be located in the distribution where this | ||
// file was obtained (LICENSE.txt). A copy of the license may also be | ||
// found online at https://opensource.org/licenses/MIT. | ||
// | ||
|
||
// | ||
// Forwarder example based on https://github.com/C-o-r-E/nng_pubsub_proxy | ||
// | ||
// This example shows how to use raw sockets to set up a forwarder or proxy for | ||
// pub/sub. | ||
// | ||
// An example setup for running this example would involve the following: | ||
// | ||
// - Run this example binary (in the background or a terminal, etc) | ||
// - In a new terminal, run | ||
// `nngcat --sub --dial "tcp://localhost:3328" --quoted` | ||
// - In a second terminal, run | ||
// `nngcat --sub --dial "tcp://localhost:3328" --quoted` | ||
// - In a third terminal, run | ||
// `for n in $(seq 0 99);` | ||
// `do nngcat --pub --dial "tcp://localhost:3327" --data "$n";` | ||
// `done` | ||
// | ||
#include <stdarg.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#include <nng/nng.h> | ||
#include <nng/protocol/pubsub0/pub.h> | ||
#include <nng/protocol/pubsub0/sub.h> | ||
|
||
#define PROXY_FRONT_URL "tcp://localhost:3327" | ||
#define PROXY_BACK_URL "tcp://localhost:3328" | ||
|
||
void | ||
panic_on_error(int should_panic, const char *format, ...) | ||
{ | ||
if (should_panic) { | ||
va_list args; | ||
va_start(args, format); | ||
vprintf(format, args); | ||
va_end(args); | ||
exit(EXIT_FAILURE); | ||
} | ||
} | ||
|
||
int | ||
main() | ||
{ | ||
nng_socket sock_front_end = NNG_SOCKET_INITIALIZER; | ||
nng_socket sock_back_end = NNG_SOCKET_INITIALIZER; | ||
int ret = 0; | ||
|
||
// | ||
// First we need some nng sockets. Not to be confused with network | ||
// sockets | ||
// | ||
ret = nng_sub0_open_raw(&sock_front_end); | ||
panic_on_error(ret, "Failed to open front end socket\n"); | ||
|
||
ret = nng_pub0_open_raw(&sock_back_end); | ||
panic_on_error(ret, "Failed to open back end socket\n"); | ||
|
||
// | ||
// Now we need to set up a listener for each socket so that they have | ||
// addresses | ||
// | ||
|
||
nng_listener front_ls = NNG_LISTENER_INITIALIZER; | ||
nng_listener back_ls = NNG_LISTENER_INITIALIZER; | ||
|
||
ret = nng_listener_create(&front_ls, sock_front_end, PROXY_FRONT_URL); | ||
panic_on_error(ret, "Failed to create front listener\n"); | ||
|
||
ret = nng_listener_create(&back_ls, sock_back_end, PROXY_BACK_URL); | ||
panic_on_error(ret, "Failed to create back listener\n"); | ||
|
||
ret = nng_listener_start(front_ls, 0); | ||
panic_on_error(ret, "Failed to start front listener\n"); | ||
|
||
ret = nng_listener_start(back_ls, 0); | ||
panic_on_error(ret, "Failed to start back listener\n"); | ||
|
||
// | ||
// Finally let nng do the forwarding/proxying | ||
// | ||
|
||
ret = nng_device(sock_front_end, sock_back_end); | ||
panic_on_error( | ||
ret, "nng_device returned %d: %s\n", ret, nng_strerror(ret)); | ||
|
||
printf("done"); | ||
return 0; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent documentation for this addition! Thank you very much!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I tried to follow the other demos as an example.