Skip to content
Browse files

dragonfly: fixes to shell integration with bug simulator and knapsack.

  • Loading branch information...
1 parent 0fd2686 commit 3c70832056f6152ef4ccd48c807033325e8decfa @kgilmer kgilmer committed Aug 3, 2011
Showing with 55 additions and 15 deletions.
  1. +55 −15 com.buglabs.bug.simulator/com/buglabs/bug/simulator/ui/ShellIOThread.java
View
70 com.buglabs.bug.simulator/com/buglabs/bug/simulator/ui/ShellIOThread.java
@@ -3,51 +3,61 @@
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
+import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
-
-import org.knapsack.shell.pub.Netcat;
+import java.net.Socket;
+import java.net.UnknownHostException;
/**
* A class to read from the stdin, send to knapsack, and print result.
*
* @author kgilmer
- *
+ *
*/
public class ShellIOThread extends Thread {
+ private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
+ private static final long START_DELAY = 1000;
+ private static final String LINE_FEED = "\n";
private final int port;
/**
* @param port
*/
public ShellIOThread(int port) {
- this.port = port;
+ this.port = port;
}
-
+
@Override
public void run() {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- OutputStream os = System.out;
-
- String line = null;
+ BufferedReader br = null;
try {
+ Thread.sleep(START_DELAY);
+ br = new BufferedReader(new InputStreamReader(System.in));
+ OutputStream os = System.out;
+
+ String line = null;
+
printPrompt(os);
while (!Thread.interrupted() && (line = br.readLine()) != null) {
- Netcat.run("localhost", port, new ByteArrayInputStream(line.getBytes()), os);
+ line = line + LINE_FEED;
+ run("localhost", port, new ByteArrayInputStream(line.getBytes()), os);
os.flush();
printPrompt(os);
}
} catch (IOException e) {
-
+
+ } catch (InterruptedException e) {
} finally {
try {
- br.close();
- } catch (IOException e) {
+ if (br != null)
+ br.close();
+ } catch (IOException e) {
}
}
}
-
+
/**
* @param os
* @throws IOException
@@ -63,8 +73,38 @@ public void shutdown() {
this.interrupt();
try {
System.out.write("\n".getBytes());
- } catch (IOException e) {
+ } catch (IOException e) {
}
}
+ public static void run(String host, int port, InputStream input, OutputStream output) throws UnknownHostException, IOException {
+ // Create and open socket
+ Socket socket = new Socket(host, port);
+ OutputStream out = socket.getOutputStream();
+ InputStream in = socket.getInputStream();
+
+ // Push, pull, close.
+ copy(input, out);
+ copy(in, output);
+ socket.close();
+ }
+
+ public static int copy(InputStream input, OutputStream output) throws IOException {
+ long count = copyLarge(input, output);
+ if (count > Integer.MAX_VALUE) {
+ return -1;
+ }
+ return (int) count;
+ }
+
+ public static long copyLarge(InputStream input, OutputStream output) throws IOException {
+ byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
+ long count = 0;
+ int n = 0;
+ while (-1 != (n = input.read(buffer))) {
+ output.write(buffer, 0, n);
+ count += n;
+ }
+ return count;
+ }
}

0 comments on commit 3c70832

Please sign in to comment.
Something went wrong with that request. Please try again.