blob: a7c4e8e49f0b9618d4cfce6ba282ea7b43e70000 [file] [log] [blame]
Yohann Roussel1b697cb2015-04-15 15:01:33 +02001#!/bin/bash
2#
3# Copyright (C) 2015 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
Benoit Lamarche43a5b492015-07-21 10:53:25 +020017# Version: 1.1-mr2
Yohann Roussel1b697cb2015-04-15 15:01:33 +020018#
19set -o nounset
20umask 077
21
22#
23# Settings
24#
25LOCAL_SETTING="$HOME/.jack"
26TMPDIR=${TMPDIR:=/tmp}
27SERVER_DIR=$TMPDIR/jack-$USER
28
29#
30# Load local settings
31#
32source "$LOCAL_SETTING" 2>/dev/null
33
34#
35# Create or update local settings if needed
36#
37if [[ ! -f "$LOCAL_SETTING" || $SETTING_VERSION -lt 2 ]]; then
38 echo "Writing local settings in" $LOCAL_SETTING
39 cat >"$LOCAL_SETTING.$$" <<-EOT
40 # Server settings
41 SERVER=${SERVER:=true}
42 SERVER_PORT_SERVICE=${SERVER_PORT_SERVICE:=8072}
43 SERVER_PORT_ADMIN=${SERVER_PORT_ADMIN:=8073}
44 SERVER_COUNT=${SERVER_COUNT:=1}
45 SERVER_NB_COMPILE=${SERVER_NB_COMPILE:=4}
46 SERVER_TIMEOUT=${SERVER_TIMEOUT:=60}
47 SERVER_LOG=\${SERVER_LOG:=\$SERVER_DIR/jack-\$SERVER_PORT_SERVICE.log}
48 JACK_VM_COMMAND=\${JACK_VM_COMMAND:=java}
49 # Internal, do not touch
50 SETTING_VERSION=2
51EOT
52 ln -f "$LOCAL_SETTING.$$" "$LOCAL_SETTING"
53 rm "$LOCAL_SETTING.$$"
54 source "$LOCAL_SETTING"
55fi
56
57#
58# Static setting
59#
60mkdir "$SERVER_DIR" 2>/dev/null
61
62#
63# Decoding argument
64#
65
66if [ $# -ne 1 ]
67then
68 echo "Usage : $0 [ start-server | stop-server | kill-server | list-server | server-log | server-stat ]"
69 exit 255
70fi
71
72case $1 in
73 stop-server)
74 echo "Stopping background server"
75 CMD="stop" ;;
76 server-stat)
77 echo "Getting statistic from background server"
78 CMD="stat" ;;
79 server-log)
80 echo "Log location of background server:"
81 echo $SERVER_LOG
82 exit 0 ;;
83 kill-server)
84 echo "Killing background server"
Yohann Roussel9ebf1c82015-06-01 09:12:40 +020085 kill $(ps aux | grep JackSimpleServer | grep -v grep | awk '{print $2}') 2>/dev/null
86 if [ $? -ne 0 ]; then
87 echo "ERROR: No Jack server to kill" >&2
88 exit 2
89 else
90 exit 0
91 fi ;;
Yohann Roussel1b697cb2015-04-15 15:01:33 +020092 list-server)
93 ps aux | grep JackSimpleServer | grep -v grep
94 exit $? ;;
95 start-server)
96 SERVER_PRG="$JACK_VM_COMMAND -cp $JACK_JAR com.android.jack.server.JackSimpleServer"
Yohann Roussel9ebf1c82015-06-01 09:12:40 +020097 echo "Launching Jack server" $SERVER_PRG
Yohann Roussel1b697cb2015-04-15 15:01:33 +020098 $SERVER_PRG $SERVER_PORT_SERVICE $SERVER_PORT_ADMIN $SERVER_COUNT $SERVER_NB_COMPILE $SERVER_TIMEOUT >>$SERVER_LOG 2>&1 &
99 exit 0 ;;
100 *)
101 echo "Usage : $0 [ start-server | stop-server | kill-server | list-server | server-log | server-stat ]"
102 exit 255 ;;
103esac
104
105#
106# Send command
107#
108
109set +o errexit
110
111# Try to cleanup if interrupted
112# Cleanup
113JACK_EXIT="$SERVER_DIR/admin-$$"
114trap 'rm -f "$JACK_EXIT" 2>/dev/null;' EXIT
115
116RETRY_SESSION=3
117DELAY_CONNECT=3
118
119
120# Send command to Jack server
121DATE_CONNECT=$(date +%s)
122while true; do
123 HTTP_CODE=$(curl --fail --silent --output $JACK_EXIT --write-out %{http_code} --connect-timeout 5 --no-proxy 127.0.0.1:$SERVER_PORT_ADMIN http://127.0.0.1:$SERVER_PORT_ADMIN/$CMD)
124 CURL_CODE=$?
125 if [ $CURL_CODE -eq 0 ]; then
126 # No problem, let's go
127 break;
128 elif [ $CURL_CODE -eq 7 ]; then
129 # Failed to connect
130 if [ $(date +%s) -ge $DATE_CONNECT ]; then
Yohann Roussel9ebf1c82015-06-01 09:12:40 +0200131 echo "ERROR: No Jack server running"
Yohann Roussel1b697cb2015-04-15 15:01:33 +0200132 exit 255
133 else
134 sleep 0.2 2>/dev/null
135 fi
136 # Trying with a new connection, let's retry session 3 times max
137 RETRY_SESSION=3
138 elif [ $CURL_CODE -eq 22 ]; then
139 # Http code not OK, let's decode and abort
140 if [ $HTTP_CODE -eq 401 ]; then
141 # 401: Unauthorized
Yohann Roussel9ebf1c82015-06-01 09:12:40 +0200142 echo "ERROR: Security problem, see Jack server log" >&2
Yohann Roussel1b697cb2015-04-15 15:01:33 +0200143 exit 255
144 elif [ $HTTP_CODE -eq 400 ]; then
145 # 400: Bad request
Yohann Roussel9ebf1c82015-06-01 09:12:40 +0200146 echo "ERROR: Bad request, see Jack server log" >&2
Yohann Roussel1b697cb2015-04-15 15:01:33 +0200147 exit 255
148 else
149 # Other
Yohann Roussel9ebf1c82015-06-01 09:12:40 +0200150 echo "ERROR: Internal unknown error ($HTTP_CODE), try other ports in ~/.jack, or see Jack server log" >&2
Yohann Roussel1b697cb2015-04-15 15:01:33 +0200151 exit 255
152 fi
153 else
154 # In case of partial, timeout, empty respond, network error, let's retry
155 if [ $RETRY_SESSION -eq 0 ]; then
Yohann Roussel9ebf1c82015-06-01 09:12:40 +0200156 echo "ERROR: Communication error with Jack server ($CURL_CODE)" >&2
Yohann Roussel1b697cb2015-04-15 15:01:33 +0200157 exit 255
158 else
159 let RETRY_SESSION=RETRY_SESSION-1
160 fi
161 fi
162done
163
164# Exit
165if [ -r $JACK_EXIT ]; then
166 cat <$JACK_EXIT 2>/dev/null
167fi
168
169exit 0