|
| 1 | +using Certify.Management; |
| 2 | +using Certify.Models.Hub; |
| 3 | +using Certify.Models.Reporting; |
| 4 | +using Certify.Server.Hub.Api.SignalR.ManagementHub; |
| 5 | + |
| 6 | +namespace Certify.Server.HubService.Services |
| 7 | +{ |
| 8 | + public class DirectInstanceManagementHub : IInstanceManagementHub |
| 9 | + { |
| 10 | + private IInstanceManagementStateProvider _stateProvider; |
| 11 | + private ILogger<DirectInstanceManagementHub> _logger; |
| 12 | + private ICertifyManager _certifyManager; |
| 13 | + public DirectInstanceManagementHub(ILogger<DirectInstanceManagementHub> logger, IInstanceManagementStateProvider stateProvider, ICertifyManager certifyManager) |
| 14 | + { |
| 15 | + _stateProvider = stateProvider; |
| 16 | + _logger = logger; |
| 17 | + _certifyManager = certifyManager; |
| 18 | + } |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Receive results from a previously issued command |
| 22 | + /// </summary> |
| 23 | + /// <param name="result"></param> |
| 24 | + /// <returns></returns> |
| 25 | + public Task ReceiveCommandResult(InstanceCommandResult result) |
| 26 | + { |
| 27 | + |
| 28 | + result.Received = DateTimeOffset.Now; |
| 29 | + |
| 30 | + // check we are awaiting this result |
| 31 | + var cmd = _stateProvider.GetAwaitedCommandRequest(result.CommandId); |
| 32 | + |
| 33 | + if (cmd == null && !result.IsCommandResponse) |
| 34 | + { |
| 35 | + // message was not requested and has been sent by the instance (e.g. heartbeat) |
| 36 | + cmd = new InstanceCommandRequest { CommandId = result.CommandId, CommandType = result.CommandType }; |
| 37 | + } |
| 38 | + |
| 39 | + if (cmd != null) |
| 40 | + { |
| 41 | + _stateProvider.RemoveAwaitedCommandRequest(cmd.CommandId); |
| 42 | + |
| 43 | + if (cmd.CommandType == ManagementHubCommands.GetInstanceInfo) |
| 44 | + { |
| 45 | + var instanceInfo = System.Text.Json.JsonSerializer.Deserialize<ManagedInstanceInfo>(result.Value); |
| 46 | + |
| 47 | + if (instanceInfo != null) |
| 48 | + { |
| 49 | + |
| 50 | + instanceInfo.LastReported = DateTimeOffset.Now; |
| 51 | + _stateProvider.UpdateInstanceConnectionInfo("internal", instanceInfo); |
| 52 | + |
| 53 | + _logger?.LogInformation("Received instance {instanceId} {instanceTitle} for mgmt hub connection.", instanceInfo.InstanceId, instanceInfo.Title); |
| 54 | + |
| 55 | + // if we don't yet have any managed items for this instance, ask for them |
| 56 | + if (!_stateProvider.HasItemsForManagedInstance(instanceInfo.InstanceId)) |
| 57 | + { |
| 58 | + var request = new InstanceCommandRequest |
| 59 | + { |
| 60 | + CommandId = Guid.NewGuid(), |
| 61 | + CommandType = ManagementHubCommands.GetManagedItems |
| 62 | + }; |
| 63 | + |
| 64 | + IssueCommand(request); |
| 65 | + } |
| 66 | + |
| 67 | + // if we dont have a status summary, ask for that |
| 68 | + if (!_stateProvider.HasStatusSummaryForManagedInstance(instanceInfo.InstanceId)) |
| 69 | + { |
| 70 | + var request = new InstanceCommandRequest |
| 71 | + { |
| 72 | + CommandId = Guid.NewGuid(), |
| 73 | + CommandType = ManagementHubCommands.GetStatusSummary |
| 74 | + }; |
| 75 | + |
| 76 | + IssueCommand(request); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + else |
| 81 | + { |
| 82 | + // for all other command results we need to resolve which instance id we are communicating with |
| 83 | + var instanceId = _stateProvider.GetInstanceIdForConnection("internal"); |
| 84 | + result.InstanceId = instanceId; |
| 85 | + |
| 86 | + if (!string.IsNullOrWhiteSpace(instanceId)) |
| 87 | + { |
| 88 | + // action this message from this instance |
| 89 | + _logger?.LogInformation("Received instance command result {result}", result.CommandType); |
| 90 | + |
| 91 | + if (cmd.CommandType == ManagementHubCommands.GetManagedItems) |
| 92 | + { |
| 93 | + // got items from an instance |
| 94 | + var val = System.Text.Json.JsonSerializer.Deserialize<ManagedInstanceItems>(result.Value); |
| 95 | + |
| 96 | + _stateProvider.UpdateInstanceItemInfo(instanceId, val.Items); |
| 97 | + } |
| 98 | + else if (cmd.CommandType == ManagementHubCommands.GetStatusSummary && result?.Value != null) |
| 99 | + { |
| 100 | + // got status summary |
| 101 | + var val = System.Text.Json.JsonSerializer.Deserialize<StatusSummary>(result.Value); |
| 102 | + |
| 103 | + _stateProvider.UpdateInstanceStatusSummary(instanceId, val); |
| 104 | + } |
| 105 | + else |
| 106 | + { |
| 107 | + // store for something else to consume |
| 108 | + if (result.IsCommandResponse) |
| 109 | + { |
| 110 | + _stateProvider.AddAwaitedCommandResult(result); |
| 111 | + } |
| 112 | + else |
| 113 | + { |
| 114 | + // item was not requested, queue for processing |
| 115 | + if (result.CommandType == ManagementHubCommands.NotificationUpdatedManagedItem) |
| 116 | + { |
| 117 | + //_uiStatusHub.Clients.All.SendAsync(Providers.StatusHubMessages.SendManagedCertificateUpdateMsg, System.Text.Json.JsonSerializer.Deserialize<Models.ManagedCertificate>(result.Value)); |
| 118 | + } |
| 119 | + else if (result.CommandType == ManagementHubCommands.NotificationManagedItemRequestProgress) |
| 120 | + { |
| 121 | + //_uiStatusHub.Clients.All.SendAsync(Providers.StatusHubMessages.SendProgressStateMsg, System.Text.Json.JsonSerializer.Deserialize<Models.RequestProgressState>(result.Value)); |
| 122 | + } |
| 123 | + else if (result.CommandType == ManagementHubCommands.NotificationRemovedManagedItem) |
| 124 | + { |
| 125 | + // deleted :TODO |
| 126 | + //_uiStatusHub.Clients.All.SendAsync(Providers.StatusHubMessages.SendMsg, $"Deleted item {result.Value}"); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + else |
| 132 | + { |
| 133 | + _logger?.LogError("Received instance command result for an unknown instance {result}", result.CommandType); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + return Task.CompletedTask; |
| 139 | + } |
| 140 | + |
| 141 | + public Task ReceiveInstanceMessage(InstanceMessage message) |
| 142 | + { |
| 143 | + |
| 144 | + var instanceId = _stateProvider.GetInstanceIdForConnection("internal"); |
| 145 | + if (instanceId != null) |
| 146 | + { |
| 147 | + // action this message from this instance |
| 148 | + _logger?.LogInformation("Received instance message {msg}", message); |
| 149 | + } |
| 150 | + else |
| 151 | + { |
| 152 | + _logger?.LogError("Received instance command result for an unknown instance {msg}", message); |
| 153 | + } |
| 154 | + |
| 155 | + return Task.CompletedTask; |
| 156 | + } |
| 157 | + |
| 158 | + public Task SendCommandRequest(InstanceCommandRequest cmd) |
| 159 | + { |
| 160 | + IssueCommand(cmd); |
| 161 | + |
| 162 | + return Task.CompletedTask; |
| 163 | + } |
| 164 | + |
| 165 | + private async void IssueCommand(InstanceCommandRequest cmd) |
| 166 | + { |
| 167 | + _stateProvider.AddAwaitedCommandRequest(cmd); |
| 168 | + |
| 169 | + // |
| 170 | + var result = await _certifyManager.PerformHubCommandWithResult(cmd); |
| 171 | + if (result.IsCommandResponse) |
| 172 | + { |
| 173 | + result.CommandType = cmd.CommandType; |
| 174 | + result.CommandId = cmd.CommandId; |
| 175 | + result.InstanceId = _stateProvider.GetManagementHubInstanceId(); |
| 176 | + |
| 177 | + await ReceiveCommandResult(result); |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments