/* ////////////////////////////// * * Here are all the normal methods of TcpEdgeListener * * ////////////////////////////// */ public override void CreateEdgeTo(TransportAddress ta, EdgeCreationCallback ecb) { try { if (!IsStarted) { throw new EdgeException("TcpEdgeListener is not started"); } else if (ta.TransportAddressType != TransportAddress.TAType.Tcp) { throw new EdgeException(ta.TransportAddressType.ToString() + " is not my type: Tcp"); } else if (_ta_auth.Authorize(ta) == TAAuthorizer.Decision.Deny) { //Too bad. Can't make this edge: throw new EdgeException(ta.ToString() + " is not authorized"); } else { //Everything looks good: ArrayList tmp_ips = new ArrayList(); tmp_ips.Add(((IPTransportAddress)ta).GetIPAddress()); CreationState cs = new CreationState(ecb, new Queue(tmp_ips), ((IPTransportAddress)ta).Port, this); ActionQueue.Enqueue(cs); } } catch (Exception e) { ecb(false, null, e); } }
//adding some kind of factory methods public static TransportAddress CreateInstance(string s) { var ta_cache = Interlocked.Exchange <WeakValueTable <string, TransportAddress> >(ref _ta_cache, null); TransportAddress result = null; if (ta_cache != null) { try { result = ta_cache.GetValue(s); if (result == null) { result = NoCacheCreateInstance(s); string r_ts = result.ToString(); if (r_ts.Equals(s)) { //Keep the internal reference which is being saved already s = r_ts; } ta_cache.Replace(s, result); } } finally { Interlocked.Exchange <WeakValueTable <string, TransportAddress> >(ref _ta_cache, ta_cache); } } else { result = NoCacheCreateInstance(s); } return(result); }
/** * Implements the EdgeListener function to * create edges of this type. */ public override void CreateEdgeTo(TransportAddress ta, EdgeCreationCallback ecb) { Edge e = null; Exception ex = null; if (!IsStarted) { ex = new EdgeException("UdpEdgeListener is not started"); } else if (ta.TransportAddressType != this.TAType) { ex = new EdgeException(ta.TransportAddressType.ToString() + " is not my type: " + this.TAType.ToString()); } else if (_ta_auth.Authorize(ta) == TAAuthorizer.Decision.Deny) { ex = new EdgeException(ta.ToString() + " is not authorized"); } else { IPAddress first_ip = ((IPTransportAddress)ta).GetIPAddress(); IPEndPoint end = new IPEndPoint(first_ip, ((IPTransportAddress)ta).Port); /* We have to keep our mapping of end point to edges up to date */ lock ( _id_ht ) { //Get a random ID for this edge: int id; do { id = _rand.Next(); //Make sure we don't have negative ids if (id < 0) { id = ~id; } } while(_id_ht.Contains(id) || id == 0); e = new UdpEdge(this, false, end, _local_ep, id, 0); _id_ht[id] = e; } NatDataPoint dp = new NewEdgePoint(DateTime.UtcNow, e); Interlocked.Exchange <NatHistory>(ref _nat_hist, _nat_hist + dp); Interlocked.Exchange <IEnumerable>(ref _nat_tas, new NatTAs(_tas, _nat_hist)); try { /* Tell me when you close so I can clean up the table */ e.CloseEvent += this.CloseHandler; } catch (Exception x) { e = null; ex = x; } } if (e != null) { ecb(true, e, null); } else { ecb(false, null, ex); } }
//adding some kind of factory methods public static TransportAddress CreateInstance(string s) { Cache ta_cache = Interlocked.Exchange <Cache>(ref _ta_cache, null); TransportAddress result = null; if (ta_cache != null) { try { result = (TransportAddress)ta_cache[s]; if (result == null) { result = NoCacheCreateInstance(s); string r_ts = result.ToString(); if (r_ts.Equals(s)) { //Keep the internal reference which is being saved already s = r_ts; } ta_cache[s] = result; } } finally { Interlocked.Exchange <Cache>(ref _ta_cache, ta_cache); } } else { result = NoCacheCreateInstance(s); } return(result); }
/** return the base TransportAddress and the path associated with it */ public static TransportAddress SplitPath(TransportAddress ta, out string path) { string tas = ta.ToString(); // Need to be careful of the case ta:////ta:9/ int init_idx = tas.IndexOf("://") + 3; int idx = init_idx; int pos = 0; bool next = false; for (; idx < tas.Length; idx++) { if (tas[idx] == '/') { if (!next) { pos = idx; } } else { next = false; } } if (pos > 0) { path = tas.Substring(pos); return(TransportAddressFactory.CreateInstance(tas.Substring(0, pos))); } else { path = "/"; return(ta); } }
protected static TransportAddress CacheInstance(TransportAddress ta) { var ta_cache = Interlocked.Exchange <WeakValueTable <string, TransportAddress> >(ref _ta_cache, null); if (ta_cache != null) { try { var ta2 = ta_cache.GetValue(ta.ToString()); if (ta.Equals(ta2)) { ta = ta2; } else { ta_cache.Replace(ta.ToString(), ta); } } finally { Interlocked.Exchange <WeakValueTable <string, TransportAddress> >(ref _ta_cache, ta_cache); } } return(ta); }
public void Test() { TransportAddress tas = TransportAddressFactory.CreateInstance("b.s://234580"); Assert.AreEqual(tas.ToString(), "b.s://234580", "Simulation string"); Assert.AreEqual((tas as SimulationTransportAddress).ID, 234580, "Simulation id"); Assert.AreEqual(TransportAddress.TAType.S, tas.TransportAddressType, "Simulation ta type"); TransportAddress ta1 = TransportAddressFactory.CreateInstance("brunet.udp://10.5.144.69:5000"); Assert.AreEqual(ta1.ToString(), "brunet.udp://10.5.144.69:5000", "Testing TA parsing"); TransportAddress ta2 = TransportAddressFactory.CreateInstance("brunet.udp://10.5.144.69:5000"); Assert.AreEqual(ta1, ta2, "Testing TA Equals"); string StrLocalHost = Dns.GetHostName(); IPHostEntry IPEntry = Dns.GetHostEntry(StrLocalHost); TransportAddress local_ta = TransportAddressFactory.CreateInstance("brunet.udp://" + IPEntry.AddressList[0].ToString() + ":" + 5000); IEnumerable locals = TransportAddressFactory.CreateForLocalHost(TransportAddress.TAType.Udp, 5000); bool match = false; foreach (TransportAddress test_ta1 in locals) { //Console.WriteLine("test_ta: {0}", test_ta1); if (test_ta1.Equals(local_ta)) { match = true; } } Assert.AreEqual(match, true, "testing local TA matches"); //testing function TA TransportAddress func_ta = TransportAddressFactory.CreateInstance("brunet.function://localhost:3000"); TransportAddress func_ta2 = TransportAddressFactory.CreateInstance("brunet.function://localhost:3000"); Assert.AreEqual(func_ta, func_ta2, "equality of instances"); Assert.IsTrue(func_ta == func_ta2, "reference equality, test of caching"); Assert.AreEqual(func_ta.ToString(), "brunet.function://localhost:3000", "Testing function TA parsing"); }
/** return the base TransportAddress and the path associated with it */ public static TransportAddress SplitPath(TransportAddress ta, out string path) { string tas = ta.ToString(); // Need to be careful of the case ta:////ta:9/ int init_idx = tas.IndexOf("://") + 3; int idx = init_idx; int pos = 0; bool next = false; for(; idx < tas.Length; idx++) { if(tas[idx] == '/') { if(!next) { pos = idx; } } else { next = false; } } if(pos > 0) { path = tas.Substring(pos); return TransportAddressFactory.CreateInstance(tas.Substring(0, pos)); } else { path = "/"; return ta; } }
/** * Implements the EdgeListener function to * create edges of this type. */ public override void CreateEdgeTo(TransportAddress ta, EdgeCreationCallback ecb) { Edge e = null; Exception ex = null; if( !IsStarted ) { ex = new EdgeException("UdpEdgeListener is not started"); } else if( ta.TransportAddressType != this.TAType ) { ex = new EdgeException(ta.TransportAddressType.ToString() + " is not my type: " + this.TAType.ToString() ); } else if( _ta_auth.Authorize(ta) == TAAuthorizer.Decision.Deny ) { ex = new EdgeException( ta.ToString() + " is not authorized"); } else { IPAddress first_ip = ((IPTransportAddress) ta).GetIPAddress(); IPEndPoint end = new IPEndPoint(first_ip, ((IPTransportAddress) ta).Port); /* We have to keep our mapping of end point to edges up to date */ lock( _id_ht ) { //Get a random ID for this edge: int id; do { id = _rand.Next(); //Make sure we don't have negative ids if( id < 0 ) { id = ~id; } } while( _id_ht.Contains(id) || id == 0 ); e = new UdpEdge(this, false, end, _local_ep, id, 0); _id_ht[id] = e; } NatDataPoint dp = new NewEdgePoint(DateTime.UtcNow, e); Interlocked.Exchange<NatHistory>(ref _nat_hist, _nat_hist + dp); Interlocked.Exchange<IEnumerable>(ref _nat_tas, new NatTAs( _tas, _nat_hist )); try { /* Tell me when you close so I can clean up the table */ e.CloseEvent += this.CloseHandler; } catch (Exception x) { e = null; ex = x; } } if(e != null) { ecb(true, e, null); } else { ecb(false, null, ex); } }
/* * Implements the EdgeListener function to * create edges of this type. */ public override void CreateEdgeTo(TransportAddress ta, EdgeCreationCallback ecb) { if( !IsStarted ) { // it should return null and not throw an exception // for graceful disconnect and preventing others to // connect to us after we've disconnected. ecb(false, null, new EdgeException("Not started")); return; } if( ta.TransportAddressType != this.TAType ) { //Can't make an edge of this type ecb(false, null, new EdgeException("Can't make edge of this type")); return; } if( _ta_auth.Authorize(ta) == TAAuthorizer.Decision.Deny ) { //Not authorized. Can't make this edge: ecb(false, null, new EdgeException( ta.ToString() + " is not authorized") ); return; } int remote_id = ((SimulationTransportAddress) ta).ID; //Get the edgelistener: //Outbound edge: int delay = 0; if(_use_delay) { if(LatencyMap != null) { // id != 0, so we reduce all by 1 delay = LatencyMap[_listener_id][remote_id] / 1000; } else { delay = 100; } } SimulationEdge se_l = new SimulationEdge(this, _listener_id, remote_id, false, delay); AddEdge(se_l); SimulationEdgeListener remote = (SimulationEdgeListener) _listener_map[remote_id]; if( remote != null ) { // // Make sure that the remote listener does not deny // our TAs. // foreach (TransportAddress ta_local in LocalTAs) { if (remote.TAAuth.Authorize(ta_local) == TAAuthorizer.Decision.Deny ) { //Not authorized. Can't make this edge: ecb(false, null, new EdgeException( ta_local.ToString() + " is not authorized by remote node.") ); return; } } SimulationEdge se_r = new SimulationEdge(remote, remote_id, _listener_id, true, delay); remote.AddEdge(se_r); se_l.Partner = se_r; se_r.Partner = se_l; remote.SendEdgeEvent(se_r); } else { //There is no other edge, for now, we use "udp-like" //behavior of just making an edge that goes nowhere. } ecb(true, se_l, null); }
/* * Implements the EdgeListener function to * create edges of this type. */ public override void CreateEdgeTo(TransportAddress ta, EdgeCreationCallback ecb) { if( !IsStarted ) { // it should return null and not throw an exception // for graceful disconnect and preventing others to // connect to us after we've disconnected. ecb(false, null, new EdgeException("Not started")); return; } else if( ta.TransportAddressType != this.TAType ) { //Can't make an edge of this type ecb(false, null, new EdgeException("Can't make edge of this type")); return; } else if( _ta_auth.Authorize(ta) == TAAuthorizer.Decision.Deny ) { //Not authorized. Can't make this edge: ecb(false, null, new EdgeException( ta.ToString() + " is not authorized") ); return; } int remote_id = ((SimulationTransportAddress) ta).ID; int real_remote_id = (remote_id >= 0) ? remote_id : ~remote_id; //Outbound edge: int delay = 0; if(_use_delay) { if(LatencyMap != null) { int local = LocalID % LatencyMap.Count; int remote = real_remote_id % LatencyMap.Count; delay = LatencyMap[local][remote] / 1000; } else { delay = 100; } } SimulationEdge se_l = new SimulationEdge(this, LocalID, remote_id, false, delay, _ta_type); if(real_remote_id == remote_id) { CreateRemoteEdge(se_l); } ecb(true, se_l, null); }
/* * Implements the EdgeListener function to * create edges of this type. */ public override void CreateEdgeTo(TransportAddress ta, EdgeCreationCallback ecb) { if (!IsStarted) { // it should return null and not throw an exception // for graceful disconnect and preventing others to // connect to us after we've disconnected. ecb(false, null, new EdgeException("Not started")); return; } #if FUNCTION_DEBUG foreach (TransportAddress local_ta in LocalTAs) { Console.Error.WriteLine("Create edge local: {0} <-> remote {1}.", local_ta, ta); } #endif if (ta.TransportAddressType != this.TAType) { //Can't make an edge of this type #if FUNCTION_DEBUG Console.Error.WriteLine("Can't make edge of this type."); #endif ecb(false, null, new EdgeException("Can't make edge of this type")); return; } if (_ta_auth.Authorize(ta) == TAAuthorizer.Decision.Deny) { //Not authorized. Can't make this edge: #if FUNCTION_DEBUG Console.Error.WriteLine("Can't make edge. Remote TA {0} is not authorized locally.", ta); #endif ecb(false, null, new EdgeException(ta.ToString() + " is not authorized")); return; } int remote_id = ((IPTransportAddress)ta).Port; //Get the edgelistener: //Outbound edge: FunctionEdge fe_l = new FunctionEdge(this, _listener_id, remote_id, false); lock ( _listener_map ) { FunctionEdgeListener remote = (FunctionEdgeListener)_listener_map[remote_id]; if (remote != null) { // // Make sure that the remote listener does not deny // our TAs. // foreach (TransportAddress ta_local in LocalTAs) { if (remote.TAAuth.Authorize(ta_local) == TAAuthorizer.Decision.Deny) { //Not authorized. Can't make this edge: #if FUNCTION_DEBUG Console.Error.WriteLine("Can't make edge. local TA {0} is not authorized remotely by {1}.", ta_local, ta); #endif ecb(false, null, new EdgeException(ta_local.ToString() + " is not authorized by remote node.")); return; } } FunctionEdge fe_r = new FunctionEdge(remote, remote_id, _listener_id, true); fe_l.Partner = fe_r; fe_r.Partner = fe_l; remote.SendEdgeEvent(fe_r); } else { //There is no other edge, for now, we use "udp-like" //behavior of just making an edge that goes nowhere. } ecb(true, fe_l, null); } }
protected static TransportAddress CacheInstance(TransportAddress ta) { var ta_cache = Interlocked.Exchange<WeakValueTable<string, TransportAddress>>(ref _ta_cache, null); if( ta_cache != null ) { try { var ta2 = ta_cache.GetValue(ta.ToString()); if(ta.Equals(ta2)) { ta = ta2; } else { ta_cache.Replace(ta.ToString(), ta); } } finally { Interlocked.Exchange<WeakValueTable<string, TransportAddress>>(ref _ta_cache, ta_cache); } } return ta; }
public void Test() { TransportAddress tas = TransportAddressFactory.CreateInstance("b.s://234580"); Assert.AreEqual(tas.ToString(), "b.s://234580", "Simulation string"); Assert.AreEqual((tas as SimulationTransportAddress).ID, 234580, "Simulation id"); Assert.AreEqual(TransportAddress.TAType.S, tas.TransportAddressType, "Simulation ta type"); TransportAddress ta1 = TransportAddressFactory.CreateInstance("brunet.udp://10.5.144.69:5000"); Assert.AreEqual(ta1.ToString(), "brunet.udp://10.5.144.69:5000", "Testing TA parsing"); TransportAddress ta2 = TransportAddressFactory.CreateInstance("brunet.udp://10.5.144.69:5000"); Assert.AreEqual(ta1, ta2, "Testing TA Equals"); string ta_string = "brunet.tunnel://UBU72YLHU5C3SY7JMYMJRTKK4D5BGW22/FE4QWASN+FE4QWASM"; TransportAddress ta = TransportAddressFactory.CreateInstance("brunet.tunnel://UBU72YLHU5C3SY7JMYMJRTKK4D5BGW22/FE4QWASN+FE4QWASM"); Assert.AreEqual(ta.ToString(), ta_string, "testing tunnel TA parsing"); //Console.WriteLine(ta); TunnelTransportAddress tun_ta = (TunnelTransportAddress)TransportAddressFactory.CreateInstance("brunet.tunnel://OIHZCNNUAXTLLARQIOBNCUWXYNAS62LO/CADSL6GV+CADSL6GU"); ArrayList fwd = new ArrayList(); fwd.Add(new AHAddress(Base32.Decode("CADSL6GVVBM6V442CETP4JTEAWACLC5A"))); fwd.Add(new AHAddress(Base32.Decode("CADSL6GUVBM6V442CETP4JTEAWACLC5A"))); TunnelTransportAddress test_ta = new TunnelTransportAddress(tun_ta.Target, fwd); Assert.AreEqual(tun_ta, test_ta, "testing tunnel TA compression enhancements"); //Console.WriteLine(tun_ta.ToString()); //Console.WriteLine(test_ta.ToString()); Assert.AreEqual(tun_ta.ToString(), test_ta.ToString(), "testing tunnel TA compression enhancements (toString)"); Assert.AreEqual(tun_ta.ContainsForwarder(new AHAddress(Base32.Decode("CADSL6GVVBM6V442CETP4JTEAWACLC5A"))), true, "testing tunnel TA contains forwarder (1)"); Assert.AreEqual(tun_ta.ContainsForwarder(new AHAddress(Base32.Decode("CADSL6GUVBM6V442CETP4JTEAWACLC5A"))), true, "testing tunnel TA contains forwarder (2)"); string StrLocalHost = Dns.GetHostName(); IPHostEntry IPEntry = Dns.GetHostEntry(StrLocalHost); TransportAddress local_ta = TransportAddressFactory.CreateInstance("brunet.udp://" + IPEntry.AddressList[0].ToString() + ":" + 5000); IEnumerable locals = TransportAddressFactory.CreateForLocalHost(TransportAddress.TAType.Udp, 5000); bool match = false; foreach (TransportAddress test_ta1 in locals) { //Console.WriteLine("test_ta: {0}", test_ta1); if (test_ta1.Equals(local_ta)) { match = true; } } Assert.AreEqual(match, true, "testing local TA matches"); //testing function TA TransportAddress func_ta = TransportAddressFactory.CreateInstance("brunet.function://localhost:3000"); TransportAddress func_ta2 = TransportAddressFactory.CreateInstance("brunet.function://localhost:3000"); Assert.AreEqual(func_ta, func_ta2, "equality of instances"); Assert.IsTrue(func_ta == func_ta2, "reference equality, test of caching"); Assert.AreEqual(func_ta.ToString(), "brunet.function://localhost:3000", "Testing function TA parsing"); }
/* ////////////////////////////// * * Here are all the normal methods of TcpEdgeListener * * ////////////////////////////// */ public override void CreateEdgeTo(TransportAddress ta, EdgeCreationCallback ecb) { try { if( !IsStarted ) { throw new EdgeException("TcpEdgeListener is not started"); } else if( ta.TransportAddressType != TransportAddress.TAType.Tcp ) { throw new EdgeException(ta.TransportAddressType.ToString() + " is not my type: Tcp"); } else if( _ta_auth.Authorize(ta) == TAAuthorizer.Decision.Deny ) { //Too bad. Can't make this edge: throw new EdgeException( ta.ToString() + " is not authorized"); } else { //Everything looks good: ArrayList tmp_ips = new ArrayList(); tmp_ips.Add(((IPTransportAddress)ta).GetIPAddress()); CreationState cs = new CreationState(ecb, new Queue( tmp_ips ), ((IPTransportAddress) ta).Port, this); ActionQueue.Enqueue(cs); } } catch(Exception e) { ecb(false, null, e); } }
/* * Implements the EdgeListener function to * create edges of this type. */ public override void CreateEdgeTo(TransportAddress ta, EdgeCreationCallback ecb) { if (!IsStarted) { // it should return null and not throw an exception // for graceful disconnect and preventing others to // connect to us after we've disconnected. ecb(false, null, new EdgeException("Not started")); return; } if (ta.TransportAddressType != this.TAType) { //Can't make an edge of this type ecb(false, null, new EdgeException("Can't make edge of this type")); return; } if (_ta_auth.Authorize(ta) == TAAuthorizer.Decision.Deny) { //Not authorized. Can't make this edge: ecb(false, null, new EdgeException(ta.ToString() + " is not authorized")); return; } int remote_id = ((SimulationTransportAddress)ta).ID; //Get the edgelistener: //Outbound edge: int delay = 0; if (_use_delay) { if (LatencyMap != null) { // id != 0, so we reduce all by 1 delay = LatencyMap[_listener_id][remote_id] / 1000; } else { lock (_sync) { delay = _rand.Next(10, 250); } } } SimulationEdge se_l = new SimulationEdge(this, _listener_id, remote_id, false, delay); AddEdge(se_l); SimulationEdgeListener remote = (SimulationEdgeListener)_listener_map[remote_id]; if (remote != null) { // // Make sure that the remote listener does not deny // our TAs. // foreach (TransportAddress ta_local in LocalTAs) { if (remote.TAAuth.Authorize(ta_local) == TAAuthorizer.Decision.Deny) { //Not authorized. Can't make this edge: ecb(false, null, new EdgeException(ta_local.ToString() + " is not authorized by remote node.")); return; } } SimulationEdge se_r = new SimulationEdge(remote, remote_id, _listener_id, true, delay); remote.AddEdge(se_r); se_l.Partner = se_r; se_r.Partner = se_l; remote.SendEdgeEvent(se_r); } else { //There is no other edge, for now, we use "udp-like" //behavior of just making an edge that goes nowhere. } ecb(true, se_l, null); }