Пример #1
0
        public IActionResult GetServiceInfo(string service, string nodePubKey = null, int node_count = 1)
        {
            //TODO: refactor this into a class so it becomes a thin controller.

            ConnectedNodeResponse        serviceNode;
            List <ConnectedNodeResponse> otherNodes;

            var connectResponse = xrouterService.xrConnect(service, node_count);
            var configReply     = xrouterService.xrShowConfigs();

            var connectReply = connectResponse.Reply;

            if (string.IsNullOrWhiteSpace(nodePubKey))
            {
                serviceNode = serviceNode = connectReply.OrderByDescending(n => n.Score).FirstOrDefault();
                // split node list
                otherNodes = connectReply.Where(s => s.NodePubKey != serviceNode.NodePubKey).ToList();
            }
            else
            {
                serviceNode = connectReply.Find(s => s.NodePubKey == nodePubKey);
                otherNodes  = connectReply.Where(s => s.NodePubKey != nodePubKey).ToList();
            }

            if (string.IsNullOrEmpty(serviceNode.NodePubKey))
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Service node SnodeKey is empty."));
            }

            var serviceNodeConfig = configReply.Find(c => c.NodePubKey == serviceNode.NodePubKey);

            var serviceNodeConfigElements = serviceNodeConfig.Config.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries)
                                            .Select(value => value.Split('='));

            string serviceNodeHost = null;

            if (serviceNodeConfigElements.Any(lc => lc[0] == "host"))
            {
                serviceNodeHost = serviceNodeConfigElements.FirstOrDefault(e => e[0] == "host")[1];
            }

            string serviceNodePort = null;

            if (serviceNodeConfigElements.Any(lc => lc[0] == "port"))
            {
                serviceNodePort = serviceNodeConfigElements.FirstOrDefault(e => e[0] == "port")[1];
            }


            var serviceName   = service.Replace("xrs::", "");
            var serviceConfig = serviceNode.Services[serviceName];

            var configReader = new ConfigReader(serviceNodeConfig.Config);

            var mainSectionSettings = configReader.EnumSection("Main");

            string xcloudConfig = string.Empty;

            if (!string.IsNullOrEmpty(serviceConfig.Parameters))
            {
                xcloudConfig += ("parameters=" + serviceConfig.Parameters + "\n");
            }

            var mainFee = configReader.GetSetting("Main", "fee");

            if (!string.IsNullOrEmpty(mainFee))
            {
                if (serviceConfig.Fee != double.Parse(mainFee))
                {
                    xcloudConfig += ("fee=" + serviceConfig.Fee.ToString(new NumberFormatInfo()
                    {
                        NumberDecimalSeparator = "."
                    }) + "\n");
                }
            }
            else
            {
                xcloudConfig += ("fee=" + serviceConfig.Fee.ToString(new NumberFormatInfo()
                {
                    NumberDecimalSeparator = "."
                } +"\n"));
            }

            if (serviceConfig.Disabled)
            {
                xcloudConfig += ("disabled=" + Convert.ToByte(serviceConfig.Disabled) + "\n");
            }

            var mainFetchLimit = configReader.GetSetting("Main", "fetchlimit");

            if (!string.IsNullOrEmpty(mainFetchLimit))
            {
                if (serviceConfig.FetchLimit != int.Parse(mainFetchLimit))
                {
                    xcloudConfig += ("fetchlimit=" + serviceConfig.FetchLimit + "\n");
                }
            }
            else
            {
                xcloudConfig += ("fetchlimit=" + serviceConfig.FetchLimit + "\n");
            }

            var mainClientRequestLimit = configReader.GetSetting("Main", "clientrequestlimit");

            if (!string.IsNullOrEmpty(mainClientRequestLimit))
            {
                if (serviceConfig.RequestLimit != int.Parse(mainClientRequestLimit))
                {
                    xcloudConfig += ("clientrequestlimit=" + serviceConfig.RequestLimit + "\n");
                }
            }
            else
            {
                xcloudConfig += ("clientrequestlimit=" + int.Parse(mainClientRequestLimit) + "\n");
            }

            string help        = string.Empty;
            string description = string.Empty;

            if (serviceNodeConfig?.Plugins.Count > 0)
            {
                // Try get help key from config string
                var configDictionary = serviceNodeConfig.Plugins[serviceName]
                                       .Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries)
                                       .Select(value => value.Split('=')).ToDictionary(l => l[0], l => string.Join("", l.Skip(1).ToArray()));

                if (configDictionary.ContainsKey("help"))
                {
                    help = configDictionary["help"];
                }

                if (configDictionary.ContainsKey("description"))
                {
                    description = configDictionary["description"];
                }
            }

            //TODO: Add AutoMapper to replace
            var viewModel = new XCloudServiceResultViewModel
            {
                Service = new XCloudServiceViewModel
                {
                    Help           = help,
                    Description    = description,
                    Disabled       = serviceConfig.Disabled,
                    Fee            = serviceConfig.Fee,
                    FetchLimit     = serviceConfig.FetchLimit,
                    Parameters     = serviceConfig.Parameters,
                    ParametersList = serviceConfig.Parameters != string.Empty ? serviceConfig.Parameters.Split(',').ToList() : null,
                    PaymentAddress = serviceConfig.PaymentAddress,
                    RequestLimit   = serviceConfig.RequestLimit,
                    Config         = xcloudConfig
                },
                Node = new NodeInfoViewModel
                {
                    Type           = (serviceNodePort == "41412" || string.IsNullOrEmpty(serviceNodePort)) ? "Regular" : "Enterprise",
                    Host           = serviceNodeHost,
                    Port           = serviceNodePort,
                    Banned         = serviceNode.Banned,
                    NodePubKey     = serviceNode.NodePubKey,
                    PaymentAddress = serviceNode.PaymentAddress,
                    Score          = serviceNode.Score
                },
                OtherNodes = otherNodes.Select(n => new NodeInfoViewModel {
                    Banned         = n.Banned,
                    NodePubKey     = n.NodePubKey,
                    PaymentAddress = n.PaymentAddress,
                    Score          = n.Score
                }).ToList()
            };

            return(Ok(viewModel));
        }