diff src/rep/channel/REPServerSocketChannel.java @ 205:540d7a8a9e33

add sessionmanager's file
author one
date Sat, 18 Dec 2010 17:35:25 +0900
parents 3133040ee4f4
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/rep/channel/REPServerSocketChannel.java	Sat Dec 18 17:35:25 2010 +0900
@@ -0,0 +1,134 @@
+package rep.channel;
+
+import java.io.IOException;
+import java.net.ServerSocket;
+import java.nio.channels.ClosedChannelException;
+import java.nio.channels.SelectableChannel;
+import java.nio.channels.SelectionKey;
+import java.nio.channels.Selector;
+import java.nio.channels.ServerSocketChannel;
+import java.nio.channels.SocketChannel;
+import java.nio.channels.spi.SelectorProvider;
+
+/* 
+ * シミュレーションでは inheritance のServerChannelSimulator を生成、
+ * リアルコミュニケーションでは 自身を生成、内部にもつ ServerSocketChannelを扱う
+ */
+public class REPServerSocketChannel<P> extends SelectableChannel {
+
+	public static boolean isSimulation=false;
+	private ServerSocketChannel ssc;
+	private REPPack<P> packer;
+
+	public REPServerSocketChannel() {
+		
+	}
+	
+	public static <T> REPServerSocketChannel<T> open(REPPack<T> packer) throws IOException{
+		if(isSimulation){
+			return new ServerChannelSimulator<T>();
+		}else{
+			return new REPServerSocketChannel<T>(ServerSocketChannel.open(), packer);
+		}
+	}
+	
+	public static <T> REPServerSocketChannel<T> open(SelectableChannel c,REPPack<T> packer) throws IOException{
+		assert(!isSimulation);
+		return new REPServerSocketChannel<T>((ServerSocketChannel)c, packer);
+	}
+	
+	public REPServerSocketChannel(ServerSocketChannel open, REPPack<P> packer) {
+		ssc = open; 
+		this.packer = packer;
+		REPSocketChannel.addChannel(ssc,this);
+	}
+	
+	public void close1() throws IOException {
+		REPSocketChannel.removeChannel(ssc);
+		ssc.close();
+	}
+
+	public REPServerSocketChannel(SelectableChannel channel, REPPack<P> packer) {
+		this.ssc = (ServerSocketChannel)channel;
+		this.packer = packer;
+		REPSocketChannel.addChannel(ssc,this);
+	}
+
+	public REPSocketChannel<P> accept1() throws IOException {
+		return new REPSocketChannel<P>(ssc.accept(), packer);
+	}
+	
+	public SelectionKey register(REPSelector<P> sel, int ops, Object att) throws ClosedChannelException {
+		assert(!isSimulation);
+		if(sel!=null)
+			return sel.register(ssc, ops, att);
+		else
+			return null;
+	}
+
+	public SocketChannel accept() throws IOException {
+		return accept1().sc;
+	}
+
+
+	public ServerSocket socket() {
+		return ssc.socket();
+	}
+
+	public SelectableChannel configureBlocking(boolean block) throws IOException
+	{
+		ssc.configureBlocking(block);
+		return this;
+	}
+
+	@Override
+	public Object blockingLock() {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	@Override
+	public boolean isBlocking() {
+		// TODO Auto-generated method stub
+		return false;
+	}
+
+	@Override
+	public boolean isRegistered() {
+		// TODO Auto-generated method stub
+		return false;
+	}
+
+	@Override
+	public SelectionKey keyFor(Selector sel) {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	@Override
+	public SelectorProvider provider() {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	@Override
+	public SelectionKey register(Selector sel, int ops, Object att)
+			throws ClosedChannelException {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	@Override
+	public int validOps() {
+		// TODO Auto-generated method stub
+		return 0;
+	}
+
+	@Override
+	protected void implCloseChannel() throws IOException {
+		// TODO Auto-generated method stub
+		
+	}
+
+
+}