diff src/rep/channel/REPSocketChannel.java @ 193:3133040ee4f4

(no commit message)
author one
date Wed, 31 Dec 2008 15:06:22 +0900
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/rep/channel/REPSocketChannel.java	Wed Dec 31 15:06:22 2008 +0900
@@ -0,0 +1,181 @@
+package rep.channel;
+
+import java.io.IOException;
+import java.net.Socket;
+import java.net.SocketAddress;
+import java.nio.ByteBuffer;
+import java.nio.channels.ClosedChannelException;
+import java.nio.channels.SelectableChannel;
+import java.nio.channels.SelectionKey;
+import java.nio.channels.Selector;
+import java.nio.channels.SocketChannel;
+import java.nio.channels.spi.AbstractSelector;
+import java.nio.channels.spi.SelectorProvider;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+public class REPSocketChannel<P> extends SelectableChannel{
+
+	public SocketChannel sc;
+	private REPPack<P> pack;
+	static public Map<SelectableChannel,SelectableChannel> channels = 
+		Collections.synchronizedMap(new HashMap<SelectableChannel,SelectableChannel>());  
+
+	public REPSocketChannel(SocketChannel channel, REPPack<P> packer) {
+		sc = channel;
+		pack = packer;
+		addChannel(sc,this);
+	}
+
+	public REPSocketChannel(SelectableChannel channel, REPPack<P> packer) {
+		sc = (SocketChannel)channel;
+		pack = packer;
+		addChannel(sc,this);
+	}
+
+	public static void addChannel(SelectableChannel sc,SelectableChannel rc) {
+		channels.put(sc, rc);
+	}
+
+	public void close1() throws IOException {
+		removeChannel(sc);
+		sc.close();
+	}
+
+	public static void removeChannel(SelectableChannel sc) throws IOException {
+		if(channels.containsKey(sc)) channels.remove(sc);
+	}
+	
+	@Override
+	public Object blockingLock() {
+		return sc.blockingLock();
+	}
+
+	@Override
+	public SelectableChannel configureBlocking(boolean block) throws IOException {
+		return sc.configureBlocking(block);
+	}
+
+	@Override
+	public boolean isBlocking() {
+		return sc.isBlocking();
+	}
+
+	@Override
+	public boolean isRegistered() {
+		return sc.isRegistered();
+	}
+
+	@Override
+	public SelectionKey keyFor(Selector sel) {
+		return sc.keyFor(sel);
+	}
+	
+	public SelectionKey keyFor(REPSelector<?> sel) {
+		return sc.keyFor(sel.selector);
+	}
+	
+	public REPSelectionKey<P> keyFor1(REPSelector<P> sel) {
+		return new REPSelectionKey<P>(sc.keyFor(sel.selector),
+				new REPSelector<P>((AbstractSelector) sel.selector));
+	}
+
+	@Override
+	public SelectorProvider provider() {
+		return sc.provider();
+	}
+
+
+	@Override
+	public int validOps() {
+		return sc.validOps();
+	}
+
+	@Override
+	protected void implCloseChannel() throws IOException {
+		close1();
+	}
+
+
+	public int read(ByteBuffer header) throws IOException {
+		return sc.read(header);
+	}
+
+	public void write(ByteBuffer buffer) throws IOException {
+		sc.write(buffer);
+		
+	}
+
+	public boolean finishConnect() throws IOException {
+		return sc.finishConnect();
+	}
+
+	public Socket socket() {
+		return sc.socket();
+	}
+	
+	public P read() throws IOException{
+		return pack.unpackUConv(sc);
+	}
+	
+	public boolean write(P p){
+		ByteBuffer bb = pack.packUConv(p);
+		if (bb==null) return true;
+		try {
+			while (bb.remaining() > 0 ){
+				sc.write(bb);
+			}
+			return true;
+		} catch (IOException e) {
+			return false;
+		}
+	}
+
+	public static <T> REPSocketChannel<T> create(REPPack<T> packer) throws IOException {
+		if (REPServerSocketChannel.isSimulation) {
+			return new ChannelSimulator<T>();
+		} else {
+			REPSocketChannel<T> rsc = new REPSocketChannel<T>(SocketChannel.open(), packer);
+			return rsc;
+		}
+	}
+
+
+	public boolean connect(SocketAddress semaIP) throws IOException {
+		return sc.connect(semaIP);
+	}
+
+	public SelectionKey register(REPSelector<P> sel, int ops, Object att) throws ClosedChannelException {
+		return sc.register(sel.selector, ops, att);
+	}
+
+
+
+	public SelectionKey register1(REPSelector<P> sel, int ops, Object att)
+			throws ClosedChannelException {
+		if(sel instanceof REPSelector) {
+			REPSelector<P> s = (REPSelector<P>)sel;
+			return sc.register(s.selector, ops,att);
+		}
+		return sc.register(sel, ops,att);
+	}
+	
+	@SuppressWarnings("unchecked")
+	@Override
+	public SelectionKey register(Selector sel, int ops, Object att)
+			throws ClosedChannelException {
+		if(sel instanceof REPSelector) {
+			REPSelector<P> s = (REPSelector<P>)sel;
+			return sc.register(s.selector, ops,att);
+		}
+		return sc.register(sel, ops,att);
+	}
+
+	public String getLocalHostName() {
+		return sc.socket().getLocalAddress().getHostName();
+
+	}
+
+
+}
\ No newline at end of file