示例#1
0
        static void Main(string[] args)
        {
            IConfiguration config = new ConfigurationBuilder()
                                    .AddJsonFile("appsettings.json", true, false)
                                    .AddJsonFile("appsettings.dev.json", true, true)
                                    .Build();

            string signerHexStr = config["signer"];

            if (string.IsNullOrWhiteSpace(signerHexStr))
            {
                Console.WriteLine("Signer is not configured");
                return;
            }

            string ip = config["bindIP"];

            if (string.IsNullOrWhiteSpace(ip))
            {
                Console.WriteLine("bindIP is not set.. defaulting to 127.0.0.1 local connection only");
                ip = "127.0.0.1";
            }

            using (var socket = new ResponseSocket())
            {
                socket.Bind($"tcp://{ip}:55555");

                while (true)
                {
                    Console.WriteLine("running");
                    string requestString = socket.ReceiveFrameString();

                    string[] command = requestString.Split();
                    string   response;

                    if (command.Length < 2)
                    {
                        response = "poor";
                        Console.WriteLine(string.Empty);
                    }
                    else
                    {
                        var loader = new Loader <ICCGatewayPlugin>();
                        var msgs   = new List <string>();

                        string root   = Directory.GetCurrentDirectory();
                        string folder = TxBuilder.GetPluginsFolder(root);
                        if (folder == null)
                        {
                            response = "fail";
                        }
                        else
                        {
                            loader.Load(folder, msgs);
                            foreach (var msg in msgs)
                            {
                                Console.WriteLine(msg);
                            }

                            string action = command[0];
                            command = command.Skip(1).ToArray();

                            ICCGatewayPlugin plugin = loader.Get(action);
                            var pluginConfig        = config.GetSection(action);
                            if (plugin == null)
                            {
                                response = "miss";
                            }
                            else
                            {
                                string msg;
                                bool   done = plugin.Run(pluginConfig, signerHexStr, command, out msg);
                                if (done)
                                {
                                    if (msg == null)
                                    {
                                        msg = "Success!";
                                    }
                                    response = "good";
                                }
                                else
                                {
                                    response = "fail";
                                }
                            }
                        }
                    }

                    Console.WriteLine(response);
                    socket.SendFrame(response);
                }
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            IConfiguration config = new ConfigurationBuilder()
                                    .AddJsonFile("appsettings.json", true, false)
                                    .AddJsonFile("appsettings.dev.json", true, true)
                                    .Build();

            string root   = Directory.GetCurrentDirectory();
            string folder = TxBuilder.GetPluginsFolder(root);

            if (folder == null)
            {
                Console.WriteLine("Failed to locate plugin folder");
                return;
            }

            string ip = config["bindIP"];

            if (string.IsNullOrWhiteSpace(ip))
            {
                Console.WriteLine("bindIP is not set.. defaulting to 127.0.0.1 local connection only");
                ip = "127.0.0.1";
            }

            using (var socket = new ResponseSocket())
            {
                socket.Bind($"tcp://{ip}:55555");

                while (true)
                {
                    string response;
                    string requestString = null;
                    try
                    {
                        requestString = socket.ReceiveFrameString();

                        string[] command = requestString.Split();

                        if (command.Length < 2)
                        {
                            response = "poor";
                            Console.WriteLine(requestString + ": not enough parameters");
                        }
                        else
                        {
                            var loader = new Loader <ICCGatewayPlugin>();
                            var msgs   = new List <string>();

                            loader.Load(folder, msgs);
                            foreach (var msg in msgs)
                            {
                                Console.WriteLine(msg);
                            }

                            string action = command[0];
                            command = command.Skip(1).ToArray();

                            ICCGatewayPlugin plugin = loader.Get(action);
                            var pluginConfig        = config.GetSection(action);
                            if (plugin == null)
                            {
                                response = "miss";
                            }
                            else
                            {
                                string msg;
                                bool   done = plugin.Run(pluginConfig, command, out msg);
                                if (done)
                                {
                                    Debug.Assert(msg == null);
                                    response = "good";
                                }
                                else
                                {
                                    Debug.Assert(msg != null);
                                    StringBuilder err = new StringBuilder();
                                    err.Append(requestString).Append(": ").Append(msg);
                                    Console.WriteLine(err.ToString());
                                    response = "fail";
                                }
                            }
                        }
                    }
                    catch (Exception x)
                    {
                        StringBuilder err = new StringBuilder();
                        if (requestString != null)
                        {
                            err.Append(requestString).Append(": ");
                        }
                        err.Append(x.Message);
                        Console.WriteLine(err.ToString());
                        response = "fail";
                    }
                    socket.SendFrame(response);
                }
            }
        }