view src/msgpack/test/ForkTestMsgPack.java @ 1:830afc6475b0 draft

add sime files
author one
date Tue, 22 May 2012 02:45:56 +0900
parents d86770305c8b
children
line wrap: on
line source

package msgpack.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class ForkTestMsgPack {

	static class CheckStd implements Runnable {
		Process p;
		InputStream in, ein;
		BufferedReader br, ebr;
		String name;
		CheckStd(Process _p, String str) {
			p = _p;
			in = p.getInputStream();
			ein = p.getErrorStream();
			name = str;
		}

		@Override
		public void run() {

			Runnable inputStreamThread = new Runnable() {
				public void run() {
					String line;
					try {
						System.out.println(name+"Thread stdRun start");
						br = new BufferedReader(new InputStreamReader(in));
						while ((line = br.readLine()) != null) {
							System.out.println(name+line);
						}
						System.out.println(name+"Thread stdRun end");
					} catch (Exception e) {
						e.printStackTrace();
					}
				}

			};

			Runnable errStreamThread = new Runnable() {
				public void run() {
					try {
						System.out.println(name+"Thread errRun start");
						ebr = new BufferedReader(new InputStreamReader(ein));
						String errLine;
						while ((errLine = ebr.readLine()) != null) {
							System.out.println(name+errLine);
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}

			};

			Thread stdRun = new Thread(inputStreamThread);
			Thread errRun = new Thread(errStreamThread);
			
			stdRun.start();
			errRun.start();


			try {
				int c = p.waitFor();
				stdRun.join();
				errRun.join();
				in.close();
				ein.close();				
			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();				
			}			
		}
	}

	private static InputStream in = null;
	private static InputStream ein = null;
	static BufferedReader ebr = null;
	static BufferedReader br = null;

	static final String[] cmdServer = { "/usr/bin/java",
			"TestMsgPackServer","-cp bin/classes/msgpack/test/" };
	static final String[] cmdClient = { "/usr/bin/java",
			"TestMsgPackClient", "-cp bin/classes/msgpack/test/" };

	public static void main(String[] args) {
		Runtime run = Runtime.getRuntime();
		try {
			Process ps = run.exec(cmdServer);
			Process pc = run.exec(cmdClient);
			
			Thread ts = new Thread(new CheckStd(ps, "Server:"));
			Thread tc = new Thread(new CheckStd(pc, "Client:"));
			
			ts.start();
			tc.start();

			ts.join();
			tc.join();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

	}

}