Files
ntpa/Ntp.Analyzer.Data/File/TimeServerFileMapper.cs
T
2016-03-06 00:15:13 +01:00

123 lines
4.7 KiB
C#

//
// TimeServerFileMapper.cs
//
// Author:
// Carsten Sonne Larsen <cs@innolan.dk>
//
// Copyright (c) 2013 Carsten Sonne Larsen
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
namespace Ntp.Analyzer.Data.File
{
/*
public class TimeServerFileMapper
{
public TimeServerFileMapper (string path)
{
this.path = path;
}
private readonly string path;
private const string filePrefix = "ntp.server.";
public TimeServer Read (int id)
{
string file = GetFileName (id);
if (!File.Exists (file))
return null;
string[] lines = File.ReadAllLines (file);
Dictionary<string, string> entries = new Dictionary<string, string> ();
foreach (string line in lines) {
int assingPos = line.IndexOf ('=');
string key = line.Substring (0, assingPos).Trim ();
string value = line.Substring (assingPos + 1, line.Length - assingPos - 1).Trim ();
entries.Add (key, value);
}
int stratus = Convert.ToInt32 (entries ["ServerStratum"]);
IPAddress address = null;
IPAddress.TryParse (entries ["IP Address"], out address);
bool useDns = false;
Boolean.TryParse (entries ["UseDNS"], out useDns);
bool poolMember = false;
Boolean.TryParse (entries ["PoolMember"], out poolMember);
bool notification = false;
Boolean.TryParse (entries ["NotificationMessage"], out notification);
return new TimeServer (
id, id, stratus, entries ["CountryCode"], entries ["Hostname"], address,
entries ["IPv6 Address"], useDns, poolMember, entries ["ServerLocation"],
entries ["DisplayLocation"], entries ["HostOrganization"], entries ["GeographicCoordinates"],
entries ["ServerSynchronization"], entries ["ServiceArea"],
entries ["AccessPolicy"], entries ["AccessDetails"],
notification, entries ["AutoKeyURL"],
entries ["SymmetricKeyType"], entries ["SymmetricKeyURL"],
entries ["ServerContact"]);
}
public void Write (TimeServer server)
{
if (server.OrgId == null)
return;
StringBuilder builder = new StringBuilder ();
builder.Append ("ID=" + server.OrgId + Environment.NewLine);
builder.Append ("ServerStratum=" + server.Stratum + Environment.NewLine);
builder.Append ("CountryCode=" + server.Country + Environment.NewLine);
builder.Append ("Hostname=" + server.Name + Environment.NewLine);
builder.Append ("IP Address=" + server.Address + Environment.NewLine);
builder.Append ("IPv6 Address=" + server.V6address + Environment.NewLine);
builder.Append ("UseDNS=" + server.UseDns + Environment.NewLine);
builder.Append ("PoolMember=" + server.PoolMember + Environment.NewLine);
builder.Append ("ServerLocation=" + server.Location + Environment.NewLine);
builder.Append ("HostOrganization=" + server.Organization + Environment.NewLine);
builder.Append ("GeographicCoordinates=" + server.Geo + Environment.NewLine);
builder.Append ("ServerSynchronization=" + server.Server + Environment.NewLine);
builder.Append ("ServiceArea=" + server.ServiceArea + Environment.NewLine);
builder.Append ("AccessPolicy=" + server.AccessDetails + Environment.NewLine);
builder.Append ("AccessDetails=" + server.AccessPolicy + Environment.NewLine);
builder.Append ("NotificationMessage=" + server.Notification + Environment.NewLine);
builder.Append ("AutoKeyURL=" + server.AutoKey + Environment.NewLine);
builder.Append ("SymmetricKeyType=" + server.SymKey + Environment.NewLine);
builder.Append ("SymmetricKeyURL=" + server.SymUrl + Environment.NewLine);
builder.Append ("ServerContact=" + server.Contact + Environment.NewLine);
string persistText = builder.ToString ();
File.WriteAllText (GetFileName (server.OrgId), persistText);
}
private string GetFileName (int? id)
{
return path + filePrefix + id;
}
}
*/
}