Mercurial > hg > FederatedLinda
view src/fdl/IOHandler.java @ 71:0352536c33fa
(example: writer) get linda server addr from commandline arg.
author | kazz@e065701.local |
---|---|
date | Fri, 23 Oct 2009 14:11:07 +0900 |
parents | 046feb56a196 |
children | 96c63bc659d4 |
line wrap: on
line source
package fdl; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.channels.ClosedChannelException; import java.nio.channels.SelectionKey; import java.nio.channels.SocketChannel; import java.util.logging.Level; public class IOHandler implements TupleHandler { static final boolean debug = true; public TupleSpace tupleSpace; public SocketChannel ch; public FDLindaServ fds; String remoteString; String localString; public int cnt = 0; public IOHandler(FDLindaServ fds, TupleSpace tupleSpace,SocketChannel ch) { this.tupleSpace = tupleSpace; remoteString = PSX.getRemoteHostAndPort(ch); localString = PSX.getLocalHostAndPort(ch); this.ch = ch; this.fds = fds; } public void handle(SelectionKey key) { // 書き込み可であれば,読み込みを行う if (key.isReadable()) { try { SocketChannel channel = (SocketChannel)key.channel(); if (ch!=channel) { fds.log(Level.INFO,"Wrong socket on IOHandler"); } // 読み込み用のバッファの生成 ByteBuffer command = ByteBuffer.allocate(PSX.LINDA_HEADER_SIZE); command.order(ByteOrder.BIG_ENDIAN); ByteBuffer data = PSX.receivePacket(channel,command); manager_run(key, command, data); } catch (ClosedChannelException e) { key.cancel(); tupleSpace.hook.closeHook(key); } catch (IOException e) { key.cancel(); tupleSpace.hook.closeHook(key); } } } public void manager_run(SelectionKey key, ByteBuffer command, ByteBuffer data) throws IOException { command.order(ByteOrder.BIG_ENDIAN); int mode = command.get(PSX.LINDA_MODE_OFFSET); command.rewind(); if (debug) { fds.log(Level.INFO,"data from : "+key.channel()); } if(mode == '!') { tupleSpace.hook.closeHook(key); } else if(mode == PSX.PSX_CHECK) { tupleSpace.Check(key, command); } else if(mode == PSX.PSX_IN || mode == PSX.PSX_RD){ tupleSpace.In_Rd(key, command, mode); } else if (mode == PSX.PSX_WAIT_RD) { tupleSpace.Wait_Rd(key, command, mode); } else if(mode == PSX.PSX_OUT) { tupleSpace.Out(key, command, data); } else { tupleSpace.hook.closeHook(key); fds.log(Level.SEVERE,"Incorrect tuple operation"); } } void Connection_Close(SelectionKey key) throws IOException { fds.log(Level.INFO,"Connection closed by "+key.channel()); SocketChannel channel = (SocketChannel)key.channel(); key.cancel(); channel.close(); } }