Ejemplo n.º 1
0
        public static RequestResponse SendRequest(
            MicroserviceTypes type, string data, int[] idsToSend = null
            )
        {
            int servicesLimit = 5;

            var sendToAll = idsToSend == null;

            idsToSend = idsToSend ?? new int[servicesLimit]; // TODO: user idsToSend

            var servicesToSend = new List <MicroServiceNode>();

            if (sendToAll)
            {
                foreach (var service in get().GetServices(type))
                {
                    if (service.Value != null)
                    {
                        servicesToSend.Add(service.Value);
                    }
                }
            }
            var requestsKeys = new int[servicesLimit];

            for (int i = 0; i < servicesToSend.Count; i++)
            {
                if (servicesToSend[i].Client != null)
                {
                    requestsKeys[i] = servicesToSend[i].Client.SendMessage(data);
                }
            }

            Thread.Sleep(50);

            int waitIterations = 20;

            for (int i = 0; i < waitIterations; i++)
            {
                foreach (int key in requestsKeys)
                {
                    if (key <= 0 || WebsocketRequests.Responses[key] == null)
                    {
                        continue;
                    }

                    var response = RequestResponse.BuildFromString(WebsocketRequests.Responses[key]);

                    WebsocketRequests.Responses[key] = null;

                    if (response != null)
                    {
                        return(response);
                    }
                }
                Thread.Sleep(50);
            }

            return(null);
        }