private static void ParseConfig(string filename) { var doc = XDocument.Load(filename); var mode = RequireAttr(doc.Elements("xml").Elements("policy").Single(), "mode").ToLower(); if (mode == "allow") _policy = AccessTrafficRule.AllowRules; else if (mode == "deny") _policy = AccessTrafficRule.DenyRules; else throw new Exception("Invalid mode '" + mode + ", expected 'deny' or 'allow'"); foreach (var schedule in doc.Elements("xml").Elements("schedules").Elements("schedule")) { var name = RequireAttr(schedule, "name"); var id = 0; foreach (var item in schedule.Elements("rule")) { var rule = new AccessSchedule { Group = name, Name = name + "-" + (++id) }; var start = GetAttr(item, "start"); var end = GetAttr(item, "end"); if (string.IsNullOrEmpty(start) && string.IsNullOrEmpty(end)) rule.AllHours = true; else { if (string.IsNullOrEmpty(start)) start = "00:00:00"; if (string.IsNullOrEmpty(end)) end = "23:59:00"; rule.TimeStart = TimeSpan.Parse(start); rule.TimeEnd = TimeSpan.Parse(end); } rule.Days = ParseDays(RequireAttr(item, "days")); _schedules.Add(rule); } } foreach (var item in doc.Elements("xml").Elements("hosts").Elements("host")) { _hosts.Add(new AccessHost { Name = RequireAttr(item, "name"), MacAddress = RequireAttr(item, "mac"), Schedule = GetAttr(item, "schedule") }); } // Verify integrity var fails = _hosts.GroupBy(x => x.Name).Where(x => x.Count() > 1).Select(x => x.Key).ToList(); if (fails.Any()) throw new Exception("Hosts " + string.Join(", ", fails) + " do not have a unique name"); fails = _hosts.GroupBy(x => x.MacAddress).Where(x => x.Count() > 1).Select(x => x.Key).ToList(); if (fails.Any()) throw new Exception("MAC addresses " + string.Join(", ", fails) + " exists more than once"); var schedules = _schedules.Select(x => x.Group).Distinct().ToList(); foreach(var host in _hosts.Where(h => !string.IsNullOrEmpty(h.Schedule))) if (!schedules.Contains(host.Schedule, StringComparer.CurrentCultureIgnoreCase)) throw new Exception("Host " + host.Name + " has an undefined schedule " + host.Schedule); }
private static void ParseConfig(string filename) { var doc = XDocument.Load(filename); var mode = RequireAttr(doc.Elements("xml").Elements("policy").Single(), "mode").ToLower(); if (mode == "allow") { _policy = AccessTrafficRule.AllowRules; } else if (mode == "deny") { _policy = AccessTrafficRule.DenyRules; } else { throw new Exception("Invalid mode '" + mode + ", expected 'deny' or 'allow'"); } foreach (var schedule in doc.Elements("xml").Elements("schedules").Elements("schedule")) { var name = RequireAttr(schedule, "name"); var id = 0; foreach (var item in schedule.Elements("rule")) { var rule = new AccessSchedule { Group = name, Name = name + "-" + (++id) }; var start = GetAttr(item, "start"); var end = GetAttr(item, "end"); if (string.IsNullOrEmpty(start) && string.IsNullOrEmpty(end)) { rule.AllHours = true; } else { if (string.IsNullOrEmpty(start)) { start = "00:00:00"; } if (string.IsNullOrEmpty(end)) { end = "23:59:00"; } rule.TimeStart = TimeSpan.Parse(start); rule.TimeEnd = TimeSpan.Parse(end); } rule.Days = ParseDays(RequireAttr(item, "days")); _schedules.Add(rule); } } foreach (var item in doc.Elements("xml").Elements("hosts").Elements("host")) { _hosts.Add(new AccessHost { Name = RequireAttr(item, "name"), MacAddress = RequireAttr(item, "mac"), Schedule = GetAttr(item, "schedule") }); } // Verify integrity var fails = _hosts.GroupBy(x => x.Name).Where(x => x.Count() > 1).Select(x => x.Key).ToList(); if (fails.Any()) { throw new Exception("Hosts " + string.Join(", ", fails) + " do not have a unique name"); } fails = _hosts.GroupBy(x => x.MacAddress).Where(x => x.Count() > 1).Select(x => x.Key).ToList(); if (fails.Any()) { throw new Exception("MAC addresses " + string.Join(", ", fails) + " exists more than once"); } var schedules = _schedules.Select(x => x.Group).Distinct().ToList(); foreach (var host in _hosts.Where(h => !string.IsNullOrEmpty(h.Schedule))) { if (!schedules.Contains(host.Schedule, StringComparer.CurrentCultureIgnoreCase)) { throw new Exception("Host " + host.Name + " has an undefined schedule " + host.Schedule); } } }
public void AddSchedule(AccessSchedule schedule) { DoRequest("userRpm/AccessCtrlTimeSchedRpm.htm?" + schedule); }