public string ExtendSession(string address, string sessionID, int expiry)
        {
            lock (m_clientSessions)
            {
                List <SIPMonitorClientSession> sessions = (m_clientSessions.ContainsKey(address)) ? m_clientSessions[address] : null;
                if (sessions != null)
                {
                    for (int index = 0; index < sessions.Count; index++)
                    {
                        SIPMonitorClientSession session = sessions[index];

                        if (session.SessionID == sessionID)
                        {
                            session.LastGetNotificationsRequest = DateTime.Now;
                            session.SessionEndTime = (expiry != 0) ? DateTime.Now.AddSeconds(expiry) : (DateTime?)null;

                            string sessionEndTime = (session.SessionEndTime != null) ? session.SessionEndTime.Value.ToString("HH:mm:ss") : session.LastGetNotificationsRequest.AddSeconds(-1 * REMOVE_EXPIRED_SESSIONS_NOREQUESTS).ToString("HH:mm:ss");
                            logger.Debug("Extending session for customer=" + session.CustomerUsername + ", sessionID=" + sessionID + " to " + sessionEndTime + ".");
                        }
                    }
                }
                else
                {
                    return("Session was not found.");
                }
            }

            return(null);
        }
        public void CloseSession(string address, string sessionID)
        {
            lock (m_clientSessions)
            {
                List <SIPMonitorClientSession> sessions = (m_clientSessions.ContainsKey(address)) ? m_clientSessions[address] : null;
                if (sessions != null)
                {
                    for (int index = 0; index < sessions.Count; index++)
                    {
                        SIPMonitorClientSession session = sessions[index];

                        if (session.SessionID == sessionID)
                        {
                            logger.Debug("Closing session for customer=" + session.CustomerUsername + ", sessionID=" + sessionID + ".");

                            session.Close();

                            m_clientSessions[address].Remove(session);

                            if (m_clientSessions[address].Count == 0)
                            {
                                m_clientSessions.Remove(address);
                            }
                        }
                    }
                }
            }
        }
        public string Subscribe(string customerUsername, string adminId, string address, string sessionID, string subject, string filter, int expiry, string udpSocket, out string subscribeError)
        {
            subscribeError = null;

            try
            {
                logger.Debug("SIPMonitorClientManager subscribe for customer=" + customerUsername + ", adminID=" + adminId + ", subject=" + subject + ", filter=" + filter + ".");

                if (customerUsername.IsNullOrBlank() || address.IsNullOrBlank() || subject.IsNullOrBlank())
                {
                    throw new ArgumentException("Subscribe was passed a required parameter that was empty.");
                }

                DateTime?sessionEndTime         = (expiry != 0) ? DateTime.Now.AddSeconds(expiry) : (DateTime?)null;
                SIPMonitorClientSession session = new SIPMonitorClientSession(customerUsername, adminId, address, sessionID, sessionEndTime, udpSocket);
                session.SetFilter(subject, filter);

                string endTime = (session.SessionEndTime != null) ? session.SessionEndTime.Value.ToString("HH:mm:ss") : "not set";
                logger.Debug("Filter set for customer=" + customerUsername + ", adminID=" + adminId + ", sessionID=" + sessionID + " as " + session.Filter.GetFilterDescription() + ", end time=" + endTime + ".");

                lock (m_clientSessions)
                {
                    if (!m_clientSessions.ContainsKey(session.Address))
                    {
                        m_clientSessions.Add(session.Address, new List <SIPMonitorClientSession>()
                        {
                            session
                        });
                    }
                    else
                    {
                        m_clientSessions[session.Address].Add(session);
                    }
                }

                // A suscription can belong to a second session on the same address in which case there could already
                // be a pending notificatins request waiting.

                /*Action<string> notificationReady = null;
                 *
                 * lock (m_clientsWaiting)
                 * {
                 *  if (m_clientsWaiting.ContainsKey(address))
                 *  {
                 *      notificationReady = m_clientsWaiting[address];
                 *      m_clientsWaiting.Remove(address);
                 *  }
                 * }*/

                //if (notificationReady != null)
                //{
                //    logger.Debug("Subscribe Firing notification available callback for address " + address + ".");
                //    notificationReady(address);
                // }

                //logger.Debug("SIPMonitorClientManager subscribed " + session.Address + " " + session.CustomerUsername + ".");

                return(sessionID);
            }
            catch (Exception excp)
            {
                logger.Error("Exception SIPMonitorClientManager Subscribe (" + excp.GetType() + "). " + excp.Message);
                subscribeError = excp.Message;
                return(null);
            }
        }
        public List <string> GetNotifications(string address, out string sessionId, out string sessionError)
        {
            sessionId    = null;
            sessionError = null;

            try
            {
                //logger.Debug("SIPMonitorClientManager GetNotifications for address " + address + ".");

                /*lock (m_clientsWaiting)
                 * {
                 *  // If there is a callback set for this address the consumer has now received it and it can be removed.
                 *  if (m_clientsWaiting.ContainsKey(address))
                 *  {
                 *      m_clientsWaiting.Remove(address);
                 *  }
                 * }*/

                List <SIPMonitorClientSession> sessions = (m_clientSessions.ContainsKey(address)) ? m_clientSessions[address] : null;
                if (sessions != null)
                {
                    sessions = sessions.OrderBy(s => s.LastGetNotificationsRequest).ToList();

                    for (int index = 0; index < sessions.Count; index++)
                    {
                        SIPMonitorClientSession session = sessions[index];
                        session.LastGetNotificationsRequest = DateTime.Now;

                        if (!session.FilterDescriptionNotificationSent && session.SessionType == SIPMonitorClientTypesEnum.Console)
                        {
                            //logger.Debug("First notifications request after new console client filter set.");
                            session.FilterDescriptionNotificationSent = true;
                            sessionId = session.SessionID;
                            SIPMonitorConsoleEvent filterDescriptionEvent = new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.Monitor, SIPMonitorEventTypesEnum.Monitor, session.Filter.GetFilterDescription(), session.CustomerUsername);
                            return(new List <string>()
                            {
                                filterDescriptionEvent.ToConsoleString(session.AdminId)
                            });
                        }
                        else if (session.Events.Count > 0)
                        {
                            List <string> eventList = new List <string>();
                            sessionId = session.SessionID;
                            lock (session.Events)
                            {
                                while (session.Events.Count > 0)
                                {
                                    SIPMonitorEvent monitorEvent = session.Events.Dequeue();
                                    if (monitorEvent is SIPMonitorConsoleEvent)
                                    {
                                        eventList.Add(((SIPMonitorConsoleEvent)monitorEvent).ToConsoleString(session.AdminId));
                                    }
                                    else
                                    {
                                        eventList.Add(monitorEvent.ToCSV());
                                    }
                                }
                            }

                            return(eventList);
                        }
                    }
                }

                return(null);
            }
            catch (Exception excp)
            {
                logger.Error("Exception SIPMonitorClientManager GetNotifications. " + excp.Message);
                sessionError = "Exception SIPMonitorClientManager GetNotifications. " + excp.Message;
                return(null);
                //throw;
            }
        }