示例#1
0
        static async Task Main()
        {
            (CancellationTokenSource cts, ManualResetEventSlim completed, Option <object> handler) = ShutdownHandler.Init(TimeSpan.FromSeconds(5), Log);

            Log.LogInformation($"Starting with {Settings.Current.NetworkRunProfile.ProfileType} Settings: {Settings.Current.NetworkRunProfile.ProfileSetting}");

            var controllers = new List <INetworkController>();

            try
            {
                var networkInterfaceName = DockerHelper.GetDockerInterfaceName();

                if (networkInterfaceName.HasValue)
                {
                    await networkInterfaceName.ForEachAsync(async name =>
                    {
                        string hubHostname = !string.IsNullOrEmpty(Settings.Current.ParentHostname) ? Settings.Current.ParentHostname : Settings.Current.IotHubHostname;
                        var offline        = new OfflineController(name, hubHostname, Settings.Current.NetworkRunProfile.ProfileSetting);
                        var satellite      = new SatelliteController(name, hubHostname, Settings.Current.NetworkRunProfile.ProfileSetting);
                        var cellular       = new CellularController(name, hubHostname, Settings.Current.NetworkRunProfile.ProfileSetting);
                        controllers.AddRange(new List <INetworkController> {
                            offline, satellite, cellular
                        });

                        // Reset network status before start delay to ensure network status is in designed state before test starts.
                        var sw = new Stopwatch();
                        sw.Start();
                        await RemoveAllControllingRules(controllers, cts.Token);
                        sw.Stop();
                        TimeSpan durationBeforeTestStart = Settings.Current.StartAfter <= sw.Elapsed ? TimeSpan.Zero : Settings.Current.StartAfter - sw.Elapsed;

                        Log.LogInformation($"Delay {durationBeforeTestStart} before starting network controller.");
                        await Task.Delay(durationBeforeTestStart, cts.Token);

                        switch (Settings.Current.NetworkRunProfile.ProfileType)
                        {
                        case NetworkControllerType.Offline:
                            await StartAsync(offline, cts.Token);
                            break;

                        case NetworkControllerType.Satellite:
                            await StartAsync(satellite, cts.Token);
                            break;

                        case NetworkControllerType.Cellular:
                            await StartAsync(cellular, cts.Token);
                            break;

                        case NetworkControllerType.Online:
                            await SetToggleConnectivityMethod(name, cts.Token);
                            Log.LogInformation($"No restrictions to be set, running as online");
                            break;

                        default:
                            throw new NotSupportedException($"Network type {Settings.Current.NetworkRunProfile.ProfileType} is not supported.");
                        }
                    });
                }
                else
                {
                    Log.LogError($"No network interface found for docker network {Settings.Current.NetworkId}");
                }
            }
            catch (Exception ex)
            {
                Log.LogError(ex, $"Unexpected exception thrown from {nameof(Main)} method");
            }

            await cts.Token.WhenCanceled();

            await CleanupControllingRulesOnShutdown(controllers, CancellationToken.None);

            completed.Set();
            handler.ForEach(h => GC.KeepAlive(h));
        }
示例#2
0
        static async Task Main()
        {
            (CancellationTokenSource cts, ManualResetEventSlim completed, Option <object> handler) = ShutdownHandler.Init(TimeSpan.FromSeconds(5), Log);

            Log.LogInformation($"Starting with {Settings.Current.NetworkRunProfile.ProfileType} Settings: {Settings.Current.NetworkRunProfile.ProfileSetting}");

            try
            {
                var networkInterfaceName = DockerHelper.GetDockerInterfaceName();

                if (networkInterfaceName.HasValue)
                {
                    await networkInterfaceName.ForEachAsync(async name =>
                    {
                        var offline     = new OfflineController(name, Settings.Current.IotHubHostname, Settings.Current.NetworkRunProfile.ProfileSetting);
                        var satellite   = new SatelliteController(name, Settings.Current.IotHubHostname, Settings.Current.NetworkRunProfile.ProfileSetting);
                        var cellular    = new CellularController(name, Settings.Current.IotHubHostname, Settings.Current.NetworkRunProfile.ProfileSetting);
                        var controllers = new List <INetworkController>()
                        {
                            offline, satellite, cellular
                        };
                        await RemoveAllControllingRules(controllers, cts.Token);

                        switch (Settings.Current.NetworkRunProfile.ProfileType)
                        {
                        case NetworkControllerType.Offline:
                            await StartAsync(offline, cts.Token);
                            break;

                        case NetworkControllerType.Satellite:
                            await StartAsync(satellite, cts.Token);
                            break;

                        case NetworkControllerType.Cellular:
                            await StartAsync(cellular, cts.Token);
                            break;

                        case NetworkControllerType.Online:
                            Log.LogInformation($"No restrictions to be set, running as online");
                            break;

                        default:
                            throw new NotSupportedException($"Network type {Settings.Current.NetworkRunProfile.ProfileType} is not supported.");
                        }
                    });
                }
                else
                {
                    Log.LogError($"No network interface found for docker network {Settings.Current.NetworkId}");
                }
            }
            catch (Exception ex)
            {
                Log.LogError(ex, $"Unexpected exception thrown from {nameof(Main)} method");
            }

            await cts.Token.WhenCanceled();

            completed.Set();
            handler.ForEach(h => GC.KeepAlive(h));
        }