FileDocCategorySizeDatePackage
NetworkGlueUDP.javaAPI DocAzureus 3.0.3.45549Tue Jul 18 04:51:50 BST 2006com.aelitis.azureus.core.networkmanager.impl.udp

NetworkGlueUDP

public class NetworkGlueUDP extends Object implements com.aelitis.net.udp.uc.PRUDPPrimordialHandler, NetworkGlue

Fields Summary
private static final org.gudy.azureus2.core3.logging.LogIDs
LOGID
private NetworkGlueListener
listener
private com.aelitis.net.udp.uc.PRUDPPacketHandler
handler
private LinkedList
msg_queue
private org.gudy.azureus2.core3.util.AESemaphore
msg_queue_sem
private org.gudy.azureus2.core3.util.AESemaphore
msg_queue_slot_sem
private long
total_packets_received
private long
total_bytes_received
private long
total_packets_sent
private long
total_bytes_sent
Constructors Summary
protected NetworkGlueUDP(NetworkGlueListener _listener)

	
	
	
				 
	
		listener	= _listener;
						
		COConfigurationManager.addAndFireParameterListeners(
			new String[]{ "UDP.Listen.Port", "UDP.Listen.Port.Enable" },
			new ParameterListener()
			{
				public void
				parameterChanged(
					String	name )
				{
					boolean	enabled = COConfigurationManager.getBooleanParameter( "UDP.Listen.Port.Enable" );
					
					if ( enabled ){
						
						int	port = COConfigurationManager.getIntParameter( "UDP.Listen.Port" );				
						
						if ( handler == null || port != handler.getPort()){
							
							if ( handler != null ){
								
								Logger.log(new LogEvent(LOGID, "Deactivating UDP listener on port " + handler.getPort()));

								handler.setPrimordialHandler( null );
							}

							Logger.log(new LogEvent(LOGID, "Activating UDP listener on port " + port ));
								
							handler = PRUDPPacketHandlerFactory.getHandler( port );
	
							handler.setPrimordialHandler( NetworkGlueUDP.this );
						}
					}else{
						
						if ( handler != null ){
							
							Logger.log(new LogEvent(LOGID, "Deactivating UDP listener on port " + handler.getPort()));
							
							handler.setPrimordialHandler( null );
						}
					}
				}
			});
				
		new AEThread( "NetworkGlueUDP", true )
		{
			public void
			runSupport()
			{
				while( true ){
					
			
					InetSocketAddress	target_address 	= null;
					byte[]				data			= null;
					
					msg_queue_sem.reserve();
						
					synchronized( msg_queue ){
												
						Object[]	entry = (Object[])msg_queue.removeFirst();
								
						target_address 	= (InetSocketAddress)entry[0];
						data			= (byte[])entry[1];
					}
					
					msg_queue_slot_sem.release();
					
					total_packets_sent++;
					total_bytes_sent	+= data.length;
					
					try{
						handler.primordialSend( data, target_address );
						
						Thread.sleep(3);
						
					}catch( Throwable e ){
						
						Debug.printStackTrace( e );
					}
				}
			}
		}.start();
	
Methods Summary
public long[]getStats()

		return( new long[]{ total_packets_sent, total_bytes_sent, total_packets_received, total_bytes_received });
	
public booleanpacketReceived(java.net.DatagramPacket packet)

		if ( packet.getLength() >= 12 ){
								
			byte[]	data = packet.getData();
			
				// first and third word must have something set in mask: 0xfffff800
			
			if ( 	(	( data[0] & 0xff ) != 0 ||
						( data[1] & 0xff ) != 0 ||
						( data[2] & 0xf8 ) != 0 ) &&
					
					(	( data[8] & 0xff ) != 0 ||
						( data[9] & 0xff ) != 0 ||
						( data[10]& 0xf8 ) != 0 )){
				
				total_packets_received++;
				total_bytes_received += packet.getLength();
				
				listener.receive( handler.getPort(), new InetSocketAddress( packet.getAddress(), packet.getPort()), packet.getData(), packet.getLength());
				
					// consume this packet 
				
				return( true );
			}
		}
		
			// don't consume it, allow it to be passed on for further processing
		
		return( false );
	
public intsend(int local_port, java.net.InetSocketAddress target, byte[] data)

	
		msg_queue_slot_sem.reserve();
		
		synchronized( msg_queue ){
			
			msg_queue.add( new Object[]{ target, data });
		}
		
		msg_queue_sem.release();
		
		return( data.length );