Skip to content

Commit

Permalink
Added Cancellation token in ListenCommand
Browse files Browse the repository at this point in the history
Signed-off-by: Kirill Krylov <[email protected]>
  • Loading branch information
kirillkrylov committed Nov 24, 2023
1 parent 7f0fb7f commit 4b7835c
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions clio/Command/ListenCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class ListenCommand : Command<ListenOptions>
private const string StopLogBroadcast = "/rest/ATFLogService/ResetConfiguration";
private string LogFilePath = string.Empty;
private bool Silent;
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();

#region Constructors: Public

public ListenCommand(IApplicationClient applicationClient,ILogger logger,EnvironmentSettings environmentSettings, IFileSystem fileSystem){
Expand All @@ -61,11 +63,13 @@ public ListenCommand(IApplicationClient applicationClient,ILogger logger,Environ
#region Methods: Public

public override int Execute(ListenOptions options){
CancellationToken token = _cancellationTokenSource.Token;
LogFilePath = options.FileName;
Silent = options.Silent;
_applicationClient.Listen(CancellationToken.None);
_applicationClient.Listen(token);
StartLogger(options);
Console.ReadKey();
_cancellationTokenSource.Cancel();
StopLogger();
return 0;
}
Expand All @@ -81,13 +85,13 @@ private void StartLogger(ListenOptions options){
JsonSerializerOptions serializerOptions = new (){PropertyNamingPolicy = JsonNamingPolicy.CamelCase};
string payloadString = JsonSerializer.Serialize(payload,serializerOptions);
_applicationClient.ExecutePostRequest(requestUrl,payloadString);

}

private void StopLogger(){
string rootPath = _environmentSettings.IsNetCore ? _environmentSettings.Uri : _environmentSettings.Uri + @"/0";
string requestUrl = rootPath+StopLogBroadcast;
_applicationClient.ExecutePostRequest(requestUrl,string.Empty);

}

private void OnMessageReceived(object sender, WsMessage message){
Expand Down

0 comments on commit 4b7835c

Please sign in to comment.