package org.kne.cloud.network; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.*; import java.nio.channels.SocketChannel; import java.util.concurrent.locks.LockSupport; import org.kne.cloud.network.klalb.CannotAssociateException; import org.kne.cloud.network.klalb.KLALBVirtualSocket; import org.kne.cloud.network.klalb.KLALBVirtualSocketChannel; import org.kne.io.Task; public class SocketChannelBridge extends Task{ protected SocketChannel a; protected SocketChannel b; protected ChannelBridge bridgeAB; protected ChannelBridge bridgeBA; public ChannelBridge getBridgeAB() { return bridgeAB; } public ChannelBridge getBridgeBA() { return bridgeBA; } public SocketChannelBridge(SocketChannel a, SocketChannel b) throws IOException { super(); this.a = a; this.b = b; createStreamChannelBridge(); } protected void createStreamChannelBridge() throws IOException { bridgeAB = new ChannelBridge(a, b); bridgeBA = new ChannelBridge(b, a); } @Override protected void runTask() { try { Thread ta=bridgeAB.runAtNewThread("SocketBridge A->B thread"); Thread tb=bridgeBA.runAtNewThread("SocketBridge B->A thread"); while((ta.isAlive()||tb.isAlive())&&a.isOpen()&&b.isOpen()) { LockSupport.parkNanos(1000000L); } } catch (Exception e) { e.printStackTrace(); }finally { //new Exception().printStackTrace(); try { a.close(); //System.out.println("closeA"); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } try { b.close(); //System.out.println("closeB"); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } } public SocketChannel getA() { return a; } public SocketChannel 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(); }*/ }