Exemple #1
0
        private static async Task <MethodResponse> ToggleConnectivity(MethodRequest methodRequest, object userContext)
        {
            Log.LogInformation("Direct method toggleConnectivity has been invoked.");
            (string networkInterfaceName, CancellationToken token) = (Tuple <string, CancellationToken>)userContext;

            // true for network on (restriction disabled), false for network off (restriction enabled)
            if (!bool.TryParse(JObject.Parse(methodRequest.DataAsJson)["networkOnValue"].ToString(), out bool networkOnValue))
            {
                throw new ArgumentException($"Unable to parse methodRequest. JsonData: {methodRequest.DataAsJson}");
            }

            Log.LogInformation($"Toggling network {networkInterfaceName} {(networkOnValue ? "on" : "off")}");
            INetworkStatusReporter reporter = new NetworkStatusReporter(Settings.Current.TestResultCoordinatorEndpoint, Settings.Current.ModuleId, Settings.Current.TrackingId);
            NetworkProfileSetting  customNetworkProfileSetting = Settings.Current.NetworkRunProfile.ProfileSetting;

            customNetworkProfileSetting.PackageLoss = 100;
            var controller = new OfflineController(networkInterfaceName, Settings.Current.IotHubHostname, customNetworkProfileSetting);
            NetworkControllerStatus networkControllerStatus = networkOnValue ? NetworkControllerStatus.Disabled : NetworkControllerStatus.Enabled;

            await SetNetworkControllerStatus(controller, networkControllerStatus, reporter, token);

            return(new MethodResponse((int)HttpStatusCode.OK));
        }
Exemple #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}");

            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));
        }
Exemple #3
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));
        }