Last active
December 10, 2015 02:19
-
-
Save He-Pin/4367061 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package us.sosia.net.udt.channel; | |
import java.io.IOException; | |
import java.net.SocketAddress; | |
import java.nio.channels.SelectionKey; | |
import java.nio.channels.Selector; | |
import java.nio.channels.ServerSocketChannel; | |
import java.nio.channels.SocketChannel; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
import us.sosia.net.udt.handler.UDTHandler; | |
import com.barchart.udt.nio.SelectorProviderUDT; | |
public class UDTServerSocketChannel { | |
protected final ExecutorService acceptWoker ; | |
protected final ExecutorService dataWoker; | |
protected ServerSocketChannel udtServerSocketChannel; | |
protected final UDTHandler udtHandler; | |
public UDTServerSocketChannel(ExecutorService acceptWoker, | |
ExecutorService dataWoker,UDTHandler udtHandler){ | |
super(); | |
this.acceptWoker = acceptWoker; | |
this.dataWoker = dataWoker; | |
this.udtHandler = udtHandler; | |
} | |
public UDTServerSocketChannel(ExecutorService dataWoker,UDTHandler udtHandler) { | |
super(); | |
this.acceptWoker = Executors.newCachedThreadPool(); | |
this.dataWoker = dataWoker; | |
this.udtHandler = udtHandler; | |
} | |
public UDTServerSocketChannel(UDTHandler udtHandler) { | |
super(); | |
this.acceptWoker = Executors.newCachedThreadPool(); | |
this.dataWoker = Executors.newCachedThreadPool(); | |
this.udtHandler = udtHandler; | |
} | |
public ServerSocketChannel openChannel(boolean blocking) throws IOException{ | |
udtServerSocketChannel = SelectorProviderUDT.STREAM.openServerSocketChannel(); | |
udtServerSocketChannel.configureBlocking(blocking); | |
return udtServerSocketChannel; | |
} | |
public void bind(SocketAddress socketAddress) throws IOException{ | |
System.out.println("bind at :"+socketAddress); | |
this.udtServerSocketChannel.socket().bind(socketAddress); | |
} | |
public void bind(SocketAddress socketAddress,int backlog) throws IOException{ | |
this.udtServerSocketChannel.socket().bind(socketAddress,backlog); | |
} | |
public void accept() throws IOException{ | |
Boss boss = new Boss(udtServerSocketChannel); | |
if (boss != null) { | |
acceptWoker.execute(boss); | |
} | |
} | |
public class Boss implements Runnable{ | |
protected final Selector selector; | |
protected final ServerSocketChannel serverSocketChannel; | |
public Boss(ServerSocketChannel serverSocketChannel) throws IOException { | |
super(); | |
this.serverSocketChannel = serverSocketChannel; | |
this.selector = SelectorProviderUDT.STREAM.openSelector(); | |
this.serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); | |
} | |
@Override | |
public void run() { | |
for(;;){ | |
try { | |
System.out.println("select"); | |
int selected = selector.select(); | |
if(selected == 0 ){ | |
continue; | |
} | |
System.out.println("selected"); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
selector.selectedKeys().clear(); | |
for(;;){ | |
SocketChannel socketChannel = null; | |
try { | |
socketChannel = serverSocketChannel.accept(); | |
udtHandler.onConnected(socketChannel); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
if (socketChannel == null) { | |
break; | |
}else { | |
//handle to the worker | |
try { | |
socketChannel.configureBlocking(false); | |
} catch (IOException e1) { | |
// TODO Auto-generated catch block | |
e1.printStackTrace(); | |
} | |
DataWorker worker = null; | |
try { | |
worker = new DataWorker(socketChannel,udtHandler); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
if (worker != null) { | |
dataWoker.execute(worker); | |
} | |
} | |
} | |
try { | |
Thread.sleep(500); | |
} catch (InterruptedException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package us.sosia.net.udt.channel; | |
import java.io.IOException; | |
import java.net.InetSocketAddress; | |
import java.nio.ByteBuffer; | |
import java.nio.channels.SocketChannel; | |
import us.sosia.net.udt.handler.UDTHandler; | |
public class ServerTest { | |
/** | |
* @param args | |
* @throws IOException | |
*/ | |
public static void main(String[] args) throws IOException { | |
// TODO Auto-generated method stub | |
UDTServerSocketChannel udtServerSocketChannel = new UDTServerSocketChannel(new UDTHandler() { | |
@Override | |
public void onMessageReceved(SocketChannel socketChannel, | |
ByteBuffer byteBuffer, int read) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void onDataWrited() { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void onConnected(SocketChannel socketChannel) { | |
// TODO Auto-generated method stub | |
System.out.println("remote connected in"); | |
} | |
}); | |
udtServerSocketChannel.openChannel(false); | |
udtServerSocketChannel.bind(new InetSocketAddress("localhost",20000)); | |
udtServerSocketChannel.accept(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package us.sosia.net.udt.channel; | |
import java.io.IOException; | |
import java.net.InetSocketAddress; | |
import java.nio.ByteBuffer; | |
import java.nio.channels.SocketChannel; | |
import us.sosia.net.udt.handler.UDTHandler; | |
public class ClientTest { | |
/** | |
* @param args | |
* @throws IOException | |
*/ | |
public static void main(String[] args) throws IOException { | |
UDTClientSocketChannel udtClientSocketChannel = new UDTClientSocketChannel(new UDTHandler() { | |
@Override | |
public void onMessageReceved(SocketChannel socketChannel, | |
ByteBuffer byteBuffer, int read) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void onDataWrited() { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void onConnected(SocketChannel socketChannel) { | |
System.out.println("connectted to remote addr"); | |
} | |
}); | |
udtClientSocketChannel.openChannel(false); | |
udtClientSocketChannel.bind(new InetSocketAddress(20001)); | |
udtClientSocketChannel.connect(new InetSocketAddress("localhost",20000)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment