Ejemplo n.º 1
0
        void ProcessInterCommunicateMessage(CommunicateMessage message)
        {
            // controller info
            if (message.Type.IsStartsWith("Controller#"))
            {
                ControllerInfo controller = null;
                switch (message.Type.ToArray('#').Last().ToLower())
                {
                case "info":
                case "connect":
                    if (this.Controllers.TryGetValue(message.Data.Get <string>("ID"), out controller))
                    {
                        controller.CopyFrom(message.Data, "ID".ToHashSet());
                    }
                    else
                    {
                        controller = message.Data.FromJson <ControllerInfo>();
                        this.Controllers.TryAdd(controller.ID, controller);
                    }
#if DEBUG
                    Global.OnProcess?.Invoke($"{(message.Type.ToArray('#').Last().IsEquals("info") ? "Got information of a controller" : "A controller was connected")} => {message.ToJson()}");
#endif
                    break;

                case "disconnect":
                    controller = message.Data.FromJson <ControllerInfo>();
                    if (this.Controllers.TryGetValue(controller.ID, out controller))
                    {
                        controller.Available = false;
                        controller.Timestamp = DateTime.Now;
                        this.Services.ForEach(kvp =>
                        {
                            var svcInfo = kvp.Value.FirstOrDefault(svc => svc.Name.IsEquals(kvp.Key) && svc.ControllerID.IsEquals(controller.ID));
                            if (svcInfo != null)
                            {
                                svcInfo.Available = svcInfo.Running = false;
                            }
                        });
                    }
#if DEBUG
                    Global.OnProcess?.Invoke($"A controller was disconnected => {message.ToJson()}");
#endif
                    break;

                case "requestinfo":
                    this.RequestTime = DateTime.Now;
#if DEBUG
                    Global.OnProcess?.Invoke($"Got a request to update information of a controller => {message.ToJson()}");
#endif
                    break;
                }
            }

            // service info
            else if (message.Type.IsEquals("Service#Info"))
            {
                var serviceInfo = message.Data.FromJson <ServiceInfo>();

                if (!this.Services.TryGetValue(serviceInfo.Name.ToLower(), out var services))
                {
                    services = new List <ServiceInfo>();
                    this.Services.TryAdd(serviceInfo.Name.ToLower(), services);
                }

                var svcInfo = services.FirstOrDefault(svc => svc.Name.Equals(serviceInfo.Name) && svc.UniqueName.Equals(serviceInfo.UniqueName) && svc.ControllerID.Equals(serviceInfo.ControllerID));
                if (svcInfo == null)
                {
                    services.Add(serviceInfo);
                }
                else
                {
                    if (svcInfo.Available != serviceInfo.Available || svcInfo.Running != serviceInfo.Running)
                    {
                        svcInfo.Timestamp = DateTime.Now;
                    }
                    svcInfo.Available = serviceInfo.Available;
                    svcInfo.Running   = serviceInfo.Running;
                    if (svcInfo.Running)
                    {
                        svcInfo.InvokeInfo = serviceInfo.InvokeInfo;
                    }
                }

                if (serviceInfo.Running)
                {
                    this.OnServiceStarted?.Invoke(serviceInfo.ControllerID, serviceInfo.Name);
                }
                else
                {
                    this.OnServiceStopped?.Invoke(serviceInfo.ControllerID, serviceInfo.Name);
                }
            }

            // registered handler
            this.OnInterCommunicateMessageReceived?.Invoke(message);
        }