/** * When the other node gives us an error code, this * method tells us what to do based on that. * We always finish, but our result is not * fixed */ protected Result GetResultForErrorCode(int c) { Result result = Result.ProtocolError; if (c == (int)ErrorMessage.ErrorCode.InProgress) { result = Result.RetryThisTA; } else if (c == (int)ErrorMessage.ErrorCode.AlreadyConnected) { /* * The other side thinks we are already connected. This is * odd, let's see if we agree */ Address target = _linker.Target; ConnectionTable tab = _node.ConnectionTable; if (target == null) { //This can happen with leaf connections. In this case, we //should move on to another TA. result = Result.MoveToNextTA; } else if (tab.Contains(Connection.StringToMainType(_contype), target)) { //This shouldn't happen result = Result.ProtocolError; if (ProtocolLog.LinkDebug.Enabled) { ProtocolLog.Write(ProtocolLog.LinkDebug, String.Format( "Already connected: {0}, {1}", _contype, _target_lock)); } } else { //The other guy thinks we are connected, but we disagree, //let's retry. This can happen if we get disconnected //and reconnect, but the other node hasn't realized we //are disconnected. result = Result.RetryThisTA; } } else if (c == (int)ErrorMessage.ErrorCode.TargetMismatch) { /* * This could happen in some NAT cases, or perhaps due to * some other as of yet undiagnosed bug. * * Move to the next TA since this TA definitely connects to * the wrong guy. */ if (ProtocolLog.LinkDebug.Enabled) { ProtocolLog.Write(ProtocolLog.LinkDebug, String.Format( "LPS: from {0} target mismatch: {1}", _e, _target_lock)); } result = Result.MoveToNextTA; } else if (c == (int)ErrorMessage.ErrorCode.ConnectToSelf) { /* * Somehow we connected to ourself, this TA is no good. */ result = Result.MoveToNextTA; } else if (c == (int)ErrorMessage.ErrorCode.Disconnecting) { /* The other node is going offline */ if (_linker.Target == null) { result = Result.MoveToNextTA; } else { result = Result.ProtocolError; //Give up now } } else { if (ProtocolLog.LinkDebug.Enabled) { ProtocolLog.Write(ProtocolLog.LinkDebug, String.Format( "Unrecognized error code: {0}", c)); } } return(result); }
public void Test() { Address addr_x = new AHAddress(new RNGCryptoServiceProvider()); byte[] addrbuff = Address.ConvertToAddressBuffer(addr_x.ToBigInteger() + (Address.Full / 2)); Address.SetClass(addrbuff, AHAddress._class); Address addr_y = new AHAddress(addrbuff); List<Connection> connections = new List<Connection>(); ConnectionTable ct_x = new ConnectionTable(); ConnectionTable ct_y = new ConnectionTable(); ConnectionTable ct_empty = new ConnectionTable(); NCService ncservice = new NCService(); Connection fast_con = null; for(int i = 1; i <= 11; i++) { addrbuff = Address.ConvertToAddressBuffer(addr_x.ToBigInteger() + (i * Address.Full / 16)); Address.SetClass(addrbuff, AHAddress._class); Address addr = new AHAddress(addrbuff); Connection con = null; TransportAddress ta = TransportAddressFactory.CreateInstance("brunet.tcp://158.7.0.1:5000"); Edge fe = new FakeEdge(ta, ta, TransportAddress.TAType.Tcp); if(i <= 10) { con = new Connection(fe, addr, "structured", null, null); ct_x.Add(con); if(i % 2 == 0) { ncservice.ProcessSample(DateTime.UtcNow, String.Empty, addr, new Point(new double[] {0, 0}, 0), 0, i*10); } } else { fast_con = new Connection(fe, addr, "structured", null, null); ncservice.ProcessSample(DateTime.UtcNow, String.Empty, addr, new Point(new double[] {0, 0}, 0), 0, 5); } if(i == 10) { ct_y.Add(con); } connections.Add(con); } ITunnelOverlap sto = new SimpleTunnelOverlap(); ITunnelOverlap nto = new NCTunnelOverlap(ncservice); ConnectionType con_type = ConnectionType.Structured; List<Connection> pre_cons = new List<Connection>(); pre_cons.Add(connections[9]); IDictionary id = nto.GetSyncMessage(pre_cons, addr_x, ct_x.GetConnections(con_type)); // We do have some pre-existing overlap Assert.AreEqual(nto.EvaluateOverlap(ct_y.GetConnections(con_type), id)[0], connections[9], "NC: Have an overlap!"); Assert.AreEqual(sto.EvaluateOverlap(ct_y.GetConnections(con_type), id)[0], connections[9], "Simple: Have an overlap!"); // We have no overlap with an empty connection table Assert.AreEqual(nto.EvaluateOverlap(ct_empty.GetConnections(con_type), id).Count, 0, "No overlap!"); Assert.AreEqual(sto.EvaluateOverlap(ct_empty.GetConnections(con_type), id).Count, 0, "No overlap!"); // latency[0] == -1 Assert.AreEqual(connections[1].Address.Equals(nto.EvaluatePotentialOverlap(id)), true, "NC: EvaluatePotentialOverlap returns expected!"); Assert.AreEqual(ct_x.Contains(con_type, sto.EvaluatePotentialOverlap(id)), true, "Simple: EvaluatePotentialOverlap returns valid!"); ct_y.Add(fast_con); ct_x.Add(fast_con); id = nto.GetSyncMessage(pre_cons, addr_x, ct_x.GetConnections(con_type)); Assert.AreEqual(fast_con.Address.Equals(nto.EvaluatePotentialOverlap(id)), true, "NC: EvaluatePotentialOverlap returns expected!"); Assert.AreEqual(nto.EvaluateOverlap(ct_y.GetConnections(con_type), id)[0], fast_con, "NC: Have better overlap!"); }
public void Test() { Address addr_x = new AHAddress(new RNGCryptoServiceProvider()); byte[] addrbuff = Address.ConvertToAddressBuffer(addr_x.ToBigInteger() + (Address.Full / 2)); Address.SetClass(addrbuff, AHAddress._class); Address addr_y = new AHAddress(addrbuff); List <Connection> connections = new List <Connection>(); ConnectionTable ct_x = new ConnectionTable(); ConnectionTable ct_y = new ConnectionTable(); ConnectionTable ct_empty = new ConnectionTable(); NCService ncservice = new NCService(); Connection fast_con = null; for (int i = 1; i <= 11; i++) { addrbuff = Address.ConvertToAddressBuffer(addr_x.ToBigInteger() + (i * Address.Full / 16)); Address.SetClass(addrbuff, AHAddress._class); Address addr = new AHAddress(addrbuff); Connection con = null; TransportAddress ta = TransportAddressFactory.CreateInstance("brunet.tcp://158.7.0.1:5000"); Edge fe = new FakeEdge(ta, ta, TransportAddress.TAType.Tcp); if (i <= 10) { con = new Connection(fe, addr, "structured", null, null); ct_x.Add(con); if (i % 2 == 0) { ncservice.ProcessSample(DateTime.UtcNow, String.Empty, addr, new Point(new double[] { 0, 0 }, 0), 0, i * 10); } } else { fast_con = new Connection(fe, addr, "structured", null, null); ncservice.ProcessSample(DateTime.UtcNow, String.Empty, addr, new Point(new double[] { 0, 0 }, 0), 0, 5); } if (i == 10) { ct_y.Add(con); } connections.Add(con); } IRelayOverlap sto = new SimpleRelayOverlap(); IRelayOverlap nto = new NCRelayOverlap(ncservice); ConnectionType con_type = ConnectionType.Structured; List <Connection> pre_cons = new List <Connection>(); pre_cons.Add(connections[9]); IDictionary id = nto.GetSyncMessage(pre_cons, addr_x, ct_x.GetConnections(con_type)); // We do have some pre-existing overlap Assert.AreEqual(nto.EvaluateOverlap(ct_y.GetConnections(con_type), id)[0], connections[9], "NC: Have an overlap!"); Assert.AreEqual(sto.EvaluateOverlap(ct_y.GetConnections(con_type), id)[0], connections[9], "Simple: Have an overlap!"); // We have no overlap with an empty connection table Assert.AreEqual(nto.EvaluateOverlap(ct_empty.GetConnections(con_type), id).Count, 0, "No overlap!"); Assert.AreEqual(sto.EvaluateOverlap(ct_empty.GetConnections(con_type), id).Count, 0, "No overlap!"); // latency[0] == -1 Assert.AreEqual(connections[1].Address.Equals(nto.EvaluatePotentialOverlap(id)), true, "NC: EvaluatePotentialOverlap returns expected!"); Assert.AreEqual(ct_x.Contains(con_type, sto.EvaluatePotentialOverlap(id)), true, "Simple: EvaluatePotentialOverlap returns valid!"); ct_y.Add(fast_con); ct_x.Add(fast_con); id = nto.GetSyncMessage(pre_cons, addr_x, ct_x.GetConnections(con_type)); Assert.AreEqual(fast_con.Address.Equals(nto.EvaluatePotentialOverlap(id)), true, "NC: EvaluatePotentialOverlap returns expected!"); Assert.AreEqual(nto.EvaluateOverlap(ct_y.GetConnections(con_type), id)[0], fast_con, "NC: Have better overlap!"); }