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

Language C# Lines 185
MD5 Hash 9116411334b2afe99f68c1c13b970cb5
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SIPSorcery.Persistence;

namespace SIPSorcery.SIP.App
{
    public class SIPPresenceEventSubscription : SIPEventSubscription
    {
        private const int MAX_SIPACCOUNTS_TO_RETRIEVE = 25;
        public const string SWITCHBOARD_FILTER = "switchboard";    // If a client specifies this value as a filter it's only interested in SIP accounts that are switchboard enabled.

        private static string m_wildcardUser = SIPMonitorFilter.WILDCARD;
        private static string m_contentType = SIPMIMETypes.PRESENCE_NOTIFY_CONTENT_TYPE;

        private SIPEventPresence Presence;

        private SIPAssetCountDelegate<SIPRegistrarBinding> GetSIPRegistrarBindingsCount_External;
        private SIPAssetPersistor<SIPAccount> m_sipAccountPersistor;

        private bool m_switchboardSIPAccountsOnly;      // If true means this subscription should only generate notifications for SIP accounts that are switchboard enabled.

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

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

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

        public SIPPresenceEventSubscription(
            SIPMonitorLogDelegate log,
            string sessionID,
            SIPURI resourceURI,
            SIPURI canonincalResourceURI,
            string filter,
            SIPDialogue subscriptionDialogue,
            int expiry,
            SIPAssetPersistor<SIPAccount> sipAccountPersistor,
            SIPAssetCountDelegate<SIPRegistrarBinding> getBindingsCount,
            bool switchboardSIPAccountsOnly
            )
            : base(log, sessionID, resourceURI, canonincalResourceURI, filter, subscriptionDialogue, expiry)
        {
            m_sipAccountPersistor = sipAccountPersistor;
            GetSIPRegistrarBindingsCount_External = getBindingsCount;
            Presence = new SIPEventPresence(resourceURI);
            m_switchboardSIPAccountsOnly = switchboardSIPAccountsOnly;
        }

        public override void GetFullState()
        {
            try
            {
                List<SIPAccount> sipAccounts = null;

                if (ResourceURI.User == m_wildcardUser)
                {
                    if (m_switchboardSIPAccountsOnly)
                    {
                        sipAccounts = m_sipAccountPersistor.Get(s => s.Owner == SubscriptionDialogue.Owner && s.IsSwitchboardEnabled, "SIPUsername", 0, MAX_SIPACCOUNTS_TO_RETRIEVE);
                    }
                    else
                    {
                        sipAccounts = m_sipAccountPersistor.Get(s => s.Owner == SubscriptionDialogue.Owner, "SIPUsername", 0, MAX_SIPACCOUNTS_TO_RETRIEVE);
                    }
                }
                else
                {
                    if (m_switchboardSIPAccountsOnly)
                    {
                        sipAccounts = m_sipAccountPersistor.Get(s => s.SIPUsername == CanonicalResourceURI.User && s.SIPDomain == CanonicalResourceURI.Host && s.IsSwitchboardEnabled, "SIPUsername", 0, MAX_SIPACCOUNTS_TO_RETRIEVE);
                    }
                    else
                    {
                        sipAccounts = m_sipAccountPersistor.Get(s => s.SIPUsername == CanonicalResourceURI.User && s.SIPDomain == CanonicalResourceURI.Host, "SIPUsername", 0, MAX_SIPACCOUNTS_TO_RETRIEVE);
                    }
                }

                foreach (SIPAccount sipAccount in sipAccounts)
                {
                    SIPURI aor = SIPURI.ParseSIPURIRelaxed(sipAccount.SIPUsername + "@" + sipAccount.SIPDomain);

                    int bindingsCount = GetSIPRegistrarBindingsCount_External(b => b.SIPAccountId == sipAccount.Id);
                    if (bindingsCount > 0)
                    {
                        string safeSIPAccountID = sipAccount.Id.ToString();
                        Presence.Tuples.Add(new SIPEventPresenceTuple(safeSIPAccountID, SIPEventPresenceStateEnum.open, aor, Decimal.Zero));
                        //logger.Debug(" full presence " + aor.ToString() + " open.");
                    }
                    else
                    {
                        string safeSIPAccountID = sipAccount.Id.ToString();
                        Presence.Tuples.Add(new SIPEventPresenceTuple(safeSIPAccountID, SIPEventPresenceStateEnum.closed, null, Decimal.Zero));
                        //logger.Debug(" full presence " + aor.ToString() + " closed.");
                    }
                }

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

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

        /// <summary>
        /// Checks and where required adds a presence related monitor event to the list of pending notifications.
        /// </summary>
        /// <param name="machineEvent">The monitor event that has been received.</param>
        /// <returns>True if a notification needs to be sent as a result of this monitor event, false otherwise.</returns>
        public override bool AddMonitorEvent(SIPMonitorMachineEvent machineEvent)
        {
            try
            {
                MonitorLogEvent_External(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Notifier, SIPMonitorEventTypesEnum.Monitor, "Monitor event " + machineEvent.MachineEventType + " presence " + machineEvent.ResourceURI.ToString() + " for subscription to " + ResourceURI.ToString() + ".", SubscriptionDialogue.Owner));

                string safeSIPAccountID = machineEvent.ResourceID;
                SIPURI sipAccountURI = machineEvent.ResourceURI;
                bool sendNotificationForEvent = true;

                if (m_switchboardSIPAccountsOnly)
                {
                    // Need to check whether the SIP account is switchboard enabled before forwarding the notification.
                    Guid sipAccountID = new Guid(machineEvent.ResourceID);
                    sendNotificationForEvent = Convert.ToBoolean(m_sipAccountPersistor.GetProperty(sipAccountID, "IsSwitchboardEnabled"));
                }

                if (sendNotificationForEvent)
                {
                    if (machineEvent.MachineEventType == SIPMonitorMachineEventTypesEnum.SIPRegistrarBindingUpdate)
                    {
                        // A binding has been updated so there is at least one device online for the SIP account.
                        Presence.Tuples.Add(new SIPEventPresenceTuple(safeSIPAccountID, SIPEventPresenceStateEnum.open, sipAccountURI, Decimal.Zero));
                        //logger.Debug(" single presence open.");
                    }
                    else
                    {
                        // A binding has been removed but there could still be others.
                        Guid sipAccountID = new Guid(machineEvent.ResourceID);
                        int bindingsCount = GetSIPRegistrarBindingsCount_External(b => b.SIPAccountId == sipAccountID);
                        if (bindingsCount > 0)
                        {
                            Presence.Tuples.Add(new SIPEventPresenceTuple(safeSIPAccountID, SIPEventPresenceStateEnum.open, sipAccountURI, Decimal.Zero));
                        }
                        else
                        {
                            Presence.Tuples.Add(new SIPEventPresenceTuple(safeSIPAccountID, SIPEventPresenceStateEnum.closed, sipAccountURI, Decimal.Zero));
                        }
                    }

                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception excp)
            {
                logger.Error("Exception SIPresenceEventSubscription AddMonitorEvent. " + excp.Message);
                throw;
            }
        }

        public override void NotificationSent()
        {
            Presence.Tuples.Clear();
        }
    }
}
Back to Top