예제 #1
0
        /// <summary>
        ///   Starts the services.
        /// </summary>
        /// <returns>
        ///   A task that represents the asynchronous operation.
        /// </returns>
        /// <remarks>
        ///   Starts the various IPFS and Peer2Peer services.  This should
        ///   be called after any configuration changes.
        /// </remarks>
        /// <exception cref="Exception">
        ///   When the engine is already started.
        /// </exception>
        public async Task StartAsync()
        {
            if (stopTasks.Count > 0)
            {
                throw new Exception("Already started");
            }

            var tasks = new List <Task>
            {
                new Task(async() =>
                {
                    var bootstrap = new Peer2Peer.Discovery.Bootstrap
                    {
                        Addresses = await this.Bootstrap.ListAsync()
                    };
                    bootstrap.PeerDiscovered += async(s, e) =>
                    {
                        await SwarmService.RegisterPeerAsync(e.Address);
                    };
                    await bootstrap.StartAsync();
                    stopTasks.Add(new Task(async() => await bootstrap.StopAsync()));
                }),
                new Task(async() =>
                {
                    await SwarmService.StartAsync();
                    stopTasks.Add(new Task(async() => await SwarmService.StopAsync()));
                })
            };

            foreach (var task in tasks)
            {
                task.Start();
            }
            await Task.WhenAll(tasks);
        }