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

jadex.micro.testcases.longcall.ProviderAgent Maven / Gradle / Ivy

Go to download

The Jadex micro applications package contains several example applications, benchmarks and testcases using micro agents.

There is a newer version: 4.0.267
Show newest version
package jadex.micro.testcases.longcall;

import jadex.bridge.IComponentStep;
import jadex.bridge.IInternalAccess;
import jadex.bridge.SFuture;
import jadex.bridge.ServiceCall;
import jadex.bridge.component.IExecutionFeature;
import jadex.bridge.service.annotation.Service;
import jadex.commons.ICommand;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;
import jadex.commons.future.IIntermediateFuture;
import jadex.commons.future.IPullIntermediateFuture;
import jadex.commons.future.IPullSubscriptionIntermediateFuture;
import jadex.commons.future.ISubscriptionIntermediateFuture;
import jadex.commons.future.ITerminableFuture;
import jadex.commons.future.IntermediateFuture;
import jadex.commons.future.PullIntermediateFuture;
import jadex.commons.future.PullSubscriptionIntermediateFuture;
import jadex.commons.future.SubscriptionIntermediateFuture;
import jadex.commons.future.TerminableFuture;
import jadex.micro.annotation.Agent;
import jadex.micro.annotation.Implementation;
import jadex.micro.annotation.ProvidedService;
import jadex.micro.annotation.ProvidedServices;


/**
 * 
 */
@Agent
@ProvidedServices(@ProvidedService(type=ITestService.class, implementation=@Implementation(expression="$pojoagent")))
@Service
public class ProviderAgent implements ITestService
{
	@Agent
	protected IInternalAccess agent;
	
	/**
	 *  Call a method that must use a secure
	 *  transport under the hood.
	 */
	public IFuture method1()
	{
//		final Future ret = new Future();
		final Future ret = (Future)SFuture.getNoTimeoutFuture(agent);
//		System.out.println("Called method1");
		doCall(ret);
		return ret;
	}
	
	/**
	 *  A test method.
	 */
	public ITerminableFuture method2()
	{
		TerminableFuture ret = (TerminableFuture)SFuture.getNoTimeoutFuture(TerminableFuture.class, agent);
//		System.out.println("Called tmethod2");
		doCall(ret);
		return ret;
	}
	
	/**
	 *  A test method.
	 */
	public IIntermediateFuture method3()
	{
		final IntermediateFuture ret = (IntermediateFuture)SFuture.getNoTimeoutFuture(IntermediateFuture.class, agent);
//		System.out.println("Called imethod3");
		doCall(ret);
		return ret;
	}
	
	/**
	 *  A test method.
	 */
	public ISubscriptionIntermediateFuture method4()
	{
		final SubscriptionIntermediateFuture ret = (SubscriptionIntermediateFuture)SFuture.getNoTimeoutFuture(SubscriptionIntermediateFuture.class, agent);
//		System.out.println("Called smethod4");
		doCall(ret);
		return ret;
	}
	
	/**
	 *  A test method.
	 */
	public IPullIntermediateFuture method5()
	{
		final PullIntermediateFuture ret = new PullIntermediateFuture((ICommand>)null);
		SFuture.avoidCallTimeouts(ret, agent);
//		System.out.println("Called pmethod5");
		doCall(ret);
		return ret;
	}
	
	/**
	 *  A test method.
	 */
	public IPullSubscriptionIntermediateFuture method6()
	{
		final PullSubscriptionIntermediateFuture ret = new PullSubscriptionIntermediateFuture((ICommand>)null);
		SFuture.avoidCallTimeouts(ret, agent);
//		System.out.println("Called psmethod6");
		doCall(ret);
		return ret;
	}

	/**
	 * 
	 */
	protected void doCall(final Future ret)
	{
//		SFuture.avoidCallTimeouts(ret, agent.getExternalAccess());
		
		ServiceCall sc = ServiceCall.getCurrentInvocation();
		long to = sc.getTimeout();

		System.out.println("Timeout is: " + to);
	
		final long wait = to>0? to*2: 0;
		final long startwait = System.currentTimeMillis();
//		System.out.println("waiting: "+wait+", "+System.currentTimeMillis());
		agent.getComponentFeature(IExecutionFeature.class).waitForDelay(wait, new IComponentStep()
		{
			public IFuture execute(IInternalAccess ia)
			{
				System.out.println("waited: "+ (System.currentTimeMillis() - startwait));
				ret.setResultIfUndone(null);
				return IFuture.DONE;
			}
		});
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy