PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/sipsorcery-core/SIPSorcery.SIP.App/SIPNotifications/SIPDialogEventSubscription.cs

https://github.com/thecc4re/sipsorcery-mono
C# | 164 lines | 140 code | 22 blank | 2 comment | 21 complexity | 474b9b9b03d6963fe1184be42bddf9e3 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using SIPSorcery.Persistence;
  6. namespace SIPSorcery.SIP.App
  7. {
  8. public class SIPDialogEventSubscription : SIPEventSubscription
  9. {
  10. private const int MAX_DIALOGUES_FOR_NOTIFY = 25;
  11. private static string m_contentType = SIPMIMETypes.DIALOG_INFO_CONTENT_TYPE;
  12. private SIPEventDialogInfo DialogInfo;
  13. private SIPAssetGetListDelegate<SIPDialogueAsset> GetDialogues_External;
  14. private SIPAssetGetByIdDelegate<SIPDialogueAsset> GetDialogue_External;
  15. public override SIPEventPackage SubscriptionEventPackage
  16. {
  17. get { return SIPEventPackage.Dialog; }
  18. }
  19. public override string MonitorFilter
  20. {
  21. get { return "dialog " + ResourceURI.ToString(); }
  22. }
  23. public override string NotifyContentType
  24. {
  25. get { return m_contentType; }
  26. }
  27. public SIPDialogEventSubscription(
  28. SIPMonitorLogDelegate log,
  29. string sessionID,
  30. SIPURI resourceURI,
  31. SIPURI canonincalResourceURI,
  32. string filter,
  33. SIPDialogue subscriptionDialogue,
  34. int expiry,
  35. SIPAssetGetListDelegate<SIPDialogueAsset> getDialogues,
  36. SIPAssetGetByIdDelegate<SIPDialogueAsset> getDialogue
  37. )
  38. : base(log, sessionID, resourceURI, canonincalResourceURI, filter, subscriptionDialogue, expiry)
  39. {
  40. GetDialogues_External = getDialogues;
  41. GetDialogue_External = getDialogue;
  42. DialogInfo = new SIPEventDialogInfo(0, SIPEventDialogInfoStateEnum.full, resourceURI);
  43. }
  44. public override void GetFullState()
  45. {
  46. try
  47. {
  48. DialogInfo.State = SIPEventDialogInfoStateEnum.full;
  49. List<SIPDialogueAsset> dialogueAssets = GetDialogues_External(d => d.Owner == SubscriptionDialogue.Owner, "Inserted", 0, MAX_DIALOGUES_FOR_NOTIFY);
  50. foreach (SIPDialogueAsset dialogueAsset in dialogueAssets)
  51. {
  52. DialogInfo.DialogItems.Add(new SIPEventDialog(dialogueAsset.SIPDialogue.Id.ToString(), "confirmed", dialogueAsset.SIPDialogue));
  53. }
  54. MonitorLogEvent_External(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Notifier, SIPMonitorEventTypesEnum.NotifySent, "Full state notification for dialog and " + ResourceURI.ToString() + ".", SubscriptionDialogue.Owner));
  55. }
  56. catch (Exception excp)
  57. {
  58. logger.Error("Exception SIPDialogEventSubscription GetFullState. " + excp.Message);
  59. }
  60. }
  61. public override string GetNotifyBody()
  62. {
  63. return DialogInfo.ToXMLText();
  64. }
  65. public override bool AddMonitorEvent(SIPMonitorMachineEvent machineEvent)
  66. {
  67. try
  68. {
  69. MonitorLogEvent_External(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Notifier, SIPMonitorEventTypesEnum.Monitor, "Monitor event " + machineEvent.MachineEventType + " dialog " + ResourceURI.ToString() + " (ID " + machineEvent.ResourceID + ").", SubscriptionDialogue.Owner));
  70. string state = GetStateForEventType(machineEvent.MachineEventType);
  71. if (machineEvent.MachineEventType == SIPMonitorMachineEventTypesEnum.SIPDialogueRemoved)
  72. {
  73. DialogInfo.DialogItems.Add(new SIPEventDialog(machineEvent.ResourceID, state, null));
  74. return true;
  75. }
  76. else
  77. {
  78. SIPDialogueAsset sipDialogue = GetDialogue_External(new Guid(machineEvent.ResourceID));
  79. if (sipDialogue == null)
  80. {
  81. // Couldn't find the dialogue in the database so it must be terminated.
  82. DialogInfo.DialogItems.Add(new SIPEventDialog(machineEvent.ResourceID, "terminated", null));
  83. return true;
  84. }
  85. else if (machineEvent.MachineEventType == SIPMonitorMachineEventTypesEnum.SIPDialogueTransfer)
  86. {
  87. // For dialog transfer events add both dialogs involved to the notification.
  88. DialogInfo.DialogItems.Add(new SIPEventDialog(sipDialogue.Id.ToString(), state, sipDialogue.SIPDialogue));
  89. if (sipDialogue.SIPDialogue.BridgeId != Guid.Empty)
  90. {
  91. SIPDialogueAsset bridgedDialogue = GetDialogues_External(d => d.BridgeId == sipDialogue.BridgeId && d.Id != sipDialogue.Id, null, 0, 1).FirstOrDefault();
  92. if (bridgedDialogue != null)
  93. {
  94. DialogInfo.DialogItems.Add(new SIPEventDialog(bridgedDialogue.Id.ToString(), state, bridgedDialogue.SIPDialogue));
  95. }
  96. }
  97. return true;
  98. }
  99. else
  100. {
  101. DialogInfo.DialogItems.Add(new SIPEventDialog(sipDialogue.Id.ToString(), state, sipDialogue.SIPDialogue));
  102. return true;
  103. }
  104. }
  105. }
  106. catch (Exception excp)
  107. {
  108. logger.Error("Exception SIPDialogEventSubscription AddMonitorEvent. " + excp.Message);
  109. throw;
  110. }
  111. }
  112. public override void NotificationSent()
  113. {
  114. if (DialogInfo.State == SIPEventDialogInfoStateEnum.full)
  115. {
  116. MonitorLogEvent_External(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Notifier, SIPMonitorEventTypesEnum.NotifySent, "Full state notification sent for " + DialogInfo.Entity.ToString() + ", version " + DialogInfo.Version + ", cseq " + SubscriptionDialogue.CSeq + ".", SubscriptionDialogue.Owner));
  117. }
  118. else
  119. {
  120. foreach (SIPEventDialog dialog in DialogInfo.DialogItems)
  121. {
  122. string remoteURI = (dialog.RemoteParticipant != null && dialog.RemoteParticipant.URI != null) ? ", " + dialog.RemoteParticipant.URI.ToString() : null;
  123. MonitorLogEvent_External(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Notifier, SIPMonitorEventTypesEnum.NotifySent, "Partial state notification sent for " + DialogInfo.Entity.ToString() + " dialog ID " + dialog.ID + " " + dialog.State + ", version " + DialogInfo.Version + remoteURI + ", cseq " + SubscriptionDialogue.CSeq + ".", SubscriptionDialogue.Owner));
  124. }
  125. }
  126. DialogInfo.State = SIPEventDialogInfoStateEnum.partial;
  127. DialogInfo.DialogItems.Clear();
  128. DialogInfo.Version++;
  129. }
  130. private string GetStateForEventType(SIPMonitorMachineEventTypesEnum machineEventType)
  131. {
  132. switch (machineEventType)
  133. {
  134. case SIPMonitorMachineEventTypesEnum.SIPDialogueCreated: return "confirmed";
  135. case SIPMonitorMachineEventTypesEnum.SIPDialogueRemoved: return "terminated";
  136. case SIPMonitorMachineEventTypesEnum.SIPDialogueUpdated: return "updated";
  137. case SIPMonitorMachineEventTypesEnum.SIPDialogueTransfer: return "updated";
  138. default: throw new ApplicationException("The state for a dialog SIP event could not be determined from the monitor event type of " + machineEventType + ".");
  139. }
  140. }
  141. }
  142. }