aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/fil/libre/repwifiapp/helpers/ShellCommand.java
blob: d6b7553c9eeee0884786d58bd74599a185757069 (plain)
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
//
// Copyright 2017, 2018 Filippo "Fil" Bergamo <fil.bergamo@riseup.net>
// 
// This file is part of RepWifiApp.
//
// RepWifiApp is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// RepWifiApp is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with RepWifiApp.  If not, see <http://www.gnu.org/licenses/>.
// 
// ********************************************************************

package fil.libre.repwifiapp.helpers;

import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeoutException;

public class ShellCommand {

    public static final int EXITCODE_INVALID_INPUT = -9;
    public static final int EXITCODE_PARSING_ERROR = 999;
    public static final int MIN_TIMEOUT_MILLIS = 100;

    protected String _cmdOut = "";
    protected String _cmdTxt = "";

    public ShellCommand(String commandText) {
        this._cmdTxt = commandText;
    }

    public int execute() throws Exception {

        if (this._cmdTxt == null) {
            return EXITCODE_INVALID_INPUT;
        }

        Logger.logDebug("EXEC: " + this._cmdTxt);

        Process cmd = Runtime.getRuntime().exec(this._cmdTxt);
        

        InputStream os = cmd.getInputStream();
        InputStream es = cmd.getErrorStream();

        StringBuilder sb = new StringBuilder();

        sb.append(getStringFromStream(es));
        sb.append(getStringFromStream(os));

        int res = cmd.waitFor();

        // re-read the output, in case it was empty when first tried
        sb.append(getStringFromStream(es));
        sb.append(getStringFromStream(os));

        this._cmdOut = sb.toString();

        Logger.logDebug("EXITCODE: " + res);
        Logger.logDebug("OUT: " + getOutput());

        return res;

    }

    protected String getStringFromStream(InputStream s) throws IOException {
        java.util.Scanner sc = new java.util.Scanner(s,"UTF-8").useDelimiter("\\A");
        return sc.hasNext() ? sc.next() : "";
    }

    public String getOutput() {

        return this._cmdOut;

    }
    
    protected class ProcessTimeout{
        
        private Process _proc;
        private int _exitCode;
        private boolean _hasExited = false;
        private InterruptedException e = null;
        
        public int waitFor(Process p, long timeoutMillis) throws InterruptedException, TimeoutException{
            
            // waits for a maximum of timeoutMillis milliseconds
            // for the process to exit.        
            int msWaited = 0;
            this._proc = p;
            startWaitingForProcess();
            while (msWaited < timeoutMillis) {

                if (e != null){
                    // exception from the underlying thread;
                    throw e;
                }
                
                if (! _hasExited){
                    Thread.sleep(MIN_TIMEOUT_MILLIS);
                    msWaited += MIN_TIMEOUT_MILLIS;
                    
                } else {
                    return _exitCode;
                }

            }
            
            throw new TimeoutException("Timeout elapsed while waiting for inner process to finish its execution.");
            
        }
        
        private void startWaitingForProcess(){

            new Thread(new Runnable() {
                public void run(){
                    try {
                        int exitCode = _proc.waitFor();
                        setExitCode(exitCode);
                    } catch (InterruptedException e) {
                        setException(e);
                    }
                }
            }).start();           
            
        }
        
        private void setExitCode(int ec){
            _hasExited = true;
            _exitCode = ec;
        }
        
        private void setException(InterruptedException e){
            this.e = e;
        }
        
    }
    
}