PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/sipsorcery-core/SIPSorcery.SIP.Core/SIPEvents/Dialog/SIPEventDialogParticipant.cs

https://github.com/thecc4re/sipsorcery-mono
C# | 152 lines | 92 code | 18 blank | 42 comment | 16 complexity | 5def853ddbe62d42f00fe2402ef7a0d8 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. // ============================================================================
  2. // FileName: SIPEventDialog.cs
  3. //
  4. // Description:
  5. // Represents a child level XML element on a SIP event dialog payload that contains the specifics
  6. // of a participant in a dialog as described in:
  7. // RFC4235 "An INVITE-Initiated Dialog Event Package for the Session Initiation Protocol (SIP)".
  8. //
  9. // Author(s):
  10. // Aaron Clauson
  11. //
  12. // History:
  13. // 28 Feb 2010 Aaron Clauson Created.
  14. //
  15. // License:
  16. // This software is licensed under the BSD License http://www.opensource.org/licenses/bsd-license.php
  17. //
  18. // Copyright (c) 2010 Aaron Clauson (aaron@sipsorcery.com), SIPSorcery Ltd, London, UK (www.sipsorcery.com)
  19. // All rights reserved.
  20. //
  21. // Redistribution and use in source and binary forms, with or without modification, are permitted provided that
  22. // the following conditions are met:
  23. //
  24. // Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  25. // Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
  26. // disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of Blue Face Ltd.
  27. // nor the names of its contributors may be used to endorse or promote products derived from this software without specific
  28. // prior written permission.
  29. //
  30. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
  31. // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  32. // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  33. // OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  34. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  35. // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. // POSSIBILITY OF SUCH DAMAGE.
  37. // ============================================================================
  38. using System;
  39. using System.Collections.Generic;
  40. using System.Linq;
  41. using System.Text;
  42. using System.Text.RegularExpressions;
  43. using System.Xml.Linq;
  44. using SIPSorcery.Sys;
  45. namespace SIPSorcery.SIP
  46. {
  47. public class SIPEventDialogParticipant
  48. {
  49. private static readonly string m_dialogXMLNS = SIPEventConsts.DIALOG_XML_NAMESPACE_URN;
  50. private static readonly string m_sipsorceryXMLNS = SIPEventConsts.SIPSORCERY_DIALOG_XML_NAMESPACE_URN;
  51. public string DisplayName;
  52. public SIPURI URI;
  53. public SIPURI TargetURI;
  54. public int CSeq;
  55. public string SwitchboardDescription; // A user definable field that can attach a description to the SIP account the call was received on or the call destination.
  56. public string SwitchboardCallerDescription; // A user definable field that can attach a description about the caller.
  57. private SIPEventDialogParticipant()
  58. { }
  59. public SIPEventDialogParticipant(string displayName, SIPURI uri, SIPURI targetURI, int cseq)
  60. {
  61. DisplayName = displayName;
  62. URI = uri;
  63. TargetURI = targetURI;
  64. CSeq = cseq;
  65. }
  66. public static SIPEventDialogParticipant Parse(string participantXMLStr)
  67. {
  68. XElement dialogElement = XElement.Parse(participantXMLStr);
  69. return Parse(dialogElement);
  70. }
  71. public static SIPEventDialogParticipant Parse(XElement participantElement)
  72. {
  73. XNamespace ns = m_dialogXMLNS;
  74. XNamespace ss = m_sipsorceryXMLNS;
  75. SIPEventDialogParticipant participant = new SIPEventDialogParticipant();
  76. XElement identityElement = participantElement.Element(ns + "identity");
  77. if (identityElement != null)
  78. {
  79. participant.DisplayName = (identityElement.Attribute("display-name") != null) ? identityElement.Attribute("display-name").Value : null;
  80. participant.URI = SIPURI.ParseSIPURI(identityElement.Value);
  81. }
  82. XElement targetElement = participantElement.Element(ns + "target");
  83. if (targetElement != null)
  84. {
  85. participant.TargetURI = SIPURI.ParseSIPURI(targetElement.Attribute("uri").Value);
  86. }
  87. participant.CSeq = (participantElement.Element(ns + "cseq") != null) ? Convert.ToInt32(participantElement.Element(ns + "cseq").Value) : 0;
  88. participant.SwitchboardDescription = (participantElement.Element(ss + "switchboarddescription") != null) ? participantElement.Element(ss + "switchboarddescription").Value : null;
  89. participant.SwitchboardCallerDescription = (participantElement.Element(ss + "switchboardcallerdescription") != null) ? participantElement.Element(ss + "switchboardcallerdescription").Value : null;
  90. return participant;
  91. }
  92. /// <summary>
  93. /// Puts the dialog participant information to an XML element.
  94. /// </summary>
  95. /// <param name="nodeName">A participant can represent a local or remote party, the node name needs to be set to either "local" or "remote".</param>
  96. /// <returns>An XML element representing the dialog participant.</returns>
  97. public XElement ToXML(string nodeName)
  98. {
  99. XNamespace ns = m_dialogXMLNS;
  100. XNamespace ss = m_sipsorceryXMLNS;
  101. XElement participantElement = new XElement(ns + nodeName);
  102. if(URI != null)
  103. {
  104. XElement identityElement = new XElement(ns + "identity", URI.ToString());
  105. if(!DisplayName.IsNullOrBlank())
  106. {
  107. identityElement.Add(new XAttribute("display-name", DisplayName));
  108. }
  109. participantElement.Add(identityElement);
  110. }
  111. if(TargetURI != null)
  112. {
  113. XElement targetElement = new XElement(ns + "target", new XAttribute("uri", TargetURI.ToString()));
  114. participantElement.Add(targetElement);
  115. }
  116. if(CSeq > 0)
  117. {
  118. XElement cseqElement = new XElement(ns + "cseq", CSeq);
  119. participantElement.Add(cseqElement);
  120. }
  121. if (!SwitchboardDescription.IsNullOrBlank())
  122. {
  123. XElement switchDescriptionElement = new XElement(ss + "switchboarddescription", SwitchboardDescription);
  124. participantElement.Add(switchDescriptionElement);
  125. }
  126. if (!SwitchboardCallerDescription.IsNullOrBlank())
  127. {
  128. XElement switchCallerDescriptionElement = new XElement(ss + "switchboardcallerdescription", SwitchboardCallerDescription);
  129. participantElement.Add(switchCallerDescriptionElement);
  130. }
  131. return participantElement;
  132. }
  133. }
  134. }