Exemplo n.º 1
0
 internal SubRec(Topic t, Action <Perform, SubRec> func, SubRec.SubMask mask, string prefix)
 {
     this.setTopic = t;
     this.func     = func;
     this.mask     = mask;
     this.prefix   = (prefix == null && (mask & SubMask.Field) == SubMask.Field)?string.Empty:prefix;
 }
Exemplo n.º 2
0
        public SubRec Subscribe(SubRec.SubMask mask, string prefix, Action <Perform, SubRec> func)
        {
            if (func == null)
            {
                throw new ArgumentNullException(this.path + ".Subscribe(func == NULL, " + mask.ToString() + (prefix == null ? string.Empty : ", " + prefix) + ")");
            }
            SubRec sb;
            bool   exist = true;

            if (_subRecords == null)
            {
                lock (this._sync) {
                    if (_subRecords == null)
                    {
                        var srs = new List <SubRec>();
                        _subRecords = srs; // PVS V3054. Potentially unsafe double-checked locking.
                    }
                }
            }
            lock (_subRecords) {
                sb = _subRecords.FirstOrDefault(z => z.func == func && z.setTopic == this && z.mask == mask && ((z.mask & SubRec.SubMask.Field) == SubRec.SubMask.None || z.prefix == prefix));
                if (sb == null)
                {
                    exist = false;
                    sb    = new SubRec(this, func, mask, prefix);
                    _subRecords.Add(sb);
                }
            }
            if (!exist)
            {
                var c = Perform.Create(this, Perform.Art.subscribe, this);
                c.o = sb;
                _repo.DoCmd(c, false);
            }
            else
            {
                var c = Perform.Create(this, Perform.Art.subAck, this);
                c.o = sb;
                _repo.DoCmd(c, false);
            }
            return(sb);
        }
Exemplo n.º 3
0
        protected override void OnMessage(MessageEventArgs e)
        {
            string[] sa;
            if (e.IsText && !string.IsNullOrEmpty(e.Data) && (sa = e.Data.Split('\t')) != null && sa.Length > 0)
            {
                if (WebUI_Pl.verbose)
                {
                    X13.Log.Debug("ws.msg({0})", string.Join(", ", sa));
                }
                if (sa[0] == "C" && sa.Length == 3) // Connect, username, password

                /*if((sa[1]!="local" || _ses.ip.IsLocal()) && MQTT.MqBroker.CheckAuth(sa[1], sa[2])) {
                 * _ses.userName=sa[1];
                 * Send("C\ttrue");
                 * if(WebUI_Pl.verbose) {
                 *  X13.Log.Info("{0} logon as {1} success", _ses.owner.name, _ses.ToString());
                 * }
                 * } else */{
                    {
                        Send("C\tfalse");
                        if (WebUI_Pl.verbose)
                        {
                            X13.Log.Warning("{0}@{2} logon  as {1} failed", _ses.owner.name, sa[1], _ses.owner.GetState());
                        }
                        Sessions.CloseSession(base.ID);
                    }
                }
                else if (/*!_disAnonym.value || */ (_ses != null /*&& !string.IsNullOrEmpty(_ses.userName)*/))
                {
                    if (sa[0] == "P" && sa.Length == 3)
                    {
                        if (sa[1] != null && (sa[1].StartsWith("/export/") || CheckAccess(sa[1])))
                        {
                            WebUI_Pl.ProcessPublish(sa[1], sa[2], _ses);
                        }
                        else
                        {
                            X13.Log.Warning("{0}.publish({1}) - access forbinden", (_ses == null || _ses.owner == null)?"UNK":_ses.owner.name, sa[1]);
                        }
                    }
                    else if (sa[0] == "S" && sa.Length == 2)
                    {
                        if (sa[1] != null && (sa[1].StartsWith("/export/") || CheckAccess(sa[1])))
                        {
                            string         p    = sa[1];
                            SubRec.SubMask mask = Repository.SubRec.SubMask.Value;
                            Topic          t;
                            int            idx = p.IndexOfAny(new[] { '+', '#' });
                            if (idx < 0)
                            {
                                mask |= SubRec.SubMask.Once;
                            }
                            else if (idx == p.Length - 1 && p[idx - 1] == '/')
                            {
                                mask |= p[idx] == '#'?SubRec.SubMask.All:SubRec.SubMask.Chldren;
                                p     = p.Substring(0, p.Length - 2);
                            }
                            else
                            {
                                X13.Log.Warning("{0}.subscribe({1}) - access forbinden", (_ses == null || _ses.owner == null)?"UNK":_ses.owner.name, sa[1]);
                                return;
                            }
                            if (Topic.root.Exist(p, out t))
                            {
                                _subscriptions.Add(t.Subscribe(mask, SubChanged));
                            }
                            else
                            {
                                X13.Log.Warning("{0}.subscribe({1}) - path not exist", (_ses == null || _ses.owner == null)?"UNK":_ses.owner.name, sa[1]);
                            }
                        }
                        else
                        {
                            X13.Log.Warning("{0}.subscribe({1}) - bad path", (_ses == null || _ses.owner == null)?"UNK":_ses.owner.name, sa[1]);
                        }
                    }
                }
            }
        }
Exemplo n.º 4
0
 public SubRec Subscribe(SubRec.SubMask mask, Action <Perform, SubRec> func)
 {
     return(Subscribe(mask, null, func));
 }
Exemplo n.º 5
0
        public MqSite(MQTTPl pl, MqClient client, Topic owner, Uri uUri)
        {
            this.Client = client;
            this.Owner  = owner;
            this._pl    = pl;
            this._uri   = uUri;

            _subEn      = ReadFlag("MQTT.subscribe", true);
            _pubEn      = ReadFlag("MQTT.publish", true);
            _retainedEn = ReadFlag("MQTT.retained", false);

            remotePath = _uri.PathAndQuery + _uri.Fragment;
            var sl = remotePath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

            remotePrefix = string.Empty;
            _mask        = SubRec.SubMask.Value;
            for (int i = 0; i < sl.Length; i++)
            {
                if (sl[i] == "+")
                {
                    _mask |= SubRec.SubMask.Chldren;
                    break;
                }
                if (sl[i] == "#")
                {
                    _mask |= SubRec.SubMask.All;
                    break;
                }
                remotePrefix = remotePrefix + "/" + sl[i];
            }
            Client.Sites.Add(this);
            if (Client.status == MqClient.Status.Connected)
            {
                this.Connected();
            }
            var act = this.Owner.GetField("Action");

            JSC.JSValue txt;
            if (act == null || !act.Any(z => z.Value.ValueType == JSC.JSValueType.Object && (txt = z.Value["name"]).ValueType == JSC.JSValueType.String && (txt.Value as string) == "MQTT.Reconnect"))
            {
                int       i;
                JSL.Array act_n;
                if (act == null)
                {
                    act_n = new JSL.Array(1);
                    i     = 0;
                }
                else
                {
                    int j = act.Count();
                    act_n = new JSL.Array(j + 1);
                    for (i = 0; i < j; i++)
                    {
                        act_n[i] = act[i.ToString()];
                    }
                }
                var r_a = JSC.JSObject.CreateObject();
                r_a["name"] = "MQTT.Reconnect";
                r_a["text"] = "Reconnect MQTT connection";
                act_n[i]    = r_a;
                this.Owner.SetField("Action", act_n);
            }
        }