public NotifyAck reply() { return new NotifyAck(this.getDestination(), this.getSource(), this.getProtocol(), id); }
@Override protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { ByteBuf frame = (ByteBuf) super.decode(ctx, in); if (frame == null) { return null; } component.setCustomMDC(); try { component.extLog.trace("Trying to decode incoming {} bytes of data from {} to {}.", new Object[]{frame.readableBytes(), ctx.channel().remoteAddress(), ctx.channel().localAddress()}); Object o = Serializers.fromBinary(frame, Optional.absent()); component.extLog.trace("Decoded incoming data from {}: {}", ctx.channel().remoteAddress(), o); if (o instanceof AckRequestMsg) { AckRequestMsg arm = (AckRequestMsg) o; component.extLog.trace("Got AckRequest for {}. Replying...", arm.id); NotifyAck an = arm.reply(); ctx.channel().writeAndFlush(new MessageWrapper(an)); return arm.content; } else if (o instanceof Msg) { return o; } else { component.extLog.warn("Got unexpected Stream message type: {} -> {}", o.getClass().getCanonicalName(), o); } return o; } finally { MDC.clear(); } }
MessageNotify.Req msgr = msgw.notify.get(); component.extLog.trace("Serialising message with AckRequest: {}", msgr.getMsgId()); AckRequestMsg arm = new AckRequestMsg(msgw.msg, msgr.getMsgId()); Serializers.toBinary(arm, out); } else {
ChannelFuture sendUdpMessage(MessageWrapper msgw) { ByteBuf buf = udpChannel.alloc().ioBuffer(INITIAL_BUFFER_SIZE, SEND_BUFFER_SIZE); try { if (msgw.notify.isPresent() && msgw.notify.get().notifyOfDelivery) { MessageNotify.Req msgr = msgw.notify.get(); AckRequestMsg arm = new AckRequestMsg(msgw.msg, msgr.getMsgId()); Serializers.toBinary(arm, buf); } else { Serializers.toBinary(msgw.msg, buf); } msgw.injectSize(buf.readableBytes(), System.nanoTime()); DatagramPacket pack = new DatagramPacket(buf, msgw.msg.getDestination().asSocket()); logger.debug("Sending Datagram message {} ({}bytes)", msgw.msg, buf.readableBytes()); return udpChannel.writeAndFlush(pack); } catch (Exception e) { // serialization might fail horribly with size bounded buff logger.warn("Could not send Datagram message {}, error was: {}", msgw, e); return null; } }
UUID id = (UUID) SpecialSerializers.UUIDSerializer.INSTANCE.fromBinary(buf, Optional.absent()); Msg msg = (Msg) Serializers.fromBinary(buf, Optional.absent()); return new AckRequestMsg(msg, id);
@Override protected void messageReceived(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception { component.setCustomMDC(); try { Object o = Serializers.fromBinary(msg.content(), msg); if (o instanceof AckRequestMsg) { AckRequestMsg arm = (AckRequestMsg) o; ByteBuf buf = ctx.alloc().ioBuffer(NettyNetwork.INITIAL_BUFFER_SIZE, NettyNetwork.SEND_BUFFER_SIZE); component.deliverMessage(arm.content, ctx.channel()); component.extLog.trace("Got AckRequest for {}. Replying...", arm.id); NotifyAck ack = arm.reply(); Serializers.toBinary(ack, buf); DatagramPacket pack = new DatagramPacket(buf, ack.getDestination().asSocket()); component.extLog.trace("Sending Datagram ACK {} ({}bytes)", ack, buf.readableBytes()); ctx.writeAndFlush(pack); } else if (o instanceof Msg) { Msg m = (Msg) o; component.deliverMessage(m, ctx.channel()); } else { component.extLog.warn("Got unexpected Datagram message type: {} -> {}", o.getClass().getCanonicalName(), o); } } catch (Exception e) { // Catch anything...the Serializer could throw any kind of weird exception if you get message that were send by someone else component.extLog.warn("Got weird Datagram message, ignoring it: {}", ByteBufUtil.hexDump(msg.content())); component.extLog.trace("Exception was: \n{}", e); } finally { MDC.clear(); } }
@Override public Object fromBinary(ByteBuf buf, DatagramPacket datagram) { byte type = buf.readByte(); SpecialSerializers.MessageSerializationUtil.MessageFields fields = SpecialSerializers.MessageSerializationUtil.msgFromBinary(buf); switch (type) { case DIS: { // Port 2 byte byte portUpper = buf.readByte(); byte portLower = buf.readByte(); int udtPort = Ints.fromBytes((byte) 0, (byte) 0, portUpper, portLower); return new DisambiguateConnection(new NettyAddress(datagram.sender()), new NettyAddress(datagram.recipient()), Transport.UDP, udtPort, fields.flag1); } case ACK: { UUID id = (UUID) SpecialSerializers.UUIDSerializer.INSTANCE.fromBinary(buf, Optional.absent()); return new NotifyAck(new NettyAddress(datagram.sender()), new NettyAddress(datagram.recipient()), Transport.UDP, id); } case ACK_REQ: { UUID id = (UUID) SpecialSerializers.UUIDSerializer.INSTANCE.fromBinary(buf, Optional.absent()); Msg msg = (Msg) Serializers.fromBinary(buf, datagram); return new AckRequestMsg(msg, id); } } return null; }