Skip to content
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

Master drbitboy async demo client part 1 #1800

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions demo/async/arun.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env bash
########################################################################
### Start asynchronous clients that can
### wait for asynchronous server to start
########################################################################

### Usage: ./arun.sh [client#]
### - client# => do not start server until just before this client

### Socket file; client count
ADDR=ipc:///tmp/async_demo
COUNT=10

### Client # to match when server should be started; clear server PID
SERVER_ORDINAL=$1
unset SERVER_PID

### Create CLIENT_PID as an array
typeset -a CLIENT_PID
i=0
while (( i < COUNT ))
do
i=$(( i + 1 ))

### Start server before the matching client
if [ "$SERVER_ORDINAL" == "$i" ] ; then
./server $ADDR &
SERVER_PID=$!
echo Started server before client $i
trap "kill $SERVER_PID" 0
fi

### Start start client with NONBLOCK envvar set
### so client will wait for socket to be open on nng_dial
rnd=$(( RANDOM % 1000 + 500 ))
echo "Starting client $i: server will reply after $rnd msec"
NONBLOCK= ./client $ADDR $rnd &
### Add this client's PID to client PID array
eval CLIENT_PID[$i]=$!
done

### Start server if not yet started
[ "$SERVER_PID" ] || \
{
./server $ADDR &
SERVER_PID=$!
echo Starting server after last client - SERVER_PID=$SERVER_PID
trap "kill $SERVER_PID" 0
}

### Wait for clients to complete
i=0
while (( i < COUNT ))
do
i=$(( i + 1 ))
wait ${CLIENT_PID[$i]}
done
### Kill server
kill $SERVER_PID
3 changes: 2 additions & 1 deletion demo/async/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ client(const char *url, const char *msecstr)
fatal("nng_req0_open", rv);
}

if ((rv = nng_dial(sock, url, NULL, 0)) != 0) {
if ((rv = nng_dial(sock, url, NULL,
getenv("NONBLOCK") ? NNG_FLAG_NONBLOCK : 0)) != 0) {
fatal("nng_dial", rv);
}

Expand Down
Loading