functions.sh 8.36 KB
Newer Older
Juon Kawakami's avatar
init  
Juon Kawakami committed
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
DIR=`pwd`
BASEDIR="`cd .. && pwd`"
PIDS=

# Wait for a regular expression to appear in a file.
# $1 is the log to check
# $2 is the regex to wait for
# $3 is the optional output frequency. Messages will be output every n sleeps. Default 1.
# $4 is the optional sleep time. Defaults to 1 second.
function waitFor {
  SLEEP_TIME=1
  FREQUENCY=1
  if [ ! -z "$3" ]; then
    FREQUENCY=$3
  fi
  if [ ! -z "$4" ]; then
    SLEEP_TIME=$4
  fi
  F=$FREQUENCY
  echo "Waiting for '$1' to exist..."
  while [[ ! -e $1 ]]; do
    if (( --F == 0 )); then
      echo "Still waiting for '$1' to exist..."
      F=$FREQUENCY
    fi
    sleep $SLEEP_TIME
  done
  echo "Waiting for '$2'..."
  while [ -z "`grep \"$2\" \"$1\"`" ]; do
    if (( --F == 0 )); then
      echo "Still waiting for '$2'..."
      F=$FREQUENCY
    fi
    sleep $SLEEP_TIME
  done
}

# Make a classpath argument by looking in a directory of jar files.
# Positional parameters are the directories to look in
function makeClasspath {
  RESULT="../."
  while [[ ! -z "$1" ]]; do
    for NEXT in $1/*.jar; do
      RESULT="$RESULT:$NEXT"
    done
    shift
  done
  CP=${RESULT#:}
}

# Print the usage statement
function printUsage {
  echo "Usage: $0 [scenario] [options]"
  echo
  echo "[scenario]  Scenario directory including the map and config directories. Default: \"../maps/test\""
  echo
  echo "[options]"
  echo "-m    --map       <mapdir>    Set the map directory. Default: \"../maps/map\""
  echo "-c    --config    <configdir> Set the config directory. Default: \"../maps/test/config\""
  echo "-t    --team      <teamname>  Set the team name. Default: \"\""
  echo "-l    --log       <logdir>    Set the log directory. Default: \"logs/log\""
  echo "-s    --timestamp             Create a log sub-directory including timestamp, team name and map name"
  echo "-g    --nogui                 Disable GUI"
  echo "-j    --jlog                  Enable Jlog Recorder (startViewerEventLogger)"
  echo "-r    --jlog-dir <jlog_dir>   Set Jlog Recorder log dir. Default: \"logs/jlog\""
  echo "[+|-]x                        Enable/Disable XTerm use. Default: \"Disable\""
}

# Process command-line arguments
function processArgs {
  LOGDIR="../logs/log"
  RECORDSDIR="../logs/jlog"
  MAP="$BASEDIR/maps/test/map"
  CONFIGDIR="$BASEDIR/maps/test/config"
  TEAM=""
  TIMESTAMP_LOGS=""
  NOGUI="no"
  JLOG_RECORD="no"
  XTERM="no"

  while [[ ! -z "$1" ]]; do
    case "$1" in
      -m | --map)
        MAP="$2"
        shift 2
        ;;
      -c | --config)
        CONFIGDIR="$2"
        shift 2
        ;;
      -l | --log)
        LOGDIR="$2"
        shift 2
        ;;
      -j | --jlog)
        JLOG_RECORD="yes"
        shift
        ;;
      -r | --jlog-dir)
        RECORDSDIR="$2"
        shift 2
        ;;
      -t | --team)
        TEAM="$2"
        shift 2
        ;;
      -s | --timestamp)
        TIMESTAMP_LOGS="yes"
        shift
        ;;
      -g | --nogui)
        NOGUI="yes"
        shift
        ;;
      -x)
        XTERM="no"
        shift
        ;;
      +x)
        XTERM="yes"
        shift
        ;;
      -h | --help)
        printUsage
        exit 1
        ;;
      *)
        echo "Unrecognized option: $1"
        printUsage
        exit 1
        ;;
    esac
  done

  # Check if the Map directory exists
  if [ -z $MAP ] || [ ! -d $MAP ]; then
    echo "Directory does not exist or does not have the \"map\" and \"config\" sub-directories"
    exit 1
  fi

  # Append timestamp, team and map name to the log directory
  if [ ! -z "$TIMESTAMP_LOGS" ]; then
    TIME="`date +%Y%m%d-%H%M%S`"

    # Extract map name
    MAPNAME="`basename $MAP`"
    if [ "$MAPNAME" == "map" ]; then
      MAPNAME="$(basename $(dirname $MAP))"
    fi

    if [ -z "$TEAM" ]; then
      LOGDIR="$LOGDIR/$TIME-$MAPNAME"
    else
      LOGDIR="$LOGDIR/$TIME-$TEAM-$MAPNAME"
    fi
  fi

  if [ "$(uname -s)" = 'Linux' ]; then
    LOGDIR=`readlink -f $LOGDIR`
  fi
  mkdir -p $LOGDIR
  mkdir -p $RECORDSDIR
}

function execute {
  title=$1
  command=$2
  if [[ $XTERM == "yes" ]]; then
    xterm -T $title -e "$command 2>&1 | tee $LOGDIR/$title-out.log" &
  else
    sh -c "$command 2>&1 | tee $LOGDIR/$title-out.log" &
  fi
  PIDS="$PIDS $!"
}

# Start the kernel
function startKernel {
  GUI_OPTION=""
  if [[ $NOGUI == "yes" ]]; then
    GUI_OPTION="--nogui"
  fi

  KERNEL_OPTIONS="-c $CONFIGDIR/kernel.cfg --gis.map.dir=$MAP --kernel.logname=$LOGDIR/rescue.log.7z $GUI_OPTION $*"
  makeClasspath $BASEDIR/jars $BASEDIR/lib

  execute kernel "java -Xmx2048m -cp $CP -Dlog4j.log.dir=$LOGDIR kernel.StartKernel $KERNEL_OPTIONS"
  # Wait for the kernel to start
  waitFor $LOGDIR/kernel.log "Listening for connections"
}

# Start the simulators
function startSims {
  GUI_OPTION=""
  if [[ $NOGUI == "yes" ]]; then
    GUI_OPTION="--nogui"
  fi

  makeClasspath $BASEDIR/lib

  # Execute the simulators
  execute misc "java -Xmx512m -cp $CP:$BASEDIR/jars/rescuecore2.jar:$BASEDIR/jars/standard.jar:$BASEDIR/jars/misc.jar -Dlog4j.log.dir=$LOGDIR rescuecore2.LaunchComponents misc.MiscSimulator -c $CONFIGDIR/misc.cfg $GUI_OPTION $*"
  echo "waiting for misc to connect..."
  waitFor $LOGDIR/misc-out.log "success"

  execute traffic "java -Xmx1024m -cp $CP:$BASEDIR/jars/rescuecore2.jar:$BASEDIR/jars/standard.jar:$BASEDIR/jars/traffic3.jar -Dlog4j.log.dir=$LOGDIR rescuecore2.LaunchComponents traffic3.simulator.TrafficSimulator -c $CONFIGDIR/traffic3.cfg $GUI_OPTION $*"
  echo "waiting for traffic to connect..."
  waitFor $LOGDIR/traffic-out.log "success"

  # execute fire "java -Xmx1024m -cp $CP:$BASEDIR/jars/rescuecore2.jar:$BASEDIR/jars/standard.jar:$BASEDIR/jars/resq-fire.jar -Dlog4j.log.dir=$LOGDIR rescuecore2.LaunchComponents firesimulator.FireSimulatorWrapper -c $CONFIGDIR/resq-fire.cfg $GUI_OPTION $*"
  # echo "waiting for fire to connect..."
  # waitFor $LOGDIR/fire-out.log "success"

  # execute ignition "java -Xmx512m -cp $CP:$BASEDIR/jars/rescuecore2.jar:$BASEDIR/jars/standard.jar:$BASEDIR/jars/ignition.jar -Dlog4j.log.dir=$LOGDIR rescuecore2.LaunchComponents ignition.IgnitionSimulator -c $CONFIGDIR/ignition.cfg $GUI_OPTION $*"
  # echo "waiting for ignition to connect..."
  # waitFor $LOGDIR/ignition-out.log "success"

  execute collapse "java -Xmx512m -cp $CP:$BASEDIR/jars/rescuecore2.jar:$BASEDIR/jars/standard.jar:$BASEDIR/jars/collapse.jar -Dlog4j.log.dir=$LOGDIR rescuecore2.LaunchComponents collapse.CollapseSimulator -c $CONFIGDIR/collapse.cfg $GUI_OPTION $*"
  echo "waiting for collapse to connect..."
  waitFor $LOGDIR/collapse-out.log "success"

  execute clear "java -Xmx512m -cp $CP:$BASEDIR/jars/rescuecore2.jar:$BASEDIR/jars/standard.jar:$BASEDIR/jars/clear.jar -Dlog4j.log.dir=$LOGDIR rescuecore2.LaunchComponents clear.ClearSimulator -c $CONFIGDIR/clear.cfg $GUI_OPTION $*"
  echo "waiting for clear to connect..."
  waitFor $LOGDIR/clear-out.log "success"

  execute civilian "java -Xmx1512m -cp $CP:$BASEDIR/jars/rescuecore2.jar:$BASEDIR/jars/standard.jar:$BASEDIR/jars/sample.jar:$BASEDIR/jars/kernel.jar -Dlog4j.log.dir=$LOGDIR rescuecore2.LaunchComponents sample.SampleCivilian*n -c $CONFIGDIR/civilian.cfg $*"
}

# Start the viewer
function startViewer {
  if [[ $NOGUI == "yes" ]]; then
    return 0
  fi

  makeClasspath $BASEDIR/lib

  TEAM_NAME_ARG=""
  if [ ! -z "$TEAM" ]; then
    TEAM_NAME_ARG="\"--viewer.team-name=$TEAM\""
  fi

  # Execute the viewer
  execute viewer "java -Xmx512m -cp $CP:$BASEDIR/jars/rescuecore2.jar:$BASEDIR/jars/standard.jar:$BASEDIR/jars/sample.jar -Dlog4j.log.dir=$LOGDIR rescuecore2.LaunchComponents sample.SampleViewer -c $CONFIGDIR/viewer.cfg $TEAM_NAME_ARG $*"
  echo "waiting for viewer to connect..."
  waitFor $LOGDIR/viewer-out.log "success"
}


# Start the viewer event logger
function startViewerEventLogger {
  if [[ $JLOG_RECORD == "no" ]]; then
    return 0
  fi

  makeClasspath $BASEDIR/lib

  TEAM_NAME_ARG=""
  if [ ! -z "$TEAM" ]; then
    TEAM_NAME_ARG="\"--viewer.team-name=$TEAM\""
  fi

  # Execute the viewer
  execute viewer "java -Xmx512m -cp $CP:$BASEDIR/jars/rescuecore2.jar:$BASEDIR/jars/standard.jar:$BASEDIR/jars/sample.jar -Dlog4j.log.dir=$LOGDIR rescuecore2.LaunchComponents sample.SampleViewerEventLogger -c $CONFIGDIR/viewer.cfg --records.dir=$RECORDSDIR $TEAM_NAME_ARG $*"
}

function abspath {
  if [[ -d "$1" ]]
  then
    pushd "$1" >/dev/null
    pwd
    popd >/dev/null
  elif [[ -e $1 ]]
  then
    pushd "$(dirname "$1")" >/dev/null
    echo "$(pwd)/$(basename "$1")"
    popd >/dev/null
  else
    echo "$1" does not exist! >&2
    return 127
  fi
}