public KamailioMessageHandlerResult DoHandleMessage(KamailioMessageBase message)
        {
            if (message is KamailioRegistrationMessage)
            {
                var regMessage = (KamailioRegistrationMessage)message;

                if (regMessage.Expires == 0)
                {
                    return(UnregisterCodec(new KamailioRegistrationExpireMessage()
                    {
                        SipAddress = regMessage.Sip
                    }));
                }
                else
                {
                    return(RegisterSip((KamailioRegistrationMessage)message));
                }
            }


            if (message is KamailioRegistrationExpireMessage)
            {
                return(UnregisterCodec((KamailioRegistrationExpireMessage)message));
            }

            var dialogMessage = message as KamailioDialogMessage;

            if (dialogMessage != null)
            {
                if (dialogMessage.Status == DialogStatus.Start)
                {
                    return(RegisterCall(dialogMessage));
                }
                if (dialogMessage.Status == DialogStatus.End)
                {
                    // TODO: Check hangup reason. Only close calls where reason = Normal
                    return(CloseCall(dialogMessage));
                }
                if (dialogMessage.Status == DialogStatus.SingleBye)
                {
                    // TODO: Handle single bye message
                    // TODO: Close call
                    log.Info("Received SingleBye command from Kamailio. {0}", message);
                    return(NothingChangedResult);
                }
            }

            log.Info("Unhandled Kamailio message: {0}", message.ToDebugString());
            return(NothingChangedResult);
        }
        public KamailioMessageHandlerResult HandleMessage(string message)
        {
            KamailioMessageBase sipMessage = _kamailioMessageParser.Parse(message);

            if (sipMessage == null)
            {
                log.Warn("Incorrect Kamailio message format: {0}", message);
                return(new KamailioMessageHandlerResult {
                    ChangeStatus = KamailioMessageChangeStatus.NothingChanged
                });
            }

            if (log.IsInfoEnabled)
            {
                log.Debug("Parsed Kamailio Message {0}", sipMessage.ToDebugString());
            }

            var kamailioMessageResult = DoHandleMessage(sipMessage);

            log.Debug("Handled Kamailio message with result {0}. {1}", kamailioMessageResult.ChangeStatus, sipMessage.ToDebugString());
            return(kamailioMessageResult);
        }