public ListenerTester(ServerTester tester, bool nodeOnly) { _Network = tester.Configuration.Indexer.Network; Random rand = new Random(); _Server = tester; _Server._disposables.Add(this); _Listener = new QBitNinjaNodeListener(_Server.Configuration); _NodeServer = new NodeServer(_Server.Configuration.Indexer.Network, internalPort: rand.Next(20000, 50000)); _NodeListener = new EventLoopMessageListener <IncomingMessage>(NewNodeMessage); _NodeServer.ExternalEndpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), _NodeServer.LocalEndpoint.Port); _NodeServer.AllowLocalPeers = true; _NodeServer.InboundNodeConnectionParameters.IsRelay = true; _NodeServer.AllMessages.AddMessageListener(_NodeListener); _NodeServer.Listen(); _Listener.Configuration.Indexer.Node = "127.0.0.1:" + _NodeServer.LocalEndpoint.Port; if (!nodeOnly) { _Listener.Listen(); } _Server.Configuration.Indexer.CreateIndexer().Index(_Server.Configuration.Indexer.Network.GetGenesis()); _Server.ChainBuilder.SkipIndexer = true; _Server.ChainBuilder.NewBlock += ChainBuilder_NewBlock; _Server.ChainBuilder.NewTransaction += ChainBuilder_NewTransaction; var genesis = _Server.Configuration.Indexer.Network.GetGenesis(); _Blocks.AddOrReplace(genesis.Header.GetHash(), genesis); }
static void Main(string[] args) { var options = new ListenerOptions(); if (args.Length == 0) { System.Console.WriteLine(options.GetUsage()); } if (Parser.Default.ParseArguments(args, options)) { if (options.Configuration != null) { if (!File.Exists(options.Configuration)) { System.Console.WriteLine("File " + new FileInfo(options.Configuration).FullName + " not found"); return; } AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", new FileInfo(options.Configuration).FullName); } var conf = QBitNinjaConfiguration.FromConfiguration(); conf.EnsureSetup(); if (options.CancelInit) { var indexer = new InitialIndexer(conf); indexer.Cancel(); } if (options.Init) { var indexer = new InitialIndexer(conf); indexer.Run(); } List <IDisposable> dispo = new List <IDisposable>(); List <Task> running = new List <Task>(); try { if (options.Listen) { QBitNinjaNodeListener listener = new QBitNinjaNodeListener(conf); dispo.Add(listener); listener.Listen(); running.Add(listener.Running); } if (options.Web) { System.Console.WriteLine("Trying to listen on http://*:" + options.Port + "/"); var server = WebApp.Start("http://*:" + options.Port, appBuilder => { var config = new HttpConfiguration(); var qbit = QBitNinjaConfiguration.FromConfiguration(); qbit.EnsureSetup(); WebApiConfig.Register(config, qbit); UpdateChainListener listener = new UpdateChainListener(); dispo.Add(listener); listener.Listen(config); appBuilder.UseWebApi(config); running.Add(new TaskCompletionSource <int>().Task); }); dispo.Add(server); System.Console.WriteLine("Server started"); Process.Start("http://localhost:" + options.Port + "/blocks/tip"); } if (running.Count != 0) { try { running.Add(WaitInput()); Task.WaitAny(running.ToArray()); } catch (AggregateException aex) { ExceptionDispatchInfo.Capture(aex.InnerException).Throw(); throw; } } } finally { foreach (var d in dispo) { d.Dispose(); } } } }