Esempio n. 1
0
        public HttpResponseMessage Post([FromBody] AdditionRequest request)
        {
            var    jsonFormatter = new JsonMediaTypeFormatter();
            string trackingId    = null;

            if (Request.Headers.Contains("X-Evi-Tracking-Id"))
            {
                trackingId = Request.Headers.GetValues("X-Evi-Tracking-Id").First();
            }

            try
            {
                var response = new HttpResponseDto <AdditionResponse>
                {
                    Status  = HttpStatusCode.OK.ToString(),
                    Code    = (int)HttpStatusCode.OK,
                    Message = repository.Add(request.Addends, trackingId)
                };

                // In case of success, return a Json with the corresponding calculation total
                return(Request.CreateResponse(HttpStatusCode.OK, response, jsonFormatter));
            }
            catch (Exception ex)
            {
                var response = new HttpResponseDto <string>
                {
                    Status  = HttpStatusCode.BadRequest.ToString(),
                    Code    = (int)HttpStatusCode.BadRequest,
                    Message = ex.Message
                };

                // In case of error, return a Json with the corresponding message to be shown
                return(Request.CreateResponse(HttpStatusCode.BadRequest, response, jsonFormatter));
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Implementation of addition method.
        /// </summary>
        /// <param name="additionRequest"></param>
        /// <param name="logAdd"></param>
        /// <returns></returns>
        public static AdditionResult Add(AdditionRequest additionRequest, string logAdd)
        {
            try
            {
                AdditionResult additionResult = new AdditionResult();
                int            counter        = 0;
                string         calculation    = "";

                foreach (int number in additionRequest.Addends)
                {
                    counter            += 1;
                    additionResult.Sum += number;
                    calculation        += number.ToString() + (counter == additionRequest.Addends.Length ? "" : "+");
                }

                if (int.Parse(logAdd) != 0)
                {
                    SaveLog(int.Parse(logAdd), "Suma ", calculation, additionResult.Sum);
                }


                return(additionResult);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public AdditionResult Add([FromBody] AdditionRequest AdditionRequest)
        {
            var    headers     = this.Request.Headers.ToList();
            string trackingAdd = headers[headers.Count - 1].Value.First().ToString();

            return(CalculatorBusiness.Add(AdditionRequest, trackingAdd));
        }
 public override Task <AdditionResponse> Add(AdditionRequest request, ServerCallContext context)
 {
     return(Task.FromResult(new AdditionResponse
     {
         Result = request.Op1 + request.Op2
     }));
 }
        /// <summary>
        /// Method that connects to the Web API service to perform the addition operation.
        /// </summary>
        /// <param name="values"></param>
        /// <param name="logAdd"></param>
        public static void Addition(AdditionRequest values, string logAdd)
        {
            try
            {
                var clientRest  = new RestClient(urlWebAPI);
                var requestRest = new RestRequest("Add", Method.POST);

                if (logAdd == "")
                {
                    logAdd = "0";
                }

                requestRest.AddHeader("x-evi-tracking-id", logAdd);

                requestRest.AddParameter("application/json", new JavaScriptSerializer().Serialize(values), ParameterType.RequestBody);
                IRestResponse responseRest = clientRest.Execute(requestRest);
                Console.WriteLine();

                if (responseRest.StatusCode == HttpStatusCode.OK)
                {
                    AdditionResult resultAdd = (new JavaScriptSerializer()).Deserialize <AdditionResult>(responseRest.Content);
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("\n\nRESULTADO, SUMATORIA: " + resultAdd.Sum + ". " + (logAdd != "0" ? " La operacion fue registrada en el log." : ""));
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Error al realizar la operación: " + responseRest.ErrorMessage);
                }

                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("Operación finalizada.");
                Console.WriteLine();
            }
            catch (Exception)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("\n\nError al conectarse con el servicio que realiza la operacion de suma. ");
                throw;
            }
        }
Esempio n. 6
0
    private static async Task <bool> CreateAdditions(INode myNode, DiscoveryRequest request)
    {
        bool propagationAllowed = false;

        //Additions
        foreach (uint?node in myNode.CommsModule.Discover().Except(myNode.Router.NetworkMap.Entries.Select(entry => entry.ID)))
        {
            if (myNode.Router.NetworkMap.GetEntryByID(node) == null)
            {
                AdditionRequest additionRequest = new AdditionRequest()
                {
                    SourceID         = myNode.Id,
                    DestinationID    = node,
                    Command          = Request.Commands.ADDITION,
                    AckExpected      = false,
                    ResponseExpected = true,
                    plan             = myNode.ActivePlan
                };

                NodeAdditionResponse response = await myNode.CommsModule.SendAsync(additionRequest.DestinationID, additionRequest, Constants.COMMS_TIMEOUT, 3) as NodeAdditionResponse;

                Vector3      position       = response.Position;
                List <uint?> nodeNeighbours = response.Neighbours;

                NetworkMapEntry neigbourEntry = new NetworkMapEntry(node, nodeNeighbours, position);
                NetworkMapEntry ent           = new NetworkMapEntry(neigbourEntry.ID, neigbourEntry.Neighbours, neigbourEntry.Position);

                myNode.Router.NetworkMap.GetEntryByID(myNode.Id).Neighbours.Add(node);
                myNode.Router.NetworkMap.Entries.Add(ent);

                request.Alterations.Add(new NetworkMapAddition(ent));
                request.SourceID = myNode.Id;
                request.SenderID = myNode.Id;

                propagationAllowed = true;
            }
        }

        return(propagationAllowed);
    }
Esempio n. 7
0
        /// <summary>
        /// Group the addition operations.
        /// </summary>
        static public void AdditionMethod()
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("\nSUMA ");
            Console.ForegroundColor = ConsoleColor.White;

            bool valor;

            string[] numbersAdd;
            do
            {
                valor = true;
                Console.WriteLine("Ingrese los números que intervienen en la sumatoria separados por espacio.");
                Console.ForegroundColor = ConsoleColor.DarkCyan;
                Console.WriteLine("Ejemplo 9 5 4 7 7.\n");
                Console.ForegroundColor = ConsoleColor.White;
                numbersAdd = Console.ReadLine().ToString().Trim().Split(' ');

                foreach (var item in numbersAdd)
                {
                    if (ValidarNumeroCadena(item.ToString()) == false)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("\n\nLos números ingresados deben ser enteros y separados por espacio:\n");
                        Console.ForegroundColor = ConsoleColor.White;
                        valor = false;
                        break;
                    }
                }
            } while (valor == false);

            string          logAdd     = ValidarNumeroEspacio("\nIngrese el identificador del Log OPCIONAL:");
            AdditionRequest addRequest = new AdditionRequest();

            addRequest.Addends = Array.ConvertAll(numbersAdd, s => int.Parse(s));
            ManagementCalculator.Addition(addRequest, logAdd);
        }
Esempio n. 8
0
 public AdditionRequest(AdditionRequest other) : base(other)
 {
     plan = other.plan;
 }
Esempio n. 9
0
 public static AdditionResult Add(AdditionRequest additionRequest, string trackingID)
 {
     return(CalculatorDataAccess.Add(additionRequest, trackingID));
 }
Esempio n. 10
0
    private async void ExecuteRequest(Request request, bool isDependency)
    {
        switch (request.Command)
        {
        case Request.Commands.GENERATE:
            PlanGenerator.GeneratePlan(this, request as PlanRequest);
            break;

        case Request.Commands.EXECUTE:
            PlanExecuter.ExecutePlan(this, request as PlanRequest);
            break;

        case Request.Commands.HEARTBEAT:
            Heartbeat.RespondToHeartbeat(this, request);
            break;

        case Request.Commands.PING:
            Ping.RespondToPing(this, request);
            break;

        case Request.Commands.DETECTFAILURE:
            FailureDetection.DetectFailure(this, request as DetectFailureRequest);
            break;

        case Request.Commands.DISCOVER:

            await Discovery.DiscoverAsync(this, request as DiscoveryRequest);

            break;

        case Request.Commands.POSITION:
            PositionResponse response = new PositionResponse(Id, request.SourceID, Response.ResponseCodes.OK, request.MessageIdentifer, Position);
            CommsModule.Send(request.SourceID, response);
            break;

        case Request.Commands.ADDITION:

            List <uint?>    neighbours = CommsModule.Discover();
            AdditionRequest addition   = (request as AdditionRequest).DeepCopy();

            List <ConstellationPlanField> fields = new List <ConstellationPlanField> {
                new ConstellationPlanField("DeltaV", 0, (x, y) => x.CompareTo(y))
            };
            addition.plan.Entries.Add(new ConstellationPlanEntry(Id, Position, fields, (x, y) => 1));
            ActivePlan = addition.plan;
            Router.UpdateNetworkMap(addition.plan);

            NodeAdditionResponse additionResponse = new NodeAdditionResponse(Id, request.SourceID, Response.ResponseCodes.OK, request.MessageIdentifer, Position, neighbours);
            CommsModule.Send(request.SourceID, additionResponse);
            break;

        case Request.Commands.UPDATENETWORKMAP:
            NetworkUpdateRequest updateRequest = request as NetworkUpdateRequest;

            //Remove any dead nodes from the networkmap
            Router.NetworkMap.Entries.RemoveAll(entry => updateRequest.DeadNodes.Contains(entry.ID));

            //Remove any dead nodes from neighbour lists
            foreach (NetworkMapEntry entry in Router.NetworkMap.Entries)
            {
                foreach (uint?deadNode in updateRequest.DeadNodes)
                {
                    entry.Neighbours.Remove(deadNode);
                }
            }
            break;

        default:
            throw new NotImplementedException(request.Command.ToString() + " was not implemented.");
        }
    }