Inheritance: Stopwatch
コード例 #1
0
        private static void GetSettings()
        {
            Console.WriteLine("\n-- {0} --", MethodBase.GetCurrentMethod().Name);
            using (IFileZillaApi fileZillaApi = new FileZillaApi(IPAddress.Parse(Ip), Port)
            {
                Log = DebugLog
            })
            {
                var stopWatch = Stopwatch2.StartNew();

                fileZillaApi.Connect(ServerPassword);
                var settings = fileZillaApi.GetSettings();
                Console.WriteLine("Settings retrieved in {0}.", stopWatch.GetDelta());
                int optionNumber = 0;
                foreach (var o in settings.Options)
                {
                    Console.WriteLine("  {0} = {1}",
                                      o.Label,
                                      o.OptionType == OptionType.Text
                            ? o.TextValue
                            : o.NumericValue.ToString()
                                      );
                    optionNumber++;
                }
            }
        }
コード例 #2
0
        private static void DeleteLotsOfUsersAndGroups()
        {
            Console.WriteLine("\n-- {0} --", MethodBase.GetCurrentMethod().Name);
            using (IFileZillaApi fileZillaApi = new FileZillaApi(IPAddress.Parse(Ip), Port)
            {
                Log = DebugLog
            })
            {
                var stopWatch = Stopwatch2.StartNew();

                fileZillaApi.Connect(ServerPassword);
                var serverState = fileZillaApi.GetServerState();
                Console.WriteLine("Connected in {0}. State is {1}", stopWatch.GetDelta(), serverState);

                var accountSettings = fileZillaApi.GetAccountSettings();
                Console.WriteLine("Account settings with {0} groups and {1} users fetched in {2}.",
                                  accountSettings.Groups.Count,
                                  accountSettings.Users.Count,
                                  stopWatch.GetDelta());

                accountSettings.Users.RemoveAll(x => x.UserName.StartsWith(UserName));
                accountSettings.Groups.RemoveAll(x => x.GroupName.StartsWith(GroupName));

                fileZillaApi.SetAccountSettings(accountSettings);
                Console.WriteLine("Finished saving account settings in {0}.", stopWatch.GetDelta());
            }
        }
コード例 #3
0
        private static void ReuseConnection()
        {
            Console.WriteLine("\n-- {0} --", MethodBase.GetCurrentMethod().Name);
            using (IFileZillaApi fileZillaApi = new FileZillaApi(IPAddress.Parse(Ip), Port)
            {
                Log = DebugLog
            })
            {
                var stopWatch = Stopwatch2.StartNew();

                fileZillaApi.Connect(ServerPassword);
                var serverState = fileZillaApi.GetServerState();
                Console.WriteLine("Connected in {0}. State is {1}", stopWatch.GetDelta(), serverState);

                // Get to _socket variable using reflection.
                var field  = typeof(SocketCommunication).GetField("_socket", BindingFlags.NonPublic | BindingFlags.Instance);
                var socket = (Socket)field.GetValue(fileZillaApi);
                // Disconnect socket to simulate that connection has been broken.
                socket.Disconnect(false);

                if (fileZillaApi.IsConnected)
                {
                    throw new ApplicationException("Hmm, it is supposed to be disconnected");
                }
                fileZillaApi.Connect(ServerPassword);
                serverState = fileZillaApi.GetServerState();
                Console.WriteLine("Connected in {0}. State is {1}", stopWatch.GetDelta(), serverState);
            }
        }
コード例 #4
0
        private static void ChangeExistingUser()
        {
            Console.WriteLine("\n-- {0} --", MethodBase.GetCurrentMethod().Name);
            using (IFileZillaApi fileZillaApi = new FileZillaApi(IPAddress.Parse(Ip), Port)
            {
                Log = DebugLog
            })
            {
                var stopWatch = Stopwatch2.StartNew();
                fileZillaApi.Connect(ServerPassword);

                var accountSettings = fileZillaApi.GetAccountSettings();
                Console.WriteLine("Account settings with {0} groups and {1} users fetched in {2}.",
                                  accountSettings.Groups.Count,
                                  accountSettings.Users.Count,
                                  stopWatch.GetDelta());

                // Find user using LINQ.
                var existingUser = accountSettings.Users.FirstOrDefault(x => x.UserName == ExampleUserName);

                // Did we find a user with the provided user name?
                if (existingUser != null)
                {
                    // Modify all aspects of the user.
                    existingUser.AssignPassword("NewPassword", fileZillaApi.ProtocolVersion);

                    // Save the changed user
                    Console.WriteLine("Save all settings including the modified user");
                    fileZillaApi.SetAccountSettings(accountSettings);
                    Console.WriteLine("Finished saving account settings in {0}.", stopWatch.GetDelta());
                }
            }
        }
コード例 #5
0
        public new static Stopwatch2 StartNew()
        {
            var stopwatch2 = new Stopwatch2();

            stopwatch2.Start();
            return(stopwatch2);
        }
コード例 #6
0
        private static void GetServerState()
        {
            Console.WriteLine("\n-- {0} --", MethodBase.GetCurrentMethod().Name);
            using (IFileZillaApi fileZillaApi = new FileZillaApi(IPAddress.Parse(Ip), Port)
            {
                Log = DebugLog
            })
            {
                var stopWatch = Stopwatch2.StartNew();

                fileZillaApi.Connect(ServerPassword);
                var serverState = fileZillaApi.GetServerState();
                Console.WriteLine("Connected in {0}. State is {1}", stopWatch.GetDelta(), serverState);
            }
        }
コード例 #7
0
        private static void CreateLotsOfUsersAndGroups()
        {
            Console.WriteLine("\n-- {0} --", MethodBase.GetCurrentMethod().Name);
            using (IFileZillaApi fileZillaApi = new FileZillaApi(IPAddress.Parse(Ip), Port)
            {
                Log = DebugLog
            })
            {
                var stopWatch = Stopwatch2.StartNew();

                fileZillaApi.Connect(ServerPassword);
                var serverState = fileZillaApi.GetServerState();
                Console.WriteLine("Connected in {0}. State is {1}", stopWatch.GetDelta(), serverState);
                var accountSettings = fileZillaApi.GetAccountSettings();
                Console.WriteLine("Account settings with {0} groups and {1} users fetched in {2}.",
                                  accountSettings.Groups.Count,
                                  accountSettings.Users.Count,
                                  stopWatch.GetDelta());

                accountSettings.Groups.RemoveAll(x => x.GroupName.StartsWith(GroupName));
                accountSettings.Users.RemoveAll(x => x.UserName.StartsWith(UserName));

                for (var i = 0; i < MaxGroups; i++)
                {
                    var group = new Group()
                    {
                        GroupName     = GroupName + i,
                        SharedFolders = new List <SharedFolder>()
                        {
                            new SharedFolder()
                            {
                                Directory    = @"C:\Group" + i + @"\Shared",
                                AccessRights = AccessRights.DirList | AccessRights.DirSubdirs | AccessRights.FileRead | AccessRights.IsHome
                            }
                        },
                    };

                    accountSettings.Groups.Add(group);
                }

                var maxUsers = fileZillaApi.ProtocolVersion < ProtocolVersions.User16M ? MaxUsers64K : MaxUsers16M;
                for (var i = 0; i < maxUsers; i++)
                {
                    var user = new User
                    {
                        GroupName     = GroupName + (i % MaxGroups), // Reference to group
                        UserName      = UserName + i,
                        SharedFolders = new List <SharedFolder>()
                        {
                            new SharedFolder()
                            {
                                Directory    = @"C:\User" + i + @"\Private",
                                AccessRights = AccessRights.DirList | AccessRights.DirSubdirs | AccessRights.FileRead | AccessRights.IsHome
                            }
                        },
                    };
                    user.AssignPassword("LonglongPasswordwithnumber" + i, fileZillaApi.ProtocolVersion);

                    accountSettings.Users.Add(user);
                }

                Console.WriteLine("Created {0} groups and {1} users in {2}.",
                                  MaxGroups,
                                  maxUsers,
                                  stopWatch.GetDelta());

                fileZillaApi.SetAccountSettings(accountSettings);
                Console.WriteLine("Finished saving account settings in {0}.", stopWatch.GetDelta());
            }
        }
コード例 #8
0
        private static void CreateUserAndGroup()
        {
            Console.WriteLine("\n-- {0} --", MethodBase.GetCurrentMethod().Name);
            using (IFileZillaApi fileZillaApi = new FileZillaApi(IPAddress.Parse(Ip), Port)
            {
                Log = DebugLog
            })
            {
                var stopWatch = Stopwatch2.StartNew();

                fileZillaApi.Connect(ServerPassword);
                var serverState = fileZillaApi.GetServerState();
                Console.WriteLine("Connected in {0}. State is {1}", stopWatch.GetDelta(), serverState);

                var accountSettings = fileZillaApi.GetAccountSettings();
                Console.WriteLine("Account settings with {0} groups and {1} users fetched in {2}.",
                                  accountSettings.Groups.Count,
                                  accountSettings.Users.Count,
                                  stopWatch.GetDelta());

                var group = new Group()
                {
                    GroupName     = GroupName,
                    SharedFolders = new List <SharedFolder>()
                    {
                        new SharedFolder()
                        {
                            Directory    = @"C:\Group\Shared",
                            AccessRights = AccessRights.DirList | AccessRights.DirSubdirs | AccessRights.FileRead | AccessRights.IsHome
                        },
                        new SharedFolder()
                        {
                            Directory    = @"C:\foo\bar",
                            AccessRights = AccessRights.DirList | AccessRights.DirSubdirs | AccessRights.FileRead
                        }
                    },
                    AllowedIPs = new List <string>()
                    {
                        "127.0.0.1", "10.10.10.10", "42.42.42.42", "::1"
                    },
                    DisallowedIPs = new List <string>()
                    {
                        "172.0.0.0"
                    },
                    ForceSsl        = true,
                    Comment         = "The quick brown fox jumps over the lazy dog",
                    BypassUserLimit = TriState.No,
                };
                accountSettings.Groups.RemoveAll(x => x.GroupName == GroupName);
                accountSettings.Groups.Add(@group);

                var user = new User
                {
                    GroupName     = GroupName, // Reference to group
                    UserName      = UserName,
                    SharedFolders = new List <SharedFolder>()
                    {
                        new SharedFolder()
                        {
                            Directory    = @"C:\UserX\Home",
                            AccessRights = AccessRights.DirList | AccessRights.DirSubdirs | AccessRights.FileRead | AccessRights.FileWrite | AccessRights.IsHome
                        },
                        new SharedFolder()
                        {
                            Directory    = @"C:\Shared\foo\bar",
                            AccessRights = AccessRights.DirList | AccessRights.DirSubdirs | AccessRights.FileRead
                        }
                    }
                };
                user.AssignPassword("test42", fileZillaApi.ProtocolVersion);
                accountSettings.Users.RemoveAll(x => x.UserName == UserName);
                accountSettings.Users.Add(user);

                Console.WriteLine("Created {0} groups and {1} users in {2}.",
                                  1,
                                  1,
                                  stopWatch.GetDelta());

                fileZillaApi.SetAccountSettings(accountSettings);
                Console.WriteLine("Finished saving account settings in {0}.", stopWatch.GetDelta());
            }
        }
コード例 #9
0
        private static void SetSettings()
        {
            Console.WriteLine("\n-- {0} --", MethodBase.GetCurrentMethod().Name);
            using (IFileZillaApi fileZillaApi = new FileZillaApi(IPAddress.Parse(Ip), Port)
            {
                Log = DebugLog
            })
            {
                var stopWatch = Stopwatch2.StartNew();

                fileZillaApi.Connect(ServerPassword);
                var settings = fileZillaApi.GetSettings();
                Console.WriteLine("Settings retrieved in {0}.", stopWatch.GetDelta());

                // Select option to modify
                var option = settings.GetOption(OptionId.WELCOMEMESSAGE);

                // Modify
                string       originalTextValue = option.TextValue;
                const string newMessage        = "Hello world";
                option.TextValue = newMessage;

                // Save
                if (!fileZillaApi.SetSettings(settings))
                {
                    throw new Exception("Uh uh");
                }
                var settings2 = fileZillaApi.GetSettings();

                // Verify
                if (settings.Options.Count() != settings2.Options.Count())
                {
                    throw new Exception("Uh uh");
                }
                for (int i = 0; i < settings.Options.Count(); i++)
                {
                    if (settings.Options[i].Label != settings2.Options[i].Label)
                    {
                        throw new Exception("Uh uh");
                    }
                    if (settings.Options[i].NotRemotelyChangeable != settings2.Options[i].NotRemotelyChangeable)
                    {
                        throw new Exception("Uh uh");
                    }
                    if (settings.Options[i].OptionType != settings2.Options[i].OptionType)
                    {
                        throw new Exception("Uh uh");
                    }
                    if (settings.Options[i].NumericValue != settings2.Options[i].NumericValue)
                    {
                        // Numeric value of "No Transfer Timeout" is bumped up to 600 by the server. Ignore that.
                        if (!(settings.Options[i].Label == "No Transfer Timeout" && settings.Options[i].NumericValue < 600 && settings2.Options[i].NumericValue == 600))
                        {
                            throw new Exception("Uh uh");
                        }
                    }

                    if (settings.Options[i].TextValue != settings2.Options[i].TextValue)
                    {
                        // Admin Password is sent as "*" when not set
                        if (!(settings.Options[i].Label == "Admin Password" && settings.Options[i].TextValue == "*" && settings2.Options[i].TextValue == null))
                        {
                            throw new Exception("Uh uh");
                        }
                    }
                }

                // Restore
                settings.GetOption(OptionId.WELCOMEMESSAGE).TextValue = originalTextValue;
                fileZillaApi.SetSettings(settings);
            }
        }
コード例 #10
0
 public new static Stopwatch2 StartNew()
 {
     var stopwatch2 = new Stopwatch2();
     stopwatch2.Start();
     return stopwatch2;
 }