From d7af18acf1c3d312ad72abcbe6d70ab1324f9f85 Mon Sep 17 00:00:00 2001 From: BoneFuusen Date: Tue, 17 Mar 2026 17:09:14 +0600 Subject: [PATCH 1/3] fix: remove WebHttp - it just doesnt work --- SpaceBattle.Lib.Tests/EndPointTests.cs | 169 ------------------ .../SpaceBattle.Lib.Tests.csproj | 1 - WebHttp/Endpoint.cs | 16 -- WebHttp/IWebApi.cs | 19 -- WebHttp/MessageContract.cs | 29 --- WebHttp/Program.cs | 22 --- WebHttp/Startup.cs | 50 ------ WebHttp/WebHttp.csproj | 21 --- spacebattle.sln | 2 - 9 files changed, 329 deletions(-) delete mode 100644 SpaceBattle.Lib.Tests/EndPointTests.cs delete mode 100644 WebHttp/Endpoint.cs delete mode 100644 WebHttp/IWebApi.cs delete mode 100644 WebHttp/MessageContract.cs delete mode 100644 WebHttp/Program.cs delete mode 100644 WebHttp/Startup.cs delete mode 100644 WebHttp/WebHttp.csproj 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/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 From dbdaa66e220feb4dd066e4e291e7c0c76363ae7d Mon Sep 17 00:00:00 2001 From: BoneFuusen Date: Tue, 17 Mar 2026 18:01:32 +0600 Subject: [PATCH 2/3] feat: LAB6_2 Server start and stop --- SpaceBattle.App/App.cs | 18 +++++ SpaceBattle.Lib.Tests/StartServerTest.cs | 28 ++++++++ SpaceBattle.Lib.Tests/StopServerTest.cs | 84 ++++++++++++++++++++++++ SpaceBattle.Lib/InitCommands.cs | 51 ++++++++++++++ 4 files changed, 181 insertions(+) create mode 100644 SpaceBattle.App/App.cs create mode 100644 SpaceBattle.Lib.Tests/StartServerTest.cs create mode 100644 SpaceBattle.Lib.Tests/StopServerTest.cs create mode 100644 SpaceBattle.Lib/InitCommands.cs 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/StartServerTest.cs b/SpaceBattle.Lib.Tests/StartServerTest.cs new file mode 100644 index 0000000..cd7806e --- /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)); + } +} \ No newline at end of file diff --git a/SpaceBattle.Lib.Tests/StopServerTest.cs b/SpaceBattle.Lib.Tests/StopServerTest.cs new file mode 100644 index 0000000..0bc07b3 --- /dev/null +++ b/SpaceBattle.Lib.Tests/StopServerTest.cs @@ -0,0 +1,84 @@ +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(); + } + + public class SendCommandMock : Hwdtech.ICommand + { + private readonly Hwdtech.ICommand _command; + private readonly Hwdtech.ICommand _stopCommand; + + public SendCommandMock(Hwdtech.ICommand command, Hwdtech.ICommand stopCommand) + { + _command = command; + _stopCommand = stopCommand; + } + + public void Execute() + { + _command.Execute(); + _stopCommand.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 SendCommandMock((Hwdtech.ICommand)args[1], stopCommand.Object); + }).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..8533971 --- /dev/null +++ b/SpaceBattle.Lib/InitCommands.cs @@ -0,0 +1,51 @@ +namespace SpaceBattle.Lib; +using System.Linq; +using Hwdtech; + +public class InitCommand : Hwdtech.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(); + } +} From 80cbe0625d1b3e422dd462c914214e7feee1b887 Mon Sep 17 00:00:00 2001 From: BoneFuusen Date: Tue, 17 Mar 2026 19:29:30 +0600 Subject: [PATCH 3/3] fix: after 500 years of shuffling dependencies --- SpaceBattle.Lib.Tests/StartServerTest.cs | 4 ++-- SpaceBattle.Lib.Tests/StopServerTest.cs | 28 ++++++------------------ SpaceBattle.Lib/InitCommands.cs | 10 ++++----- 3 files changed, 14 insertions(+), 28 deletions(-) diff --git a/SpaceBattle.Lib.Tests/StartServerTest.cs b/SpaceBattle.Lib.Tests/StartServerTest.cs index cd7806e..33a0936 100644 --- a/SpaceBattle.Lib.Tests/StartServerTest.cs +++ b/SpaceBattle.Lib.Tests/StartServerTest.cs @@ -16,7 +16,7 @@ public StartServerTest() [Fact] public void Start_Server_Test() { - var startCommand = new Mock(); + var startCommand = new Mock(); startCommand.Setup(cmd => cmd.Execute()).Verifiable(); IoC.Resolve("IoC.Register", "Game.Commands.CreateAndStartThread", (object[] args) => { @@ -25,4 +25,4 @@ public void Start_Server_Test() IoC.Resolve("Game.Commands.StartServerCommand", 5).Execute(); startCommand.Verify(cmd => cmd.Execute(), Times.Exactly(5)); } -} \ No newline at end of file +} diff --git a/SpaceBattle.Lib.Tests/StopServerTest.cs b/SpaceBattle.Lib.Tests/StopServerTest.cs index 0bc07b3..42a85e2 100644 --- a/SpaceBattle.Lib.Tests/StopServerTest.cs +++ b/SpaceBattle.Lib.Tests/StopServerTest.cs @@ -20,30 +20,13 @@ public StopServerTest() a.Execute(); } - public class SendCommandMock : Hwdtech.ICommand - { - private readonly Hwdtech.ICommand _command; - private readonly Hwdtech.ICommand _stopCommand; - - public SendCommandMock(Hwdtech.ICommand command, Hwdtech.ICommand stopCommand) - { - _command = command; - _stopCommand = stopCommand; - } - - public void Execute() - { - _command.Execute(); - _stopCommand.Execute(); - } - } - [Fact] + public void StopServer_Test() { var threadList = new List { 1, 2, 3 }; var barrier = new Barrier(4); - var stopCommand = new Mock(); + var stopCommand = new Mock(); stopCommand.Setup(x => x.Execute()).Verifiable(); IoC.Resolve("IoC.Register", "Game.Commands.GetThreadIDs", (object[] args) => { return threadList; }).Execute(); @@ -61,10 +44,13 @@ public void StopServer_Test() { return new ActionCommand((Action)args[1]); }).Execute(); - IoC.Resolve("IoC.Register", "Game.Commands.SendCommand", (object[] args) => { - return new SendCommandMock((Hwdtech.ICommand)args[1], stopCommand.Object); + return new ActionCommand(() => + { + ((ICommand)args[1]).Execute(); + stopCommand.Object.Execute(); + }); }).Execute(); IoC.Resolve("Game.Commands.StopServerCommand").Execute(); diff --git a/SpaceBattle.Lib/InitCommands.cs b/SpaceBattle.Lib/InitCommands.cs index 8533971..96e978e 100644 --- a/SpaceBattle.Lib/InitCommands.cs +++ b/SpaceBattle.Lib/InitCommands.cs @@ -2,7 +2,7 @@ namespace SpaceBattle.Lib; using System.Linq; using Hwdtech; -public class InitCommand : Hwdtech.ICommand +public class InitCommand : ICommand { public void Execute() { @@ -14,7 +14,7 @@ public void Execute() { Enumerable.Range(0, NumberOfThreads).ToList().ForEach(i => { - IoC.Resolve("Game.Commands.CreateAndStartThread").Execute(); + IoC.Resolve("Game.Commands.CreateAndStartThread").Execute(); }); }); }).Execute(); @@ -39,12 +39,12 @@ public void Execute() { ThreadList.ForEach(thread_id => { - IoC.Resolve("Game.Commands.SendCommand", thread_id, - IoC.Resolve("Game.Commands.SoftStopThread", 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(); + IoC.Resolve("Game.Commands.StopServerBarrierWait").Execute(); }); }).Execute(); }