private SipEventChangeStatus GetChangeStatus(DbContext cxt, RegisteredSipEntity dbSip)
        {
            var entry = cxt.Entry(dbSip);

            if (entry.State == EntityState.Added)
            {
                log.Debug($"User-agent added '{dbSip.SIP}'");
                return(SipEventChangeStatus.CodecAdded);
            }

            if (entry.State == EntityState.Deleted || CodecWasExpired(entry))
            {
                // There is a problem here, CodecWasExpired just tells us that it was expired when a new registration is sent. Since this is not checked anywhere else
                log.Debug($"User-agent expired or deleted '{dbSip.SIP}', CodecWasExpired={CodecWasExpired(entry)}, Entry.State={entry.State.ToString()}, Expires={dbSip.Expires}");
                return(SipEventChangeStatus.CodecRemoved);
            }

            if (entry.State == EntityState.Modified && dbSip.Expires == 0)
            {
                log.Error($"User-agent removed, expired '{dbSip.SIP}', this should not happen");
                return(SipEventChangeStatus.CodecRemoved);
            }

            if (entry.State == EntityState.Modified && HasRelevantChange(entry))
            {
                log.Debug($"User-agent updated, relevant changes '{dbSip.SIP}'");
                return(SipEventChangeStatus.CodecUpdated);
            }

            log.Debug($"User-agent nothing changed '{dbSip.SIP}'");
            return(SipEventChangeStatus.NothingChanged);
        }
 private CodecInformation MapToCodecInformation(RegisteredSipEntity rs)
 {
     return(new CodecInformation
     {
         SipAddress = rs.SIP,
         Api = rs.UserAgent.Api,
         Ip = rs.IP,
         GpoNames = rs.UserAgent.GpoNames,
         NrOfInputs = rs.UserAgent.Inputs
     });
 }
        public CodecInformation GetCodecInformationById(Guid id)
        {
            using (var db = GetDbContext())
            {
                RegisteredSipEntity rs = db.RegisteredSips
                                         .Include(o => o.UserAgent)
                                         .SingleOrDefault(o => o.Id == id);

                return(rs == null ? null : MapToCodecInformation(rs));
            }
        }
        public CodecInformation GetCodecInformationBySipAddress(string sipAddress)
        {
            using (var db = GetDbContext())
            {
                RegisteredSipEntity rs = db.RegisteredSips
                                         .Include(o => o.UserAgent)
                                         .SingleOrDefault(o => o.SIP == sipAddress);

                return(rs == null ? null : MapToCodecInformation(rs));
            }
        }
Esempio n. 5
0
 public static string GetDisplayName(RegisteredSipEntity regSip, string callDisplayName, string callUserName, string sipDomain)
 {
     return(DisplayNameHelper.GetDisplayName(
                regSip != null ? regSip.DisplayName : string.Empty,
                regSip != null && regSip.User != null ? regSip.User.DisplayName : string.Empty,
                callDisplayName,
                regSip != null ? regSip.Username : string.Empty,
                regSip != null ? regSip.SIP : string.Empty,
                callUserName,
                sipDomain));
 }
Esempio n. 6
0
        private RegisteredSip MapRegisteredSip(RegisteredSipEntity dbSip)
        {
            var sip = dbSip == null ? null : new RegisteredSip()
            {
                Id            = dbSip.Id,
                SIP           = dbSip.SIP,
                DisplayName   = dbSip.DisplayName,
                UserAgentHead = dbSip.UserAgentHead,
                Username      = dbSip.Username,
                User          = MapUser(dbSip.User),
            };

            return(sip);
        }
Esempio n. 7
0
        public RegisteredSipInfo GetRegisteredSipInfoById(Guid id)
        {
            using (var db = GetDbContext())
            {
                RegisteredSipEntity dbSip = db.RegisteredSips
                                            .Include(rs => rs.User)
                                            .SingleOrDefault(r => r.Id == id);

                return(dbSip == null
                    ? null
                    : new RegisteredSipInfo
                {
                    SipAddress = dbSip.SIP,
                    DisplayName = DisplayNameHelper.GetDisplayName(dbSip.DisplayName,
                                                                   dbSip.User != null ? dbSip.User.DisplayName : string.Empty, string.Empty, dbSip.Username,
                                                                   dbSip.SIP, "", _settingsManager.SipDomain)
                });
            }
        }
Esempio n. 8
0
        private RegisteredSip MapToRegisteredSip(RegisteredSipEntity dbRegisteredSip)
        {
            if (dbRegisteredSip == null)
            {
                return(null);
            }

            var registredSip = new RegisteredSip
            {
                DisplayName     = dbRegisteredSip.DisplayName,
                Expires         = dbRegisteredSip.Expires,
                Id              = dbRegisteredSip.Id,
                IP              = dbRegisteredSip.IP,
                Port            = dbRegisteredSip.Port,
                SIP             = dbRegisteredSip.SIP,
                ServerTimeStamp = dbRegisteredSip.ServerTimeStamp,
                Updated         = dbRegisteredSip.Updated,
                UserAgentHead   = dbRegisteredSip.UserAgentHeader,
                Username        = dbRegisteredSip.Username,
            };

            return(registredSip);
        }
        public RegisteredSip Single(Expression <Func <RegisteredSipEntity, bool> > expression)
        {
            // TODO: Only for tests???
            using (var db = GetDbContext())
            {
                RegisteredSipEntity dbRegisteredSip = db.RegisteredSips.SingleOrDefault(expression);

                return(dbRegisteredSip == null
                    ? null
                    : new RegisteredSip
                {
                    Id = dbRegisteredSip.Id,
                    DisplayName = dbRegisteredSip.DisplayName,
                    Expires = dbRegisteredSip.Expires,
                    IP = dbRegisteredSip.IP,
                    Port = dbRegisteredSip.Port,
                    SIP = dbRegisteredSip.SIP,
                    ServerTimeStamp = dbRegisteredSip.ServerTimeStamp,
                    Updated = dbRegisteredSip.Updated,
                    UserAgentHead = dbRegisteredSip.UserAgentHeader,
                    Username = dbRegisteredSip.Username,
                });
            }
        }
Esempio n. 10
0
        public SipEventHandlerResult UpdateRegisteredSip(UserAgentRegistration registration)
        {
            // Return value indicates if
            // 1. Codec been added
            // 2. Codec existed but registration has relevant changes
            // 3. Codec existed and registration is identical = NothingChanged

            try
            {
                using (var db = GetDbContext())
                {
                    var dbSip = db.RegisteredSips.SingleOrDefault(rs => rs.SIP == registration.SipUri);
                    // Is it a new registration?
                    if (dbSip == null)
                    {
                        if (registration.ExpirationTimeSeconds == 0)
                        {
                            // Unregistration of not registered user-agent. Do nothing.
                            log.Debug($"User-agent nothing changed, unregistration of not registered user-agent '{registration.SipUri}'");
                            return(SipEventHandlerResult.NothingChanged);
                        }

                        // New registration of user-agent
                        dbSip = new RegisteredSipEntity {
                            Id = Guid.NewGuid()
                        };
                        db.RegisteredSips.Add(dbSip);
                    }

                    // Match and map
                    var userAgentId = GetUserAgentId(registration.UserAgentHeader);
                    dbSip.UserAgentId     = userAgentId;
                    dbSip.UserAgentHeader = registration.UserAgentHeader;

                    var locationId = LocationManager.GetLocationIdByIp(registration.IpAddress);
                    dbSip.Location_LocationId = locationId != Guid.Empty ? locationId : (Guid?)null;

                    var registeredSipUsername = registration.Username.ToLower().Trim();
                    dbSip.Username = registeredSipUsername;

                    // TODO: Cache test
                    //var sipAccount = db.SipAccounts.FirstOrDefault(u => u.UserName.ToLower() == registeredSipUsername);
                    var sipAccount = _sipAccountManager.GetSipAccountByUserName(registeredSipUsername);
                    dbSip.User_UserId = sipAccount?.Id;

                    dbSip.SIP             = registration.SipUri;
                    dbSip.DisplayName     = registration.DisplayName;
                    dbSip.IP              = registration.IpAddress;
                    dbSip.Port            = registration.Port;
                    dbSip.ServerTimeStamp = registration.ServerTimeStamp;
                    dbSip.Updated         = DateTime.UtcNow;
                    dbSip.Expires         = registration.ExpirationTimeSeconds;
                    dbSip.Registrar       = registration.Registrar;

                    var changeStatus = GetChangeStatus(db, dbSip);
                    db.SaveChanges();

                    return(new SipEventHandlerResult
                    {
                        ChangeStatus = changeStatus,
                        ChangedObjectId = dbSip.Id,
                        SipAddress = dbSip.SIP
                    });
                }
            }
            catch (Exception ex)
            {
                log.Error(ex, "Error while updating registered sip {0}", registration.SipUri);
                return(SipEventHandlerResult.NothingChanged);
            }
        }
Esempio n. 11
0
        //private void AddCallInfoToCachedRegisteredSip(CcmDbContext db, IEnumerable<RegisteredSipDto> registeredSips)
        //{
        //    var sipDomain = SettingsManager.SipDomain;

        //    var dbCalls = db.Calls
        //        .Include(c => c.FromSip)
        //        .Include(c => c.FromSip.Location)
        //        .Include(c => c.FromSip.UserAgent)
        //        .Include(c => c.ToSip)
        //        .Include(c => c.ToSip.Location)
        //        .Include(c => c.ToSip.UserAgent)
        //        .Where(call => !call.Closed)
        //        .ToList();

        //    foreach (var item in registeredSips)
        //    {
        //        var sip = item;
        //        var call = dbCalls.FirstOrDefault(c => c.ToUsername == sip.Sip || c.FromUsername == sip.Sip);
        //        if (call == null)
        //        {
        //            sip.InCall = false;
        //            continue;
        //        }

        //        sip.InCall = true;
        //        sip.IsPhoneCall = call.IsPhoneCall;
        //        sip.CallStartedAt = call.Started;

        //        if (call.FromUsername == sip.Sip)
        //        {
        //            sip.InCallWithId = GuidHelper.GuidString(call.ToId);
        //            sip.InCallWithName =
        //                CallDisplayNameHelper.GetDisplayName(call.ToSip, call.ToDisplayName, call.ToUsername,
        //                    sipDomain);
        //            sip.InCallWithSip = call.ToUsername;
        //            sip.InCallWithLocation = call.ToSip != null && call.ToSip.Location != null
        //                ? call.ToSip.Location.Name
        //                : string.Empty;
        //            sip.InCallWithHasGpo = call.ToSip != null && call.ToSip.UserAgent != null &&
        //                                   !string.IsNullOrEmpty(call.ToSip.UserAgent.GpoNames);
        //            sip.IsCallingPart = true;
        //        }
        //        else
        //        {
        //            sip.InCallWithId = GuidHelper.GuidString(call.FromId);
        //            sip.InCallWithName = CallDisplayNameHelper.GetDisplayName(call.FromSip, call.FromDisplayName,
        //                call.FromUsername, sipDomain);
        //            sip.InCallWithSip = call.FromUsername;
        //            sip.InCallWithLocation = call.FromSip != null && call.FromSip.Location != null
        //                ? call.FromSip.Location.Name
        //                : string.Empty;
        //            sip.InCallWithHasGpo = call.FromSip != null && call.FromSip.UserAgent != null &&
        //                                   !string.IsNullOrEmpty(call.FromSip.UserAgent.GpoNames);
        //            sip.IsCallingPart = false;
        //        }
        //    }
        //}

        private List <KeyValuePair <string, string> > GetMetaData(List <MetaType> metaList, RegisteredSipEntity sip)
        {
            metaList = metaList ?? new List <MetaType>();

            var userAgentMetaDataList = metaList
                                        .Select(meta =>
                                                new KeyValuePair <string, string>(meta.Name,
                                                                                  MetadataHelper.GetPropertyValue(sip, meta.FullPropertyName)))
                                        .Where(m => !string.IsNullOrEmpty(m.Value))
                                        .ToList();

            return(userAgentMetaDataList);
        }