SEBWIN-542: Ensured re-attempting to start a service session doesn't fail.

This commit is contained in:
Damian Büchel 2022-02-03 12:15:54 +01:00
parent 0d8d05166f
commit 1c3e4b450c
2 changed files with 78 additions and 66 deletions

View file

@ -63,17 +63,7 @@ namespace SafeExamBrowser.Runtime.Operations
if (success)
{
var exam = default(Exam);
if (!string.IsNullOrWhiteSpace(Context.Next.Settings.Server.ExamId))
{
exam = exams.First();
logger.Info("Automatically selected exam as defined in configuration.");
}
else
{
success = TrySelectExam(exams, out exam);
}
success = TrySelectExam(exams, out var exam);
if (success)
{
@ -81,31 +71,7 @@ namespace SafeExamBrowser.Runtime.Operations
if (success)
{
var info = server.GetConnectionInfo();
var status = TryLoadSettings(uri, UriSource.Server, out _, out var settings);
fileSystem.Delete(uri.LocalPath);
if (status == LoadStatus.Success)
{
var serverSettings = Context.Next.Settings.Server;
Context.Next.AppConfig.ServerApi = info.Api;
Context.Next.AppConfig.ServerConnectionToken = info.ConnectionToken;
Context.Next.AppConfig.ServerExamId = exam.Id;
Context.Next.AppConfig.ServerOauth2Token = info.Oauth2Token;
Context.Next.Settings = settings;
Context.Next.Settings.Browser.StartUrl = exam.Url;
Context.Next.Settings.Server = serverSettings;
Context.Next.Settings.SessionMode = SessionMode.Server;
result = OperationResult.Success;
}
else
{
result = OperationResult.Failed;
}
result = TryLoadServerSettings(exam, uri);
}
}
}
@ -132,15 +98,7 @@ namespace SafeExamBrowser.Runtime.Operations
{
if (Context.Current.Settings.SessionMode == SessionMode.Server)
{
logger.Info("Initializing server configuration for next session...");
Context.Next.AppConfig.ServerApi = Context.Current.AppConfig.ServerApi;
Context.Next.AppConfig.ServerConnectionToken = Context.Current.AppConfig.ServerConnectionToken;
Context.Next.AppConfig.ServerExamId = Context.Current.AppConfig.ServerExamId;
Context.Next.AppConfig.ServerOauth2Token = Context.Current.AppConfig.ServerOauth2Token;
Context.Next.Settings.Server = Context.Current.Settings.Server;
Context.Next.Settings.SessionMode = SessionMode.Server;
InitializeNextSession();
}
else if (Context.Next.Settings.SessionMode == SessionMode.Server)
{
@ -179,6 +137,47 @@ namespace SafeExamBrowser.Runtime.Operations
ActionRequired?.Invoke(args);
}
private void InitializeNextSession()
{
logger.Info("Initializing server configuration for next session...");
Context.Next.AppConfig.ServerApi = Context.Current.AppConfig.ServerApi;
Context.Next.AppConfig.ServerConnectionToken = Context.Current.AppConfig.ServerConnectionToken;
Context.Next.AppConfig.ServerExamId = Context.Current.AppConfig.ServerExamId;
Context.Next.AppConfig.ServerOauth2Token = Context.Current.AppConfig.ServerOauth2Token;
Context.Next.Settings.Server = Context.Current.Settings.Server;
Context.Next.Settings.SessionMode = SessionMode.Server;
}
private OperationResult TryLoadServerSettings(Exam exam, Uri uri)
{
var info = server.GetConnectionInfo();
var result = OperationResult.Failed;
var status = TryLoadSettings(uri, UriSource.Server, out _, out var settings);
fileSystem.Delete(uri.LocalPath);
if (status == LoadStatus.Success)
{
var serverSettings = Context.Next.Settings.Server;
Context.Next.AppConfig.ServerApi = info.Api;
Context.Next.AppConfig.ServerConnectionToken = info.ConnectionToken;
Context.Next.AppConfig.ServerExamId = exam.Id;
Context.Next.AppConfig.ServerOauth2Token = info.Oauth2Token;
Context.Next.Settings = settings;
Context.Next.Settings.Browser.StartUrl = exam.Url;
Context.Next.Settings.Server = serverSettings;
Context.Next.Settings.SessionMode = SessionMode.Server;
result = OperationResult.Success;
}
return result;
}
private (bool abort, bool fallback, bool success) TryPerformWithFallback(Func<ServerResponse> request)
{
var abort = false;
@ -206,7 +205,7 @@ namespace SafeExamBrowser.Runtime.Operations
var fallback = false;
var success = false;
value = default(T);
value = default;
while (!success)
{
@ -238,12 +237,24 @@ namespace SafeExamBrowser.Runtime.Operations
private bool TrySelectExam(IEnumerable<Exam> exams, out Exam exam)
{
var args = new ExamSelectionEventArgs(exams);
var success = true;
ActionRequired?.Invoke(args);
exam = args.SelectedExam;
if (string.IsNullOrWhiteSpace(Context.Next.Settings.Server.ExamId))
{
var args = new ExamSelectionEventArgs(exams);
return args.Success;
ActionRequired?.Invoke(args);
exam = args.SelectedExam;
success = args.Success;
}
else
{
exam = exams.First();
logger.Info("Automatically selected exam as defined in configuration.");
}
return success;
}
}
}

View file

@ -36,7 +36,6 @@ namespace SafeExamBrowser.Server
{
private readonly AppConfig appConfig;
private readonly FileSystem fileSystem;
private readonly HttpClient httpClient;
private readonly ConcurrentQueue<string> instructionConfirmations;
private readonly ILogger logger;
private readonly ConcurrentQueue<ILogContent> logContent;
@ -55,6 +54,7 @@ namespace SafeExamBrowser.Server
private int currentWlanValue;
private string examId;
private int handNotificationId;
private HttpClient httpClient;
private string oauth2Token;
private int pingNumber;
private ServerSettings settings;
@ -69,13 +69,12 @@ namespace SafeExamBrowser.Server
ILogger logger,
ISystemInfo systemInfo,
IUserInfo userInfo,
IPowerSupply powerSupply = default(IPowerSupply),
IWirelessAdapter wirelessAdapter = default(IWirelessAdapter))
IPowerSupply powerSupply = default,
IWirelessAdapter wirelessAdapter = default)
{
this.api = new ApiVersion1();
this.appConfig = appConfig;
this.fileSystem = new FileSystem(appConfig, logger);
this.httpClient = new HttpClient();
this.instructionConfirmations = new ConcurrentQueue<string>();
this.logger = logger;
this.logContent = new ConcurrentQueue<ILogContent>();
@ -144,10 +143,10 @@ namespace SafeExamBrowser.Server
return new ServerResponse(success, message);
}
public ServerResponse<IEnumerable<Exam>> GetAvailableExams(string examId = default(string))
public ServerResponse<IEnumerable<Exam>> GetAvailableExams(string examId = default)
{
var authorization = ("Authorization", $"Bearer {oauth2Token}");
var content = $"institutionId={settings.Institution}{(examId == default(string) ? "" : $"&examId={examId}")}";
var content = $"institutionId={settings.Institution}{(examId == default ? "" : $"&examId={examId}")}";
var contentType = "application/x-www-form-urlencoded";
var exams = default(IList<Exam>);
@ -188,7 +187,7 @@ namespace SafeExamBrowser.Server
var token = ("SEBConnectionToken", connectionToken);
var uri = default(Uri);
var success = TryExecute(HttpMethod.Get, $"{api.ConfigurationEndpoint}?examId={exam.Id}", out var response, default(string), default(string), authorization, token);
var success = TryExecute(HttpMethod.Get, $"{api.ConfigurationEndpoint}?examId={exam.Id}", out var response, default, default, authorization, token);
var message = response.ToLogString();
if (success)
@ -227,6 +226,8 @@ namespace SafeExamBrowser.Server
public void Initialize(ServerSettings settings)
{
this.settings = settings;
httpClient = new HttpClient();
httpClient.BaseAddress = new Uri(settings.ServerUrl);
if (settings.RequestTimeout > 0)
@ -442,7 +443,7 @@ namespace SafeExamBrowser.Server
break;
}
if (instructionConfirmation != default(string))
if (instructionConfirmation != default)
{
instructionConfirmations.Enqueue(instructionConfirmation);
}
@ -503,7 +504,7 @@ namespace SafeExamBrowser.Server
};
var content = json.ToString();
TryExecute(HttpMethod.Post, api.LogEndpoint, out var response, content, contentType, authorization, token);
TryExecute(HttpMethod.Post, api.LogEndpoint, out _, content, contentType, authorization, token);
}
private void WirelessAdapter_NetworksChanged()
@ -546,21 +547,21 @@ namespace SafeExamBrowser.Server
HttpMethod method,
string url,
out HttpResponseMessage response,
string content = default(string),
string contentType = default(string),
string content = default,
string contentType = default,
params (string name, string value)[] headers)
{
response = default(HttpResponseMessage);
response = default;
for (var attempt = 0; attempt < settings.RequestAttempts && (response == default(HttpResponseMessage) || !response.IsSuccessStatusCode); attempt++)
for (var attempt = 0; attempt < settings.RequestAttempts && (response == default || !response.IsSuccessStatusCode); attempt++)
{
var request = new HttpRequestMessage(method, url);
if (content != default(string))
if (content != default)
{
request.Content = new StringContent(content, Encoding.UTF8);
if (contentType != default(string))
if (contentType != default)
{
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(contentType);
}
@ -591,7 +592,7 @@ namespace SafeExamBrowser.Server
}
}
return response != default(HttpResponseMessage) && response.IsSuccessStatusCode;
return response != default && response.IsSuccessStatusCode;
}
}
}