PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://github.com/thecc4re/sipsorcery-mono
C# | 172 lines | 111 code | 19 blank | 42 comment | 40 complexity | d14886f15449b210d2386ca3f4f2fd14 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 an individual 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.Xml.Linq;
  43. using SIPSorcery.Sys;
  44. namespace SIPSorcery.SIP
  45. {
  46. public class SIPEventDialog
  47. {
  48. private static readonly string m_dialogXMLNS = SIPEventConsts.DIALOG_XML_NAMESPACE_URN;
  49. private static readonly string m_sipsorceryXMLNS = SIPEventConsts.SIPSORCERY_DIALOG_XML_NAMESPACE_URN;
  50. public string ID; // The ID is a only mandatory attribute for a dialog element.
  51. public string CallID;
  52. public string LocalTag;
  53. public string RemoteTag;
  54. public SIPEventDialogDirectionEnum Direction; // Optional setting indicating whether this dialog was initiated by a sipsorcery user or not.
  55. public string State; // The state a mandatory value for a dialog element.
  56. public int StateCode;
  57. public SIPEventDialogStateEvent StateEvent;
  58. public int Duration;
  59. public SIPEventDialogParticipant LocalParticipant;
  60. public SIPEventDialogParticipant RemoteParticipant;
  61. public string BridgeID; // SIPSorcery custom field that is used to show when two dialogues are bridged together by the B2BUA.
  62. public string SwitchboardOwner; // SIP Sorcery custom filed that can be used to specify a sub-account as the owner of the call this dialog belongs to.
  63. private SIPEventDialog()
  64. { }
  65. public SIPEventDialog(string id, string state, SIPDialogue sipDialogue)
  66. {
  67. ID = id;
  68. State = state;
  69. if (sipDialogue != null)
  70. {
  71. CallID = sipDialogue.CallId;
  72. LocalTag = sipDialogue.LocalTag;
  73. RemoteTag = sipDialogue.RemoteTag;
  74. Direction = (sipDialogue.Direction == SIPCallDirection.In) ? SIPEventDialogDirectionEnum.recipient : SIPEventDialogDirectionEnum.initiator;
  75. Duration = Convert.ToInt32((DateTimeOffset.UtcNow - sipDialogue.Inserted).TotalSeconds % Int32.MaxValue);
  76. //LocalParticipant = new SIPEventDialogParticipant(sipDialogue.LocalUserField.Name, sipDialogue.LocalUserField.URI, null, sipDialogue.CSeq, sipDialogue.SDP);
  77. LocalParticipant = new SIPEventDialogParticipant(sipDialogue.LocalUserField.Name, sipDialogue.LocalUserField.URI, null, sipDialogue.CSeq);
  78. //RemoteParticipant = new SIPEventDialogParticipant(sipDialogue.RemoteUserField.Name, sipDialogue.RemoteUserField.URI, sipDialogue.RemoteTarget, sipDialogue.CSeq, sipDialogue.RemoteSDP);
  79. RemoteParticipant = new SIPEventDialogParticipant(sipDialogue.RemoteUserField.Name, sipDialogue.RemoteUserField.URI, sipDialogue.RemoteTarget, sipDialogue.CSeq);
  80. BridgeID = (sipDialogue.BridgeId != Guid.Empty) ? sipDialogue.BridgeId.ToString() : null;
  81. SwitchboardOwner = (sipDialogue.SwitchboardOwner != null) ? sipDialogue.SwitchboardOwner : null;
  82. if (sipDialogue.Direction == SIPCallDirection.In)
  83. {
  84. RemoteParticipant.SwitchboardCallerDescription = sipDialogue.SwitchboardCallerDescription;
  85. LocalParticipant.SwitchboardDescription = sipDialogue.SwitchboardDescription;
  86. }
  87. else if (sipDialogue.Direction == SIPCallDirection.Out)
  88. {
  89. LocalParticipant.SwitchboardCallerDescription = sipDialogue.SwitchboardCallerDescription;
  90. RemoteParticipant.SwitchboardDescription = sipDialogue.SwitchboardDescription;
  91. }
  92. }
  93. }
  94. public SIPEventDialog(string id, string state, int stateCode, SIPEventDialogStateEvent stateEvent, int duration)
  95. {
  96. ID = id;
  97. State = state;
  98. StateCode = stateCode;
  99. StateEvent = stateEvent;
  100. Duration = duration;
  101. }
  102. public static SIPEventDialog Parse(string dialogXMLStr)
  103. {
  104. XElement dialogElement = XElement.Parse(dialogXMLStr);
  105. return Parse(dialogElement);
  106. }
  107. public static SIPEventDialog Parse(XElement dialogElement)
  108. {
  109. XNamespace ns = m_dialogXMLNS;
  110. XNamespace ss = m_sipsorceryXMLNS;
  111. SIPEventDialog eventDialog = new SIPEventDialog();
  112. eventDialog.ID = dialogElement.Attribute("id").Value;
  113. eventDialog.CallID = (dialogElement.Attribute("call-id") != null) ? dialogElement.Attribute("call-id").Value : null;
  114. eventDialog.LocalTag = (dialogElement.Attribute("local-tag") != null) ? dialogElement.Attribute("local-tag").Value : null;
  115. eventDialog.RemoteTag = (dialogElement.Attribute("remote-tag") != null) ? dialogElement.Attribute("remote-tag").Value : null;
  116. eventDialog.Direction = (dialogElement.Attribute("direction") != null) ? (SIPEventDialogDirectionEnum)Enum.Parse(typeof(SIPEventDialogDirectionEnum), dialogElement.Attribute("direction").Value, true) : SIPEventDialogDirectionEnum.none;
  117. XElement stateElement = dialogElement.Element(ns + "state");
  118. eventDialog.State = stateElement.Value;
  119. eventDialog.StateCode = (stateElement.Attribute("code") != null) ? Convert.ToInt32(stateElement.Attribute("code").Value) : 0;
  120. eventDialog.StateEvent = (stateElement.Attribute("event") != null) ? SIPEventDialogStateEvent.Parse(stateElement.Attribute("event").Value) : SIPEventDialogStateEvent.None;
  121. eventDialog.Duration = (dialogElement.Element(ns + "duration") != null) ? Convert.ToInt32(dialogElement.Element(ns + "duration").Value) : 0;
  122. eventDialog.BridgeID = (dialogElement.Element(ss + "bridgeid") != null) ? dialogElement.Element(ss + "bridgeid").Value : null;
  123. eventDialog.SwitchboardOwner = (dialogElement.Element(ss + "switchboardowner") != null) ? dialogElement.Element(ss + "switchboardowner").Value : null;
  124. eventDialog.LocalParticipant = (dialogElement.Element(ns + "local") != null) ? SIPEventDialogParticipant.Parse(dialogElement.Element(ns + "local")) : null;
  125. eventDialog.RemoteParticipant = (dialogElement.Element(ns + "remote") != null) ? SIPEventDialogParticipant.Parse(dialogElement.Element(ns + "remote")) : null;
  126. return eventDialog;
  127. }
  128. public XElement ToXML()
  129. {
  130. XNamespace ns = m_dialogXMLNS;
  131. XNamespace ss = m_sipsorceryXMLNS;
  132. XElement eventDialogElement = new XElement(ns + "dialog",
  133. new XAttribute("id", ID),
  134. new XElement(ns + "state", State)
  135. );
  136. // Add the optional information if available.
  137. if (!CallID.IsNullOrBlank()) { eventDialogElement.Add(new XAttribute("call-id", CallID)); }
  138. if (!LocalTag.IsNullOrBlank()) { eventDialogElement.Add(new XAttribute("local-tag", LocalTag)); }
  139. if (!RemoteTag.IsNullOrBlank()) { eventDialogElement.Add(new XAttribute("remote-tag", RemoteTag)); }
  140. if (Direction != SIPEventDialogDirectionEnum.none) { eventDialogElement.Add(new XAttribute("direction", Direction)); }
  141. if (StateCode != 0) { eventDialogElement.Element(ns + "state").Add(new XAttribute("code", StateCode)); }
  142. if (StateEvent != SIPEventDialogStateEvent.None) { eventDialogElement.Element(ns + "state").Add(new XAttribute("event", StateEvent.ToString())); }
  143. if (Duration != 0) { eventDialogElement.Add(new XElement(ns + "duration", Duration)); }
  144. if (BridgeID != null) { eventDialogElement.Add(new XElement(ss + "bridgeid", BridgeID)); }
  145. if (SwitchboardOwner != null) { eventDialogElement.Add(new XElement(ss + "switchboardowner", SwitchboardOwner)); }
  146. //if (LocalParticipant != null) { eventDialogElement.Add(LocalParticipant.ToXML("local", filter)); }
  147. if (LocalParticipant != null) { eventDialogElement.Add(LocalParticipant.ToXML("local")); }
  148. //if (RemoteParticipant != null) { eventDialogElement.Add(RemoteParticipant.ToXML("remote", filter)); }
  149. if (RemoteParticipant != null) { eventDialogElement.Add(RemoteParticipant.ToXML("remote")); }
  150. return eventDialogElement;
  151. }
  152. }
  153. }