From 8929dd83447ab6f0efcf72e0461d8cd011c2773e Mon Sep 17 00:00:00 2001 From: Thibault D'Archivio Date: Mon, 1 Jun 2015 17:24:08 +0200 Subject: [PATCH 1/5] Fork --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 4ceb1eb..3b3a078 100644 --- a/README.md +++ b/README.md @@ -2,5 +2,6 @@ [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/rhwy/cleancode-webget-tool?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) +Cloned by Teybeo English readme not available at this time, it's only available in [french](readme.fr.md) From e805c881327f6a9d0238146517d19baf7c8d459a Mon Sep 17 00:00:00 2001 From: Thibault D'Archivio Date: Mon, 1 Jun 2015 18:56:15 +0200 Subject: [PATCH 2/5] Working v1 --- Students/Teybeo/nget-v1/nget/Program.cs | 122 ++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 Students/Teybeo/nget-v1/nget/Program.cs diff --git a/Students/Teybeo/nget-v1/nget/Program.cs b/Students/Teybeo/nget-v1/nget/Program.cs new file mode 100644 index 0000000..d453b25 --- /dev/null +++ b/Students/Teybeo/nget-v1/nget/Program.cs @@ -0,0 +1,122 @@ +using System; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Net; + +namespace nget +{ + class Program + { + public static void Main(string[] args) + { + Console.WriteLine("Hello World!"); + + Console.WriteLine(args.Length); + + if (args.Length == 0) + return; + + var url = extractArg(args, "-url"); + + // The url is mandatory, so stop here if there is none + if (url == null) { + Console.WriteLine("The -url argument is missing"); + return; + } + + if (args[0].Equals("get")) { + + // save file is optional + var fileOutput = extractArg(args, "-save"); + + var result = getUrl(url); + + // Si -save valide, on sauvegarde sur fichier, sinon on sort en console + if (fileOutput != null) { + saveFile(fileOutput, result); + } else { + Console.WriteLine(result); + } + + } else if (args[0].Equals("test")) { + + // samples count is mandatory + string samplesCountString = extractArg(args, "-times"); + if (samplesCountString == null) { + Console.WriteLine("The -times argument is missing"); + return; + } + + int samplesCount = int.Parse(samplesCountString); + var samples = new long[samplesCount]; + + for (int i = 0; i < samplesCount; i++) { + getUrl(url, ref samples[i]); + } + + if (args.Contains("-avg")) { + Console.WriteLine("average: " + computeAvg(samples)); + } else { + printTimes(samples); + } + } + } + + public static string extractArg(string[] args, string argName) { + + // For each string in the array + for (int i = 0; i < args.Length; i++) { + + // If we find the string we are looking for, return the next string (if it exists) + if (args[i].Equals(argName)) { + if (i + 1 < args.Length) + return args[i + 1]; + } + } + + return null; + } + + public static string getUrl(string url) { + long duration = 0; + return getUrl(url, ref duration); + } + + public static string getUrl(string url, ref long duration) { + + string response = null; + var webclient = new WebClient(); + + var chrono = new Stopwatch(); + chrono.Start(); + using (webclient) { + response = webclient.DownloadString(url); + } + chrono.Stop(); + duration = chrono.ElapsedMilliseconds; + return response; + } + + public static void saveFile(string fileName, string data) { + File.WriteAllText(fileName, data); + } + + public static void printTimes(long[] times) { + for (int i = 0; i < times.Length; i++) { + Console.WriteLine(times[i]); + } + } + + public static long computeAvg(long[] times) { + + long avg = 0; + + for (int i = 0; i < times.Length; i++) { + avg += times[i]; + } + + return avg / times.Length; + } + } +} \ No newline at end of file From 242ad97e60bdf1d4d323de196a6c50801a0bb4f5 Mon Sep 17 00:00:00 2001 From: Thibault D'Archivio Date: Mon, 1 Jun 2015 19:03:25 +0200 Subject: [PATCH 3/5] Added config files --- Students/Teybeo/nget-v1/nget.sln | 18 +++++ Students/Teybeo/nget-v1/nget/Program.cs | 6 +- .../nget-v1/nget/Properties/AssemblyInfo.cs | 31 +++++++++ Students/Teybeo/nget-v1/nget/nget.csproj | 68 +++++++++++++++++++ 4 files changed, 119 insertions(+), 4 deletions(-) create mode 100644 Students/Teybeo/nget-v1/nget.sln create mode 100644 Students/Teybeo/nget-v1/nget/Properties/AssemblyInfo.cs create mode 100644 Students/Teybeo/nget-v1/nget/nget.csproj diff --git a/Students/Teybeo/nget-v1/nget.sln b/Students/Teybeo/nget-v1/nget.sln new file mode 100644 index 0000000..0ef2ccc --- /dev/null +++ b/Students/Teybeo/nget-v1/nget.sln @@ -0,0 +1,18 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +# SharpDevelop 4.4 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "nget", "nget\nget.csproj", "{98625EA3-2BCD-4938-B748-7C5CD260A80B}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {98625EA3-2BCD-4938-B748-7C5CD260A80B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {98625EA3-2BCD-4938-B748-7C5CD260A80B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {98625EA3-2BCD-4938-B748-7C5CD260A80B}.Release|Any CPU.Build.0 = Release|Any CPU + {98625EA3-2BCD-4938-B748-7C5CD260A80B}.Release|Any CPU.ActiveCfg = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/Students/Teybeo/nget-v1/nget/Program.cs b/Students/Teybeo/nget-v1/nget/Program.cs index d453b25..a8782da 100644 --- a/Students/Teybeo/nget-v1/nget/Program.cs +++ b/Students/Teybeo/nget-v1/nget/Program.cs @@ -10,8 +10,6 @@ class Program { public static void Main(string[] args) { - Console.WriteLine("Hello World!"); - Console.WriteLine(args.Length); if (args.Length == 0) @@ -32,7 +30,7 @@ public static void Main(string[] args) var result = getUrl(url); - // Si -save valide, on sauvegarde sur fichier, sinon on sort en console + // if -save is valid, we save in file, else print on console if (fileOutput != null) { saveFile(fileOutput, result); } else { @@ -56,7 +54,7 @@ public static void Main(string[] args) } if (args.Contains("-avg")) { - Console.WriteLine("average: " + computeAvg(samples)); + Console.WriteLine("average: " + computeAvg(samples) + "ms"); } else { printTimes(samples); } diff --git a/Students/Teybeo/nget-v1/nget/Properties/AssemblyInfo.cs b/Students/Teybeo/nget-v1/nget/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..1b0bef5 --- /dev/null +++ b/Students/Teybeo/nget-v1/nget/Properties/AssemblyInfo.cs @@ -0,0 +1,31 @@ +#region Using directives + +using System; +using System.Reflection; +using System.Runtime.InteropServices; + +#endregion + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("nget")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("nget")] +[assembly: AssemblyCopyright("Copyright 2015")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// This sets the default COM visibility of types in the assembly to invisible. +// If you need to expose a type to COM, use [ComVisible(true)] on that type. +[assembly: ComVisible(false)] + +// The assembly version has following format : +// +// Major.Minor.Build.Revision +// +// You can specify all the values or you can use the default the Revision and +// Build Numbers by using the '*' as shown below: +[assembly: AssemblyVersion("1.0.*")] diff --git a/Students/Teybeo/nget-v1/nget/nget.csproj b/Students/Teybeo/nget-v1/nget/nget.csproj new file mode 100644 index 0000000..ec96b35 --- /dev/null +++ b/Students/Teybeo/nget-v1/nget/nget.csproj @@ -0,0 +1,68 @@ + + + + {98625EA3-2BCD-4938-B748-7C5CD260A80B} + Debug + AnyCPU + Exe + nget + nget + v4.0 + Client + Properties + False + False + False + OnBuildSuccess + False + False + False + obj\$(Configuration)\ + 4 + + + x86 + 4194304 + False + Auto + 4096 + + + bin\Debug\ + True + Full + False + True + DEBUG;TRACE + obj\ + Project + get -url "http://api.openweathermap.org/data/2.5/weather?q=paris&units=metric" + + + bin\Release\ + False + None + True + False + TRACE + + + + + 3.5 + + + + 3.5 + + + + 3.5 + + + + + + + + \ No newline at end of file From 13efc5c96a1932f0d5ecef19f025af7227e38cd4 Mon Sep 17 00:00:00 2001 From: Thibault D'Archivio Date: Wed, 17 Jun 2015 16:51:20 +0200 Subject: [PATCH 4/5] =?UTF-8?q?Commit=20v2=20Thibault=20D'Archivio=20Gr?= =?UTF-8?q?=C3=A9gory=20Flamant?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Students/Teybeo/nget-v2/nget-v2.sln | 18 +++++ Students/Teybeo/nget-v2/nget-v2/Get.cs | 69 ++++++++++++++++ Students/Teybeo/nget-v2/nget-v2/ICommand.cs | 14 ++++ Students/Teybeo/nget-v2/nget-v2/Program.cs | 56 +++++++++++++ .../nget-v2/Properties/AssemblyInfo.cs | 31 ++++++++ Students/Teybeo/nget-v2/nget-v2/Test.cs | 78 +++++++++++++++++++ .../Teybeo/nget-v2/nget-v2/nget-v2.csproj | 55 +++++++++++++ 7 files changed, 321 insertions(+) create mode 100644 Students/Teybeo/nget-v2/nget-v2.sln create mode 100644 Students/Teybeo/nget-v2/nget-v2/Get.cs create mode 100644 Students/Teybeo/nget-v2/nget-v2/ICommand.cs create mode 100644 Students/Teybeo/nget-v2/nget-v2/Program.cs create mode 100644 Students/Teybeo/nget-v2/nget-v2/Properties/AssemblyInfo.cs create mode 100644 Students/Teybeo/nget-v2/nget-v2/Test.cs create mode 100644 Students/Teybeo/nget-v2/nget-v2/nget-v2.csproj diff --git a/Students/Teybeo/nget-v2/nget-v2.sln b/Students/Teybeo/nget-v2/nget-v2.sln new file mode 100644 index 0000000..6709505 --- /dev/null +++ b/Students/Teybeo/nget-v2/nget-v2.sln @@ -0,0 +1,18 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +# SharpDevelop 4.4 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "nget-v2", "nget-v2\nget-v2.csproj", "{EAEEF979-34F9-4C1B-9507-0D4289892F85}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {EAEEF979-34F9-4C1B-9507-0D4289892F85}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EAEEF979-34F9-4C1B-9507-0D4289892F85}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EAEEF979-34F9-4C1B-9507-0D4289892F85}.Release|Any CPU.Build.0 = Release|Any CPU + {EAEEF979-34F9-4C1B-9507-0D4289892F85}.Release|Any CPU.ActiveCfg = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/Students/Teybeo/nget-v2/nget-v2/Get.cs b/Students/Teybeo/nget-v2/nget-v2/Get.cs new file mode 100644 index 0000000..a6d7bd4 --- /dev/null +++ b/Students/Teybeo/nget-v2/nget-v2/Get.cs @@ -0,0 +1,69 @@ + +using System; +using System.Diagnostics; +using System.IO; +using System.Net; + +namespace nget_v2 +{ + /// + /// Description of Get. + /// + public class Get: ICommand + { + public Get() + { + } + + public bool match(string arg) { + return arg.Equals("get"); + } + + public void execute(string[] args, string url) { + // save file is optional + var fileOutput = extractArg(args, "-save"); + + var result = getUrl(url); + + // if -save is valid, we save in file, else print on console + if (fileOutput != null) { + saveFile(fileOutput, result); + } else { + Console.WriteLine(result); + } + } + public static string getUrl(string url) { + long duration = 0; + return getUrl(url, ref duration); + } + + public static string getUrl(string url, ref long duration) { + + string response = null; + var webclient = new WebClient(); + + var chrono = new Stopwatch(); + chrono.Start(); + using (webclient) { + response = webclient.DownloadString(url); + } + chrono.Stop(); + duration = chrono.ElapsedMilliseconds; + return response; + } + + private static void saveFile(string fileName, string data) { + File.WriteAllText(fileName, data); + } + + public static string extractArg(string[] args, string argName) { + + int index = Array.IndexOf(args, argName); + + if (index == -1 || (index + 1 == args.Length)) + return null; + + return args[index + 1]; + } + } +} diff --git a/Students/Teybeo/nget-v2/nget-v2/ICommand.cs b/Students/Teybeo/nget-v2/nget-v2/ICommand.cs new file mode 100644 index 0000000..16e4ba7 --- /dev/null +++ b/Students/Teybeo/nget-v2/nget-v2/ICommand.cs @@ -0,0 +1,14 @@ + +using System; + +namespace nget_v2 +{ + /// + /// Description of ICommand. + /// + public interface ICommand + { + bool match(string arg); + void execute(string[] args, string url); + } +} diff --git a/Students/Teybeo/nget-v2/nget-v2/Program.cs b/Students/Teybeo/nget-v2/nget-v2/Program.cs new file mode 100644 index 0000000..c1529d6 --- /dev/null +++ b/Students/Teybeo/nget-v2/nget-v2/Program.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Net; +using nget_v2; + +namespace nget +{ + class Program + { + public static void Main(string[] args) + { + List list = new List(); + list.Add(new Get()); + list.Add(new Test()); + + Console.WriteLine(args.Length); + + if (args.Length == 0) + return; + + var url = extractArg(args, "-url"); + + // The url is mandatory, so stop here if there is none + if (url == null) { + Console.WriteLine("The -url argument is missing"); + return; + } + + foreach (var command in list) { + if (command.match(args[0])) { + command.execute(args, url); + } + } + + } + + public static string extractArg(string[] args, string argName) { + + int index = Array.IndexOf(args, argName); + + if (index == -1 || (index + 1 == args.Length)) + return null; + + return args[index + 1]; + } + + + + + + + } +} \ No newline at end of file diff --git a/Students/Teybeo/nget-v2/nget-v2/Properties/AssemblyInfo.cs b/Students/Teybeo/nget-v2/nget-v2/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..b2291b5 --- /dev/null +++ b/Students/Teybeo/nget-v2/nget-v2/Properties/AssemblyInfo.cs @@ -0,0 +1,31 @@ +#region Using directives + +using System; +using System.Reflection; +using System.Runtime.InteropServices; + +#endregion + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("nget-v2")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("nget-v2")] +[assembly: AssemblyCopyright("Copyright 2015")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// This sets the default COM visibility of types in the assembly to invisible. +// If you need to expose a type to COM, use [ComVisible(true)] on that type. +[assembly: ComVisible(false)] + +// The assembly version has following format : +// +// Major.Minor.Build.Revision +// +// You can specify all the values or you can use the default the Revision and +// Build Numbers by using the '*' as shown below: +[assembly: AssemblyVersion("1.0.*")] diff --git a/Students/Teybeo/nget-v2/nget-v2/Test.cs b/Students/Teybeo/nget-v2/nget-v2/Test.cs new file mode 100644 index 0000000..ae5e7c2 --- /dev/null +++ b/Students/Teybeo/nget-v2/nget-v2/Test.cs @@ -0,0 +1,78 @@ + +using System; +using System.Diagnostics; +using System.Linq; +using System.Net; + +namespace nget_v2 +{ + /// + /// Description of Test. + /// + public class Test: ICommand + { + public Test() + { + } + + public bool match(string arg) { + return arg.Equals("test"); + } + + public void execute(string[] args, string url) { + // samples count is mandatory + string samplesCountString = extractArg(args, "-times"); + if (samplesCountString == null) { + Console.WriteLine("The -times argument is missing"); + return; + } + + int samplesCount = int.Parse(samplesCountString); + var samples = new long[samplesCount]; + + for (int i = 0; i < samplesCount; i++) { + getUrl(url, ref samples[i]); + } + + if (args.Contains("-avg")) { + Console.WriteLine("average: " + samples.Average() + "ms"); + } else { + printTimes(samples); + } + } + + public static string getUrl(string url) { + long duration = 0; + return getUrl(url, ref duration); + } + + public static string getUrl(string url, ref long duration) { + + string response = null; + var webclient = new WebClient(); + + var chrono = new Stopwatch(); + chrono.Start(); + using (webclient) { + response = webclient.DownloadString(url); + } + chrono.Stop(); + duration = chrono.ElapsedMilliseconds; + return response; + } + private static void printTimes(long[] times) { + for (int i = 0; i < times.Length; i++) { + Console.WriteLine(times[i]); + } + } + public static string extractArg(string[] args, string argName) { + + int index = Array.IndexOf(args, argName); + + if (index == -1 || (index + 1 == args.Length)) + return null; + + return args[index + 1]; + } + } +} diff --git a/Students/Teybeo/nget-v2/nget-v2/nget-v2.csproj b/Students/Teybeo/nget-v2/nget-v2/nget-v2.csproj new file mode 100644 index 0000000..4997d16 --- /dev/null +++ b/Students/Teybeo/nget-v2/nget-v2/nget-v2.csproj @@ -0,0 +1,55 @@ + + + + {EAEEF979-34F9-4C1B-9507-0D4289892F85} + Debug + AnyCPU + Exe + nget_v2 + nget-v2 + v4.0 + Client + Properties + + + x86 + + + bin\Debug\ + True + Full + False + True + DEBUG;TRACE + + + bin\Release\ + False + None + True + False + TRACE + + + + + 3.5 + + + + 3.5 + + + + 3.5 + + + + + + + + + + + \ No newline at end of file From db4e679d72135ccbadae9c97a3584d2956a677b7 Mon Sep 17 00:00:00 2001 From: Thibault D'Archivio Date: Wed, 17 Jun 2015 19:23:59 +0200 Subject: [PATCH 5/5] Cleaned redundant code --- Students/Teybeo/nget-v2/nget-v2/Arg.cs | 28 ++++++++ Students/Teybeo/nget-v2/nget-v2/Get.cs | 56 ++++++--------- Students/Teybeo/nget-v2/nget-v2/ICommand.cs | 7 +- Students/Teybeo/nget-v2/nget-v2/Program.cs | 63 +++++++++++------ Students/Teybeo/nget-v2/nget-v2/Test.cs | 70 ++++++++----------- .../Teybeo/nget-v2/nget-v2/UrlDownloader.cs | 37 ++++++++++ 6 files changed, 163 insertions(+), 98 deletions(-) create mode 100644 Students/Teybeo/nget-v2/nget-v2/Arg.cs create mode 100644 Students/Teybeo/nget-v2/nget-v2/UrlDownloader.cs diff --git a/Students/Teybeo/nget-v2/nget-v2/Arg.cs b/Students/Teybeo/nget-v2/nget-v2/Arg.cs new file mode 100644 index 0000000..10f9454 --- /dev/null +++ b/Students/Teybeo/nget-v2/nget-v2/Arg.cs @@ -0,0 +1,28 @@ + +using System; + +namespace nget_v2 +{ + /// + /// Description of Arg. + /// + public class Arg + { + public string name { + get; set; + } + public bool isRequired { + get; set; + } + public bool hasValue { + get; set; + } + + public Arg(string _name, bool _isRequired, bool _hasValue) + { + name = _name; + isRequired = _isRequired; + hasValue = _hasValue; + } + } +} diff --git a/Students/Teybeo/nget-v2/nget-v2/Get.cs b/Students/Teybeo/nget-v2/nget-v2/Get.cs index a6d7bd4..fc5243d 100644 --- a/Students/Teybeo/nget-v2/nget-v2/Get.cs +++ b/Students/Teybeo/nget-v2/nget-v2/Get.cs @@ -1,5 +1,6 @@  using System; +using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Net; @@ -11,19 +12,34 @@ namespace nget_v2 /// public class Get: ICommand { + string fileOutput; + string url; + public Get() { } + public string getName() + { + return "get"; + } - public bool match(string arg) { - return arg.Equals("get"); + public List getArgs() { + var list = new List(); + list.Add(new Arg("-save", false, true)); + list.Add(new Arg("-url", true, true)); + return list; } - public void execute(string[] args, string url) { - // save file is optional - var fileOutput = extractArg(args, "-save"); + public void setValues(Dictionary values) { - var result = getUrl(url); + // save file is optional + fileOutput = values["-save"]; + url = values["-url"]; + } + + public void execute() { + + var result = UrlDownloader.download(url); // if -save is valid, we save in file, else print on console if (fileOutput != null) { @@ -32,38 +48,10 @@ public void execute(string[] args, string url) { Console.WriteLine(result); } } - public static string getUrl(string url) { - long duration = 0; - return getUrl(url, ref duration); - } - public static string getUrl(string url, ref long duration) { - - string response = null; - var webclient = new WebClient(); - - var chrono = new Stopwatch(); - chrono.Start(); - using (webclient) { - response = webclient.DownloadString(url); - } - chrono.Stop(); - duration = chrono.ElapsedMilliseconds; - return response; - } - private static void saveFile(string fileName, string data) { File.WriteAllText(fileName, data); } - public static string extractArg(string[] args, string argName) { - - int index = Array.IndexOf(args, argName); - - if (index == -1 || (index + 1 == args.Length)) - return null; - - return args[index + 1]; - } } } diff --git a/Students/Teybeo/nget-v2/nget-v2/ICommand.cs b/Students/Teybeo/nget-v2/nget-v2/ICommand.cs index 16e4ba7..7b37ff8 100644 --- a/Students/Teybeo/nget-v2/nget-v2/ICommand.cs +++ b/Students/Teybeo/nget-v2/nget-v2/ICommand.cs @@ -1,5 +1,6 @@  using System; +using System.Collections.Generic; namespace nget_v2 { @@ -8,7 +9,9 @@ namespace nget_v2 /// public interface ICommand { - bool match(string arg); - void execute(string[] args, string url); + string getName(); + List getArgs(); + void execute(); + void setValues(Dictionary values); } } diff --git a/Students/Teybeo/nget-v2/nget-v2/Program.cs b/Students/Teybeo/nget-v2/nget-v2/Program.cs index c1529d6..179e8c5 100644 --- a/Students/Teybeo/nget-v2/nget-v2/Program.cs +++ b/Students/Teybeo/nget-v2/nget-v2/Program.cs @@ -12,31 +12,58 @@ class Program { public static void Main(string[] args) { - List list = new List(); - list.Add(new Get()); - list.Add(new Test()); - - Console.WriteLine(args.Length); - if (args.Length == 0) return; - var url = extractArg(args, "-url"); - - // The url is mandatory, so stop here if there is none - if (url == null) { - Console.WriteLine("The -url argument is missing"); - return; + foreach (var element in args) { + Console.Write(element + " "); } + Console.WriteLine(); + + List list = new List(); + list.Add(new Get()); + list.Add(new Test()); foreach (var command in list) { - if (command.match(args[0])) { - command.execute(args, url); + + if (match(command, args)) { + command.execute(); + break; } } - + Console.ReadKey(); } + public static bool match(ICommand command, string[] args) { + + if (args[0].Equals(command.getName()) == false) + return false; + + List command_args = command.getArgs(); + + var values = new Dictionary(); + + foreach (var arg in command_args) { + + string value; + if (arg.hasValue) { + value = extractArg(args, arg.name); + } + else { + value = args.Contains(arg.name) ? "found" : null; + } + + if (arg.isRequired && value == null) { + Console.WriteLine("argument <" + arg.name + "> is missing"); + return false; + } + values.Add(arg.name, value); + + } + command.setValues(values); + return true; + } + public static string extractArg(string[] args, string argName) { int index = Array.IndexOf(args, argName); @@ -46,11 +73,5 @@ public static string extractArg(string[] args, string argName) { return args[index + 1]; } - - - - - - } } \ No newline at end of file diff --git a/Students/Teybeo/nget-v2/nget-v2/Test.cs b/Students/Teybeo/nget-v2/nget-v2/Test.cs index ae5e7c2..88ba4ca 100644 --- a/Students/Teybeo/nget-v2/nget-v2/Test.cs +++ b/Students/Teybeo/nget-v2/nget-v2/Test.cs @@ -1,5 +1,6 @@  using System; +using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Net; @@ -10,69 +11,56 @@ namespace nget_v2 /// Description of Test. /// public class Test: ICommand - { + { + + int samplesCount; + string url; + bool average; + public Test() { } - public bool match(string arg) { - return arg.Equals("test"); + public string getName() + { + return "test"; } - public void execute(string[] args, string url) { - // samples count is mandatory - string samplesCountString = extractArg(args, "-times"); - if (samplesCountString == null) { - Console.WriteLine("The -times argument is missing"); - return; - } + public List getArgs() + { + var list = new List(); + list.Add(new Arg("-times", true, true)); + list.Add(new Arg("-avg", false, false)); + list.Add(new Arg("-url", true, true)); + return list; + } + + public void setValues(Dictionary values) + { + samplesCount = int.Parse(values["-times"]); + url = values["-url"]; + average = values["-avg"] != null ? true : false; + } + + public void execute() { - int samplesCount = int.Parse(samplesCountString); var samples = new long[samplesCount]; for (int i = 0; i < samplesCount; i++) { - getUrl(url, ref samples[i]); + UrlDownloader.download(url, ref samples[i]); } - if (args.Contains("-avg")) { + if (average) { Console.WriteLine("average: " + samples.Average() + "ms"); } else { printTimes(samples); } } - public static string getUrl(string url) { - long duration = 0; - return getUrl(url, ref duration); - } - - public static string getUrl(string url, ref long duration) { - - string response = null; - var webclient = new WebClient(); - - var chrono = new Stopwatch(); - chrono.Start(); - using (webclient) { - response = webclient.DownloadString(url); - } - chrono.Stop(); - duration = chrono.ElapsedMilliseconds; - return response; - } private static void printTimes(long[] times) { for (int i = 0; i < times.Length; i++) { Console.WriteLine(times[i]); } } - public static string extractArg(string[] args, string argName) { - - int index = Array.IndexOf(args, argName); - - if (index == -1 || (index + 1 == args.Length)) - return null; - - return args[index + 1]; - } } } diff --git a/Students/Teybeo/nget-v2/nget-v2/UrlDownloader.cs b/Students/Teybeo/nget-v2/nget-v2/UrlDownloader.cs new file mode 100644 index 0000000..bc9a90d --- /dev/null +++ b/Students/Teybeo/nget-v2/nget-v2/UrlDownloader.cs @@ -0,0 +1,37 @@ + +using System; +using System.Diagnostics; +using System.Net; + +namespace nget_v2 +{ + /// + /// Description of UrlDownloader. + /// + public class UrlDownloader + { + public UrlDownloader() + { + } + + public static string download(string url) { + long duration = 0; + return download(url, ref duration); + } + + public static string download(string url, ref long duration) { + + string response = null; + var webclient = new WebClient(); + + var chrono = new Stopwatch(); + chrono.Start(); + using (webclient) { + response = webclient.DownloadString(url); + } + chrono.Stop(); + duration = chrono.ElapsedMilliseconds; + return response; + } + } +}