Example #1
0
        public Client(string remoteHost, int remotePort, App.ILogger logger)
        {
            this.remoteHost = remoteHost;
            this.remotePort = remotePort;
            this.logger     = logger;

            _client  = new TcpClient();
            _context = new Context(_client, string.Concat(remoteHost, ":", remotePort), isOutbound: false);
            _context.OnMessageReceived += (peerCtx, msg) => { OnMessageReceived?.Invoke(peerCtx, msg); };
        }
Example #2
0
        public Daemon(Mutex chainMutex, ChainManager chainManager, App.ILogger logger, App.IInventoryNotifier inventoryNotifier, CancellationToken cancellationToken)
        {
            this.chainMutex        = chainMutex;
            this.chainManager      = chainManager;
            this.logger            = logger;
            this.inventoryNotifier = inventoryNotifier;
            this.cancellationToken = cancellationToken;

            nodeNonce = createNodeNonce();

            peers = new List <Context>();

            ibd = new InitialBlockDownload();

            houseKeepingLock = new object();

            runHouseKeeping();
        }
Example #3
0
 public Server(int port, App.ILogger logger)
 {
     this.port     = port;
     this.listener = new TcpListener(IPAddress.Any, port);
     this.logger   = logger;
 }
Example #4
0
        private static void runMainLoop(CLOptions options)
        {
            switch (options.LogMode)
            {
            case "console":
                _logger = App.ConsoleLogger;
                break;

            case "both":
                _logger = App.FileAndConsoleLogger;
                break;

            case "file":
            default:
                _logger         = App.FileLogger;
                options.LogMode = LogMode.File;
                break;
            }

            _app = new App(App.DefaultPathsProvider, _logger);

            _options = options;

            if (options.ListeningPort > 0)
            {
                _app.Listen(options.ListeningPort);
            }

            _app.ConnectToPeers();

            if (_app.IsInIbd)
            {
                ensureLogToConsole(App.LogLevel.Info, "Initial block download is running...");

                Console.Out.Flush();

                while (_app.IsInIbd)
                {
                    Thread.Sleep(100);
                }

                if (_app.Daemon.CurrentIbdPhase == InitialBlockDownload.Phase.Succeeded)
                {
                    ensureLogToConsole(App.LogLevel.Info, "Initial block download is finished.");
                    ensureLogToConsole(App.LogLevel.Info, $"Current height: {_app.ChainManager.Height}");
                }
                else
                {
                    ensureLogToConsole(App.LogLevel.Info, "Initial block download failed. Exitting...");

                    return;
                }
            }

            Console.CancelKeyPress += onSigInt;

            var token = _app.CancellationTokenSource.Token;

            if (options.IsNode)
            {
                while (!token.IsCancellationRequested)
                {
                    Thread.Sleep(500);
                }
            }
            else
            {
                Console.WriteLine("Welcome to ameow-cli. Enter `help` to show help message.");
                Console.WriteLine();

                var inputSeparator = new string[] { " " };
                while (!token.IsCancellationRequested)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.Write(">>> ");
                    string input = Console.ReadLine();
                    Console.ForegroundColor = ConsoleColor.Gray;

                    if (input == null)
                    {
                        Thread.Sleep(100);
                        if (token.IsCancellationRequested)
                        {
                            break;
                        }
                    }

                    string[] args = input.Split(inputSeparator, StringSplitOptions.RemoveEmptyEntries);
                    if (args.Length == 0)
                    {
                        continue;
                    }

                    string  cmdName = args[0];
                    Command cmd     = commands.Find(c => c.Name == cmdName);
                    if (cmd != null)
                    {
                        cmd.Handler(args);
                    }
                    else
                    {
                        cmd_help(args);
                    }
                }
            }

            try
            {
                _app.Shutdown();
            }
            catch (Exception ex)
            {
                _app.Logger.Log(App.LogLevel.Error, string.Format("Cannot shutdown properly: {0}", ex.Message));
            }
        }