public async Task RunSimulation(RunSimulationRequest request) { File.Delete("logs.txt"); Console.WriteLine($"Client Id: {Context.ConnectionId} " + $"Time Called: {DateTime.UtcNow:D}"); try { var heightMap = new Bitmap(request.HeightMap.Path); Console.WriteLine("Start processing heightMap.."); var settlementInfo = await new SettlementBuilder() .WithHeightMap(this.BitmapToPixelArray(heightMap)) .BuildAsync(); Console.WriteLine("Finished processing heightMap."); var generator = new StructureGeneratorBuilder() .WithMaxIterations(request.MaxIterations) .WithBreakpointStep(request.BreakpointStep) .WithFields(settlementInfo.Fields) .WithMainRoad(settlementInfo.MainRoad) .Build(); Console.WriteLine("Start running simulation.."); generator.Initialized += OnSettlementStateUpdate; generator.Breakpoint += OnSettlementStateUpdate; generator.NextEpoch += OnSettlementStateUpdate; generator.Finished += OnSettlementStateUpdate; generator.Finished += OnFinished; await generator.Start(); } catch (Exception e) { var formattedException = $"Server exception at {nameof(NotificationHub)}.{nameof(RunSimulation)}:" + $"\nMessage: {e.Message}," + $"\nInner exception: {e.InnerException}," + $"\nStackTrace: {e.StackTrace}"; Console.WriteLine(formattedException); Clients.All.onException(formattedException); throw; } }
static void RunSimulation(RunSimulationRequest request) { var url = ConfigurationManager.AppSettings["SettlementSimulationUrl"]; var conn = new HubConnection(url); var proxy = conn.CreateHubProxy("notificationHub"); try { conn.Start().Wait(); proxy.On <RunSimulationResponse>("OnSettlementStateUpdate", response => { Console.Clear(); Console.WriteLine($"Response at {DateTime.UtcNow:G}"); Console.WriteLine(response); }); proxy.On <string>("OnFinished", Console.WriteLine); proxy.On <string>("OnException", Console.WriteLine); proxy.Invoke("RunSimulation", request); } catch (Exception e) { Console.WriteLine(e.Message); } }