Example #1
0
        protected override void Encode(IChannelHandlerContext context, object message, List <object> output)
        {
            _logger.LogDebug("Encoding {0}", message);
            var w = Wrapper.CreateBuilder();

            message.Match()
            .With <Hello>(
                hello =>
                w.SetHello(
                    TCP.Hello.CreateBuilder()
                    .SetName(hello.Name)
                    .SetAddress(Address2Proto(hello.Address))))
            .With <EnterBarrier>(barrier =>
            {
                var protoBarrier = TCP.EnterBarrier.CreateBuilder().SetName(barrier.Name);
                if (barrier.Timeout.HasValue)
                {
                    protoBarrier.SetTimeout(barrier.Timeout.Value.Ticks);
                }
                protoBarrier.SetOp(BarrierOp.Enter);
                w.SetBarrier(protoBarrier);
            })
            .With <BarrierResult>(result =>
            {
                var res = result.Success ? BarrierOp.Succeeded : BarrierOp.Failed;
                w.SetBarrier(
                    TCP.EnterBarrier.CreateBuilder().SetName(result.Name).SetOp(res));
            })
            .With <FailBarrier>(
                barrier =>
                w.SetBarrier(TCP.EnterBarrier.CreateBuilder()
                             .SetName(barrier.Name)
                             .SetOp(BarrierOp.Fail)))
            .With <ThrottleMsg>(
                throttle =>
            {
                w.SetFailure(
                    InjectFailure.CreateBuilder()
                    .SetFailure(TCP.FailType.Throttle)
                    .SetAddress(Address2Proto(throttle.Target))
                    .SetFailure(TCP.FailType.Throttle)
                    .SetDirection(Direction2Proto(throttle.Direction))
                    .SetRateMBit(throttle.RateMBit));
            })
            .With <DisconnectMsg>(
                disconnect =>
                w.SetFailure(
                    InjectFailure.CreateBuilder()
                    .SetAddress(Address2Proto(disconnect.Target))
                    .SetFailure(disconnect.Abort ? TCP.FailType.Abort : TCP.FailType.Disconnect)))
            .With <TerminateMsg>(terminate =>
            {
                if (terminate.ShutdownOrExit.IsRight)
                {
                    w.SetFailure(
                        InjectFailure.CreateBuilder()
                        .SetFailure(TCP.FailType.Exit)
                        .SetExitValue(terminate.ShutdownOrExit.ToRight().Value));
                }
                else if (terminate.ShutdownOrExit.IsLeft && !terminate.ShutdownOrExit.ToLeft().Value)
                {
                    w.SetFailure(
                        InjectFailure.CreateBuilder()
                        .SetFailure(TCP.FailType.Shutdown));
                }
                else
                {
                    w.SetFailure(
                        InjectFailure.CreateBuilder()
                        .SetFailure(TCP.FailType.ShutdownAbrupt));
                }
            })
            .With <GetAddress>(
                address => w.SetAddr(AddressRequest.CreateBuilder().SetNode(address.Node.Name)))
            .With <AddressReply>(
                reply =>
                w.SetAddr(
                    AddressRequest.CreateBuilder()
                    .SetNode(reply.Node.Name)
                    .SetAddr(Address2Proto(reply.Addr))))
            .With <Done>(done => w.SetDone(string.Empty))
            .Default(obj => w.SetDone(string.Empty));

            output.Add(w.Build());
        }
Example #2
0
        public void Encode(IConnection connection, object message, out List <IByteBuf> encoded)
        {
            encoded = new List <IByteBuf>();
            var x = message as INetworkOp;

            if (x != null)
            {
                var w = Wrapper.CreateBuilder();
                x.Match()
                .With <Hello>(
                    hello =>
                    w.SetHello(
                        TCP.Hello.CreateBuilder()
                        .SetName(hello.Name)
                        .SetAddress(Address2Proto(hello.Address))))
                .With <EnterBarrier>(barrier =>
                {
                    var protoBarrier = TCP.EnterBarrier.CreateBuilder().SetName(barrier.Name);
                    if (barrier.Timeout.HasValue)
                    {
                        protoBarrier.SetTimeout(barrier.Timeout.Value.Ticks);
                    }
                    protoBarrier.SetOp(BarrierOp.Enter);
                    w.SetBarrier(protoBarrier);
                })
                .With <BarrierResult>(result =>
                {
                    var res = result.Success ? BarrierOp.Succeeded : BarrierOp.Failed;
                    w.SetBarrier(
                        TCP.EnterBarrier.CreateBuilder().SetName(result.Name).SetOp(res));
                })
                .With <FailBarrier>(
                    barrier =>
                    w.SetBarrier(TCP.EnterBarrier.CreateBuilder()
                                 .SetName(barrier.Name)
                                 .SetOp(BarrierOp.Fail)))
                .With <ThrottleMsg>(
                    throttle =>
                    w.SetFailure(
                        InjectFailure.CreateBuilder()
                        .SetAddress(Address2Proto(throttle.Target))
                        .SetDirection(Direction2Proto(throttle.Direction))
                        .SetRateMBit(throttle.RateMBit)))
                .With <DisconnectMsg>(
                    disconnect =>
                    w.SetFailure(
                        InjectFailure.CreateBuilder()
                        .SetAddress(Address2Proto(disconnect.Target))
                        .SetFailure(disconnect.Abort ? TCP.FailType.Abort : TCP.FailType.Disconnect)))
                .With <TerminateMsg>(terminate =>
                {
                    if (terminate.ShutdownOrExit.IsRight)
                    {
                        w.SetFailure(
                            InjectFailure.CreateBuilder()
                            .SetFailure(TCP.FailType.Exit)
                            .SetExitValue(terminate.ShutdownOrExit.ToRight().Value));
                    }
                    else if (terminate.ShutdownOrExit.IsLeft && !terminate.ShutdownOrExit.ToLeft().Value)
                    {
                        w.SetFailure(
                            InjectFailure.CreateBuilder()
                            .SetFailure(TCP.FailType.Shutdown));
                    }
                    else
                    {
                        w.SetFailure(
                            InjectFailure.CreateBuilder()
                            .SetFailure(TCP.FailType.ShutdownAbrupt));
                    }
                })
                .With <GetAddress>(
                    address => w.SetAddr(AddressRequest.CreateBuilder().SetNode(address.Node.Name)))
                .With <AddressReply>(
                    reply =>
                    w.SetAddr(
                        AddressRequest.CreateBuilder()
                        .SetNode(reply.Node.Name)
                        .SetAddr(Address2Proto(reply.Addr))))
                .With <Done>(done => w.SetDone(string.Empty))
                .Default(obj => w.SetDone(string.Empty));

                encoded.Add(connection.Allocator.Buffer().WriteBytes(w.Build().ToByteArray()));
            }
            else
            {
                throw new ArgumentException(string.Format("wrong message {0}", message));
            }
        }