4cae67e59a75063789572674392d36ca181173ae
[p2p-testing-infrastructure.git] / ControlScripts / schedule_client.sh
1 #!/bin/bash
2 #
3 # 2010 Razvan Deaconescu, razvan.deaconescu@cs.pub.ro
4 #
5 # Schedule clients
6 # The script
7 #  * starts/stops a client
8 #  * manages when client completes
9
10 # use _DEBUG="off" to turn off debug printing
11 _DEBUG="off"
12
13 # Read the global configuration file
14 # Check if the global configuration file exists
15 if [ ! -e globalconfig ]; then
16         echo "Warning: The global config file globalconfig does not exist."
17 else
18         source globalconfig
19 fi
20
21 # Read the node-specific configuration file
22 # Check if the node-specific configuration file exists
23 if [ ! -e ../ClientWorkingFolders/TmpLogs/node_config ]; then
24         echo "Warning: The global config file ../ClientWorkingFolders/TmpLogs/node_config does not exist."
25 else
26         source ../ClientWorkingFolders/TmpLogs/node_config
27 fi
28
29 # Read client mappings configuration file
30 # Check if the client mappings configuration file exists
31 if [ ! -e client_script_mappings ]; then
32         echo "Warning: The client mappings config file client_script_mappings does not exist."
33 else
34         source client_script_mappings
35 fi
36
37 client_pid=0
38
39 declare start_time
40 declare stop_time
41 declare -a suspend_resume
42
43 parse_periods()
44 {
45         local tmp_array=($(echo "$PERIODS" | sed 's/[,()]/ /g'))
46         start_time=${tmp_array[0]}
47         local len=${#tmp_array[@]}
48         DEBUG echo "PERIODS: $PERIODS"
49
50         stop_time=${tmp_array[$(($len-1))]}
51
52         for ((i = 1; i < $len-1; i++)); do
53                 suspend_resume[$(($i - 1))]=${tmp_array[$i]}
54         done
55 }
56
57 suspend_client()
58 {
59         local client_pid=$1
60         DEBUG echo "suspending client ..."
61         kill -STOP ${client_pid}
62 }
63
64 resume_client()
65 {
66         local client_pid=$1
67         DEBUG echo "resuming client ..."
68         kill -CONT ${client_pid}
69 }
70
71 # test whether time in period (t1, t2) is infinte ("-" sign)
72 # if that is the case, sleep indefinitely, waiting for shut down
73 test_infinite()
74 {
75         time="$1"
76
77         if test "$time" = "-"; then
78                 sleep 1000d
79         fi
80 }
81
82 parse_periods
83
84 DEBUG echo "start_time is ${start_time}"
85 DEBUG echo "suspend_resume is ${suspend_resume[@]}"
86 DEBUG echo "stop_time is ${stop_time}"
87
88 # Initial sleep (before the client is started for the first time)
89 to_sleep=${start_time}
90 # Saving sleep time to log file - this script's output is redirected to the client log file
91 echo "SLEEP $to_sleep"
92 sleep ${to_sleep}
93
94 # Starting the client for the first time
95 DEBUG echo "CLIENT_TYPE is ${CLIENT_TYPE}"
96 start_client ${CLIENT_TYPE} ${DL_BW} ${UL_BW} ${NO_CONNECTIONS}
97 background_pid=$!
98 DEBUG echo "background_pid is ${background_pid}"
99 old_time=${start_time}
100
101 for ((i = 0; i < ${#suspend_resume[@]}; i += 2)); do
102         # Sleeping while the client runs. When i wake up i will suspend the client
103         test_infinite ${suspend_resume[$i]}
104
105         to_sleep=$((${suspend_resume[$i]} - ${old_time}))
106         sleep ${to_sleep}
107         client_pid=$(pgrep -P ${background_pid})
108         DEBUG echo "client_pid is ${client_pid}"
109         suspend_client ${client_pid}
110         old_time=${suspend_resume[$i]}
111
112         # Client is suspended. Sleeping until the client should be restarted. When i wake up i will start the client
113         test_infinite ${suspend_resume[$(($i+1))]}
114
115         to_sleep=$((${suspend_resume[$(($i+1))]} - ${old_time}))
116         # Saving sleep time to log file - this script's output is redirected to the client log file
117         echo "SLEEP $to_sleep"
118         sleep ${to_sleep}
119         resume_client ${client_pid}
120         old_time=${suspend_resume[$(($i+1))]}
121 done
122
123 # Sleeping while the client runs. When i wake up i will suspend the client for the last time
124 test_infinite ${stop_time}
125
126 to_sleep=$((${stop_time} - ${old_time}))
127 sleep ${to_sleep}
128 stop_client ${CLIENT_TYPE}