public SipEventHandlerResult RegisterCodec(SipRegistrationMessage sipMessage) { var userAgentRegistration = new UserAgentRegistration( sipUri: sipMessage.Sip.UserAtHost, userAgentHeader: sipMessage.UserAgent, username: sipMessage.Sip.UserAtHost, displayName: string.IsNullOrEmpty(sipMessage.ToDisplayName) ? sipMessage.FromDisplayName : sipMessage.ToDisplayName, registrar: sipMessage.Registrar, ipAddress: sipMessage.Ip, port: sipMessage.Port, expirationTimeSeconds: sipMessage.Expires, serverTimeStamp: sipMessage.UnixTimeStamp ); return(_sipRepository.UpdateRegisteredSip(userAgentRegistration)); }
public void RegisterSip(string sipAddress, string ip, string displayName, string userAgent) { var userAgentRegistration = new UserAgentRegistration( sipUri: sipAddress, userAgentHeader: userAgent, username: sipAddress, displayName: displayName, registrar: string.Empty, ipAddress: ip, port: 5060, expirationTimeSeconds: 60, serverTimeStamp: 0 ); var changeStatus = _registeredSipRepository.UpdateRegisteredSip(userAgentRegistration); Console.WriteLine("Registered {0} with result: {1}", sipAddress, changeStatus); }
public SipEventHandlerResult UpdateRegisteredSip(UserAgentRegistration userAgentRegistration) { var result = _internalRepository.UpdateRegisteredSip(userAgentRegistration); // When reregistration of codec already in cache, just update timestamp if (result.ChangeStatus == SipEventChangeStatus.NothingChanged && result.ChangedObjectId != Guid.Empty) { // TODO: When GetRegisteredSips is gone, decide over this //var regSip = GetRegisteredSips().FirstOrDefault(rs => rs.Id == result.ChangedObjectId); //if (regSip != null) //{ // regSip.Updated = DateTime.UtcNow; // return result; //} return(result); } // Otherwise reload cache. _lazyCache.ClearRegisteredUserAgents(); return(result); }
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); } }