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;
            }
        }
Exemple #2
0
        public override TimeSpan Ping(Contact source, TimeSpan timeout)
        {
            UdpContact uSource = source as UdpContact;

            using (MemoryStream m = new MemoryStream())
            {
                using (BinaryWriter w = new BinaryWriter(m))
                {
                    w.Write((byte)PacketFlag.Ping);

                    WriteContact(w, this);

                    var callback = localTable.GetConsumer <Callback>(Callback.CONSUMER_ID);
                    var token    = callback.AllocateToken();

                    w.Write(IPAddress.HostToNetworkOrder(token.Id));

                    try
                    {
                        SendUdpMessage(m.ToArray(), Ip, Port);

                        DateTime start = DateTime.Now;
                        if (token.Wait(timeout.Milliseconds))
                        {
                            return(DateTime.Now - start);
                        }
                        return(TimeSpan.MaxValue);
                    }
                    finally
                    {
                        callback.FreeToken(token);
                    }
                }
            }
        }
Exemple #3
0
        private static void ParsePing(BinaryReader reader)
        {
            UdpContact c = ReadContact(reader);

            long tokenId = IPAddress.NetworkToHostOrder(reader.ReadInt64());

            localTable.DeliverPing(c);

            var callback = localTable.GetConsumer <Callback>(Callback.CONSUMER_ID);

            callback.SendResponse(localTable.LocalContact, c, tokenId, new byte[] { 1, 3, 3, 7 });
        }
Exemple #4
0
        private static void ParseData(BinaryReader reader)
        {
            UdpContact source = ReadContact(reader);

            int guidLength = IPAddress.NetworkToHostOrder(reader.ReadInt32());

            byte[] guidBytes  = reader.ReadBytes(guidLength);
            Guid   consumerId = new Guid(guidBytes);

            int msgLength = IPAddress.NetworkToHostOrder(reader.ReadInt32());

            byte[] msg = reader.ReadBytes(msgLength);

            localTable.Deliver(source, consumerId, msg);
        }
Exemple #5
0
        private static void WriteContact(BinaryWriter w, UdpContact c)
        {
            byte[] idBytes = c.Identifier.GetBytes().ToArray();
            w.Write(IPAddress.HostToNetworkOrder(idBytes.Length));
            w.Write(idBytes);

            byte[] netIdBytes = c.NetworkId.ToByteArray();
            w.Write(IPAddress.HostToNetworkOrder(netIdBytes.Length));
            w.Write(netIdBytes);

            w.Write(IPAddress.HostToNetworkOrder(c.Port));

            byte[] addrBytes = c.Ip.GetAddressBytes();
            w.Write(IPAddress.HostToNetworkOrder(addrBytes.Length));
            w.Write(addrBytes);
        }
        private static void WriteContact(BinaryWriter w, UdpContact c)
        {
            byte[] idBytes = c.Identifier.GetBytes().ToArray();
            w.Write(IPAddress.HostToNetworkOrder(idBytes.Length));
            w.Write(idBytes);

            byte[] netIdBytes = c.NetworkId.ToByteArray();
            w.Write(IPAddress.HostToNetworkOrder(netIdBytes.Length));
            w.Write(netIdBytes);

            w.Write(IPAddress.HostToNetworkOrder(c.Port));

            byte[] addrBytes = c.Ip.GetAddressBytes();
            w.Write(IPAddress.HostToNetworkOrder(addrBytes.Length));
            w.Write(addrBytes);
        }