All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jadex.base.service.awareness.discovery.ipbroadcast.BroadcastReceiveHandler Maven / Gradle / Ivy

Go to download

The Jadex platform base package contains functionality useful for constructing platforms.

The newest version!
package jadex.base.service.awareness.discovery.ipbroadcast;

import jadex.base.service.awareness.discovery.MasterSlaveReceiveHandler;

import java.net.DatagramPacket;

/**
 *  Receiver handler for broadcast discovery.
 */
public class BroadcastReceiveHandler extends MasterSlaveReceiveHandler
{
	/** The receive buffer. */
	protected byte[] buffer;
	
	/**
	 *  Create a new receive handler.
	 */
	public BroadcastReceiveHandler(BroadcastDiscoveryAgent agent)
	{
		super(agent);
	}
	
	/**
	 *  Receive a packet.
	 */
	public Object[] receive()
	{
		Object[] ret = null;
		try
		{
			if(buffer==null)
			{
				// todo: max ip datagram length (is there a better way to determine length?)
				buffer = new byte[8192];
			}

			final DatagramPacket pack = new DatagramPacket(buffer, buffer.length);
			getAgent().getSocket().receive(pack);
			byte[] data = new byte[pack.getLength()];
			System.arraycopy(buffer, 0, data, 0, pack.getLength());
			ret = new Object[]{pack.getAddress(), new Integer(pack.getPort()), data};
//			System.out.println("received packet: "+pack.getAddress());
		}
		catch(Exception e)
		{
//			System.out.println("Message receival error: "+e);
			getAgent().getMicroAgent().getLogger().warning("Message receival error: "+e);
//			e.printStackTrace();
		}
		
		return ret;
	}
	
	/**
	 *  Get the agent.
	 */
	public BroadcastDiscoveryAgent getAgent()
	{
		return (BroadcastDiscoveryAgent)agent;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy