-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SEBSP-23: Implemented scaffolding for network redundancy.
- Loading branch information
Showing
13 changed files
with
738 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
SafeExamBrowser.Proctoring/ScreenProctoring/DataCollector.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
/* | ||
* Copyright (c) 2023 ETH Zürich, Educational Development and Technology (LET) | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using System.Timers; | ||
using System.Windows.Input; | ||
using SafeExamBrowser.Browser.Contracts; | ||
using SafeExamBrowser.Logging.Contracts; | ||
using SafeExamBrowser.Monitoring.Contracts.Applications; | ||
using SafeExamBrowser.Proctoring.ScreenProctoring.Data; | ||
using SafeExamBrowser.Proctoring.ScreenProctoring.Events; | ||
using SafeExamBrowser.Proctoring.ScreenProctoring.Imaging; | ||
using SafeExamBrowser.Settings.Proctoring; | ||
using SafeExamBrowser.WindowsApi.Contracts; | ||
using SafeExamBrowser.WindowsApi.Contracts.Events; | ||
using MouseButton = SafeExamBrowser.WindowsApi.Contracts.Events.MouseButton; | ||
using MouseButtonState = SafeExamBrowser.WindowsApi.Contracts.Events.MouseButtonState; | ||
using Timer = System.Timers.Timer; | ||
|
||
namespace SafeExamBrowser.Proctoring.ScreenProctoring | ||
{ | ||
internal class DataCollector | ||
{ | ||
private readonly object @lock = new object(); | ||
|
||
private readonly IApplicationMonitor applicationMonitor; | ||
private readonly IBrowserApplication browser; | ||
private readonly IModuleLogger logger; | ||
private readonly INativeMethods nativeMethods; | ||
private readonly ScreenProctoringSettings settings; | ||
private readonly Timer timer; | ||
|
||
private DateTime last; | ||
private Guid? keyboardHookId; | ||
private Guid? mouseHookId; | ||
|
||
internal event DataCollectedEventHandler DataCollected; | ||
|
||
internal DataCollector( | ||
IApplicationMonitor applicationMonitor, | ||
IBrowserApplication browser, | ||
IModuleLogger logger, | ||
INativeMethods nativeMethods, | ||
ScreenProctoringSettings settings) | ||
{ | ||
this.applicationMonitor = applicationMonitor; | ||
this.browser = browser; | ||
this.logger = logger; | ||
this.nativeMethods = nativeMethods; | ||
this.settings = settings; | ||
this.timer = new Timer(); | ||
} | ||
|
||
internal void Start() | ||
{ | ||
last = DateTime.Now; | ||
|
||
keyboardHookId = nativeMethods.RegisterKeyboardHook(KeyboardHookCallback); | ||
mouseHookId = nativeMethods.RegisterMouseHook(MouseHookCallback); | ||
|
||
timer.AutoReset = false; | ||
timer.Elapsed += MaxIntervalElapsed; | ||
timer.Interval = settings.MaxInterval; | ||
timer.Start(); | ||
|
||
logger.Debug("Started."); | ||
} | ||
|
||
internal void Stop() | ||
{ | ||
last = DateTime.Now; | ||
|
||
if (keyboardHookId.HasValue) | ||
{ | ||
nativeMethods.DeregisterKeyboardHook(keyboardHookId.Value); | ||
} | ||
|
||
if (mouseHookId.HasValue) | ||
{ | ||
nativeMethods.DeregisterMouseHook(mouseHookId.Value); | ||
} | ||
|
||
keyboardHookId = default; | ||
mouseHookId = default; | ||
|
||
timer.Elapsed -= MaxIntervalElapsed; | ||
timer.Stop(); | ||
|
||
logger.Debug("Stopped."); | ||
} | ||
|
||
private bool KeyboardHookCallback(int keyCode, KeyModifier modifier, KeyState state) | ||
{ | ||
var trigger = new KeyboardTrigger | ||
{ | ||
Key = KeyInterop.KeyFromVirtualKey(keyCode), | ||
Modifier = modifier, | ||
State = state | ||
}; | ||
|
||
TryCollect(keyboard: trigger); | ||
|
||
return false; | ||
} | ||
|
||
private void MaxIntervalElapsed(object sender, ElapsedEventArgs args) | ||
{ | ||
var trigger = new IntervalTrigger | ||
{ | ||
ConfigurationValue = settings.MaxInterval, | ||
TimeElapsed = Convert.ToInt32(DateTime.Now.Subtract(last).TotalMilliseconds) | ||
}; | ||
|
||
TryCollect(interval: trigger); | ||
} | ||
|
||
private bool MouseHookCallback(MouseButton button, MouseButtonState state, MouseInformation info) | ||
{ | ||
var trigger = new MouseTrigger | ||
{ | ||
Button = button, | ||
Info = info, | ||
State = state | ||
}; | ||
|
||
TryCollect(mouse: trigger); | ||
|
||
return false; | ||
} | ||
|
||
private void TryCollect(IntervalTrigger interval = default, KeyboardTrigger keyboard = default, MouseTrigger mouse = default) | ||
{ | ||
if (MinIntervalElapsed() && Monitor.TryEnter(@lock)) | ||
{ | ||
var elapsed = DateTime.Now.Subtract(last); | ||
|
||
last = DateTime.Now; | ||
timer.Stop(); | ||
|
||
Task.Run(() => | ||
{ | ||
try | ||
{ | ||
var metadata = new Metadata(applicationMonitor, browser, elapsed, logger.CloneFor(nameof(Metadata))); | ||
var screenShot = new ScreenShot(logger.CloneFor(nameof(ScreenShot)), settings); | ||
metadata.Capture(interval, keyboard, mouse); | ||
screenShot.Take(); | ||
screenShot.Compress(); | ||
DataCollected?.Invoke(metadata, screenShot); | ||
} | ||
catch (Exception e) | ||
{ | ||
logger.Error("Failed to execute data collection!", e); | ||
} | ||
}); | ||
|
||
timer.Start(); | ||
Monitor.Exit(@lock); | ||
} | ||
} | ||
|
||
private bool MinIntervalElapsed() | ||
{ | ||
return DateTime.Now.Subtract(last) >= new TimeSpan(0, 0, 0, 0, settings.MinInterval); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
SafeExamBrowser.Proctoring/ScreenProctoring/Events/DataCollectedEventHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* Copyright (c) 2023 ETH Zürich, Educational Development and Technology (LET) | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
using SafeExamBrowser.Proctoring.ScreenProctoring.Data; | ||
using SafeExamBrowser.Proctoring.ScreenProctoring.Imaging; | ||
|
||
namespace SafeExamBrowser.Proctoring.ScreenProctoring.Events | ||
{ | ||
internal delegate void DataCollectedEventHandler(Metadata metadata, ScreenShot screenShot); | ||
} |
Oops, something went wrong.