Ejemplo n.º 1
0
        static void Main()
        {
            Console.WriteLine("SIPSorcery Notification Client Demo");

            // Use verbose to get display the full SIP messages.
            AddConsoleLogger(LogEventLevel.Verbose);

            var sipTransport = new SIPTransport();

            sipTransport.EnableTraceLogs();

            var mwiURI = SIPURI.ParseSIPURIRelaxed($"{USERNAME}@{SERVER}");
            int expiry = 180;

            SIPNotifierClient mwiSubscriber = new SIPNotifierClient(sipTransport, null, SIPEventPackagesEnum.MessageSummary, mwiURI, USERNAME, null, PASSWORD, expiry, null);

            mwiSubscriber.SubscriptionFailed     += (uri, failureStatus, errorMessage) => Console.WriteLine($"MWI failed for {uri}, {errorMessage}");
            mwiSubscriber.SubscriptionSuccessful += (uri) => Console.WriteLine($"MWI subscription successful for {uri}");
            mwiSubscriber.NotificationReceived   += (evt, msg) => Console.WriteLine($"MWI notification, type {evt}, message {msg}.");

            mwiSubscriber.Start();

            Console.WriteLine("press any key to exit...");
            Console.ReadLine();

            Console.WriteLine("Exiting...");

            // Clean up.
            mwiSubscriber.Stop();
            Thread.Sleep(1000);
            sipTransport.Shutdown();
        }
        private void OkResponseReceived(SIPTransaction sipTransaction, SIPEndPoint remoteEndPoint, SIPResponse sipResponse)
        {
            try
            {
                Guid callIdGuid = new Guid(sipResponse.Header.CallId);
                SIPProviderBinding binding = GetBinding(callIdGuid);
                RemoveCachedBinding(callIdGuid);

                if (binding != null)
                {
                    // Updated contacts list.
                    // Find the contact in the list that matches the one being maintained by this agent in order to determine the expiry value.
                    int headerExpires = sipResponse.Header.Expires;
                    int contactExpires = -1;
                    if (sipResponse.Header.Contact != null && sipResponse.Header.Contact.Count > 0)
                    {
                        foreach (SIPContactHeader contactHeader in sipResponse.Header.Contact)
                        {
                            if (contactHeader.ContactURI.Parameters.Get(m_regAgentContactId) == binding.BindingSIPURI.Parameters.Get(m_regAgentContactId))
                            {
                                contactExpires = contactHeader.Expires;
                                break;
                            }
                        }
                    }

                    if (contactExpires != -1)
                    {
                        binding.BindingExpiry = contactExpires;
                    }
                    else if (headerExpires != -1)
                    {
                        binding.BindingExpiry = headerExpires;
                    }

                    if (binding.BindingExpiry < REGISTER_MINIMUM_EXPIRY)
                    {
                        // Make sure we don't do a 3CX and send registration floods.
                        binding.BindingExpiry = REGISTER_MINIMUM_EXPIRY;
                    }

                    binding.NextRegistrationTime = DateTimeOffset.UtcNow.AddSeconds(binding.BindingExpiry - REGISTRATION_HEAD_TIME);
                    binding.ContactsList = sipResponse.Header.Contact;
                    binding.IsRegistered = true;
                    binding.LastRegisterTime = DateTimeOffset.UtcNow;
                    binding.RegistrationFailureMessage = null;
                    m_bindingPersistor.Update(binding);
                    UpdateSIPProviderOutboundProxy(binding, sipResponse.Header.ProxyReceivedOn);
                    FireProxyLogEvent(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.RegisterAgent, SIPMonitorEventTypesEnum.ContactRegistered, "Contact successfully registered for " + binding.Owner + " on " + binding.RegistrarServer.ToString() + ", expiry " + binding.BindingExpiry + "s.", binding.Owner));

                    if (binding.SendMWISubscribe)
                    {
                        try
                        {
                            var mwiURI = SIPURI.ParseSIPURIRelaxed(binding.ProviderAuthUsername + "@" + binding.RegistrarRealm);

                            FireProxyLogEvent(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.RegisterAgent, SIPMonitorEventTypesEnum.MWI, "Sending MWI subscription request for provider " + binding.ProviderName + " with URI " + mwiURI.ToParameterlessString() + ".", binding.Owner));

                            SIPNotifierClient<SIPEvent> mwiSubscriber = new SIPNotifierClient<SIPEvent>(FireProxyLogEvent, m_sipTransport, m_outboundProxy, SIPEventPackage.MessageSummary, mwiURI, binding.ProviderAuthUsername, binding.RegistrarRealm, binding.ProviderPassword, binding.BindingExpiry, null);
                            mwiSubscriber.SubscriptionFailed += (resourceURI, failureStatus, errorMessage) => { FireProxyLogEvent(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.RegisterAgent, SIPMonitorEventTypesEnum.MWI, "MWI failed for " + resourceURI.ToParameterlessString() + ", " + errorMessage, binding.Owner)); };
                            mwiSubscriber.SubscriptionSuccessful += (resourceURI) => { FireProxyLogEvent(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.RegisterAgent, SIPMonitorEventTypesEnum.MWI, "MWI subscription successful for " + resourceURI.ToParameterlessString() + ".", binding.Owner)); };
                            mwiSubscriber.Subscribe(mwiURI, binding.BindingExpiry, SIPEventPackage.MessageSummary, CallProperties.CreateNewCallId(), binding.BindingSIPURI);
                        }
                        catch (Exception excp)
                        {
                            logger.Error("Exception SIPRegistrationAgent MWI Subscription. " + excp.Message);
                        }
                    }
                }
            }
            catch (Exception excp)
            {
                logger.Error("Exception SIPRegistrationAgent OkResponseReceived. " + excp.Message);
                RemoveCachedBinding(sipResponse.Header.CallId);
            }
        }