// /** // * File: IPNode.cs // * Author: haraldwolff // * // * This file and it's content is copyrighted by the Author and / or copyright holder. // * Any use wihtout proper permission is illegal and may lead to legal actions. // * // * // **/ using System; using ln.types; using System.Collections.Generic; using System.Linq; using ln.types.odb; using Newtonsoft.Json; using LiteDB; using ln.types.threads; using ln.types.net; namespace ln.skyscanner.entities { public class Node { [DocumentID] public Guid ID { get; set; } = Guid.NewGuid(); public IPv4 PrimaryIP { get; set; } public String PrimaryMac { get; set; } public String Name { get; set; } public DateTime Created { get; } public DateTime LastUpdate { get; set; } public URI[] URIs => uris.ToArray(); public GeoLocation Location { get; set; } public string Vendor { get; set; } public string Product { get; set; } public string ProductLine { get; set; } public List Interfaces { get; private set; } = new List(); public IEnumerable IPAdresses => Interfaces.SelectMany(intf => intf.ConfiguredIPs.Select(nip => nip.IP)); public IEnumerable Networks => Interfaces.SelectMany(intf => intf.ConfiguredIPs.Select(nip => nip.Network)); private HashSet uris = new HashSet(); private Node() { } public Node(IPv4 primaryIP) { PrimaryIP = primaryIP; Name = PrimaryIP.ToString(); Created = DateTime.Now; } public void RemoveURI(string scheme) { RemoveURI(FindURIs(scheme)); } public void RemoveURI(IEnumerable uris) { foreach (URI uri in uris.ToArray()) RemoveURI(uri); } public void RemoveURI(URI uri) { foreach (URI u in uris.ToArray()) { if (uri.Equals(u)) uris.Remove(u); } } public void AddURI(URI uri) { uris.Add(uri); } public IEnumerable FindURIs(string scheme) { return uris.Where((u) => scheme.Equals(u.Scheme)); } public NetworkInterface GetInterface(String intfName) { return SkyScanner.Instance.Entities.interfaceCollection.Where(intf => intf.NodeID.Equals(ID) && intf.Name.Equals(intfName)).FirstOrDefault(); } public bool HasInterface(string intfName) { return GetInterface(intfName) != null; } public override int GetHashCode() { return ID.GetHashCode(); } public override bool Equals(object obj) { return (obj is Node) && (ID.Equals((obj as Node).ID)); } public override string ToString() { return String.Format("[Node ID={0} PrimaryIP={1} Name={2}]",ID,PrimaryIP,Name); } } }