package org.kne.cloud.network;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.*;

import org.kne.io.Task;
public class SocketBridge extends Task{
	protected Socket a;
	protected Socket b;
	protected StreamBridge bridgeAB;
	protected StreamBridge bridgeBA;
	public StreamBridge getBridgeAB() {
		return bridgeAB;
	}
	public StreamBridge getBridgeBA() {
		return bridgeBA;
	}
	public SocketBridge(Socket a, Socket b) throws IOException {
		super();
		this.a = a;
		this.b = b;
		createStreamBridge();
	}
	protected void createStreamBridge() throws IOException {
		bridgeAB = new StreamBridge(a.getInputStream(), b.getOutputStream());
		bridgeBA = new StreamBridge(b.getInputStream(), a.getOutputStream());
	}
	@Override
	protected void runTask() {
		try {
			bridgeAB.runAtNewThread("SocketBridge A->B thread");
			bridgeBA.runAtNewThread("SocketBridge B->A thread");
			bridgeAB.waitfortask();
			bridgeBA.waitfortask();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			//new Exception().printStackTrace();
			try {
				a.close();
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			try {
				b.close();
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}
	}
	public Socket getA() {
		return a;
	}
	public Socket getB() {
		return b;
	}
	/*protected OutputStream getBOUT() throws IOException {
		return b.getOutputStream();
	}
	protected InputStream getBIN() throws IOException {
		return b.getInputStream();
	}
	protected OutputStream getAOUT() throws IOException {
		return a.getOutputStream();
	}
	protected InputStream getAIN() throws IOException {
		return a.getInputStream();
	}*/
	
}
