sipsorcery-mono /sipsorcery-core/SIPSorcery.SIP.Core/SIPEvents/Presence/SIPEventPresenceTuple.cs

Language C# Lines 86
MD5 Hash f07b7976b4eca255b709bd3d35dc558b
Repository https://github.com/thecc4re/sipsorcery-mono.git View Raw File
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;
using SIPSorcery.Sys;
using log4net;

namespace SIPSorcery.SIP
{
    public class SIPEventPresenceTuple
    {
        private static readonly string m_pidfXMLNS = SIPEventConsts.PIDF_XML_NAMESPACE_URN;

        public string ID;
        public SIPEventPresenceStateEnum Status;
        public SIPURI ContactURI;
        public decimal ContactPriority = Decimal.Zero;

        private SIPEventPresenceTuple()
        {}

        public SIPEventPresenceTuple(string id, SIPEventPresenceStateEnum status)
        {
            ID = id;
            Status = status;
        }

        public SIPEventPresenceTuple(string id, SIPEventPresenceStateEnum status, SIPURI contactURI, decimal contactPriority)
        {
            ID = id;
            Status = status;
            ContactURI = contactURI;
            ContactPriority = contactPriority;
        }

        public static SIPEventPresenceTuple Parse(string tupleXMLStr)
        {
            XElement tupleElement = XElement.Parse(tupleXMLStr);
            return Parse(tupleElement);
        }

        public static SIPEventPresenceTuple Parse(XElement tupleElement)
        {
            XNamespace ns = m_pidfXMLNS;

            SIPEventPresenceTuple tuple = new SIPEventPresenceTuple();
            tuple.ID = tupleElement.Attribute("id").Value;
            tuple.Status = (SIPEventPresenceStateEnum)Enum.Parse(typeof(SIPEventPresenceStateEnum), tupleElement.Element(ns + "status").Element(ns + "basic").Value, true);
            tuple.ContactURI = (tupleElement.Element(ns + "contact") != null) ? SIPURI.ParseSIPURI(tupleElement.Element(ns + "contact").Value) : null;
            tuple.ContactPriority = (tuple.ContactURI != null && tupleElement.Element(ns + "contact").Attribute("priority") != null) ? Decimal.Parse(tupleElement.Element(ns + "contact").Attribute("priority").Value) : Decimal.Zero;

            return tuple;
        }

        public XElement ToXML()
        {
            XNamespace ns = m_pidfXMLNS;

            XElement tupleElement = new XElement(ns + "tuple",
                new XAttribute("id", ID),
                new XElement(ns + "status", 
                    new XElement(ns + "basic", Status.ToString()))
                );

            if (ContactURI != null)
            {
                XElement contactElement = new XElement(ns + "contact", ContactURI.ToString());
                if (ContactPriority != Decimal.Zero)
                {
                    contactElement.Add(new XAttribute("priority", ContactPriority.ToString("0.###")));
                }
                tupleElement.Add(contactElement);
            }

            return tupleElement;
        }
    }
}
Back to Top