public Game1(int port, Identifier512 id) { this.Port = port; graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; }
public void Construction() { Identifier512 a = new Identifier512(1, 2, 3, 4, 5, 6, 7, 8, 8, 10, 11, 12, 13, 14, 15, 16); Identifier512 b = new Identifier512(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 8, 10, 11, 12, 13, 14, 15, 16 }); Assert.AreEqual(a, b); }
public void Ordering() { var a = new Identifier512(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1); var b = new Identifier512(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2); var c = new Identifier512(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3); var d = new Identifier512(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4); Assert.IsTrue(a < b); Assert.IsTrue(a < c); Assert.IsTrue(a < d); Assert.IsFalse(a < a); Assert.IsFalse(a > a); Assert.IsTrue(b > a); Assert.IsTrue(b < c); Assert.IsTrue(b < d); Assert.IsFalse(b < b); Assert.IsFalse(b > b); Assert.IsTrue(c > a); Assert.IsTrue(c > b); Assert.IsTrue(c < d); Assert.IsFalse(c < c); Assert.IsFalse(c > c); Assert.IsTrue(d > a); Assert.IsTrue(d > b); Assert.IsTrue(d > c); Assert.IsFalse(d < d); Assert.IsFalse(d > d); }
private static IEnumerable<Contact> LoadBootstrapData() { foreach (var line in File.ReadAllLines("Bootstrap.txt").OmitComments("#", "//")) { UdpContact udpC = null; try { string[] split = line.Split(' '); Guid a = Guid.Parse(split[0]); Guid b = Guid.Parse(split[1]); Guid c = Guid.Parse(split[2]); Guid d = Guid.Parse(split[3]); Identifier512 id = new Identifier512(a, b, c, d); IPAddress ip = IPAddress.Parse(split[4]); int port = Int32.Parse(split[5]); udpC = new UdpContact(id, networkId, ip, port); Console.WriteLine("Loaded bootstrap contact " + udpC); } catch (Exception e) { Console.WriteLine("Exception parsing bootstrap file: " + e); } if (udpC != null) yield return udpC; } }
/// <summary> /// Returns an enumeration of all nodes in ascending order of distance from the given identifier /// </summary> /// <param name="identifier">The identifier.</param> /// <returns></returns> public IEnumerable<Contact> ClosestNodes(Identifier512 identifier) { int mid = Identifier512.CommonPrefixLength(identifier, LocalIdentifier); if (mid != 512) foreach (var contact in buckets[mid].OrderWithComparer(new ContactComparer(identifier))) yield return contact; //loop through buckets, moving up and down from mid concatenating the two buckets either side of mid and returning them in order of distance List<Contact> contacts = new List<Contact>(); bool moreLow = true; bool moreHigh = true; for (int i = 1; moreHigh || moreLow; i++) { int indexHigh = mid + i; int indexLow = mid - i; contacts.Clear(); if (indexHigh >= buckets.Length) moreHigh = false; else contacts.AddRange(buckets[indexHigh]); if (indexLow < 0) moreLow = false; else contacts.AddRange(buckets[indexLow]); foreach (var contact in contacts.OrderWithComparer(new ContactComparer(identifier))) yield return contact; } }
private Vector2 GetPosition(Identifier512 id, SortedDictionary<Identifier512, Peer> peers) { Peer end; if (!peers.TryGetValue(id, out end)) end = peers.Where(a => a.Key >= id).Select(a => a.Value).FirstOrDefault() ?? peers.Last().Value; return end.Position; }
public void Distance() { var a = new Identifier512(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1); var b = new Identifier512(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2); var c = new Identifier512(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3); Assert.IsTrue(Identifier512.Distance(a, c) > Identifier512.Distance(b, c)); }
public void DistanceReversability() { var a = new Identifier512(new int[] { 0, 2 }); var b = new Identifier512(new int[] { 0, 1 }); var a2b = Identifier512.Distance(a, b); var b2a = Identifier512.Distance(b, a); Assert.AreEqual(a2b, b2a); }
public IterativeLookupRequest(Guid lookupId, Identifier512 LocalIdentifier, Guid NetworkId, Configuration Configuration, Identifier512 target, int limit) { this.lookupId = lookupId; this.LocalIdentifier = LocalIdentifier; this.NetworkId = NetworkId; this.Configuration = Configuration; this.target = target; this.limit = limit; }
public void AddInteger() { var a = new Identifier512(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1); var b = a + 1; var c = b + 1; Assert.AreNotEqual(a, b); Assert.AreNotEqual(b, c); Assert.AreNotEqual(c, a); Assert.IsTrue(a < b); Assert.IsTrue(b < c); }
public static Identifier512 SelectClosest(Identifier512 a, Identifier512 b, Identifier512 target) { var a2Target = Distance(a, target); var b2Target = Distance(a, target); if (a2Target < b2Target) { return(a); } return(b); }
public override bool Equals(object obj) { Identifier512 id = obj as Identifier512; if (id == null) { return(base.Equals(obj)); } else { return(Equals(id)); } }
public BroadcastPeer(Identifier512 rootNode, bool root) : base(GUID) { Root = root; RootNode = rootNode; BindProcessors(new Dictionary<byte, Action<Contact, byte[]>>() { { (byte)PacketFlag.VideoFrame, VideoFrameDataProcessor }, { (byte)PacketFlag.ParentRequest, ParentRequestProcessor }, { (byte)PacketFlag.Pong, PongRequestProcessor }, }); }
public void Put(Identifier512 key, byte[] data) { var closest = RoutingTable.GetConsumer<GetClosestNodes>(GetClosestNodes.GUID).GetClosestContacts(key).First(); using(MemoryStream mStream = new MemoryStream()) { mStream.WriteByte((byte)PacketFlag.PutRequest); Serializer.SerializeWithLengthPrefix<PutRequest>(mStream, new PutRequest(key, data), PrefixStyle.Base128); closest.Send(RoutingTable.LocalContact, ConsumerId, mStream.ToArray()); } }
public ProxyContact Construct(IPEndPoint iPEndPoint, Guid networkId, Identifier512 remoteId) { lock (proxies) { if (!proxies.ContainsKey(iPEndPoint)) { var proxy = new UdpProxy() { EndPoint = iPEndPoint }; var kvp = new KeyValuePair<ProxyContact, UdpProxy>(new ProxyContact(this.Type, proxy), proxy); proxies[iPEndPoint] = kvp; kvp.Key.NetworkId = networkId; kvp.Key.Identifier = remoteId; } return proxies[iPEndPoint].Key; } }
/// <summary> /// Calculates the distance metric between the two identifiers /// </summary> /// <param name="a">An identifier</param> /// <param name="b">An identifier</param> /// <returns></returns> public static int CommonPrefixLength(Identifier512 a, Identifier512 b) { int distance = 0; //look through to find the byte which is different for (int i = 0; i < BIT_LENGTH / 8; i++) { if (a.bytes[i] == b.bytes[i]) { distance += 8; } else { distance += LookupCommonPrefixLength(a.bytes[i], b.bytes[i]); break; } } return(distance); }
/// <summary> /// Gets a key which is the hash of this one /// </summary> /// <returns></returns> public Identifier512 GetHashedKey() { Identifier512 unhashedKey1 = this + 1; Identifier512 unhashedKey2 = unhashedKey1 + 1; Identifier512 unhashedKey3 = unhashedKey2 + 1; MD5 hasher = MD5.Create(); byte[] b = hasher.ComputeHash(this.GetBytes().ToArray()) .Append(hasher.ComputeHash(unhashedKey1.GetBytes().ToArray())) .Append(hasher.ComputeHash(unhashedKey2.GetBytes().ToArray())) .Append(hasher.ComputeHash(unhashedKey3.GetBytes().ToArray())).ToArray(); if (b.Length * 8 != 512) { throw new Exception("Length of array should be 512 bits"); } return(new Identifier512(b)); }
public bool Equals(Identifier512 other) { if (other == null) { return(false); } if (bytes.Length != other.bytes.Length) { return(false); } for (int i = 0; i < bytes.Length; i++) { if (bytes[i] != other.bytes[i]) { return(false); } } return(true); }
public void ErasureStore() { List<DistributedRoutingTable> network = CreateNetwork(); Guid baseStorageId = Guid.NewGuid(); Guid consumerId = Guid.NewGuid(); for (int i = 0; i < network.Count; i++) { var kvps = new KeyValuePairStore(baseStorageId); network[i].RegisterConsumer(kvps); network[i].RegisterConsumer(new ErasureDataStore(consumerId, kvps)); } Identifier512 key = new Identifier512(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1); byte[] data = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 }; network[1].GetConsumer<ErasureDataStore>(consumerId).Put(key, data, 1, 2); var result = network[2].GetConsumer<ErasureDataStore>(consumerId).Get(key, 1000); for (int i = 0; i < result.Length; i++) Assert.AreEqual(result[i], data[i]); }
public void KeyValuePairStore() { List<DistributedRoutingTable> network = CreateNetwork(); Guid consumerId = Guid.NewGuid(); for (int i = 0; i < network.Count; i++) network[i].RegisterConsumer(new KeyValuePairStore(consumerId)); Identifier512 key = new Identifier512(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1); byte[] data = new byte[] { 1 }; network[1].GetConsumer<KeyValuePairStore>(consumerId).Put(key, data); var result = network[2].GetConsumer<KeyValuePairStore>(consumerId).Get(key, 1000); for (int i = 0; i < result.Length; i++) Assert.AreEqual(result[i], data[i]); network[3].GetConsumer<KeyValuePairStore>(consumerId).Delete(key); result = network[4].GetConsumer<KeyValuePairStore>(consumerId).Get(key, 1000); Assert.IsNull(result); }
public byte[] Get(Identifier512 key, int timeout) { byte[] localData = GetData(key); if (localData != null) return localData; var token = Callback.AllocateToken(); GetRequest request = new GetRequest(key, token.Id); using (MemoryStream mStream = new MemoryStream()) { mStream.WriteByte((byte)PacketFlag.GetRequest); Serializer.SerializeWithLengthPrefix<GetRequest>(mStream, request, PrefixStyle.Base128); var remote = RoutingTable.GetConsumer<GetClosestNodes>(GetClosestNodes.GUID).GetClosestContacts(key).First(); remote.Send(RoutingTable.LocalContact, ConsumerId, mStream.ToArray()); } if (!token.Wait(timeout)) throw new TimeoutException(); return token.Response; }
public void ReverseTriangleInequality() { var a = new Identifier512(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1); var b = new Identifier512(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2); var c = new Identifier512(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3); var AToB = Identifier512.Distance(a, b); var AToC = Identifier512.Distance(a, c); var BToC = Identifier512.Distance(b, c); var BToA = Identifier512.Distance(b, a); var CToB = Identifier512.Distance(c, b); var CToA = Identifier512.Distance(c, a); Assert.IsTrue(AToC >= Identifier512.Distance(AToB, BToC)); Assert.IsTrue(AToB >= Identifier512.Distance(AToC, CToB)); Assert.IsTrue(BToC >= Identifier512.Distance(BToA, AToC)); Assert.IsTrue(BToA >= Identifier512.Distance(BToC, CToA)); Assert.IsTrue(CToA >= Identifier512.Distance(CToB, BToA)); Assert.IsTrue(CToB >= Identifier512.Distance(CToA, AToB)); }
public Peer(Identifier512 id) { Identifier = id; }
private static UdpContact ReadContact(BinaryReader reader) { int idBytesLength = IPAddress.NetworkToHostOrder(reader.ReadInt32()); byte[] idBytes = reader.ReadBytes(idBytesLength); Identifier512 id = new Identifier512(idBytes); int netIdBytesLength = IPAddress.NetworkToHostOrder(reader.ReadInt32()); byte[] netIdBytes = reader.ReadBytes(netIdBytesLength); Guid netId = new Guid(netIdBytes); int port = IPAddress.NetworkToHostOrder(reader.ReadInt32()); int addrBytesLength = IPAddress.NetworkToHostOrder(reader.ReadInt32()); byte[] addrBytes = reader.ReadBytes(addrBytesLength); IPAddress address = new IPAddress(addrBytes); return new UdpContact(id, netId, address, port); }
public UdpContact(Identifier512 id, Guid networkId, IPAddress ip, int port) : base(id, networkId) { Ip = ip; Port = port; }
public static Identifier512 Distance(Identifier512 a, Identifier512 b) { return(a ^ b); }
public DRTConstructed(Identifier512 localIdentifier, Guid networkId, Configuration configuration) { this.localIdentifier = localIdentifier; this.networkId = networkId; this.configuration = configuration; }
public bool Equals(Identifier512 other) { if (other == null) return false; if (bytes.Length != other.bytes.Length) return false; for (int i = 0; i < bytes.Length; i++) if (bytes[i] != other.bytes[i]) return false; return true; }
public static Identifier512 SelectClosest(Identifier512 a, Identifier512 b, Identifier512 target) { var a2Target = Distance(a, target); var b2Target = Distance(a, target); if (a2Target < b2Target) return a; return b; }
public static Identifier512 Distance(Identifier512 a, Identifier512 b) { return a ^ b; }
/// <summary> /// Calculates the distance metric between the two identifiers /// </summary> /// <param name="a">An identifier</param> /// <param name="b">An identifier</param> /// <returns></returns> public static int CommonPrefixLength(Identifier512 a, Identifier512 b) { int distance = 0; //look through to find the byte which is different for (int i = 0; i < BIT_LENGTH / 8; i++) { if (a.bytes[i] == b.bytes[i]) { distance += 8; } else { distance += LookupCommonPrefixLength(a.bytes[i], b.bytes[i]); break; } } return distance; }
public void Delete(Identifier512 key) { Put(key, null); }
public Lookup(IterativeLookupRequest request) { Start = request.LocalIdentifier; Target = request.target; }
public GetRequest(Identifier512 key, long tokenId) { Key = key; TokenId = tokenId; }
public PutRequest(Identifier512 key, byte[] data) { Key = key; Data = data; }
private byte[] GetData(Identifier512 key) { lock (localData) { byte[] b = null; localData.TryGetValue(key, out b); return b; } }