jadex.micro.testcases.nflatency.InitiatorAgent Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jadex-applications-micro Show documentation
Show all versions of jadex-applications-micro Show documentation
The Jadex micro applications package contains several example applications, benchmarks and testcases using micro agents.
package jadex.micro.testcases.nflatency;
import java.util.Collection;
import java.util.Map;
import jadex.base.test.TestReport;
import jadex.base.test.Testcase;
import jadex.bridge.ComponentIdentifier;
import jadex.bridge.IComponentIdentifier;
import jadex.bridge.IExternalAccess;
import jadex.bridge.ITransportComponentIdentifier;
import jadex.bridge.component.IExecutionFeature;
import jadex.bridge.nonfunctional.SNFPropertyProvider;
import jadex.bridge.nonfunctional.annotation.NFRProperty;
import jadex.bridge.sensor.service.LatencyProperty;
import jadex.bridge.service.IService;
import jadex.bridge.service.RequiredServiceInfo;
import jadex.bridge.service.component.IRequiredServicesFeature;
import jadex.bridge.service.types.cms.CreationInfo;
import jadex.bridge.service.types.cms.IComponentManagementService;
import jadex.commons.MethodInfo;
import jadex.commons.SUtil;
import jadex.commons.Tuple2;
import jadex.commons.future.DefaultTuple2ResultListener;
import jadex.commons.future.DelegationResultListener;
import jadex.commons.future.ExceptionDelegationResultListener;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;
import jadex.commons.future.IFutureCommandResultListener;
import jadex.commons.future.IIntermediateFuture;
import jadex.commons.future.IIntermediateResultListener;
import jadex.commons.future.IResultListener;
import jadex.commons.future.TupleResult;
import jadex.micro.annotation.Agent;
import jadex.micro.annotation.Binding;
import jadex.micro.annotation.RequiredService;
import jadex.micro.annotation.RequiredServices;
import jadex.micro.testcases.TestAgent;
/**
* Tests if non-functional properties can be used on required services.
* Declares latency on a method of a service and fetches it.
*/
@Agent
@RequiredServices(
{
@RequiredService(name="cms", type=IComponentManagementService.class, binding=@Binding(scope=RequiredServiceInfo.SCOPE_PLATFORM)),
@RequiredService(name="ts", type=ITestService.class, binding=@Binding(scope=RequiredServiceInfo.SCOPE_GLOBAL)),
@RequiredService(name="aser", type=ITestService.class, multiple=true,
binding=@Binding(scope=RequiredServiceInfo.SCOPE_GLOBAL, dynamic=true),
nfprops=@NFRProperty(value=LatencyProperty.class, methodname="methodA", methodparametertypes=long.class))
})
public class InitiatorAgent extends TestAgent
{
/**
* Perform the tests.
*/
protected IFuture performTests(final Testcase tc)
{
final Future ret = new Future();
testLocal(1).addResultListener(agent.getComponentFeature(IExecutionFeature.class).createResultListener(new ExceptionDelegationResultListener(ret)
{
public void customResultAvailable(TestReport result)
{
tc.addReport(result);
testRemote(2).addResultListener(agent.getComponentFeature(IExecutionFeature.class).createResultListener(new ExceptionDelegationResultListener(ret)
{
public void customResultAvailable(TestReport result)
{
tc.addReport(result);
ret.setResult(null);
}
}));
}
}));
return ret;
}
/**
* Test local.
*/
protected IFuture testLocal(final int testno)
{
final Future ret = new Future();
performTest(agent.getComponentIdentifier().getRoot(), testno, true)
.addResultListener(agent.getComponentFeature(IExecutionFeature.class).createResultListener(new DelegationResultListener(ret)
{
public void customResultAvailable(final TestReport result)
{
ret.setResult(result);
}
}));
return ret;
}
/**
* Test remote.
*/
protected IFuture testRemote(final int testno)
{
final Future ret = new Future();
createPlatform(null).addResultListener(agent.getComponentFeature(IExecutionFeature.class).createResultListener(
new ExceptionDelegationResultListener(ret)
{
public void customResultAvailable(final IExternalAccess platform)
{
// Hack: announce platform immediately
ComponentIdentifier.getTransportIdentifier(platform).addResultListener(new ExceptionDelegationResultListener(ret)
{
public void customResultAvailable(final ITransportComponentIdentifier result)
{
IFuture fut = agent.getComponentFeature(IRequiredServicesFeature.class).getRequiredService("cms");
fut.addResultListener(new ExceptionDelegationResultListener(ret)
{
public void customResultAvailable(final IComponentManagementService cms)
{
CreationInfo ci = new CreationInfo(SUtil.createHashMap(new String[]{"component"}, new Object[]{result.getRoot()}));
cms.createComponent("jadex.platform.service.remote.ProxyAgent.class", ci).addResultListener(
new Tuple2Listener>()
// new DefaultTuple2ResultListener>()
{
public void firstResultAvailable(IComponentIdentifier result)
{
performTest(result, testno, false)
.addResultListener(agent.getComponentFeature(IExecutionFeature.class).createResultListener(new DelegationResultListener(ret)));
}
public void secondResultAvailable(Map result)
{
System.out.println("sec");
}
public void exceptionOccurred(Exception exception)
{
ret.setExceptionIfUndone(exception);
}
});
}
});
}
});
}
}));
return ret;
}
/**
* Perform the test. Consists of the following steps:
* Create provider agent
* Call methods on it
*/
protected IFuture performTest(final IComponentIdentifier root, final int testno, final boolean hassectrans)
{
final Future ret = new Future();
final Future res = new Future();
ret.addResultListener(new DelegationResultListener(res)
{
public void exceptionOccurred(Exception exception)
{
TestReport tr = new TestReport("#"+testno, "Tests if nflatency works.");
tr.setFailed(exception);
super.resultAvailable(tr);
}
});
final Future>> resfut = new Future>>();
IResultListener>> reslis = new DelegationResultListener>>(resfut);
createComponent(ProviderAgent.class.getName()+".class", root, reslis)
.addResultListener(new ExceptionDelegationResultListener(ret)
{
public void customResultAvailable(final IComponentIdentifier cid)
{
callService(cid, testno, 5000).addResultListener(new DelegationResultListener(ret));
}
public void exceptionOccurred(Exception exception)
{
exception.printStackTrace();
super.exceptionOccurred(exception);
}
});
return res;
}
/**
* Call the service methods.
*/
protected IFuture callService(final IComponentIdentifier cid, int testno, final long to)
{
final Future ret = new Future();
final TestReport tr = new TestReport("#"+testno, "Test if returning changed nf props works");
// IFuture fut = agent.getServiceContainer().getService(ITestService.class, cid);
// Add awarenessinfo for remote platform
// IAwarenessManagementService awa = SServiceProvider.getService(agent.getServiceProvider(), IAwarenessManagementService.class, RequiredServiceInfo.SCOPE_PLATFORM).get();
// AwarenessInfo info = new AwarenessInfo(cid.getRoot(), AwarenessInfo.STATE_ONLINE, -1,
// null, null, null, SReflect.getInnerClassName(this.getClass()));
// awa.addAwarenessInfo(info).get();
IIntermediateFuture fut = agent.getComponentFeature(IRequiredServicesFeature.class).getRequiredServices("aser");
fut.addResultListener(new IIntermediateResultListener()
{
boolean called;
public void intermediateResultAvailable(ITestService result)
{
if(cid.equals(((IService)result).getServiceIdentifier().getProviderId()))
{
called = true;
callService(result);
}
}
public void finished()
{
if(!called)
{
tr.setFailed("Service not found");
ret.setResult(tr);
}
}
public void resultAvailable(Collection result)
{
for(ITestService ts: result)
{
intermediateResultAvailable(ts);
}
finished();
}
public void exceptionOccurred(Exception exception)
{
ret.setException(exception);
}
protected void callService(final ITestService ts)
{
ts.methodA(100).addResultListener(new IFutureCommandResultListener()
// ts.methodA(100).addResultListener(new IResultListener()
{
public void resultAvailable(Void result)
{
try
{
MethodInfo mi = new MethodInfo(ITestService.class.getMethod("methodA", new Class[]{long.class}));
System.out.println("service: "+ts);
Long lat = (Long)SNFPropertyProvider.getRequiredMethodNFPropertyValue(agent.getExternalAccess(), ((IService)ts).getServiceIdentifier(), mi, LatencyProperty.NAME).get();
// INFMixedPropertyProvider pp = ((INFRPropertyProvider)ts).getRequiredServicePropertyProvider().get();
// Long lat = (Long)pp.getMethodNFPropertyValue(mi, LatencyProperty.NAME).get();
System.out.println("latency: "+lat);
// Test is ok if latency could be fetched.
// todo? Could also test if local latency is faster than remote
tr.setSucceeded(true);
ret.setResult(tr);
}
catch(Exception e)
{
e.printStackTrace();
ret.setExceptionIfUndone(e);
}
}
public void exceptionOccurred(Exception exception)
{
tr.setFailed("Failed with exception: "+exception);
ret.setResult(tr);
}
public void commandAvailable(Object command)
{
}
});
}
});
return ret;
}
/**
* Hack class that avoids printouts of forward command
*/
abstract class Tuple2Listener extends DefaultTuple2ResultListener implements IFutureCommandResultListener>
{
public void commandAvailable(Object command)
{
// nop, avoids printouts
}
}
}