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

jadex.platform.service.awareness.discovery.SendHandler 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.util.Timer;

import jadex.bridge.IComponentStep;
import jadex.bridge.IInternalAccess;
import jadex.bridge.component.IExecutionFeature;
import jadex.bridge.service.types.awareness.AwarenessInfo;
import jadex.commons.SUtil;
import jadex.commons.future.ExceptionDelegationResultListener;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;
import jadex.commons.transformation.annotations.Classname;

/**
 *  Automatically reinvokes send method in intervals
 *  determined by the delay (in state).
 *  
 *  Subclasses should override send to perform
 *  specific actions.
 */
public abstract class SendHandler
{
	//-------- attributes --------
	
	/** The agent. */
	protected DiscoveryAgent agent;

	/** The timer. */
	protected Timer	timer;

	/** The current send id. */
	protected String sendid;
	
	//-------- constructors --------
	
	/**
	 *  Create a new lease time handling object.
	 */
	public SendHandler(DiscoveryAgent agent)
	{
		this.agent = agent;
//		startSendBehavior();
	}
	
	//-------- methods --------
	
	/**
	 *  Start sending awareness infos.
	 *  (Ends automatically when a new send behaviour is started).
	 */
	public void startSendBehavior()
	{
		final String sendid = SUtil.createUniqueId(agent.getMicroAgent().getComponentIdentifier().getLocalName());
		this.sendid = sendid;	
		
		agent.getMicroAgent().getComponentFeature(IExecutionFeature.class).scheduleStep(new IComponentStep()
		{
			@Classname("send")
			public IFuture execute(IInternalAccess ia)
			{
				final IComponentStep step = this;
				final Future ret = new Future();
				if(!agent.isKilled() && sendid.equals(getSendId()))
				{
					createAwarenessInfo().addResultListener(agent.getMicroAgent().getComponentFeature(IExecutionFeature.class)
						.createResultListener(new ExceptionDelegationResultListener(ret)
					{
						public void customResultAvailable(AwarenessInfo info)
						{
							send(info);
							if(agent.getDelay()>0)
								agent.doWaitFor(agent.getDelay(), step);
							ret.setResult(null);
						}
					}));
					
				}
				return ret;
			}
		});
	}
	
	/**
	 *  Get the sendid.
	 *  @return the sendid.
	 */
	public String getSendId()
	{
		return sendid;
	}

	/**
	 *  Set the sendid.
	 *  @param sendid The sendid to set.
	 */
	public void setSendId(String sendid)
	{
		this.sendid = sendid;
	}
	
	/**
	 *  Create the awareness info.
	 */
	public IFuture createAwarenessInfo()
	{
		return agent.createAwarenessInfo();
	}
	
	/**
	 *  Method to send messages.
	 */
	public abstract void send(AwarenessInfo info);

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy