Improve time server identification

This commit is contained in:
Carsten Larsen 2016-12-26 14:14:27 +01:00
parent abcf9f306e
commit fe9816b883
6 changed files with 61 additions and 17 deletions

View File

@ -34,6 +34,8 @@ protected NamedObject(int id, string name)
Name = name;
}
public string Name { get; }
public string Name { get; set; }
public virtual string DisplayName => Name;
}
}

View File

@ -37,14 +37,16 @@ public Peer(string name, string ip, TimeServer server)
Server = server;
}
public string Ip { get; }
public string Ip { get; set; }
public TimeServer Server { get; }
public TimeServer Server { get; set; }
/// <summary>
/// Gets the server identifier as specified on support.ntp.org.
/// </summary>
/// <value>The org identifier.</value>
public int? OrgId => Server?.Id;
public override string DisplayName => string.IsNullOrEmpty(Server?.Name) ? Name : Server.Name;
}
}

View File

@ -80,5 +80,7 @@ protected TimeServer(int id)
public abstract string ProviderUrl { get; }
public abstract DateTime Updated { get; }
public bool IsOrgServer => Id < 5000;
}
}

View File

@ -66,7 +66,7 @@ public Initializer(string configFile, int pid, string pidFile, string name, LogG
this.initlog = initlog;
Nodes = new List<IRequest>();
Listeners = new List<Listener>();
version = "0.8.0";
version = "0.8.1";
}
private static bool firstrun = true;
@ -196,6 +196,8 @@ private void InitializeData()
if (!config.Database.Initialize)
return;
TimeServerWebAdapter.Initialize(config.Database.Import);
try
{
// Initialize hosts
@ -252,21 +254,51 @@ private void InitializeData()
foreach (var entry in peerImporter.ToList())
{
var currentEntry = entry;
var peerList = DataFace.Instance.Peers.Where(p => p.Ip == currentEntry.Remote);
if (peerList.Any())
continue;
var peerList = DataFace.Instance.Peers.Where(p => p.Ip == currentEntry.Remote).ToList();
TimeServer timeServer = null;
if (TimeServers.List.ContainsKey(entry.Remote))
if (!peerList.Any())
{
int orgId = TimeServers.List[entry.Remote];
timeServer = DataFace.Instance.Servers[orgId];
Log.KnownServer(orgId, entry.Remote);
}
// Create new peer in database
TimeServer timeServer = null;
if (TimeServers.List.ContainsKey(entry.Remote))
{
int orgId = TimeServers.List[entry.Remote];
timeServer = DataFace.Instance.Servers[orgId];
Log.KnownServer(orgId, entry.Remote);
}
var peer = new Peer(entry.Remote, entry.Remote, timeServer);
DataFace.Instance.Peers.Save(peer);
Log.NewPeer(entry);
var hostName = entry.Remote;
try
{
var hostEntry = Dns.GetHostEntry(entry.Remote);
hostName = hostEntry.HostName;
}
catch (Exception e)
{
Log.HostNameNotFound(entry.Remote, e);
}
var peer = new Peer(hostName, entry.Remote, timeServer);
DataFace.Instance.Peers.Save(peer);
Log.NewPeer(entry);
}
else if (TimeServers.List.ContainsKey(entry.Remote))
{
// Try to update existing peer
var peer = peerList.First();
if (peer.Server != null && peer.Server.IsOrgServer)
continue;
int orgId = TimeServers.List[entry.Remote];
var timeServer = DataFace.Instance.Servers[orgId];
Log.KnownServer(orgId, entry.Remote);
if (timeServer == null)
continue;
peer.Server = timeServer;
DataFace.Instance.Peers.Save(peer);
}
}
}
}

View File

@ -187,5 +187,11 @@ internal static void UserIdError(this LogBase log, uint userId)
{
log.WriteLine($"Failed to set user id {userId}", Severity.Warn);
}
internal static void HostNameNotFound(this LogBase log, string ip, Exception e)
{
log.WriteLine($"Could not find host name for IP {ip}.", Severity.Info);
log.WriteLine(e, Severity.Debug);
}
}
}

View File

@ -189,7 +189,7 @@ public sealed class PeerGraphConfiguration : GraphBaseConfiguration, IPeerGraphC
public override string GetTitle(NamedObject namedObject)
{
return string.Concat(namedObject.Name, " ", Title);
return string.Concat(namedObject.DisplayName, " ", Title);
}
public override string GetAltName(GraphSetConfiguration owner, string postfix)