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

Language C# Lines 165
MD5 Hash 474b9b9b03d6963fe1184be42bddf9e3
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
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SIPSorcery.Persistence;

namespace SIPSorcery.SIP.App
{
    public class SIPDialogEventSubscription : SIPEventSubscription
    {
        private const int MAX_DIALOGUES_FOR_NOTIFY = 25;

        private static string m_contentType = SIPMIMETypes.DIALOG_INFO_CONTENT_TYPE;

        private SIPEventDialogInfo DialogInfo;

        private SIPAssetGetListDelegate<SIPDialogueAsset> GetDialogues_External;
        private SIPAssetGetByIdDelegate<SIPDialogueAsset> GetDialogue_External;

        public override SIPEventPackage SubscriptionEventPackage
        {
            get { return SIPEventPackage.Dialog; }
        }

        public override string MonitorFilter
        {
            get { return "dialog " + ResourceURI.ToString(); }
        }

        public override string NotifyContentType
        {
            get { return m_contentType; }
        }

        public SIPDialogEventSubscription(
            SIPMonitorLogDelegate log,
            string sessionID,
            SIPURI resourceURI,
            SIPURI canonincalResourceURI,
            string filter,
            SIPDialogue subscriptionDialogue,
            int expiry,
            SIPAssetGetListDelegate<SIPDialogueAsset> getDialogues,
            SIPAssetGetByIdDelegate<SIPDialogueAsset> getDialogue
            )
            : base(log, sessionID, resourceURI, canonincalResourceURI, filter, subscriptionDialogue, expiry)
        {
            GetDialogues_External = getDialogues;
            GetDialogue_External = getDialogue;
            DialogInfo = new SIPEventDialogInfo(0, SIPEventDialogInfoStateEnum.full, resourceURI);
        }

        public override void GetFullState()
        {
            try
            {
                DialogInfo.State = SIPEventDialogInfoStateEnum.full;
                List<SIPDialogueAsset> dialogueAssets = GetDialogues_External(d => d.Owner == SubscriptionDialogue.Owner, "Inserted", 0, MAX_DIALOGUES_FOR_NOTIFY);

                foreach (SIPDialogueAsset dialogueAsset in dialogueAssets)
                {
                    DialogInfo.DialogItems.Add(new SIPEventDialog(dialogueAsset.SIPDialogue.Id.ToString(), "confirmed", dialogueAsset.SIPDialogue));
                }

                MonitorLogEvent_External(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Notifier, SIPMonitorEventTypesEnum.NotifySent, "Full state notification for dialog and " + ResourceURI.ToString() + ".", SubscriptionDialogue.Owner));
            }
            catch (Exception excp)
            {
                logger.Error("Exception SIPDialogEventSubscription GetFullState. " + excp.Message);
            }
        }

        public override string GetNotifyBody()
        {
            return DialogInfo.ToXMLText();
        }

        public override bool AddMonitorEvent(SIPMonitorMachineEvent machineEvent)
        {
            try
            {
                MonitorLogEvent_External(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Notifier, SIPMonitorEventTypesEnum.Monitor, "Monitor event " + machineEvent.MachineEventType + " dialog " + ResourceURI.ToString() + " (ID " + machineEvent.ResourceID + ").", SubscriptionDialogue.Owner));

                string state = GetStateForEventType(machineEvent.MachineEventType);

                if (machineEvent.MachineEventType == SIPMonitorMachineEventTypesEnum.SIPDialogueRemoved)
                {
                    DialogInfo.DialogItems.Add(new SIPEventDialog(machineEvent.ResourceID, state, null));
                    return true;
                }
                else
                {
                    SIPDialogueAsset sipDialogue = GetDialogue_External(new Guid(machineEvent.ResourceID));

                    if (sipDialogue == null)
                    {
                        // Couldn't find the dialogue in the database so it must be terminated.
                        DialogInfo.DialogItems.Add(new SIPEventDialog(machineEvent.ResourceID, "terminated", null));
                        return true;
                    }
                    else if (machineEvent.MachineEventType == SIPMonitorMachineEventTypesEnum.SIPDialogueTransfer)
                    {
                        // For dialog transfer events add both dialogs involved to the notification.

                        DialogInfo.DialogItems.Add(new SIPEventDialog(sipDialogue.Id.ToString(), state, sipDialogue.SIPDialogue));

                        if (sipDialogue.SIPDialogue.BridgeId != Guid.Empty)
                        {
                            SIPDialogueAsset bridgedDialogue = GetDialogues_External(d => d.BridgeId == sipDialogue.BridgeId && d.Id != sipDialogue.Id, null, 0, 1).FirstOrDefault();
                            if (bridgedDialogue != null)
                            {
                                DialogInfo.DialogItems.Add(new SIPEventDialog(bridgedDialogue.Id.ToString(), state, bridgedDialogue.SIPDialogue));
                            }
                        }

                        return true;
                    }
                    else
                    {
                        DialogInfo.DialogItems.Add(new SIPEventDialog(sipDialogue.Id.ToString(), state, sipDialogue.SIPDialogue));
                        return true;
                    }
                }
            }
            catch (Exception excp)
            {
                logger.Error("Exception SIPDialogEventSubscription AddMonitorEvent. " + excp.Message);
                throw;
            }
        }

        public override void NotificationSent()
        {
            if (DialogInfo.State == SIPEventDialogInfoStateEnum.full)
            {
                MonitorLogEvent_External(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Notifier, SIPMonitorEventTypesEnum.NotifySent, "Full state notification sent for " + DialogInfo.Entity.ToString() + ", version " + DialogInfo.Version + ", cseq " + SubscriptionDialogue.CSeq + ".", SubscriptionDialogue.Owner));
            }
            else
            {
                foreach (SIPEventDialog dialog in DialogInfo.DialogItems)
                {
                    string remoteURI = (dialog.RemoteParticipant != null && dialog.RemoteParticipant.URI != null) ? ", " + dialog.RemoteParticipant.URI.ToString() : null;
                    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));
                }
            }

            DialogInfo.State = SIPEventDialogInfoStateEnum.partial;
            DialogInfo.DialogItems.Clear();
            DialogInfo.Version++;
        }

        private string GetStateForEventType(SIPMonitorMachineEventTypesEnum machineEventType)
        {
            switch (machineEventType)
            {
                case SIPMonitorMachineEventTypesEnum.SIPDialogueCreated: return "confirmed";
                case SIPMonitorMachineEventTypesEnum.SIPDialogueRemoved: return "terminated";
                case SIPMonitorMachineEventTypesEnum.SIPDialogueUpdated: return "updated";
                case SIPMonitorMachineEventTypesEnum.SIPDialogueTransfer: return "updated";
                default: throw new ApplicationException("The state for a dialog SIP event could not be determined from the monitor event type of " + machineEventType + ".");
            }
        }
    }
}
Back to Top