public void SMTest() { Address a = new DirectionalAddress(DirectionalAddress.Direction.Left); TransportAddress ta = TransportAddressFactory.CreateInstance("brunet.tcp://127.0.0.1:5000"); NodeInfo ni = NodeInfo.CreateInstance(a, ta); //Test with one neighbor: ArrayList neighbors = new ArrayList(); neighbors.Add(ni); StatusMessage sm1 = new StatusMessage(ConnectionType.Structured, neighbors); RoundTripHT(sm1); //Console.Error.WriteLine("\n{0}\n", sm1); //Test with many neighbors: for (int i = 5001; i < 5010; i++) { neighbors.Add(NodeInfo.CreateInstance(a, TransportAddressFactory.CreateInstance("brunet.tcp://127.0.0.1:" + i.ToString()))); } StatusMessage sm2 = new StatusMessage(ConnectionType.Unstructured, neighbors); RoundTripHT(sm2); //Console.Error.WriteLine("\n{0}\n", sm2); //Here is a StatusMessage with no neighbors (that has to be a possibility) StatusMessage sm3 = new StatusMessage("structured", new ArrayList()); RoundTripHT(sm3); //Console.Error.WriteLine("\n{0}\n", sm3); }
public DirectionalAddress(DirectionalAddress.Direction bearing) : base() { byte[] buffer = new byte[ MemSize ]; NumberSerializer.WriteInt((int)bearing, buffer, 0); SetClass(buffer, this.Class); _dir = bearing; _buffer = MemBlock.Reference(buffer, 0, MemSize); }
/** * Read the address out of the buffer This makes a copy * and calls Parse on the copy. This is a "convienience" method. * @throw ParseException if the buffer is not a valid address */ static public Address Parse(MemBlock mb) { //Read some of the least significant bytes out, //AHAddress all have last bit 0, so we skip the last byte which //will have less entropy ushort idx = (ushort)NumberSerializer.ReadShort(mb, Address.MemSize - 3); Address a = _mb_cache[idx]; if (a != null) { if (a.ToMemBlock().Equals(mb)) { return(a); } } //Else we need to read the address and put it in the cache try { if (2 * mb.Length < mb.ReferencedBufferLength) { /* * This MemBlock is much smaller than the array * we are referencing, don't keep the big one * in scope, instead make a copy */ mb = MemBlock.Copy((ICopyable)mb); } int add_class = Address.ClassOf(mb); switch (add_class) { case AHAddress.ClassValue: a = new AHAddress(mb); break; case DirectionalAddress.ClassValue: a = new DirectionalAddress(mb); break; default: a = null; throw new ParseException("Unknown Address Class: " + add_class + ", buffer:" + mb.ToString()); } //Cache this result: _mb_cache[idx] = a; return(a); } catch (ArgumentOutOfRangeException ex) { throw new ParseException("Address too short: " + mb.ToString(), ex); } catch (ArgumentException ex) { throw new ParseException("Could not parse: " + mb.ToString(), ex); } }
public void CTMSerializationTest() { Address a = new DirectionalAddress(DirectionalAddress.Direction.Left); TransportAddress ta = TransportAddressFactory.CreateInstance("brunet.tcp://127.0.0.1:5000"); NodeInfo ni = NodeInfo.CreateInstance(a, ta); RandomNumberGenerator rng = new RNGCryptoServiceProvider(); AHAddress tmp_add = new AHAddress(rng); ConnectToMessage ctm1 = new ConnectToMessage(ConnectionType.Unstructured, ni, tmp_add.ToString()); HTRoundTrip(ctm1); //Test multiple tas: ArrayList tas = new ArrayList(); tas.Add(ta); for (int i = 5001; i < 5010; i++) { tas.Add(TransportAddressFactory.CreateInstance("brunet.tcp://127.0.0.1:" + i.ToString())); } NodeInfo ni2 = NodeInfo.CreateInstance(a, tas); ConnectToMessage ctm2 = new ConnectToMessage(ConnectionType.Structured, ni2, tmp_add.ToString()); HTRoundTrip(ctm2); //Here is a ConnectTo message with a neighbor list: NodeInfo[] neighs = new NodeInfo[5]; for (int i = 0; i < 5; i++) { string ta_tmp = "brunet.tcp://127.0.0.1:" + (i + 80).ToString(); NodeInfo tmp = NodeInfo.CreateInstance(new DirectionalAddress(DirectionalAddress.Direction.Left), TransportAddressFactory.CreateInstance(ta_tmp) ); neighs[i] = tmp; } ConnectToMessage ctm3 = new ConnectToMessage("structured", ni, neighs, tmp_add.ToString()); HTRoundTrip(ctm3); #if false Console.Error.WriteLine(ctm3.ToString()); foreach (NodeInfo tni in ctm3a.Neighbors) { Console.Error.WriteLine(tni.ToString()); } #endif }
/** * Read the address out of the buffer This makes a copy * and calls Parse on the copy. This is a "convienience" method. * @throw ParseException if the buffer is not a valid address */ static public Address Parse(MemBlock mb) { #if BRUNET_SIMULATOR Address a = null; #else //Read some of the least significant bytes out, //AHAddress all have last bit 0, so we skip the last byte which //will have less entropy ushort idx = (ushort)NumberSerializer.ReadShort(mb, Address.MemSize - 3); Address a = _mb_cache[idx]; if( a != null ) { if( a.ToMemBlock().Equals(mb) ) { return a; } } #endif //Else we need to read the address and put it in the cache try { if( 2 * mb.Length < mb.ReferencedBufferLength ) { /* * This MemBlock is much smaller than the array * we are referencing, don't keep the big one * in scope, instead make a copy */ mb = MemBlock.Copy((ICopyable)mb); } int add_class = Address.ClassOf(mb); switch (add_class) { case AHAddress.ClassValue: a = new AHAddress(mb); break; case DirectionalAddress.ClassValue: a = new DirectionalAddress(mb); break; default: a = null; throw new ParseException("Unknown Address Class: " + add_class + ", buffer:" + mb.ToString()); } #if BRUNET_SIMULATOR return SimulatorCache(a); #else //Cache this result: _mb_cache[ idx ] = a; return a; #endif } catch(ArgumentOutOfRangeException ex) { throw new ParseException("Address too short: " + mb.ToString(), ex); } catch(ArgumentException ex) { throw new ParseException("Could not parse: " + mb.ToString(), ex); } }
public void SMTest() { Address a = new DirectionalAddress(DirectionalAddress.Direction.Left); TransportAddress ta = TransportAddressFactory.CreateInstance("brunet.tcp://127.0.0.1:5000"); NodeInfo ni = NodeInfo.CreateInstance(a, ta); //Test with one neighbor: ArrayList neighbors = new ArrayList(); neighbors.Add(ni); StatusMessage sm1 = new StatusMessage(ConnectionType.Structured, neighbors); RoundTripHT(sm1); //Console.Error.WriteLine("\n{0}\n", sm1); //Test with many neighbors: for(int i = 5001; i < 5010; i++) { neighbors.Add(NodeInfo.CreateInstance(a, TransportAddressFactory.CreateInstance("brunet.tcp://127.0.0.1:" + i.ToString()))); } StatusMessage sm2 = new StatusMessage(ConnectionType.Unstructured, neighbors); RoundTripHT(sm2); //Console.Error.WriteLine("\n{0}\n", sm2); //Here is a StatusMessage with no neighbors (that has to be a possibility) StatusMessage sm3 = new StatusMessage("structured", new ArrayList()); RoundTripHT(sm3); //Console.Error.WriteLine("\n{0}\n", sm3); }
protected void GetEstimatedSize(object obj, EventArgs eargs) { _local_network_size = _node.NetworkSize; ///original estimation try { short logN0 = (short)(Math.Log(_local_network_size) ); if (logN0 < 1) { logN0 = 1;} Address target = new DirectionalAddress(DirectionalAddress.Direction.Left); ISender send = new AHSender(_node, target, logN0, AHPacket.AHOptions.Last); ///log N0-hop away node Channel queue = new Channel(1); _rpc.Invoke(send, queue, "sys:link.Ping",0); queue.CloseEvent += delegate(object o, EventArgs args) { Channel q = (Channel)o; if (q.Count > 0) { RpcResult rres = (RpcResult)q.Dequeue(); AHSender res_sender = (AHSender)rres.ResultSender; AHAddress remote = (AHAddress)res_sender.Destination; ///this is logN0-hop away node's address AHAddress me = (AHAddress)_node.Address; BigInteger width = me.LeftDistanceTo(remote); ///distance between me and remote node BigInteger inv_density = width / (logN0); ///inverse density BigInteger total = Address.Full / inv_density; ///new estimation int total_int = total.IntValue(); _local_network_size = total_int; } }; } catch(Exception x) { if(ProtocolLog.Exceptions.Enabled) { ProtocolLog.Write(ProtocolLog.Exceptions, x.ToString()); } } }
public void CTMSerializationTest() { Address a = new DirectionalAddress(DirectionalAddress.Direction.Left); TransportAddress ta = TransportAddressFactory.CreateInstance("brunet.tcp://127.0.0.1:5000"); NodeInfo ni = NodeInfo.CreateInstance(a, ta); RandomNumberGenerator rng = new RNGCryptoServiceProvider(); AHAddress tmp_add = new AHAddress(rng); ConnectToMessage ctm1 = new ConnectToMessage(ConnectionType.Unstructured, ni, tmp_add.ToString()); HTRoundTrip(ctm1); //Test multiple tas: ArrayList tas = new ArrayList(); tas.Add(ta); for(int i = 5001; i < 5010; i++) tas.Add(TransportAddressFactory.CreateInstance("brunet.tcp://127.0.0.1:" + i.ToString())); NodeInfo ni2 = NodeInfo.CreateInstance(a, tas); ConnectToMessage ctm2 = new ConnectToMessage(ConnectionType.Structured, ni2, tmp_add.ToString()); HTRoundTrip(ctm2); //Here is a ConnectTo message with a neighbor list: NodeInfo[] neighs = new NodeInfo[5]; for(int i = 0; i < 5; i++) { string ta_tmp = "brunet.tcp://127.0.0.1:" + (i+80).ToString(); NodeInfo tmp = NodeInfo.CreateInstance(new DirectionalAddress(DirectionalAddress.Direction.Left), TransportAddressFactory.CreateInstance(ta_tmp) ); neighs[i] = tmp; } ConnectToMessage ctm3 = new ConnectToMessage("structured", ni, neighs, tmp_add.ToString()); HTRoundTrip(ctm3); #if false Console.Error.WriteLine( ctm3.ToString() ); foreach(NodeInfo tni in ctm3a.Neighbors) { Console.Error.WriteLine(tni.ToString()); } #endif }
public void TestWriteAndParse() { Address a = new DirectionalAddress(DirectionalAddress.Direction.Left); TransportAddress ta = TransportAddressFactory.CreateInstance("brunet.tcp://127.0.0.1:5000"); NodeInfo ni = NodeInfo.CreateInstance(a, ta); RoundTripHT(ni); RoundTrip(ni); //Test multiple tas: ArrayList tas = new ArrayList(); tas.Add(ta); for(int i = 5001; i < 5010; i++) tas.Add(TransportAddressFactory.CreateInstance("brunet.tcp://127.0.0.1:" + i.ToString())); NodeInfo ni3 = NodeInfo.CreateInstance(a, tas); RoundTripHT(ni3); RoundTrip(ni3); //Test null address: NodeInfo ni4 = NodeInfo.CreateInstance(null, ta); RoundTripHT(ni4); RoundTrip(ni4); //No TAs: NodeInfo ni5 = NodeInfo.CreateInstance( a ); RoundTripHT(ni5); RoundTrip(ni5); }
public void CacheTest() { Address a = new DirectionalAddress(DirectionalAddress.Direction.Left); Address a2 = new DirectionalAddress(DirectionalAddress.Direction.Left); TransportAddress ta = TransportAddressFactory.CreateInstance("brunet.tcp://127.0.0.1:5000"); TransportAddress ta2 = TransportAddressFactory.CreateInstance("brunet.tcp://127.0.0.1:5000"); NodeInfo ni = NodeInfo.CreateInstance(a, ta); NodeInfo ni2 = NodeInfo.CreateInstance(a2, ta2); Assert.AreSame( ni, ni2, "Reference equality of NodeInfo objects"); }