Mercurial > hg > Database > Alice
view src/alice/daemon/IncomingTcpConnection.java @ 224:409d7679cf7b
merge
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 28 Mar 2013 00:21:38 +0900 |
parents | 4bfd81352cfa 87f1a30a8c82 |
children | d9c9076d6b47 |
line wrap: on
line source
package alice.daemon; import java.io.EOFException; import java.io.IOException; import java.nio.channels.ClosedChannelException; import org.msgpack.unpacker.Unpacker; import alice.codesegment.SingletonMessage; import alice.datasegment.Command; import alice.datasegment.CommandType; import alice.datasegment.DataSegment; import alice.datasegment.DataSegmentKey; import alice.datasegment.DataSegmentManager; import alice.datasegment.LocalDataSegmentManager; public class IncomingTcpConnection extends Thread { public Connection connection; public DataSegmentManager manager; public String reverseKey; private LocalDataSegmentManager lmanager = DataSegment.getLocal(); public IncomingTcpConnection(Connection connection, DataSegmentManager manager, String reverseKey) { this.manager = manager; this.connection = connection; this.reverseKey = reverseKey; } /** * pipeline thread for receiving */ public void run() { Unpacker unpacker = null; try { unpacker = SingletonMessage.getInstance().createUnpacker(connection.socket.getInputStream()); } catch (IOException e2) { e2.printStackTrace(); } while (true) { try { CommandMessage msg = unpacker.read(CommandMessage.class); CommandType type = CommandType.getCommandTypeFromId(msg.type); switch (type) { case UPDATE: lmanager.submitCommand(getDataSegmentKey(msg), new Command(type, null, null, msg.val, 0, 0, null, null, reverseKey)); break; case PUT: lmanager.submitCommand(getDataSegmentKey(msg), new Command(type, null, null, msg.val, 0, 0, null, null, reverseKey)); break; case PEEK: lmanager.submitCommand(getDataSegmentKey(msg), new Command(type, null, null, null, msg.index, msg.seq, connection.sendQueue, null, null)); break; case TAKE: lmanager.submitCommand(getDataSegmentKey(msg), new Command(type, null, null, null, msg.index, msg.seq, connection.sendQueue, null, null)); break; case REMOVE: lmanager.submitCommand(getDataSegmentKey(msg), new Command(type, null, null, null, 0, 0, null, null, null)); break; case REPLY: manager.addReplyCommand(new Command(type, null, null, msg.val, msg.index, msg.seq, null, null, null)); break; default: break; } } catch (ClosedChannelException e) { connection.sendCommand(new Command(CommandType.CLOSE, null, null, null, 0, 0, null, null, null)); return; } catch (EOFException e) { connection.sendCommand(new Command(CommandType.CLOSE, null, null, null, 0, 0, null, null, null)); return; } catch (IOException e) { e.printStackTrace(); } } } private DataSegmentKey getDataSegmentKey(CommandMessage msg) { return lmanager.getDataSegmentKey(msg.key); } }