public void Add(DiscoveryPeer newPeer)
 {
     lock (Memory.Peers)
     {
         Memory.Peers.Add(newPeer);
         Memory.Peers = Memory.Peers.OrderByDescending(p => p.LastUpdateTimestamp).ToList();
     }
 }
Exemplo n.º 2
0
        /*  PROCESS RECEIVING MESSAGES  */

        private void ProcessInstanceConnected(string topic, MQTTMessage mqttMessage)
        {
            InstanceHealthService.Health.MQTTMessagesReceived += 1;

            var receivedMessage = mqttMessage.Message.ToString().Replace("'", "\"");

            var peerMessageContent = JsonConvert.DeserializeObject <MQTTPeerMessageContent>(receivedMessage);

            if (peerMessageContent != null && mqttMessage.FromInstanceId.CompareTo(Program.InstanceConfig.ServerInstanceID) != 0)
            {
                // respond back with my peer data
                var myMqttClient = _mqttClients.SingleOrDefault(c => c.mqttTopic.CompareTo(topic) == 0);

                if (myMqttClient != null && myMqttClient.mqttClient.IsConnected)
                {
                    var myPeerMessage = GetPeerMessage(peerMessageContent.PeerId, "INSTANCE_HEARTBEAT");
                    myMqttClient.mqttClient.PublishAsync(myMqttClient.mqttTopic, JsonConvert.SerializeObject(myPeerMessage));
                }

                // add new peer to my peers
                DiscoveryPeer existingPeer = null;

                lock (Memory.Peers)
                {
                    existingPeer = Memory.Peers.FirstOrDefault(p => p.ServerInstanceID.CompareTo(peerMessageContent.PeerId) == 0);
                }

                if (existingPeer == null)
                {
                    var newPeer = new DiscoveryPeer()
                    {
                        LastUpdateTimestamp = DateTime.UtcNow,
                        ServerInstanceID    = peerMessageContent.PeerId,
                        DiscoveryUrls       = peerMessageContent.DiscoveryUrls,
                        InstanceIP          = peerMessageContent.InstanceIP,
                        InstancePort        = peerMessageContent.InstancePort
                    };

                    Memory.Peers.Add(newPeer);
                }
            }
        }
        public void ProcessInstanceHeartbeat(string topic, MQTTMessage mqttMessage)
        {
            InstanceHealthService.Health.MQTTMessagesReceived += 1;

            var receivedMessage = mqttMessage.Message.ToString().Replace("'", "\"");

            var peerMessageContent = JsonConvert.DeserializeObject <MQTTPeerMessageContent>(receivedMessage);

            if (peerMessageContent != null && mqttMessage.FromInstanceId.CompareTo(Program.InstanceConfig.ServerInstanceID) != 0)
            {
                DiscoveryPeer existingPeer = null;

                // add new peer to my peers
                lock (Memory.Peers)
                {
                    existingPeer = Memory.Peers.FirstOrDefault(p => p.ServerInstanceID.CompareTo(peerMessageContent.PeerId) == 0);
                }

                if (existingPeer == null)
                {
                    var newPeer = new DiscoveryPeer()
                    {
                        LastUpdateTimestamp = DateTime.UtcNow,
                        ServerInstanceID    = peerMessageContent.PeerId,
                        DiscoveryUrls       = peerMessageContent.DiscoveryUrls,
                        InstanceIP          = peerMessageContent.InstanceIP,
                        InstancePort        = peerMessageContent.InstancePort
                    };

                    _memoryDiscoveryPeerRepository.Add(newPeer);
                }
                else
                {
                    existingPeer.LastUpdateTimestamp = DateTime.UtcNow;
                    existingPeer.DiscoveryUrls       = peerMessageContent.DiscoveryUrls;
                    existingPeer.InstanceIP          = peerMessageContent.InstanceIP;
                    existingPeer.InstancePort        = peerMessageContent.InstancePort;
                }
            }
        }