Add FIFO pre-create to the replay startup script
[ric-app/mc.git] / sidecars / listener / run_replay.sh
1 #!/usr/bin/env bash
2 # vim: ts=4 sw=4 noet:
3 #----------------------------------------------------------------------------------
4 #
5 #       Copyright (c) 2018-2019 AT&T Intellectual Property.
6 #
7 #   Licensed under the Apache License, Version 2.0 (the "License");
8 #   you may not use this file except in compliance with the License.
9 #   You may obtain a copy of the License at
10 #
11 #          http://www.apache.org/licenses/LICENSE-2.0
12 #
13 #   Unless required by applicable law or agreed to in writing, software
14 #   distributed under the License is distributed on an "AS IS" BASIS,
15 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 #   See the License for the specific language governing permissions and
17 #   limitations under the License.
18 #
19 #---------------------------------------------------------------------------------
20
21
22 # ----------------------------------------------------------------------
23 # Mnemonic:     run_replay.sh
24 # Abstract: Simple script to make starting the replay binary easier.
25 #                       Defaults:
26 #                               /var/lib/mc/listener  -- directory for fifos
27 #
28 #                       The input file can be supplied with -f and if omitted then
29 #                       standard intput is assumed. This should allow the following
30 #                       when run in a docker container:
31 #                               docker run --rm -i -v /tmp/replay_fifos:/var/lib/mc/listener run_replay.sh <data-file
32 #
33 # Date:         20 November 2019
34 # Author:       E. Scott Daniels
35 # ----------------------------------------------------------------------
36
37
38 fifo_dir=/var/lib/mc/listener
39 data=""                                                         # stdin by default
40 pre_open=0
41 mtype_list=""
42 gate=""
43 delay=0
44
45 while [[ $1 == -* ]]
46 do
47         case $1 in 
48                 -f) data="$2"; shift;;
49                 -d)     fifo_dir=$2; shift;;
50                 -D) delay=$2; shift;;
51                 -g) gate="$2"; shift;;
52                 -m)     mtype_list="$2"; shift;;
53                 -p) pre_open=1;;
54
55                 *)      echo "$1 is not a recognised option"
56                         echo "usage: $0 [-d fifo-dir] [-D seconds] [-f data-file] [-g gate-file] [-m mtype-list] [-p]"
57                         echo "   -p causes FIFOs to be pre-allocated"
58                         echo "   -m supplies a comma separated list of message types to preopen FIFOs for"
59                         echo "      if -p is given and -m is omitted, the input file is examined to determine message types"
60                         echo "   -D seconds will cause a delay of the specified number of seconds before starting the replay"
61                         echo "   -g file  will cause the script to wait for file to appear before starting the replay"
62                         echo "      if both -D and -g are used, the delay happens after the gate file is found"
63                         exit 1
64                         ;;
65         esac
66
67         shift
68 done
69
70 if (( pre_open )) 
71 then
72         if [[ -z $mtype_list ]]
73         then
74                 if [[ -z $data ]]
75                 then
76                         echo "error: cannot determine a mtype list from stdin:"
77                         echo "  -p given with out a list (-m) and input file set to default to stdin (missing -f)"
78                         exit 1
79                 fi
80
81                 rdc_extract $data 0 | sort -u | while read t
82                 do
83                         mtype_list="$mtype_list$t "
84                 done
85         fi
86
87         (
88                 cd $fifo_dir
89                 count=0
90                 for t in ${mtype_list//,/ }
91                 do
92                         name=$( printf "MT_%09d" t )
93                         echo "making FIFO: $name"
94                         mkfifo -m 664 $name 2>/dev/null         # if these are there, don't natter on about them
95                         (( count++ ))
96                 done
97
98                 ls MT_* | wc -l | read found
99                 if (( count != found ))
100                 then
101                         echo "warn:  after pre-create, expected $count FIFOs, but found only $found"
102                 fi
103         )
104 fi
105
106 if [[ -n $data ]]
107 then
108         if [[ ! -r $data ]]
109         then
110                 echo "abort: cannot find data file: $data"
111                 exit 1
112         fi
113
114         data="-f $data"
115 fi
116
117 if [[ -n $gate ]]
118 then
119         echo "waiting for gate file to appear: $gate"
120         while true
121         do
122                 if [[ -e $gate ]]
123                 then
124                         break
125                 fi
126                 sleep 1
127         done
128 fi
129
130 if (( delay ))
131 then
132         echo "pausing $delay seconds before starting rdc_display..."
133         sleep $delay
134 fi
135
136 echo "starting rdc_replay"
137 rdc_replay -d $fifo_dir $data
138