aboutsummaryrefslogtreecommitdiff
path: root/demo/async/arun.sh
blob: ca6c0cf587ba16bbad423f323f8d0b1f1361d742 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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