Ejemplo n.º 1
0
        public T GetLabelByName <T>(string name) where T : LinkUpLabel, new()
        {
            lock (_Labels)
            {
                LinkUpLabel label = _Labels.FirstOrDefault(c => c.Name.Equals(name) && c is T);
                if (label != null)
                {
                    return((T)label);
                }
                else
                {
                    if (_Labels.Any(c => c.Name.Equals(name)))
                    {
                        throw new Exception(string.Format("Label {0} has wrong type.", name));
                    }
                    else
                    {
                        label      = new T();
                        label.Name = name;
                        _Labels.Add(label);

                        return((T)label);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private void _Connector_ReveivedPacket(LinkUpConnector connector, LinkUpPacket packet)
        {
            try
            {
                LinkUpLogic logic = LinkUpLogic.ParseFromPacket(packet);
                if (logic is LinkUpNameRequest)
                {
                    LinkUpNameRequest nameRequest = logic as LinkUpNameRequest;

                    if (nameRequest.LabelType == LinkUpLabelType.Node)
                    {
                        _IsInitialized = true;
                        _Name          = nameRequest.Name;

                        LinkUpNameResponse nameResponse = new LinkUpNameResponse();
                        nameResponse.Name       = nameRequest.Name;
                        nameResponse.Identifier = 0;
                        nameResponse.LabelType  = LinkUpLabelType.Node;
                        //_Master.RemoveLabels(_Name);
                        Task.Run(() =>
                        {
                            _Connector.SendPacket(nameResponse.ToPacket());
                        });
                    }
                    if (nameRequest.LabelType != LinkUpLabelType.Node)
                    {
                        LinkUpLabel label = _Master.AddSubLabel(string.Format("{0}/{1}", _Name, nameRequest.Name), nameRequest.LabelType, nameRequest.Options);
                        if (label.ChildIdentifier == 0)
                        {
                            label.ChildIdentifier = GetNextIdentifier();
                            //Console.WriteLine("ID: {0} Name: {1}", label.ChildIdentifier, label.Name);
                        }
                        label.Owner = this;

                        LinkUpNameResponse nameResponse = new LinkUpNameResponse();
                        nameResponse.Name       = nameRequest.Name;
                        nameResponse.Identifier = label.ChildIdentifier;
                        nameResponse.LabelType  = nameRequest.LabelType;

                        _Connector.SendPacket(nameResponse.ToPacket());

                        if (label.LabelType == LinkUpLabelType.Event)
                        {
                            Task.Run(() =>
                            {
                                try
                                {
                                    (label as LinkUpEventLabel).Resubscribe();
                                }
                                catch (Exception ex) { }
                            });
                        }
                    }
                }
                else if (logic is LinkUpEventFireRequest)
                {
                    LinkUpEventFireRequest eventFireRequest = (LinkUpEventFireRequest)logic;
                    LinkUpLabel            label            = _Master.Labels.FirstOrDefault(c => c.ChildIdentifier == eventFireRequest.Identifier);
                    if (label != null)
                    {
                        if (label is LinkUpEventLabel)
                        {
                            (label as LinkUpEventLabel).DoEvent(eventFireRequest.Data);
                        }
                    }
                }
                else if (logic is LinkUpEventSubscribeResponse)
                {
                    LinkUpEventSubscribeResponse eventSubscribeResponse = (LinkUpEventSubscribeResponse)logic;
                    LinkUpLabel label = _Master.Labels.FirstOrDefault(c => c.ChildIdentifier == eventSubscribeResponse.Identifier);
                    if (label != null)
                    {
                        if (label is LinkUpEventLabel)
                        {
                            (label as LinkUpEventLabel).SubscribeDone();
                        }
                    }
                }
                else if (logic is LinkUpEventUnsubscribeResponse)
                {
                    LinkUpEventUnsubscribeResponse eventUnsubscribeResponse = (LinkUpEventUnsubscribeResponse)logic;
                    LinkUpLabel label = _Master.Labels.FirstOrDefault(c => c.ChildIdentifier == eventUnsubscribeResponse.Identifier);
                    if (label != null)
                    {
                        if (label is LinkUpEventLabel)
                        {
                            (label as LinkUpEventLabel).UnsubscribeDone();
                        }
                    }
                }
                else if (logic is LinkUpPropertyGetResponse)
                {
                    LinkUpPropertyGetResponse propertyGetResponse = (LinkUpPropertyGetResponse)logic;
                    LinkUpLabel label = _Master.Labels.FirstOrDefault(c => c.ChildIdentifier == propertyGetResponse.Identifier);
                    if (label != null)
                    {
                        if (label is LinkUpPropertyLabelBase)
                        {
                            (label as LinkUpPropertyLabelBase).GetDone(propertyGetResponse.Data);
                        }
                    }
                }
                else if (logic is LinkUpPropertySetResponse)
                {
                    LinkUpPropertySetResponse propertySetResponse = (LinkUpPropertySetResponse)logic;
                    LinkUpLabel label = _Master.Labels.FirstOrDefault(c => c.ChildIdentifier == propertySetResponse.Identifier);
                    if (label != null)
                    {
                        if (label is LinkUpPropertyLabelBase)
                        {
                            (label as LinkUpPropertyLabelBase).SetDone();
                        }
                    }
                }
                else if (logic is LinkUpFunctionCallResponse)
                {
                    LinkUpFunctionCallResponse functionCallResponse = (LinkUpFunctionCallResponse)logic;
                    LinkUpLabel label = _Master.Labels.FirstOrDefault(c => c.ChildIdentifier == functionCallResponse.Identifier);
                    if (label != null)
                    {
                        if (label is LinkUpFunctionLabel)
                        {
                            (label as LinkUpFunctionLabel).DoEvent(functionCallResponse.Data);
                        }
                    }
                }
                else if (logic is LinkUpPingResponse)
                {
                    _LostPings = 0;
                }
            }
            catch (Exception)
            {
            }
        }