|
| 1 | +package com.scrappers.dbtraining.androidTerminal.command; |
| 2 | + |
| 3 | +import android.view.View; |
| 4 | + |
| 5 | +import com.scrappers.dbtraining.androidTerminal.terminal.AndroidTerminal; |
| 6 | +import com.scrappers.dbtraining.androidTerminal.terminal.CommandLineExecutor; |
| 7 | +import com.scrappers.dbtraining.androidTerminal.terminal.Terminal; |
| 8 | + |
| 9 | +import java.io.File; |
| 10 | +import java.io.IOException; |
| 11 | +import java.io.InputStream; |
| 12 | +import java.util.concurrent.Callable; |
| 13 | +import java.util.concurrent.ExecutionException; |
| 14 | +import java.util.concurrent.Executors; |
| 15 | +import java.util.logging.Level; |
| 16 | +import java.util.logging.Logger; |
| 17 | + |
| 18 | +public class CommandSet implements Command, WritableData{ |
| 19 | + |
| 20 | + private String[] commands; |
| 21 | + private String id; |
| 22 | + private Process process; |
| 23 | + private static final Object mutex0 = new Object(); |
| 24 | + private Terminal.Permission permission; |
| 25 | + private boolean firstCommandSet = false; |
| 26 | + private static final Logger logger = Logger.getLogger(CommandSet.class.getName()); |
| 27 | + private BufferSize bufferSize = BufferSize.ONE_KILOS; |
| 28 | + |
| 29 | + public CommandSet(final CommandLineExecutor commandLineExecutor, final String setId, final BufferSize bufferSize){ |
| 30 | + //use the permissions provided by the android terminal class as the default permission. |
| 31 | + this.permission = commandLineExecutor.getPermission(); |
| 32 | + this.id = setId; |
| 33 | + this.bufferSize = bufferSize; |
| 34 | + synchronized (mutex0) { |
| 35 | + mutex0.notify(); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public void setFirstCommandSet(boolean firstCommandSet) { |
| 40 | + this.firstCommandSet = firstCommandSet; |
| 41 | + } |
| 42 | + |
| 43 | + public boolean isFirstCommandSet() { |
| 44 | + return firstCommandSet; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void asyncExecution(Runtime jvmRuntime, Terminal.Permission permission) throws InterruptedException { |
| 49 | + //set the new permission specifically for this command set. |
| 50 | + permission = this.permission; |
| 51 | + //calling the synchronized on hte mutex obj would make the thread own this object monitor. |
| 52 | + synchronized (mutex0) { |
| 53 | + try { |
| 54 | + mutex0.wait(); |
| 55 | + logger.log(Level.FINE, "Started executing command set with id : " + CommandSet.this.getId()); |
| 56 | + //expand to accommodate --the 2 vital commands-- |
| 57 | + final String[] finalCommands = new String[commands.length + 2]; |
| 58 | + //copy values to the final commands args |
| 59 | + finalCommands[0] = permission.getPermission(); |
| 60 | + finalCommands[1] = "-c"; |
| 61 | + for (int i = 0; i < commands.length; i++) { |
| 62 | + //copy commands to the final commands args |
| 63 | + finalCommands[i + 2] = commands[i]; |
| 64 | + } |
| 65 | + //assign the process to be used by input streams. |
| 66 | + CommandSet.this.process = jvmRuntime.exec(finalCommands); |
| 67 | + } catch (IOException e) { |
| 68 | + e.printStackTrace(); |
| 69 | + } |
| 70 | + //use this object as a mutex |
| 71 | + //calling the synchronized on hte mutex obj would make the thread own this object monitor. |
| 72 | + mutex0.notifyAll(); |
| 73 | + logger.log(Level.FINE, "Finished executing command set with id : " + CommandSet.this.getId()); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public void setProcess(Process process) { |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void setCommands(String[] commands) { |
| 84 | + this.commands = commands; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void getProcess() { |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public Terminal.Permission getPermission() { |
| 94 | + return permission; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public String asyncStringRead() { |
| 99 | + try { |
| 100 | + return Executors.newSingleThreadExecutor().submit(new AsyncStringReader()).get(); |
| 101 | + } catch (ExecutionException | InterruptedException e) { |
| 102 | + throw new IllegalStateException("Cannot read from Streams. " + e.getMessage()); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public byte[] asyncBufferRead() { |
| 108 | + try { |
| 109 | + return (byte[]) Executors.newSingleThreadExecutor().submit(new AsyncByteReader()).get(); |
| 110 | + } catch (ExecutionException | InterruptedException e) { |
| 111 | + throw new IllegalStateException("Cannot read from Streams. " + e.getMessage()); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public void asyncStringWrite(String values) { |
| 117 | + |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public void asyncBufferWrite(byte[] buffer) { |
| 122 | + |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public void setPermission(Terminal.Permission permission) { |
| 127 | + this.permission = permission; |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public void setId(String id) { |
| 132 | + this.id = id; |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public String getId() { |
| 137 | + return id; |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public void setIndex(int index) { |
| 142 | + |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public int getIndex() { |
| 147 | + return 0; |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public void writeToFile(File file) { |
| 152 | + |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public void writeToConsole(Logger logger) { |
| 157 | + |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public void printToWidget(View view) { |
| 162 | + |
| 163 | + } |
| 164 | + |
| 165 | + private class AsyncStringReader extends Reader<String> { |
| 166 | + @Override |
| 167 | + public String call() throws Exception { |
| 168 | + logger.log(Level.FINE, "Started reading command set output with id : "+CommandSet.this.getId()); |
| 169 | + //use this object as a mutex |
| 170 | + waitForCommandExecution(); |
| 171 | + final Process process = CommandSet.this.process; |
| 172 | + final InputStream inputStream = process.getInputStream(); |
| 173 | + //use at least 2kbs buffer memory |
| 174 | + byte[] buffer = new byte[Math.max(bufferSize.getMemory(), 2048)]; |
| 175 | + for (int i = 0; i < buffer.length; i++) { |
| 176 | + buffer[i] = (byte) inputStream.read(); |
| 177 | + } |
| 178 | + buffer = clampBufferLen(buffer); |
| 179 | + releaseLocks(); |
| 180 | + logger.log(Level.FINE, "Finished reading command set output with id : "+CommandSet.this.getId()); |
| 181 | + return new String(buffer); |
| 182 | + } |
| 183 | + } |
| 184 | + private class AsyncByteReader extends Reader<byte[]> { |
| 185 | + @Override |
| 186 | + public byte[] call() throws Exception { |
| 187 | + logger.log(Level.FINE, "Started reading command set output with id : "+CommandSet.this.getId()); |
| 188 | + //use this object as a mutex |
| 189 | + waitForCommandExecution(); |
| 190 | + final Process process = CommandSet.this.process; |
| 191 | + final InputStream inputStream = process.getInputStream(); |
| 192 | + byte[] buffer = new byte[2048]; |
| 193 | + for (int i = 0; i < buffer.length; i++) { |
| 194 | + buffer[i] = (byte) inputStream.read(); |
| 195 | + } |
| 196 | + buffer = clampBufferLen(buffer); |
| 197 | + releaseLocks(); |
| 198 | + logger.log(Level.FINE, "Finished reading command set output with id : "+CommandSet.this.getId()); |
| 199 | + return buffer; |
| 200 | + } |
| 201 | + } |
| 202 | + protected class AsyncWriter extends Thread{ |
| 203 | + @Override |
| 204 | + public void run() { |
| 205 | + //use this object as a mutex |
| 206 | + waitForCommandExecution(); |
| 207 | + } |
| 208 | + private void waitForCommandExecution(){ |
| 209 | + try { |
| 210 | + synchronized (mutex0) { |
| 211 | + mutex0.wait(); |
| 212 | + } |
| 213 | + } catch (InterruptedException e) { |
| 214 | + e.printStackTrace(); |
| 215 | + } |
| 216 | + } |
| 217 | + } |
| 218 | + private abstract static class Reader<T> implements Callable<T>{ |
| 219 | + protected void waitForCommandExecution() throws InterruptedException{ |
| 220 | + synchronized (mutex0) { |
| 221 | + mutex0.wait(); |
| 222 | + } |
| 223 | + } |
| 224 | + protected void releaseLocks(){ |
| 225 | + synchronized (mutex0) { |
| 226 | + mutex0.notifyAll(); |
| 227 | + } |
| 228 | + } |
| 229 | + /** |
| 230 | + * Clamps the buffer length to the only viable readable alphabetic based bytes. |
| 231 | + * @param buffer the byte buffer to clamp. |
| 232 | + * @return a new clamped buffer. |
| 233 | + */ |
| 234 | + protected synchronized byte[] clampBufferLen(byte[] buffer){ |
| 235 | + final byte[] tempBuffer = new byte[buffer.length]; |
| 236 | + int byteIndex = 0; |
| 237 | + int bufferLen = 0; |
| 238 | + //deep copy available values to a temporary buffer & recalculate the length of the initial buffer |
| 239 | + for(final byte val : buffer){ |
| 240 | + if(val >= 0){ |
| 241 | + //collect viable values & arrange them serially |
| 242 | + tempBuffer[byteIndex++] = val; |
| 243 | + ++bufferLen; |
| 244 | + } |
| 245 | + } |
| 246 | + final byte[] newBuffer = new byte[bufferLen]; |
| 247 | + for(int i = 0; i < newBuffer.length; i++){ |
| 248 | + //deep copy data into the new buffer |
| 249 | + newBuffer[i] = tempBuffer[i]; |
| 250 | + } |
| 251 | + //return a new clamped buffer to the available data |
| 252 | + return newBuffer; |
| 253 | + } |
| 254 | + } |
| 255 | + |
| 256 | +} |
0 commit comments