public string SubscribeClient(
            string owner,
            string adminID,
            SIPRequest subscribeRequest,
            string toTag,
            SIPURI canonicalResourceURI,
            out SIPResponseStatusCodesEnum errorResponse,
            out string errorReason)
        {
            try
            {
                errorResponse = SIPResponseStatusCodesEnum.None;
                errorReason   = null;

                SIPURI          resourceURI  = subscribeRequest.URI.CopyOf();
                SIPEventPackage eventPackage = SIPEventPackage.Parse(subscribeRequest.Header.Event);
                int             expiry       = subscribeRequest.Header.Expires;

                if (!(eventPackage == SIPEventPackage.Dialog || eventPackage == SIPEventPackage.Presence))
                {
                    throw new ApplicationException("Event package " + eventPackage.ToString() + " is not supported by the subscriptions manager.");
                }
                else
                {
                    if (expiry > 0)
                    {
                        string      subscribeError    = null;
                        string      sessionID         = Guid.NewGuid().ToString();
                        SIPDialogue subscribeDialogue = new SIPDialogue(subscribeRequest, owner, adminID, toTag);

                        if (eventPackage == SIPEventPackage.Dialog)
                        {
                            string monitorFilter = "dialog " + canonicalResourceURI.ToString();
                            if (!subscribeRequest.Body.IsNullOrBlank())
                            {
                                monitorFilter += " and " + subscribeRequest.Body;
                            }

                            m_publisher.Subscribe(owner, adminID, m_notificationsAddress, sessionID, SIPMonitorClientTypesEnum.Machine.ToString(), monitorFilter, expiry, null, out subscribeError);

                            if (subscribeError != null)
                            {
                                throw new ApplicationException(subscribeError);
                            }
                            else
                            {
                                SIPDialogEventSubscription subscription = new SIPDialogEventSubscription(MonitorLogEvent_External, sessionID, resourceURI, canonicalResourceURI, monitorFilter, subscribeDialogue, expiry, GetDialogues_External, GetDialogue_External);
                                m_subscriptions.Add(sessionID, subscription);
                                MonitorLogEvent_External(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Notifier, SIPMonitorEventTypesEnum.SubscribeAccept, "New dialog subscription created for " + resourceURI.ToString() + ", expiry " + expiry + "s.", owner));
                            }
                        }
                        else if (eventPackage == SIPEventPackage.Presence)
                        {
                            string monitorFilter = "presence " + canonicalResourceURI.ToString();
                            m_publisher.Subscribe(owner, adminID, m_notificationsAddress, sessionID, SIPMonitorClientTypesEnum.Machine.ToString(), monitorFilter, expiry, null, out subscribeError);

                            if (subscribeError != null)
                            {
                                throw new ApplicationException(subscribeError);
                            }
                            else
                            {
                                bool switchboardAccountsOnly = subscribeRequest.Body == SIPPresenceEventSubscription.SWITCHBOARD_FILTER;
                                SIPPresenceEventSubscription subscription =
                                    new SIPPresenceEventSubscription(MonitorLogEvent_External, sessionID, resourceURI,
                                                                     canonicalResourceURI, monitorFilter, subscribeDialogue, expiry,
                                                                     m_sipAssetPersistor.Get, m_sipAssetPersistor.GetProperty,
                                                                     GetSIPRegistrarBindingsCount_External, switchboardAccountsOnly);
                                m_subscriptions.Add(sessionID, subscription);
                                MonitorLogEvent_External(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Notifier, SIPMonitorEventTypesEnum.SubscribeAccept, "New presence subscription created for " + resourceURI.ToString() + ", expiry " + expiry + "s.", owner));
                            }
                        }

                        return(sessionID);
                    }

                    return(null);
                }
            }
            catch (Exception excp)
            {
                logger.Error("Exception NotifierSubscriptionsManager SubscribeClient. " + excp.Message);
                throw;
            }
        }
        /// <summary>
        /// Simple state machine that processes commands from the client.
        /// </summary>
        private void ProcessCommand(string command)
        {
            try
            {
                if (command.IsNullOrBlank())
                {
                    command = DEFAULT_FILTER;
                }

                string subscribeError = null;
                m_notificationsSessionID = Guid.NewGuid().ToString();
                m_publisher.Subscribe(Username, AdminId, m_notificationsAddress, m_notificationsSessionID, SIPMonitorClientTypesEnum.Console.ToString(), command, DEFAULT_SESSION_LENGTH, null, out subscribeError);

                if (subscribeError != null)
                {
                    throw new ApplicationException(subscribeError);
                }
                else
                {
                    m_lastMonitorSessionRenewal = DateTime.Now;
                    ThreadPool.QueueUserWorkItem(delegate { RenewSession(); });
                    SIPMonitorFilter filter = new SIPMonitorFilter(command);
                    WriteFilterDescription(filter.GetFilterDescription());

                    /*if (m_publisher is SIPSorcery.Servers.SIPMonitorClientManager)
                     * {
                     *  logger.Debug("VT100Server is connected to a publisher that fires an event when a notification is ready.");
                     *  // The publisher will fire an event when a new notification is ready.
                     *  m_publisher.NotificationReady += (address) => { GetNotifications(); };
                     * }
                     * else
                     * {
                     *  logger.Debug("VT100Server is connected to a publisher that needs to be polled for events.");
                     *
                     *  // The publisher does not supply an event when a notification is ready and must be polled instead.
                     *  StopPolling = false;
                     *  ThreadPool.QueueUserWorkItem(delegate
                     *  {
                     *      try
                     *      {
                     *          while (!HasClosed && !StopPolling)
                     *          {
                     *              GetNotifications();
                     *              Thread.Sleep(POLL_FOR_NOTIFICATIONS_PERIOD);
                     *          }
                     *
                     *          logger.Debug("Stopped polling for events for address " + m_notificationsAddress + ".");
                     *      }
                     *      catch (ApplicationException appExcp)
                     *      {
                     *          WriteError(appExcp.Message);
                     *      }
                     *      catch (Exception excp)
                     *      {
                     *          logger.Error("Exception SIPSorceryVT100Server ProcessCommand on GetNotifications. " + excp.Message);
                     *      }
                     *  });
                     * }*/
                }
            }
            catch (ApplicationException appExcp)
            {
                WriteError(appExcp.Message);
            }
            catch (Exception excp)
            {
                logger.Error("Exception SIPSorceryVT100Server ProcessCommand. " + excp.Message);
            }
        }
Beispiel #3
0
        public string SubscribeClient(
            string owner,
            string adminID,
            SIPRequest subscribeRequest,
            string toTag,
            SIPURI canonicalResourceURI,
            out SIPResponseStatusCodesEnum errorResponse,
            out string errorReason)
        {
            try
            {
                errorResponse = SIPResponseStatusCodesEnum.None;
                errorReason   = null;

                SIPURI          resourceURI  = subscribeRequest.URI.CopyOf();
                SIPEventPackage eventPackage = SIPEventPackage.Parse(subscribeRequest.Header.Event);
                int             expiry       = subscribeRequest.Header.Expires;

                if (!(eventPackage == SIPEventPackage.Dialog || eventPackage == SIPEventPackage.Presence))
                {
                    throw new ApplicationException("Event package " + eventPackage.ToString() + " is not supported by the subscriptions manager.");
                }
                else
                {
                    if (expiry > 0)
                    {
                        string      subscribeError    = null;
                        string      sessionID         = Guid.NewGuid().ToString();
                        SIPDialogue subscribeDialogue = new SIPDialogue(subscribeRequest, owner, adminID, toTag);

                        if (eventPackage == SIPEventPackage.Dialog)
                        {
                            string monitorFilter = "dialog " + canonicalResourceURI.ToString();
                            if (!subscribeRequest.Body.IsNullOrBlank())
                            {
                                monitorFilter += " and " + subscribeRequest.Body;
                            }

                            m_publisher.Subscribe(owner, adminID, m_notificationsAddress, sessionID, SIPMonitorClientTypesEnum.Machine.ToString(), monitorFilter, expiry, null, out subscribeError);

                            if (subscribeError != null)
                            {
                                throw new ApplicationException(subscribeError);
                            }
                            else
                            {
                                GetSIPDialogueDelegate             getSipDialogue          = (id) => { return(GetDialogue_External(id).SIPDialogue); };
                                GetDialoguesForOwnerDelegate       getSipDialoguesForOwner = (ownr, offset, limit) => { return(GetDialogues_External(x => x.Owner == owner, "Inserted", offset, limit).Select(x => x.SIPDialogue).ToList <SIPDialogue>()); };
                                GetRemoteDialogueForBridgeDelegate getSIPDialogueForBridge = (bridgeID, localDialogueID) => { return(GetDialogues_External(d => d.BridgeId == bridgeID.ToString() && d.Id != localDialogueID, null, 0, 1).Select(x => x.SIPDialogue).FirstOrDefault()); };
                                SIPDialogEventSubscription         subscription            = new SIPDialogEventSubscription(MonitorLogEvent_External, sessionID, resourceURI, canonicalResourceURI, monitorFilter, subscribeDialogue, expiry, getSipDialoguesForOwner, getSIPDialogueForBridge, getSipDialogue);
                                m_subscriptions.Add(sessionID, subscription);
                                MonitorLogEvent_External(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Notifier, SIPMonitorEventTypesEnum.SubscribeAccept, "New dialog subscription created for " + resourceURI.ToString() + ", expiry " + expiry + "s.", owner));
                            }
                        }
                        else if (eventPackage == SIPEventPackage.Presence)
                        {
                            string monitorFilter = "presence " + canonicalResourceURI.ToString();
                            m_publisher.Subscribe(owner, adminID, m_notificationsAddress, sessionID, SIPMonitorClientTypesEnum.Machine.ToString(), monitorFilter, expiry, null, out subscribeError);

                            if (subscribeError != null)
                            {
                                throw new ApplicationException(subscribeError);
                            }
                            else
                            {
                                bool switchboardAccountsOnly = subscribeRequest.Body == SIPPresenceEventSubscription.SWITCHBOARD_FILTER;
                                SIPRegistrarBindingsCountDelegate getRegistrarBindingsCount = (sipAccountID) => { return(GetSIPRegistrarBindingsCount_External(x => x.SIPAccountId == sipAccountID)); };
                                GetSIPAccountsForUserDelegate     getSIPAccountsForUser     = (username, domain, offset, limit) => { return(m_sipAssetPersistor.Get(x => x.SIPUsername == username && x.SIPDomain == domain, "SIPUsername", offset, limit).Select(y => y.SIPAccount).ToList()); };
                                GetSIPAccountsForOwnerDelegate    getSIPAccountsForOwner    = (accountOwner, offset, limit) => { return(m_sipAssetPersistor.Get(x => x.Owner == accountOwner, "SIPUsername", offset, limit).Select(y => y.SIPAccount).ToList()); };
                                SIPPresenceEventSubscription      subscription = new SIPPresenceEventSubscription(MonitorLogEvent_External, sessionID, resourceURI, canonicalResourceURI, monitorFilter, subscribeDialogue, expiry, getSIPAccountsForUser, getSIPAccountsForOwner, m_sipAssetPersistor.GetProperty, getRegistrarBindingsCount, switchboardAccountsOnly);
                                m_subscriptions.Add(sessionID, subscription);
                                MonitorLogEvent_External(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Notifier, SIPMonitorEventTypesEnum.SubscribeAccept, "New presence subscription created for " + resourceURI.ToString() + ", expiry " + expiry + "s.", owner));
                            }
                        }

                        return(sessionID);
                    }

                    return(null);
                }
            }
            catch (Exception excp)
            {
                logger.Error("Exception NotifierSubscriptionsManager SubscribeClient. " + excp.Message);
                throw;
            }
        }
 public string Subscribe(string customerUsername, string adminId, string address, string sessionID, string subject, string filter, int expiry, string udpSocket, out string subscribeError)
 {
     return(m_publisher.Subscribe(customerUsername, adminId, address, sessionID, subject, filter, expiry, udpSocket, out subscribeError));
 }