public Author(AgentAbstract agent) { Type = agent.Type; Ip = agent.Ip; SubType = agent.SubType; Version = agent.Version; MachineName = agent.MachineName; Id = agent.Id; }
private void AgentExecutable(AgentAbstract agent, CancellationToken stoppingToken) { agent.StoppingToken = stoppingToken; var agentSupported = new Dictionary <string, CancellationTokenSource>( agent.SupportedMessage.Select(x => new KeyValuePair <string, CancellationTokenSource>( x.ToString(), new CancellationTokenSource()))); string agentId = $"{agent.Type}_{agent.SubType}"; _consumer.Subscribe(agentId, agent.RpcMessageType.ToString(), stoppingToken, agentSupported.ToDictionary(x => x.Key, x => x.Value.Token) ); _consumer.OnConsumed += ProcessMessage; _consumer.OnRpcRequest += async(result, topic) => { try { if (agent.RpcMessageType.ToString() == topic) { return(JsonSerializer.Serialize( await agent.ProcessRpcAsync(JsonSerializer.Deserialize <AgentMessage>(result) .To <RpcRequest>()))); } } catch (Exception e) { _logger.LogError(e, $"Can`t process rpc from {topic} by agent {agent}, msg: {result}"); } return(null); }; async Task ProcessMessage(string id, byte[] result, string topic, bool forceBytes, Dictionary <string, string> headers) { if (agentId == id && agentSupported.Keys.Contains(topic)) { if (!agent.AllowConcurrency) { agentSupported[topic].Cancel(); _consumer.OnConsumed -= ProcessMessage; } if (!forceBytes) { var msg = JsonSerializer.Deserialize <AgentMessage>(result); if (msg.Author.Id != agent.Id) { agent.State = AgentState.InWork; if (msg.MessageType != MessageType.Connection) { _logger.LogInformation($"Consumed message"); } await agent.ProcessMessageAsync(msg); agent.State = AgentState.Online; } } else { await agent.ProcessMessageAsync(result, headers); } if (!agent.AllowConcurrency) { var nSource = new CancellationTokenSource(); agentSupported[topic] = nSource; _consumer.SubscribeOne(id, topic, nSource.Token); _consumer.OnConsumed += ProcessMessage; } } ; } }