From 1df678fdf2565c9e06551cf2dfa535b403d8cfc4 Mon Sep 17 00:00:00 2001 From: Simon G Date: Sun, 9 Feb 2020 00:34:26 +0100 Subject: [PATCH] - add server settings implementation --- GBase.Server/GBaseServerSettings.cs | 121 ++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 GBase.Server/GBaseServerSettings.cs diff --git a/GBase.Server/GBaseServerSettings.cs b/GBase.Server/GBaseServerSettings.cs new file mode 100644 index 0000000..2fe6cba --- /dev/null +++ b/GBase.Server/GBaseServerSettings.cs @@ -0,0 +1,121 @@ +// Author: Simon Gockner +// Created: 2020-02-08 +// Copyright(c) 2020 SimonG. All Rights Reserved. + +using System; +using System.Net; +using System.Threading.Tasks; +using GBase.Logging; +using GBase.Server.Exceptions; +using GBase.Server.Interfaces; + +namespace GBase.Server +{ + public class GBaseServerSettings : IGBaseServerSettings + { + private const string PROTOCOL_ARGUMENT_PREFIX = "-pr"; + private const string IP_ADDRESS_ARGUMENT_PREFIX = "-ip"; + private const string PORT_ARGUMENT_PREFIX = "-p"; + private const string ENDPOINT_ARGUMENT_PREFIX = "-e"; + private const string HELP_ARGUMENT_PREFIX = "-help"; + + private bool _protocolInitialized; + private bool _ipAddressInitialized; + private bool _portInitialized; + private bool _endpointInitialized; + + public ServerProtocol Protocol { get; private set; } + public IPAddress IpAddress { get; private set; } + public int Port { get; private set; } + public string Endpoint { get; private set; } + + + public async Task ParseArguments(string[] args) + { + for (int index = 0; index < args.Length; index++) + { + if (args[index] == PROTOCOL_ARGUMENT_PREFIX) + { + index++; + await VerifyArgsLength(index, args.Length); + + Protocol = ServerProtocolExtensions.GetServerProtocolFromString(args[index]); + _protocolInitialized = true; + } + else if (args[index] == IP_ADDRESS_ARGUMENT_PREFIX) + { + index++; + await VerifyArgsLength(index, args.Length); + + IpAddress = ParseIpAddress(args[index]); + _ipAddressInitialized = true; + } + else if (args[index] == PORT_ARGUMENT_PREFIX) + { + index++; + await VerifyArgsLength(index, args.Length); + + Port = Convert.ToInt32(args[index]); + _portInitialized = true; + } + else if (args[index] == ENDPOINT_ARGUMENT_PREFIX) + { + index++; + await VerifyArgsLength(index, args.Length); + + Endpoint = args[index]; + _endpointInitialized = true; + } + else + { + await PrintHelp(); + throw new ArgumentOutOfRangeException(nameof(args), args[index], "Invalid argument."); + } + } + + if (!await VerifySettings()) + throw new IncompleteArgumentsException($"Not all mandatory arguments were passed. Check {HELP_ARGUMENT_PREFIX} to find out which arguments are mandatory."); + } + + private async Task VerifyArgsLength(int index, int length) + { + if (length > index) + return; + + await PrintHelp(); + throw new MissingArgumentException(PROTOCOL_ARGUMENT_PREFIX); + } + + private async Task VerifySettings() + { + if (_protocolInitialized && _ipAddressInitialized && _portInitialized && _endpointInitialized) + return true; + + await PrintHelp(); + return false; + } + + private IPAddress ParseIpAddress(string ipAddressString) + { + string[] ipStringArray = ipAddressString.Split('.'); + byte[] ipByteArray = new byte[ipStringArray.Length]; + + for (var i = 0; i < ipStringArray.Length; i++) + { + string @string = ipStringArray[i]; + ipByteArray[i] = Convert.ToByte(@string); + } + + return new IPAddress(ipByteArray); + } + + private async Task PrintHelp() + { + await Log.Write($"Mandatory Arguments:"); + await Log.Write($"{PROTOCOL_ARGUMENT_PREFIX}: The used protocol (e.g 'http' or 'https')"); + await Log.Write($"{IP_ADDRESS_ARGUMENT_PREFIX}: The used ip address"); + await Log.Write($"{PORT_ARGUMENT_PREFIX}: The used port"); + await Log.Write($"{Endpoint}: The used endpoint"); + } + } +} \ No newline at end of file