diff --git a/SafeExamBrowser.Proctoring/ScreenProctoring/Service/Requests/ScreenShotRequest.cs b/SafeExamBrowser.Proctoring/ScreenProctoring/Service/Requests/ScreenShotRequest.cs index 7b681e3a..aaabcc0c 100644 --- a/SafeExamBrowser.Proctoring/ScreenProctoring/Service/Requests/ScreenShotRequest.cs +++ b/SafeExamBrowser.Proctoring/ScreenProctoring/Service/Requests/ScreenShotRequest.cs @@ -21,7 +21,7 @@ namespace SafeExamBrowser.Proctoring.ScreenProctoring.Service.Requests { } - internal bool TryExecute(MetaData metaData, ScreenShot screenShot, string sessionId, out string message) + internal bool TryExecute(MetaData metaData, ScreenShot screenShot, string sessionId, out int health, out string message) { var imageFormat = (Header.IMAGE_FORMAT, ToString(screenShot.Format)); var metdataJson = (Header.METADATA, metaData.ToJson()); @@ -29,8 +29,14 @@ namespace SafeExamBrowser.Proctoring.ScreenProctoring.Service.Requests var url = api.ScreenShotEndpoint.Replace(Api.SESSION_ID, sessionId); var success = TryExecute(HttpMethod.Post, url, out var response, screenShot.Data, ContentType.OCTET_STREAM, Authorization, imageFormat, metdataJson, timestamp); + health = default; message = response.ToLogString(); + if (success) + { + parser.TryParseHealth(response, out health); + } + return success; } diff --git a/SafeExamBrowser.Proctoring/ScreenProctoring/Service/ServiceProxy.cs b/SafeExamBrowser.Proctoring/ScreenProctoring/Service/ServiceProxy.cs index e08ac472..90d288cc 100644 --- a/SafeExamBrowser.Proctoring/ScreenProctoring/Service/ServiceProxy.cs +++ b/SafeExamBrowser.Proctoring/ScreenProctoring/Service/ServiceProxy.cs @@ -87,10 +87,10 @@ namespace SafeExamBrowser.Proctoring.ScreenProctoring.Service return new ServiceResponse(success, health, message); } - internal ServiceResponse Send(MetaData metaData, ScreenShot screenShot) + internal ServiceResponse Send(MetaData metaData, ScreenShot screenShot) { var request = new ScreenShotRequest(api, httpClient, logger, parser); - var success = request.TryExecute(metaData, screenShot, SessionId, out var message); + var success = request.TryExecute(metaData, screenShot, SessionId, out var health, out var message); if (success) { @@ -101,7 +101,7 @@ namespace SafeExamBrowser.Proctoring.ScreenProctoring.Service logger.Error("Failed to send screen shot!"); } - return new ServiceResponse(success, message); + return new ServiceResponse(success, health, message); } internal ServiceResponse TerminateSession() diff --git a/SafeExamBrowser.Proctoring/ScreenProctoring/TransmissionSpooler.cs b/SafeExamBrowser.Proctoring/ScreenProctoring/TransmissionSpooler.cs index 444c10c9..855b5654 100644 --- a/SafeExamBrowser.Proctoring/ScreenProctoring/TransmissionSpooler.cs +++ b/SafeExamBrowser.Proctoring/ScreenProctoring/TransmissionSpooler.cs @@ -71,13 +71,8 @@ namespace SafeExamBrowser.Proctoring.ScreenProctoring thread.IsBackground = true; thread.Start(); - // TODO: Only use timer when BAD, until then read health from transmission response! timer.AutoReset = false; - timer.Elapsed += Timer_Elapsed; - timer.Interval = 10000; - // TODO: Revert! - // timer.Interval = FIFTEEN_SECONDS; - timer.Start(); + timer.Interval = FIFTEEN_SECONDS; } internal void Stop() @@ -88,8 +83,8 @@ namespace SafeExamBrowser.Proctoring.ScreenProctoring { logger.Debug("Stopping..."); - timer.Elapsed -= Timer_Elapsed; timer.Stop(); + timer.Elapsed -= Timer_Elapsed; try { @@ -113,6 +108,7 @@ namespace SafeExamBrowser.Proctoring.ScreenProctoring logger.Error("Failed to stop!", e); } + recovering = false; resume = default; thread = default; token = default; @@ -157,6 +153,9 @@ namespace SafeExamBrowser.Proctoring.ScreenProctoring recovering = true; resume = DateTime.Now.AddSeconds(random.Next(0, THREE_MINUTES)); + timer.Elapsed += Timer_Elapsed; + timer.Start(); + logger.Warn($"Activating local caching and suspending transmission due to bad service health (value: {health}, resume: {resume:HH:mm:ss})."); } @@ -188,6 +187,9 @@ namespace SafeExamBrowser.Proctoring.ScreenProctoring } else { + timer.Stop(); + timer.Elapsed -= Timer_Elapsed; + logger.Info($"Deactivating local caching and resuming transmission due to improved service health (value: {health})."); } } @@ -196,23 +198,6 @@ namespace SafeExamBrowser.Proctoring.ScreenProctoring { buffer.Enqueue((metaData, schedule, screenShot)); buffer = new Queue<(MetaData, DateTime, ScreenShot)>(buffer.OrderBy((b) => b.schedule)); - - // TODO: Remove! - PrintBuffer(); - } - - private void PrintBuffer() - { - logger.Log("-------------------------------------------------------------------------------------------------------"); - logger.Info(""); - logger.Log($"\t\t\t\tBuffer: {buffer.Count} items"); - - foreach (var (m, t, s) in buffer) - { - logger.Log($"\t\t\t\t{s.CaptureTime:HH:mm:ss} -> {t:HH:mm:ss} ({m.Elapsed} {s.Data.Length / 1000:N0}kB)"); - } - - logger.Log("-------------------------------------------------------------------------------------------------------"); } private void BufferFromCache() @@ -241,9 +226,6 @@ namespace SafeExamBrowser.Proctoring.ScreenProctoring { buffer.Dequeue(); screenShot.Dispose(); - - // TODO: Remove! - PrintBuffer(); } } } @@ -313,9 +295,6 @@ namespace SafeExamBrowser.Proctoring.ScreenProctoring { buffer.Dequeue(); screenShot.Dispose(); - - // TODO: Remove! - PrintBuffer(); } } @@ -370,7 +349,13 @@ namespace SafeExamBrowser.Proctoring.ScreenProctoring if (service.IsConnected) { - success = service.Send(metaData, screenShot).Success; + var response = service.Send(metaData, screenShot); + + if (response.Success) + { + health = UpdateHealth(response.Value); + success = true; + } } else { @@ -380,58 +365,36 @@ namespace SafeExamBrowser.Proctoring.ScreenProctoring return success; } - private int factor = 2; - private int bads = 0; - private void Timer_Elapsed(object sender, ElapsedEventArgs e) { - // TODO: Revert! - //if (service.IsConnected) - //{ - // var response = service.GetHealth(); - - // if (response.Success) - // { - // var previous = health; - - // health = response.Value > BAD ? BAD : (response.Value < GOOD ? GOOD : response.Value); - - // if (previous != health) - // { - // logger.Info($"Service health {(previous < health ? "deteriorated" : "improved")} from {previous} to {health}."); - // } - // } - //} - //else - //{ - // logger.Warn("Cannot query health as service is disconnected!"); - //} - - var previous = health; - - if (bads < 2) + if (service.IsConnected) { - bads += health == BAD ? 1 : 0; - factor = health == BAD ? -2 : (health == GOOD ? 2 : factor); - health += factor; - health = health < GOOD ? GOOD : (health > BAD ? BAD : health); + var response = service.GetHealth(); + + if (response.Success) + { + health = UpdateHealth(response.Value); + } } else { - health = 0; - } - - if (previous != health) - { - logger.Warn($"Service health {(previous < health ? "deteriorated" : "improved")} from {previous} to {health}."); - - if (bads >= 2 && health == 0) - { - logger.Warn("Stopped health simulation."); - } + logger.Warn("Cannot query health as service is disconnected!"); } timer.Start(); } + + private int UpdateHealth(int value) + { + var previous = health; + var current = value > BAD ? BAD : (value < GOOD ? GOOD : value); + + if (previous != current) + { + logger.Info($"Service health {(previous < current ? "deteriorated" : "improved")} from {previous} to {current}."); + } + + return current; + } } }