Mercurial > hg > Database > Alice
view src/main/java/alice/daemon/IncomingUdpConnection.java @ 458:bcf6f4a6fcd0 dispose
need set Meta DataSegment PUT API
author | sugi |
---|---|
date | Mon, 03 Nov 2014 17:12:53 +0900 |
parents | b004f62b83e5 |
children | 6e304a7a60e7 |
line wrap: on
line source
package alice.daemon; import java.io.EOFException; import java.io.IOException; import java.nio.ByteBuffer; 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.DataSegmentManager; import alice.datasegment.ReceiveData; import alice.topology.manager.keeparive.RespondData; public class IncomingUdpConnection extends IncomingTcpConnection { // receive Data set into LocalDataSegment now but need to set into MulticastDataSegment. // and this implement has problem. If over 65507 data receive, can not read. // but Max data length is 65507 because of the max length of UDP payload public MulticastConnection receiver; public MulticastConnection sender; public IncomingUdpConnection(MulticastConnection s, MulticastConnection r, DataSegmentManager manager) { super(manager); receiver = r; sender = s; reverseKey = "multicast"; } @Override public void run() { while (true){ try { Command cmd = null; byte[] val = null; ReceiveData rData = null; // Max data length is 65507 because of the max length of UDP payload ByteBuffer receive = ByteBuffer.allocate(65507); receiver.receive(receive); Unpacker unpacker = SingletonMessage.getInstance().createBufferUnpacker(receive); receive.flip(); CommandMessage msg = unpacker.read(CommandMessage.class); CommandType type = CommandType.getCommandTypeFromId(msg.type); switch (type) { case UPDATE: case PUT: val = new byte[unpacker.readInt()]; receive.get(val); rData = new ReceiveData(val, msg.compressed, msg.serialized); cmd = new Command(type, null, null, rData, 0, 0, null, null, reverseKey); getLocalDataSegmentManager().getDataSegmentKey(msg.key).runCommand(cmd); break; case PEEK: case TAKE: cmd = new Command(type, null, null, null, msg.index, msg.seq, null, null ,sender); cmd.setQuickFlag(msg.quickFlag); getLocalDataSegmentManager().getDataSegmentKey(msg.key).runCommand(cmd); break; case REMOVE: cmd = new Command(type, null, null, null, 0, 0, null, null, ""); getLocalDataSegmentManager().getDataSegmentKey(msg.key).runCommand(cmd); break; case REPLY: cmd = manager.getAndRemoveCmd(msg.seq); val = new byte[unpacker.readInt()]; receive.get(val); rData = new ReceiveData(val, msg.compressed, msg.serialized); Command rCmd = new Command(type, null, null, rData, msg.index, msg.seq, null, null, ""); cmd.cs.ids.reply(cmd.receiver, rCmd); break; case PING: DataSegment.get(reverseKey).response(msg.key); break; case RESPONSE: rData = new ReceiveData(new RespondData(reverseKey, System.currentTimeMillis()), false, false); DataSegment.getLocal().put(msg.key, rData, null); break; default: break; } } catch (ClosedChannelException e) { return; } catch (EOFException e) { return; } catch (IOException e) { e.printStackTrace(); } } } }