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

jadex.platform.service.awareness.discovery.MasterSlaveSendHandler Maven / Gradle / Ivy

Go to download

The Jadex platform package contains implementations of platform services as well as the platform component itself.

There is a newer version: 3.0.117
Show newest version
package jadex.platform.service.awareness.discovery;

import java.net.InetAddress;
import java.net.InetSocketAddress;

import jadex.bridge.service.types.awareness.AwarenessInfo;
import jadex.commons.future.IFuture;

/**
 *  Handle sending.
 */
public abstract class MasterSlaveSendHandler extends SendHandler
{
	/**
	 *  Create a new lease time handling object.
	 */
	public MasterSlaveSendHandler(DiscoveryAgent state)
	{
		super(state);
	}
	
	/**
	 *  Create the awareness info.
	 */
	public IFuture createAwarenessInfo()
	{
		return agent.createAwarenessInfo(AwarenessInfo.STATE_ONLINE, getAgent().createMasterId());
	}
	
	/**
	 *  Method to send messages.
	 */
	public void send(AwarenessInfo info)
	{
		try
		{
			byte[] data = DiscoveryAgent.encodeObject(info, getAgent().getDefaultCodecs(), getAgent().getMicroAgent().getClassLoader());
	
//			System.out.println("packet size: "+data.length);

			sendToDiscover(data);
			
			if(getAgent().isMaster())
			{
				sendToRemotes(data);
				
				// Send to all locals a refresh awareness
				sendToLocals(data);
			}
			else
			{
				sendToMaster(data);
			}
			
//			System.out.println("sent");
//			System.out.println(getComponentIdentifier()+" sent '"+info+"' ("+data.length+" bytes)");
		}
		catch(Exception e)
		{
			agent.getMicroAgent().getLogger().warning("Could not send awareness message: "+e);
//			e.printStackTrace();
		}	
	}
	
	/**
	 *  Send/forward to discover.
	 *  @param data The data to be send.
	 */
	public int sendToDiscover(byte[] data)
	{
		return sendToDiscover(data, -1);
	}
	
	/**
	 *  Send/forward to discover.
	 *  @param data The data to be send.
	 */
	public abstract int sendToDiscover(byte[] data, int maxsend);
	
	/**
	 *  Send awareness info to remote scanner services.
	 *  @param data The data to be send.
	 *  @param maxsend The maximum number of messages to send.
	 */
	public int sendToRemotes(byte[] data)
	{
		return sendToRemotes(data, -1);
	}
	
	/**
	 *  Send awareness info to remote scanner services.
	 *  @param data The data to be send.
	 *  @param maxsend The maximum number of messages to send.
	 */
	public int sendToRemotes(byte[] data, int maxsend)
	{
		int ret = 0;
		try
		{
			DiscoveryEntry[] rems = getAgent().getRemotes().getEntries();
			for(int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy