/// <summary>
        /// Determines whether a Jid is a friend of the current device.
        /// </summary>
        /// <param name="Jid">JID to check.</param>
        /// <param name="Callback">Callback method to call, when the response is available.</param>
        /// <param name="State">State object to pass on to the response callback.</param>
        public void IsFriend(string Jid, IsFriendCallback Callback, object State)
        {
            Jid = XmppClient.StripResource(Jid);

            if (Jid == this.address)
            {
                if (Callback != null)
                {
                    IsFriendEventArgs e = new IsFriendEventArgs(Jid, true, false, State);

                    try
                    {
                        Callback(e);
                    } catch (Exception ex)
                    {
                        Log.Exception(ex);
                    }
                }
            }
            else
            {
                string      Xml   = "<isFriend xmlns='urn:xmpp:iot:provisioning' jid='" + Jid + "'/>";
                string      Hash  = this.CalcHash(Xml);
                StanzaError Error = null;
                XmlNodeList Response;

                if ((Response = this.GetCachedResponse(Hash)) != null)
                {
                    this.IsFriendResponse(this.client, string.Empty, Response, ref Error, new object[] { Callback, State, null });
                }
                else
                {
                    this.client.IqGet(Xml, this.address, this.IsFriendResponse, new object[] { Callback, State, Hash }, "Is Friend?");
                }
            }
        }
Beispiel #2
0
        private void GetInterfacesResponse(XmppClient Client, string Type, XmlNodeList Response, ref StanzaError Error, object State)
        {
            object[] P = (object[])State;
            InteroperabilityInterfacesCallback Callback = (InteroperabilityInterfacesCallback)P [0];
            object        State2     = (object)P [1];
            List <string> Interfaces = new List <string> ();
            XmlElement    E;

            if (Error != null)
            {
                Error = null;                   // XEP not supported. Just return an empty list of supported interfaces.
            }
            else
            {
                foreach (XmlNode N in Response)
                {
                    if (N.LocalName == "getInterfacesResponse")
                    {
                        foreach (XmlNode N2 in N.ChildNodes)
                        {
                            if (N2.LocalName == "interface" && (E = N2 as XmlElement) != null)
                            {
                                Interfaces.Add(XmlUtilities.GetAttribute(E, "name", string.Empty));
                            }
                        }
                    }
                }
            }

            if (Callback != null)
            {
                try
                {
                    Callback(Interfaces.ToArray(), State2);
                } catch (Exception ex)
                {
                    Log.Exception(ex);
                }
            }
        }
Beispiel #3
0
        private void GetFormResponse(XmppClient Client, string Type, XmlNodeList Response, ref StanzaError Error, object State)
        {
            ControlFormEventArgs e = (ControlFormEventArgs)State;
            XmlElement           E;

            if (Error != null)
            {
                if (!string.IsNullOrEmpty(Error.Text))
                {
                    e.ErrorMessage = Error.Text;
                }
                else
                {
                    e.ErrorMessage = "Readout rejected by remote device.";
                }

                Error = null;
            }
            else
            {
                foreach (XmlNode N in Response)
                {
                    E = N as XmlElement;
                    if (E == null)
                    {
                        continue;
                    }

                    if (E.LocalName == "x")
                    {
                        e.Form = new XmppDataForm(E);
                    }
                }

                if (e.Form == null)
                {
                    e.ErrorMessage = "Invalid response. No control form returned.";
                }
            }

            e.DoCallback(this);
        }
Beispiel #4
0
 private void UnsubscribeResponse(XmppClient Client, string Type, XmlNodeList Response, ref StanzaError Error, object State)
 {
     if (Error != null)
     {
         Error = null;
     }
 }
Beispiel #5
0
        private void ReqResponse(XmppClient Client, string Type, XmlNodeList Response, ref StanzaError Error, object State)
        {
            SensorDataEventArgs e = (SensorDataEventArgs)State;
            XmlElement          E;

            if (Error != null)
            {
                if (!string.IsNullOrEmpty(Error.Text))
                {
                    e.SetError(Error.Text);
                }
                else
                {
                    e.SetError("Readout rejected by remote device.");
                }

                Error = null;
            }
            else
            {
                foreach (XmlNode N in Response)
                {
                    E = N as XmlElement;
                    if (E == null)
                    {
                        continue;
                    }

                    if (E.LocalName == "accepted")
                    {
                        int SeqNr = XmlUtilities.GetAttribute(E, "seqnr", 0);

                        if (SeqNr == e.SeqNr)
                        {
                            e.ReadoutState = ReadoutState.Accepted;

                            lock (this.synchObj)
                            {
                                this.receiving [SeqNr] = e;
                            }
                        }
                        else
                        {
                            e.SetError("Invalid sequence number returned in response.");
                        }
                    }
                    else if (E.LocalName == "rejected")
                    {
                        e.ReadoutState = ReadoutState.Rejected;
                        e.Done         = true;

                        foreach (XmlNode N2 in E.ChildNodes)
                        {
                            if (N2.LocalName == "error")
                            {
                                if (string.IsNullOrEmpty(e.ErrorMessage))
                                {
                                    e.ErrorMessage = N2.InnerText;
                                }
                                else
                                {
                                    e.ErrorMessage += "\r\n" + N2.InnerText;
                                }
                            }
                        }

                        if (string.IsNullOrEmpty(e.ErrorMessage))
                        {
                            e.ErrorMessage = "Readout rejected by remote device.";
                        }
                    }
                }
            }

            e.DoCallback(this);
        }
        private void CanControlResponse(XmppClient Client, string Type, XmlNodeList Response, ref StanzaError Error, object State)
        {
            object[]           P        = (object[])State;
            CanControlCallback Callback = (CanControlCallback)P [0];
            object             State2   = (object)P [1];
            string             Hash     = (string)P [2];

            string[]        Parameters     = (string[])P [3];
            NodeReference[] NodeReferences = (NodeReference[])P [4];
            bool            CanControl     = false;
            string          Jid            = string.Empty;
            XmlElement      E;

            if (Error != null)
            {
                Error = null;
            }
            else if (Response != null)
            {
                if (Hash != null)
                {
                    this.AddToCache(Hash, Response);
                }

                foreach (XmlNode N in Response)
                {
                    if (N.LocalName == "canControlResponse" && (E = N as XmlElement) != null)
                    {
                        CanControl = XmlUtilities.GetAttribute(E, "result", false);

                        List <NodeReference> Nodes;
                        List <string>        ParameterNames;

                        ReadoutRequest.ParseNodesAndFieldNames(E, out Nodes, out ParameterNames);

                        if (Nodes == null)
                        {
                            NodeReferences = null;
                        }
                        else
                        {
                            NodeReferences = Nodes.ToArray();
                        }

                        if (ParameterNames == null)
                        {
                            Parameters = null;
                        }
                        else
                        {
                            Parameters = ParameterNames.ToArray();
                        }
                        break;
                    }
                }
            }

            CanControlEventArgs e = new CanControlEventArgs(Jid, CanControl, Parameters, NodeReferences, State2);

            if (Callback != null)
            {
                try
                {
                    Callback(e);
                } catch (Exception ex)
                {
                    Log.Exception(ex);
                }
            }
        }
        /// <summary>
        /// Determines whether a control operation can be performed, partially performed or be rejected.
        /// </summary>
        /// <param name="From">JID from which the request was made.</param>
        /// <param name="Callback">Callback method to call, when the response is available.</param>
        /// <param name="State">State object to pass on to the response callback.</param>
        /// <param name="ServiceToken">Optional service token provided in the request.</param>
        /// <param name="DeviceToken">Optional device token provided in the request.</param>
        /// <param name="UserToken">Optional user token provided in the request.</param>
        /// <param name="Parameters">Control parameters to control.</param>
        /// <param name="NodeReferenes">Any node references in the request. Can be null, if none.</param>
        public void CanControl(string From, CanControlCallback Callback, object State, string ServiceToken, string DeviceToken, string UserToken, string[] Parameters, NodeReference[] NodeReferences)
        {
            string BareJid = XmppClient.StripResource(From);

            if (BareJid == this.address)
            {
                if (Callback != null)
                {
                    CanControlEventArgs e = new CanControlEventArgs(BareJid, true, Parameters, NodeReferences, State);

                    try
                    {
                        Callback(e);
                    } catch (Exception ex)
                    {
                        Log.Exception(ex);
                    }
                }
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                XmlWriter     w  = XmlWriter.Create(sb, XmlUtilities.GetXmlWriterSettings(false, true, true));

                w.WriteStartElement("canControl", "urn:xmpp:iot:provisioning");
                w.WriteAttributeString("jid", BareJid);

                if (!string.IsNullOrEmpty(ServiceToken))
                {
                    w.WriteAttributeString("serviceToken", ServiceToken);
                    this.UpdateTokenSource(ServiceToken, From);
                }

                if (!string.IsNullOrEmpty(DeviceToken))
                {
                    w.WriteAttributeString("deviceToken", DeviceToken);
                    this.UpdateTokenSource(DeviceToken, From);
                }

                if (!string.IsNullOrEmpty(UserToken))
                {
                    w.WriteAttributeString("userToken", UserToken);
                    this.UpdateTokenSource(UserToken, From);
                }

                if (NodeReferences != null && NodeReferences.Length > 0)
                {
                    foreach (NodeReference Node in NodeReferences)
                    {
                        w.WriteStartElement("node");
                        w.WriteAttributeString("nodeId", Node.NodeId);

                        if (!string.IsNullOrEmpty(Node.SourceId))
                        {
                            w.WriteAttributeString("sourceId", Node.SourceId);
                        }

                        if (!string.IsNullOrEmpty(Node.CacheType))
                        {
                            w.WriteAttributeString("cacheType", Node.CacheType);
                        }

                        w.WriteEndElement();
                    }
                }

                if (Parameters != null && Parameters.Length > 0)
                {
                    foreach (string Parameter in Parameters)
                    {
                        w.WriteStartElement("parameter");
                        w.WriteAttributeString("name", Parameter);
                        w.WriteEndElement();
                    }
                }

                w.WriteEndElement();

                w.Flush();
                string      Xml   = sb.ToString();
                string      Hash  = this.CalcHash(Xml);
                StanzaError Error = null;
                XmlNodeList Response;

                if ((Response = this.GetCachedResponse(Hash)) != null)
                {
                    this.CanControlResponse(this.client, string.Empty, Response, ref Error, new object[] {
                        Callback,
                        State,
                        null,
                        Parameters,
                        NodeReferences
                    });
                }
                else
                {
                    this.client.IqGet(Xml, this.address, this.CanControlResponse, new object[] {
                        Callback,
                        State,
                        Hash,
                        Parameters,
                        NodeReferences
                    }, "Can Control?");
                }
            }
        }
        private void PropagatedTokenChallengeResponse(XmppClient Client, string Type, XmlNodeList Response, ref StanzaError Error, object State)
        {
            object[]   P         = (object[])State;
            XmppClient OrgClient = (XmppClient)P [0];
            string     OrgFrom   = (string)P [1];
            string     OrgId     = (string)P [2];

            if (Error != null)
            {
                Error = null;
                OrgClient.IqError("<error type='modify'><bad-request xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error>", OrgFrom, OrgId, "Propagated token challenge error");
            }
            else
            {
                StringBuilder sb = new StringBuilder();

                foreach (XmlNode N in Response)
                {
                    if (N is XmlElement)
                    {
                        sb.Append(N.OuterXml);
                    }
                }

                OrgClient.IqResult(sb.ToString(), OrgFrom, OrgId, "Propagated token challenge result");
            }
        }
        /// <summary>
        /// Determines whether a readout can be performed, partially performed or be rejected.
        /// </summary>
        /// <param name="Request">Readout request.</param>
        /// <param name="From">JID from which the request was made.</param>
        /// <param name="Callback">Callback method to call, when the response is available.</param>
        /// <param name="State">State object to pass on to the response callback.</param>
        public void CanRead(ReadoutRequest Request, string From, CanReadCallback Callback, object State)
        {
            string BareJid = XmppClient.StripResource(From);

            if (BareJid == this.address)
            {
                if (Callback != null)
                {
                    CanReadEventArgs e = new CanReadEventArgs(BareJid, true, Request, State);

                    try
                    {
                        Callback(e);
                    } catch (Exception ex)
                    {
                        Log.Exception(ex);
                    }
                }
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                XmlWriter     w  = XmlWriter.Create(sb, XmlUtilities.GetXmlWriterSettings(false, true, true));

                w.WriteStartElement("canRead", "urn:xmpp:iot:provisioning");
                w.WriteAttributeString("jid", BareJid);

                if (!string.IsNullOrEmpty(Request.ServiceToken))
                {
                    w.WriteAttributeString("serviceToken", Request.ServiceToken);
                    this.UpdateTokenSource(Request.ServiceToken, From);
                }

                if (!string.IsNullOrEmpty(Request.DeviceToken))
                {
                    w.WriteAttributeString("deviceToken", Request.DeviceToken);
                    this.UpdateTokenSource(Request.DeviceToken, From);
                }

                if (!string.IsNullOrEmpty(Request.UserToken))
                {
                    w.WriteAttributeString("userToken", Request.UserToken);
                    this.UpdateTokenSource(Request.UserToken, From);
                }

                WriteReadoutTypes(w, Request.Types);
                WriteNodes(w, Request.Nodes);
                WriteFields(w, Request.GetFields());

                w.WriteEndElement();

                w.Flush();
                string      Xml   = sb.ToString();
                string      Hash  = this.CalcHash(Xml);
                StanzaError Error = null;
                XmlNodeList Response;

                if ((Response = this.GetCachedResponse(Hash)) != null)
                {
                    this.CanReadResponse(this.client, string.Empty, Response, ref Error, new object[] {
                        Callback,
                        State,
                        null,
                        Request
                    });
                }
                else
                {
                    this.client.IqGet(Xml, this.address, this.CanReadResponse, new object[] { Callback, State, Hash, Request }, "Can Read?");
                }
            }
        }
        private void IsFriendResponse(XmppClient Client, string Type, XmlNodeList Response, ref StanzaError Error, object State)
        {
            object[]         P        = (object[])State;
            IsFriendCallback Callback = (IsFriendCallback)P [0];
            object           State2   = (object)P [1];
            string           Hash     = (string)P [2];
            bool             IsFriend = false;
            bool             SecondaryTrustAllowed = false;
            string           Jid = string.Empty;
            XmlElement       E;

            if (Error != null)
            {
                Error = null;
            }
            else if (Response != null)
            {
                if (Hash != null)
                {
                    this.AddToCache(Hash, Response);
                }

                foreach (XmlNode N in Response)
                {
                    if (N.LocalName == "isFriendResponse" && (E = N as XmlElement) != null)
                    {
                        Jid      = XmlUtilities.GetAttribute(E, "jid", string.Empty);
                        IsFriend = XmlUtilities.GetAttribute(E, "result", false);
                        SecondaryTrustAllowed = XmlUtilities.GetAttribute(E, "secondaryTrustAllowed", false);
                        break;
                    }
                }
            }

            IsFriendEventArgs e = new IsFriendEventArgs(Jid, IsFriend, SecondaryTrustAllowed, State2);

            if (Callback != null)
            {
                try
                {
                    Callback(e);
                } catch (Exception ex)
                {
                    Log.Exception(ex);
                }
            }
        }
 private void RemoveResponse(XmppClient Client, string Type, XmlNodeList Response, ref StanzaError Error, object State)
 {
     // Do nothing.
 }
        private void RegisterResponse(XmppClient Client, string Type, XmlNodeList Response, ref StanzaError Error, object State)
        {
            // Empty response = OK
            // Response containing claimed = OK, but already claimed. Update node state and send update command.

            if (Response != null)
            {
                XmlElement E;

                foreach (XmlNode N in Response)
                {
                    if (N.LocalName == "claimed" && (E = N as XmlElement) != null)
                    {
                        ClaimedEventHandler h = this.OnClaimed;

                        if (h != null)
                        {
                            try
                            {
                                string   Owner     = XmlUtilities.GetAttribute(E, "jid", string.Empty);
                                bool     Public    = XmlUtilities.GetAttribute(E, "public", false);
                                object[] P         = (object[])State;
                                string   NodeId    = (string)P [0];
                                string   CacheType = (string)P [1];
                                string   SourceId  = (string)P [2];

                                ClaimedEventArgs e = new ClaimedEventArgs(NodeId, SourceId, CacheType, Owner, Public);

                                h(this, e);
                            } catch (Exception ex)
                            {
                                Log.Exception(ex);
                            }
                        }
                    }
                }
            }
        }