1#!/bin/bash
2# Copyright (C) 2015-2017 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
3# This Source Code Form is subject to the terms of the Mozilla Public
4# License, v. 2.0. If a copy of the MPL was not distributed with this
5# file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7# Purpose: This script is needed to start the services with
8# one command. This is necessary as ctest - which is used to run the
9# tests - isn't able to start multiple binaries for one testcase. Therefore
10# the testcase simply executes this script. This script then runs the services
11# and checks that all exit successfully.
12
13if [ $# -lt 1 ]
14then
15    echo "Please pass a json file to this script."
16    echo "For example: $0 initial_event_test_diff_client_ids_diff_ports_slave.json UDP"
17    echo "To use the same service id but different instances on the node pass SAME_SERVICE_ID as third parameter"
18    echo "To ensure the first client only subscribes to one event pass SUBSCRIBE_ONLY_ONE as third/fourth parameter"
19    exit 1
20fi
21
22PASSED_JSON_FILE=$1
23# Remove processed options from $@
24shift 1
25REMAINING_OPTIONS=$@
26
27FAIL=0
28
29export VSOMEIP_CONFIGURATION=$PASSED_JSON_FILE
30
31# Start the services
32export VSOMEIP_APPLICATION_NAME=initial_event_test_service_four
33./initial_event_test_service 4 $REMAINING_OPTIONS &
34PID_SERVICE_FOUR=$!
35
36export VSOMEIP_APPLICATION_NAME=initial_event_test_service_five
37./initial_event_test_service 5 $REMAINING_OPTIONS &
38PID_SERVICE_FIVE=$!
39
40export VSOMEIP_APPLICATION_NAME=initial_event_test_service_six
41./initial_event_test_service 6 $REMAINING_OPTIONS &
42PID_SERVICE_SIX=$!
43
44sleep 3
45
46unset VSOMEIP_APPLICATION_NAME
47
48# Array for client pids
49CLIENT_PIDS=()
50
51# Start first client which subscribes remotely
52./initial_event_test_client 9000 DONT_EXIT $REMAINING_OPTIONS &
53FIRST_PID=$!
54
55# remove SUBSCRIBE_ONLY_ONCE parameter from $REMAINING_OPTIONS to ensure the
56# following clients subscribe normaly
57REMAINING_OPTIONS=${REMAINING_OPTIONS%SUBSCRIBE_ONLY_ONE}
58REMAINING_OPTIONS=${REMAINING_OPTIONS#SUBSCRIBE_ONLY_ONE}
59
60# Start availability checker in order to wait until the services on the remote
61# were started as well
62./initial_event_test_availability_checker 1234 $REMAINING_OPTIONS &
63PID_AVAILABILITY_CHECKER=$!
64
65# wait until the services on the remote node were started as well
66wait $PID_AVAILABILITY_CHECKER
67sleep 2;
68for client_number in $(seq 9001 9011)
69do
70   ./initial_event_test_client $client_number STRICT_CHECKING $REMAINING_OPTIONS &
71   CLIENT_PIDS+=($!)
72done
73
74# Wait until all clients are finished
75for job in ${CLIENT_PIDS[*]}
76do
77    # Fail gets incremented if a client exits with a non-zero exit code
78    wait $job || FAIL=$(($FAIL+1))
79done
80
81# wait until all clients exited on master side
82./initial_event_test_stop_service SLAVE &
83PID_STOP_SERVICE=$!
84wait $PID_STOP_SERVICE
85
86# shutdown the first client
87kill $FIRST_PID
88wait $FIRST_PID || FAIL=$(($FAIL+1))
89
90# shutdown the services
91kill $PID_SERVICE_SIX
92kill $PID_SERVICE_FIVE
93kill $PID_SERVICE_FOUR
94
95sleep 1
96echo ""
97
98# Check if both exited successfully
99if [ $FAIL -eq 0 ]
100then
101    exit 0
102else
103    exit 1
104fi
105