/* * 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); } }
/** * Implements the EdgeListener function to * create edges of this type. */ public override void CreateEdgeTo(TransportAddress ta, EdgeCreationCallback ecb) { Edge e = null; try { if( !IsStarted ) { throw new EdgeException("UdpEdgeListener is not started"); } else if( ta.TransportAddressType != this.TAType ) { throw new EdgeException(ta.TransportAddressType.ToString() + " is not my type: " + this.TAType.ToString() ); } 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 { 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 )); /* Tell me when you close so I can clean up the table */ e.CloseEvent += this.CloseHandler; ecb(true, e, null); } } catch(Exception ex) { if( e != null ) { //Clean up the edge CloseHandler(e, null); } ecb(false, null, ex); } }
/* ////////////////////////////// * * 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); }