Esempio n. 1
0
        public void Color(IEgg egg, IBunny bunny)
        {
            if (bunny.Energy > 0)
            {
                bool isThereADyes  = bunny.Dyes.Any(x => x.Power > 0);
                bool isEggFinished = false;

                if (isThereADyes)
                {
                    foreach (var item in bunny.Dyes.Where(x => x.Power > 0))
                    {
                        while (item.Power > 0 && !isEggFinished)
                        {
                            item.Use();
                            bunny.Work();
                            egg.GetColored();
                            isEggFinished = egg.IsDone();
                            if (isEggFinished)
                            {
                                break;
                            }
                        }
                        if (isEggFinished)
                        {
                            break;
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            var    pipe  = Bunny.ConnectSingleWith();
            IBunny bunny = pipe.Connect();

            Console.ReadLine();
        }
        public async Task ConfirmsAndAcksWork()
        {
            IBunny bunny = Bunny.ConnectSingle(ConnectSimple.BasicAmqp);
            IQueue queue = bunny.Setup()
                           .Queue("constraint")
                           .MaxLength(1)
                           .QueueExpiry(1500)
                           .Bind("amq.direct", "constraint-key")
                           .OverflowReject();

            bool isNacked  = false;
            bool isAcked   = false;
            var  publisher = bunny.Publisher <TestMessage>("amq.direct");
            Func <BasicNackEventArgs, Task> nacker = ea => { isNacked = true; return(Task.CompletedTask); };
            Func <BasicAckEventArgs, Task>  acker  = ea => { isAcked = true; return(Task.CompletedTask); };

            await publisher.WithQueueDeclare(queue)
            .WithConfirm(acker, nacker)
            .WithRoutingKey("constraint-key")
            .SendAsync(new TestMessage()
            {
                Text = "Confirm-1st"
            });

            await publisher.WithQueueDeclare(queue)
            .WithConfirm(acker, nacker)
            .SendAsync(new TestMessage()
            {
                Text = "Confirm-2nd"
            });

            Assert.True(isAcked);
            Assert.True(isNacked);
            bunny.Dispose();
        }
Esempio n. 4
0
        public void Color(IEgg egg, IBunny bunny)
        {
            IDye dye = null;

            while (!egg.IsDone())
            {
                if (dye == null || dye.IsFinished())
                {
                    dye = bunny.Dyes.FirstOrDefault(d => d.Power > 0);

                    if (dye == null)
                    {
                        break;
                    }
                }
                dye.Use();
                bunny.Work();
                egg.GetColored();

                if (bunny.Energy == 0)
                {
                    break;
                }
            }
        }
Esempio n. 5
0
        public void Color(IEgg egg, IBunny bunny)
        {
            if (bunny.Energy > 0 && bunny.Dyes.Any(d => d.Power > 0))
            {
                while (!egg.IsDone())
                {
                    var usedDye = bunny.Dyes.FirstOrDefault(d => d.Power > 0);
                    if (usedDye != null)
                    {
                        usedDye.Use();
                        egg.GetColored();
                    }
                    else
                    {
                        var HasNewDay = bunny.Dyes.Any(d => d.Power > 0);
                        if (HasNewDay)
                        {
                            var newDay = bunny.Dyes.First(d => d.Power > 0);
                            newDay.Use();
                            egg.GetColored();
                        }
                        else
                        {
                            bunny.Dyes.Remove(usedDye);
                            break;
                        }
                    }

                    if (bunny.Energy < 0 || bunny.Dyes.All(d => d.Power == 0))
                    {
                        break;
                    }
                }
            }
        }
Esempio n. 6
0
        public string ColorEgg(string eggName)
        {
            var readyBunnies = bunnies.Models.Where(b => b.Energy >= 50).OrderByDescending(b => b.Energy);
            var givenEgg     = eggs.FindByName(eggName);

            if (readyBunnies == null)
            {
                throw new InvalidOperationException(ExceptionMessages.BunniesNotReady);
            }
            while (readyBunnies != null && !givenEgg.IsDone())
            {
                IBunny workingBunny = readyBunnies.First();
                givenEgg.GetColored();
                workingBunny.Work();

                if (workingBunny.Energy == 0)
                {
                    bunnies.Remove(workingBunny);
                }
                readyBunnies = bunnies.Models.Where(b => b.Energy >= 50).OrderByDescending(b => b.Energy);
            }

            var status = givenEgg.IsDone() ? "done" : "not done";

            return($"Egg {eggName} is {status}.");
        }
Esempio n. 7
0
        public void Color(IEgg egg, IBunny bunny)
        {
            bool finishColoring = false;

            while (!egg.IsDone() &&
                   bunny.Energy > 0 &&
                   bunny.Dyes.Any(d => !d.IsFinished()))
            {
                foreach (IDye dye in bunny.Dyes.Where(d => !d.IsFinished()))
                {
                    if (finishColoring)
                    {
                        break;
                    }

                    while (true)
                    {
                        egg.GetColored();
                        dye.Use();
                        bunny.Work();

                        if (egg.IsDone() || bunny.Energy == 0)
                        {
                            finishColoring = true;
                            break;
                        }

                        if (dye.IsFinished())
                        {
                            break;
                        }
                    }
                }
            }
        }
Esempio n. 8
0
        public string ColorEgg(string eggName)
        {
            IEgg          eggToColor      = eggs.FindByName(eggName);
            List <IBunny> orderderBunnies = bunnies.Models.OrderByDescending(x => x.Energy).Where(y => y.Energy >= 50).ToList();

            if (orderderBunnies.Count() == 0)
            {
                throw new InvalidOperationException(ExceptionMessages.BunniesNotReady);
            }
            for (int i = 0; i < orderderBunnies.Count(); i++)
            {
                IBunny currentBunny = orderderBunnies[i];
                worshop.Color(eggToColor, currentBunny);
                if (eggToColor.IsDone())
                {
                    break;
                }
                if (currentBunny.Energy <= 0)
                {
                    orderderBunnies.Remove(currentBunny);
                    bunnies.Remove(currentBunny);
                    i--;
                }
            }
            if (eggToColor.IsDone())
            {
                return(String.Format(OutputMessages.EggIsDone, eggName));
            }
            else
            {
                //return OutputMessages.EggIsNotDone
                return(String.Format(OutputMessages.EggIsNotDone, eggName));
            }
        }
 internal DeclarePublisher(IBunny bunny, string publishTo)
 {
     _bunny       = bunny;
     _publishTo   = publishTo;
     _serialize   = Config.Serialize;
     _thisChannel = new PermanentChannel(bunny);
 }
        public void Color(IEgg egg, IBunny bunny)
        {
            while (bunny.Energy > 0 && bunny.Dyes.Any())
            {
                var dye = bunny.Dyes.FirstOrDefault(x => !x.IsFinished());

                while (!dye.IsFinished() && !egg.IsDone())
                {
                    bunny.Work();
                    dye.Use();
                    egg.GetColored();

                    if (bunny.Energy == 0)
                    {
                        break;
                    }
                }

                if (dye.IsFinished())
                {
                    bunny.Dyes.Remove(dye);
                }

                if (egg.IsDone())
                {
                    break;
                }
            }
        }
        public void Color(IEgg egg, IBunny bunny)
        {
            foreach (var dye in bunny.Dyes)
            {
                while (!dye.IsFinished())
                {
                    if (bunny.Energy == 0)
                    {
                        break;
                    }

                    egg.GetColored();
                    bunny.Work();
                    dye.Use();

                    if (egg.IsDone())
                    {
                        break;
                    }
                }

                if (egg.IsDone())
                {
                    break;
                }
            }
        }
Esempio n. 12
0
        public string AddBunny(string bunnyType, string bunnyName)
        {
            IBunny bunny   = null;
            string message = string.Empty;

            switch (bunnyType)
            {
            case "HappyBunny":
                bunny = new HappyBunny(bunnyName);
                bunnyRepository.Add(bunny);
                message = string.Format(OutputMessages.BunnyAdded, bunnyType, bunnyName);
                break;

            case "SleepyBunny":
                bunny = new SleepyBunny(bunnyName);
                bunnyRepository.Add(bunny);
                message = string.Format(OutputMessages.BunnyAdded, bunnyType, bunnyName);
                break;

            default:
                message = ExceptionMessages.InvalidBunnyType;
                break;
            }
            return(message);
        }
Esempio n. 13
0
 ///<summary>
 /// Interface for building Queues, Exchanges, Bindings and so on
 ///</summary>
 public static IDeclare Setup(this IBunny bunny)
 {
     return(new DeclareBase()
     {
          Bunny = bunny
     });
 }
Esempio n. 14
0
        public void Color(IEgg egg, IBunny bunny)
        {
            var dyes = bunny.Dyes;

            foreach (var dye in dyes)
            {
                while (!egg.IsDone())
                {
                    if (bunny.Energy > 0 && !dye.IsFinished())
                    {
                        dye.Use();
                        bunny.Work();
                        egg.GetColored();
                    }
                    else
                    {
                        break;
                    }
                }

                if (egg.IsDone() || bunny.Energy <= 0)
                {
                    break;
                }
            }
        }
Esempio n. 15
0
        public async Task GetReturnsOperationResultFailIfNoMessages()
        {
            IBunny bunny    = Bunny.ConnectSingle(ConnectSimple.BasicAmqp);
            var    opResult = await bunny.Consumer <ConsumeMessage>(get).AsAutoAck().GetAsync(carrot => Task.CompletedTask);

            Assert.NotNull(opResult);
            Assert.Equal(OperationState.GetFailed, opResult.State);
        }
        public void ConnectToSingleNodeWithConnectionPipe()
        {
            var pipe = Bunny.ConnectSingleWith();
            // not configuring anything uses default
            IBunny bunny = pipe.Connect();

            Assert.NotNull(bunny);
        }
Esempio n. 17
0
 ///<summary>
 /// Create a Consumer to subscribe to a Queue. If no queue is specified the Queue Name will be AssemblyName.TypeName
 ///</summary>
 public static IConsume <TMsg> Consumer <TMsg>(this IBunny bunny, string fromQueue = null)
 {
     if (fromQueue == null)
     {
         fromQueue = SerializeTypeName <TMsg>();
     }
     return(new DeclareConsumer <TMsg>(bunny, fromQueue));
 }
 public DeclareConsumer(IBunny bunny, string fromQueue)
 {
     _bunny            = bunny;
     _deserialize      = Config.Deserialize <TMsg>;
     _consumeFromQueue = fromQueue;
     _thisChannel      = new PermanentChannel(bunny);
     _receive          = async carrot => await carrot.SendAckAsync();
 }
Esempio n. 19
0
 internal DeclareRequest(IBunny bunny, string toExchange, string routingKey)
 {
     _bunny       = bunny;
     _toExchange  = toExchange;
     _routingKey  = routingKey;
     _serialize   = Config.Serialize;
     _deserialize = Config.Deserialize <TResponse>;
     _thisChannel = new PermanentChannel(_bunny);
 }
        public async Task PublisherSimplySendsWithoutQueueReturnsFailure()
        {
            IBunny bunny = Bunny.ConnectSingle(ConnectSimple.BasicAmqp);
            IPublish <TestMessage> publisher = bunny.Publisher <TestMessage>(Exchange);

            OperationResult <TestMessage> result = await publisher.SendAsync(new TestMessage());

            Assert.True(result.IsSuccess);
            bunny.Dispose();
        }
Esempio n. 21
0
 ///<summary>
 /// Create a Requester to send Rpc Requests. If no routingKey is specified the routingKey will be AssemblyName.TypeName
 ///</summary>
 public static IRequest <TRequest, TResponse> Request <TRequest, TResponse>(this IBunny bunny, string rpcExchange, string routingKey = null)
     where TRequest : class
     where TResponse : class
 {
     if (routingKey == null)
     {
         routingKey = SerializeTypeName <TRequest>();
     }
     return(new DeclareRequest <TRequest, TResponse>(bunny, rpcExchange, routingKey));
 }
        ///<summary>
        /// Enter Queue DeclarationMode
        ///</summary>
        public static IQueue Queue(this IDeclare declare, string name)
        {
            if ((declare is DeclareBase) == false)
            {
                throw DeclarationException.WrongType(typeof(DeclareBase), declare);
            }

            IBunny bunny = CheckGetBunny(declare, name, "queue");

            return(new DeclareQueue(bunny, name));
        }
Esempio n. 23
0
 ///<summary>
 /// Other side of the Rpc Call. Consumes fromQueue. If not Specified does consume from AssemblyName.TypeName
 ///</summary>
 public static IRespond <TRequest, TResponse> Respond <TRequest, TResponse>(this IBunny bunny, string rpcExchange
                                                                            , Func <TRequest, Task <TResponse> > respond, string fromQueue = null)
     where TRequest : class
     where TResponse : class
 {
     if (fromQueue == null)
     {
         fromQueue = SerializeTypeName <TRequest>();
     }
     return(new DeclareResponder <TRequest, TResponse>(bunny, rpcExchange, fromQueue, respond));
 }
Esempio n. 24
0
        private void SetupAndPublish(IBunny bunny, string queueName = queue)
        {
            var msg   = new ConsumeMessage();
            var bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(msg));

            var channel = bunny.Channel(newOne: true);
            var prop    = channel.CreateBasicProperties();

            prop.Persistent = true;

            channel.BasicPublish("", queueName, false, prop, bytes);
        }
Esempio n. 25
0
        public void Color(IEgg egg, IBunny bunny)   ///problemmm
        {
            while (egg.IsDone() == false && bunny.Energy > 0 && bunny.Dyes.Any(x => x.IsFinished() == false))
            {
                egg.GetColored();

                bunny.Work();
                IDye dye = bunny.Dyes.FirstOrDefault(x => x.IsFinished() == false);

                dye.Use();
            }
        }
Esempio n. 26
0
        public void Color(IEgg egg, IBunny bunny)
        {
            while (true)
            {
                if (bunny.Energy == 0)
                {
                    break;
                }

                if (bunny.Dyes.All(x => x.IsFinished()))
                {
                    break;
                }

                egg.GetColored();
                bunny.Work();

                while (bunny.Dyes.Any())
                {
                    if (bunny.Dyes.First().IsFinished() == false)
                    {
                        bunny.Dyes.First().Use();
                        break;
                    }

                    bunny.Dyes.Remove(bunny.Dyes.First());
                }
                if (egg.IsDone())
                {
                    break;
                }
            }

/*
 *          while (!egg.IsDone())
 *          {
 *              if (bunny.Energy == 0)
 *              {
 *                  break;
 *              }
 *
 *              if (bunny.Dyes.All(x => x.IsFinished()))
 *              {
 *                  break;
 *              }
 *
 *              egg.GetColored();
 *              bunny.Work(); ;
 *          }
 *
 */
        }
Esempio n. 27
0
        public string AddDyeToBunny(string bunnyName, int power)
        {
            IBunny currentBunny = bunnies.FindByName(bunnyName);

            if (currentBunny == null)
            {
                throw new InvalidOperationException(ExceptionMessages.InexistentBunny);
            }
            IDye currentDye = new Dye(power);

            currentBunny.AddDye(currentDye);
            return($"{String.Format(OutputMessages.DyeAdded, power, bunnyName)}");
        }
Esempio n. 28
0
        public string AddDyeToBunny(string bunnyName, int power)
        {
            IBunny bunny = bunnies.FindByName(bunnyName);

            if (bunny == null)
            {
                throw new InvalidOperationException("The bunny you want to add a dye to doesn't exist!");
            }

            bunny.AddDye(new Dye(power));

            return($"Successfully added dye with power {power} to bunny {bunnyName}!");
        }
Esempio n. 29
0
        public string AddDyeToBunny(string bunnyName, int power) //ok?
        {
            IBunny bunny = bunnies.FindByName(bunnyName);

            if (bunny == null)
            {
                throw new InvalidOperationException(string.Format(ExceptionMessages.InexistentBunny));
            }
            Dye dye = new Dye(power);

            bunny.AddDye(dye);
            return($"Successfully added dye with power {power} to bunny {bunnyName}!");
        }
Esempio n. 30
0
        public string AddDyeToBunny(string bunnyName, int power)
        {
            Dye    dye   = new Dye(power);
            IBunny bunny = bunnies.FindByName(bunnyName);

            if (bunny == null)
            {
                throw new InvalidOperationException(ExceptionMessages.InexistentBunny);
            }

            bunny.AddDye(dye);
            return(string.Format(OutputMessages.DyeAdded, power, bunnyName));
        }