diff --git a/SpaceBattle.App/App.cs b/SpaceBattle.App/App.cs new file mode 100644 index 0000000..255a2a7 --- /dev/null +++ b/SpaceBattle.App/App.cs @@ -0,0 +1,18 @@ +using Hwdtech; +using SpaceBattle.Lib; + +public class App +{ + public static void Main(int[] args) + { + var NumberOfTreads = args[0]; + new InitCommand().Execute(); + Console.WriteLine("Запуск сервера\nСоздание и регистрация потоков..."); + IoC.Resolve("Game.Commands.StartServerCommand", NumberOfTreads).Execute(); + Console.WriteLine("Потоки запущены\nДля завершения работы сервера нажмите любую клавишу"); + Console.ReadKey(); + Console.WriteLine("Завершение работы сервера. Закрытие потоков..."); + IoC.Resolve("Game.Commands.StopServerCommand"); + Console.WriteLine("Все потоки закрыты. Завершение работы программы"); + } +} diff --git a/SpaceBattle.Lib.Tests/EndPointTests.cs b/SpaceBattle.Lib.Tests/EndPointTests.cs deleted file mode 100644 index baf45f0..0000000 --- a/SpaceBattle.Lib.Tests/EndPointTests.cs +++ /dev/null @@ -1,169 +0,0 @@ -using System.Collections.Concurrent; -using Hwdtech; -using Hwdtech.Ioc; -using Moq; -using WebHttp; - -namespace SpaceBattle.Lib.Tests; - -public class EndpointTests -{ - public EndpointTests() - { - new InitScopeBasedIoCImplementationCommand().Execute(); - - IoC.Resolve( - "Scopes.Current.Set", - IoC.Resolve("Scopes.New", - IoC.Resolve("Scopes.Root") - ) - ).Execute(); - } - - [Fact] - public void EndpointWorksSuccessfuly() - { - var ThreadId = 1; - - IoC.Resolve( - "IoC.Register", - "SearchThreadIdByGameId", - (object[] args) => { return (object)ThreadId; }).Execute(); - - var message = new MessageContract() - { - TypeCommand = "rotate", - GameId = "1", - ObjectId = "1", - Proprties = new Dictionary() { { "AngleVelocity", 1 } } - }; - - var cmd = new Mock(); - - IoC.Resolve( - "IoC.Register", - "ICommand Message", - (object[] args) => - { - return cmd.Object; - } - ).Execute(); - - var q = new BlockingCollection(); - - IoC.Resolve( - "IoC.Register", - "Endpoint.Commands.SendCommand", - (object[] args) => - { - var cmd = new ActionCommand(() => - { - q.Add((ICommand)args[1]); - }); - return cmd; - } - ).Execute(); - - var wa = new Endpoint(); - - wa.BodyEcho(message); - - Assert.Single(q); - } - - [Fact] - public void EndpointWorksWhithExceptionInJson() - { - var ThreadId = 1; - - IoC.Resolve( - "IoC.Register", - "SearchThreadIdByGameId", - (object[] args) => { return (object)ThreadId; }).Execute(); - - var cmd = new Mock(); - - IoC.Resolve( - "IoC.Register", - "ICommand Message", - (object[] args) => - { - return cmd.Object; - } - ).Execute(); - - var q = new BlockingCollection(); - - IoC.Resolve( - "IoC.Register", - "Endpoint.Commands.SendCommand", - (object[] args) => - { - var cmd = new ActionCommand(() => - { - throw new Exception(); - }); - return cmd; - } - ).Execute(); - - var message = new Mock(); - - var wa = new Endpoint(); - - var exception_com = new ActionCommand(() => - { - wa.BodyEcho(message.Object); - }); - - Assert.Throws(exception_com.Execute); - } - - [Fact] - public void EndpointWorksWhithExceptionInId() - { - var ThreadId = new Exception(); - - IoC.Resolve( - "IoC.Register", - "SearchThreadIdByGameId", - (object[] args) => { return (object)ThreadId; }).Execute(); - - var cmd = new Mock(); - - IoC.Resolve( - "IoC.Register", - "ICommand Message", - (object[] args) => - { - return cmd.Object; - } - ).Execute(); - - var q = new BlockingCollection(); - - IoC.Resolve( - "IoC.Register", - "Endpoint.Commands.SendCommand", - (object[] args) => - { - var cmd = new ActionCommand(() => - { - q.Add((ICommand)args[1]); - }); - return cmd; - } - ).Execute(); - - var message = new Mock(); - - var wa = new Endpoint(); - - var exception_com = new ActionCommand(() => - { - wa.BodyEcho(message.Object); - }); - - Assert.Throws(exception_com.Execute); - } -} diff --git a/SpaceBattle.Lib.Tests/SpaceBattle.Lib.Tests.csproj b/SpaceBattle.Lib.Tests/SpaceBattle.Lib.Tests.csproj index 7f4f678..9da4fcd 100644 --- a/SpaceBattle.Lib.Tests/SpaceBattle.Lib.Tests.csproj +++ b/SpaceBattle.Lib.Tests/SpaceBattle.Lib.Tests.csproj @@ -31,7 +31,6 @@ - diff --git a/SpaceBattle.Lib.Tests/StartServerTest.cs b/SpaceBattle.Lib.Tests/StartServerTest.cs new file mode 100644 index 0000000..33a0936 --- /dev/null +++ b/SpaceBattle.Lib.Tests/StartServerTest.cs @@ -0,0 +1,28 @@ +using Hwdtech; +using Hwdtech.Ioc; +using Moq; + +namespace SpaceBattle.Lib.Test; + +public class StartServerTest +{ + public StartServerTest() + { + new InitScopeBasedIoCImplementationCommand().Execute(); + IoC.Resolve("Scopes.Current.Set", IoC.Resolve("Scopes.New", IoC.Resolve("Scopes.Root"))).Execute(); + var a = new InitCommand(); + a.Execute(); + } + [Fact] + public void Start_Server_Test() + { + var startCommand = new Mock(); + startCommand.Setup(cmd => cmd.Execute()).Verifiable(); + IoC.Resolve("IoC.Register", "Game.Commands.CreateAndStartThread", (object[] args) => + { + return startCommand.Object; + }).Execute(); + IoC.Resolve("Game.Commands.StartServerCommand", 5).Execute(); + startCommand.Verify(cmd => cmd.Execute(), Times.Exactly(5)); + } +} diff --git a/SpaceBattle.Lib.Tests/StopServerTest.cs b/SpaceBattle.Lib.Tests/StopServerTest.cs new file mode 100644 index 0000000..42a85e2 --- /dev/null +++ b/SpaceBattle.Lib.Tests/StopServerTest.cs @@ -0,0 +1,70 @@ +using Hwdtech; +using Hwdtech.Ioc; +using Moq; + +namespace SpaceBattle.Lib.Test; + +public class StopServerTest +{ + public StopServerTest() + { + new InitScopeBasedIoCImplementationCommand().Execute(); + IoC.Resolve("Scopes.Current.Set", IoC.Resolve("Scopes.New", IoC.Resolve("Scopes.Root"))).Execute(); + + var logfilepath = Path.GetTempFileName(); + IoC.Resolve("IoC.Register", "GetLogFilePath", (object[] args) => + { + return logfilepath; + }).Execute(); + var a = new InitCommand(); + a.Execute(); + } + + [Fact] + + public void StopServer_Test() + { + var threadList = new List { 1, 2, 3 }; + var barrier = new Barrier(4); + var stopCommand = new Mock(); + stopCommand.Setup(x => x.Execute()).Verifiable(); + IoC.Resolve("IoC.Register", "Game.Commands.GetThreadIDs", (object[] args) => { return threadList; }).Execute(); + + IoC.Resolve("IoC.Register", "Game.Commands.StopServerBarrierRemove", (object[] args) => + { + return () => { barrier.RemoveParticipant(); }; + }).Execute(); + + IoC.Resolve("IoC.Register", "Game.Commands.StopServerBarrierWait", (object[] args) => + { + return new ActionCommand(() => { barrier.SignalAndWait(); }); + }).Execute(); + + IoC.Resolve("IoC.Register", "Game.Commands.SoftStopThread", (object[] args) => + { + return new ActionCommand((Action)args[1]); + }).Execute(); + IoC.Resolve("IoC.Register", "Game.Commands.SendCommand", (object[] args) => + { + return new ActionCommand(() => + { + ((ICommand)args[1]).Execute(); + stopCommand.Object.Execute(); + }); + }).Execute(); + + IoC.Resolve("Game.Commands.StopServerCommand").Execute(); + + Assert.Equal(1, barrier.CurrentPhaseNumber); + stopCommand.Verify(cmd => cmd.Execute(), Times.Exactly(3)); + } + + [Fact] + public void StopServer_TestwithExeption() + { + IoC.Resolve("Game.Commands.ExeptionHandler", new ActionCommand(() => { }), new Exception()).Execute(); + + var testString = "Error occurred in command: SpaceBattle.Lib.ActionCommand\nException: System.Exception: Exception of type 'System.Exception' was thrown.\n"; + Assert.Equal(File.ReadAllText(IoC.Resolve("GetLogFilePath")), testString); + } +} diff --git a/SpaceBattle.Lib/InitCommands.cs b/SpaceBattle.Lib/InitCommands.cs new file mode 100644 index 0000000..96e978e --- /dev/null +++ b/SpaceBattle.Lib/InitCommands.cs @@ -0,0 +1,51 @@ +namespace SpaceBattle.Lib; +using System.Linq; +using Hwdtech; + +public class InitCommand : ICommand +{ + public void Execute() + { + IoC.Resolve("IoC.Register", "Game.Commands.StartServerCommand", (object[] args) => + { + var NumberOfThreads = (int)args[0]; + + return new ActionCommand(() => + { + Enumerable.Range(0, NumberOfThreads).ToList().ForEach(i => + { + IoC.Resolve("Game.Commands.CreateAndStartThread").Execute(); + }); + }); + }).Execute(); + + IoC.Resolve("IoC.Register", "Game.Commands.ExeptionHandler", (object[] args) => + { + var errorFile = IoC.Resolve("GetLogFilePath"); + return new ActionCommand(() => + { + using (var sw = File.AppendText(errorFile)) + { + sw.WriteLine($"Error occurred in command: {args[0]}"); + sw.WriteLine($"Exception: {args[1]}"); + } + }); + }).Execute(); + + IoC.Resolve("IoC.Register", "Game.Commands.StopServerCommand", (object[] args) => + { + var ThreadList = IoC.Resolve>("Game.Commands.GetThreadIDs"); + return new ActionCommand(() => + { + ThreadList.ForEach(thread_id => + { + IoC.Resolve("Game.Commands.SendCommand", thread_id, + IoC.Resolve("Game.Commands.SoftStopThread", thread_id, + IoC.Resolve("Game.Commands.StopServerBarrierRemove"))).Execute(); + }); + + IoC.Resolve("Game.Commands.StopServerBarrierWait").Execute(); + }); + }).Execute(); + } +} diff --git a/WebHttp/Endpoint.cs b/WebHttp/Endpoint.cs deleted file mode 100644 index dbdf586..0000000 --- a/WebHttp/Endpoint.cs +++ /dev/null @@ -1,16 +0,0 @@ -using CoreWCF; -using Hwdtech; -using System; - -namespace WebHttp -{ - [ServiceBehavior(IncludeExceptionDetailInFaults = true)] - public class Endpoint : IWebApi - { - public void BodyEcho(MessageContract message) - { - var ThreadId = (int)IoC.Resolve("SearchThreadIdByGameId", message.GameId); - IoC.Resolve("Endpoint.Commands.SendCommand", ThreadId, IoC.Resolve("ICommand Message", message)).Execute(); - } - } -} diff --git a/WebHttp/IWebApi.cs b/WebHttp/IWebApi.cs deleted file mode 100644 index 65a3943..0000000 --- a/WebHttp/IWebApi.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System.Net; -using CoreWCF; -using CoreWCF.OpenApi.Attributes; -using CoreWCF.Web; - -namespace WebHttp -{ - [ServiceContract] - [OpenApiBasePath("/api")] - public interface IWebApi - { - [OperationContract] - [WebInvoke(Method = "POST", UriTemplate = "/body")] - [OpenApiTag("Tag")] - [OpenApiResponse(ContentTypes = new[] { "application/json", "text/xml" }, Description = "Success", StatusCode = HttpStatusCode.Accepted, Type = typeof(MessageContract))] - void BodyEcho( - [OpenApiParameter(ContentTypes = new[] { "application/json", "text/xml" }, Description = "param description.")] MessageContract param); - } -} diff --git a/WebHttp/MessageContract.cs b/WebHttp/MessageContract.cs deleted file mode 100644 index 3c732c9..0000000 --- a/WebHttp/MessageContract.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System.Collections.Generic; -using System.Runtime.Serialization; -using CoreWCF.OpenApi.Attributes; -using System.Collections.Concurrent; -using System.Diagnostics.CodeAnalysis; - -namespace WebHttp -{ - [DataContract(Name = "ExampleContract", Namespace = "http://example.com")] - [ExcludeFromCodeCoverage] - public class MessageContract - { - [DataMember(Name = "Type of command", Order = 1)] - [OpenApiProperty(Description = "Type of command description.")] - public required string TypeCommand { get; set; } - - [DataMember(Name = "Game Id", Order = 2)] - [OpenApiProperty(Description = "Game Id description.")] - public required string GameId { get; set; } - - [DataMember(Name = "Id of object in game", Order = 3)] - [OpenApiProperty(Description = "Id of object in game description.")] - public required string ObjectId { get; set; } - - [DataMember(Name = "Proprties", Order = 4)] - [OpenApiProperty(Description = "Proprties description.")] - public IDictionary Proprties { get; set; } - } -} diff --git a/WebHttp/Program.cs b/WebHttp/Program.cs deleted file mode 100644 index 7c99180..0000000 --- a/WebHttp/Program.cs +++ /dev/null @@ -1,22 +0,0 @@ -using Microsoft.AspNetCore; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using System.Net; -using System.Diagnostics.CodeAnalysis; - -[ExcludeFromCodeCoverage] -class Program() -{ - static void Main(string[] args) - { - IWebHostBuilder builder = WebHost.CreateDefaultBuilder(args) - .UseKestrel(options => - { - options.ListenAnyIP(8080); - }) - .UseStartup(); - - IWebHost app = builder.Build(); - app.Run(); - } -} diff --git a/WebHttp/Startup.cs b/WebHttp/Startup.cs deleted file mode 100644 index 0c9c467..0000000 --- a/WebHttp/Startup.cs +++ /dev/null @@ -1,50 +0,0 @@ -using CoreWCF; -using CoreWCF.Configuration; -using Microsoft.AspNetCore.Builder; -using Microsoft.Extensions.DependencyInjection; -using Swashbuckle.AspNetCore.Swagger; -using System.Xml; -using WebHttp; -using System.Diagnostics.CodeAnalysis; - -[ExcludeFromCodeCoverage] -internal sealed class Startup -{ - public void ConfigureServices(IServiceCollection services) - { - services.AddServiceModelWebServices(o => - { - o.Title = "Test API"; - o.Version = "1"; - o.Description = "API Description"; - o.TermsOfService = new("http://example.com/terms"); - o.ContactName = "Contact"; - o.ContactEmail = "support@example.com"; - o.ContactUrl = new("http://example.com/contact"); - o.ExternalDocumentUrl = new("http://example.com/doc.pdf"); - o.ExternalDocumentDescription = "Documentation"; - }); - - services.AddSingleton(new SwaggerOptions()); - } - - public void Configure(IApplicationBuilder app) - { - app.UseMiddleware(); - app.UseSwaggerUI(); - - app.UseServiceModel(builder => - { - builder.AddService(); - builder.AddServiceWebEndpoint(new WebHttpBinding - { - MaxReceivedMessageSize = 5242880, - MaxBufferSize = 65536 - }, "api", behavior => - { - behavior.HelpEnabled = true; - behavior.AutomaticFormatSelectionEnabled = true; - }); - }); - } -} diff --git a/WebHttp/WebHttp.csproj b/WebHttp/WebHttp.csproj deleted file mode 100644 index c65dae4..0000000 --- a/WebHttp/WebHttp.csproj +++ /dev/null @@ -1,21 +0,0 @@ - - - - Exe - net7.0 - preview - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/spacebattle.sln b/spacebattle.sln index 5005d34..6f04802 100644 --- a/spacebattle.sln +++ b/spacebattle.sln @@ -9,8 +9,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SpaceBattle.Lib.Tests", "Sp EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{121F26CD-4C34-4047-8072-E2F5C89F1A87}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebHttp", "WebHttp\WebHttp.csproj", "{4794D930-2823-4D5C-9847-5DFD7C19CDDE}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU