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

jadex.micro.testcases.stream.InitiatorAgent 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.stream;

import java.io.InputStream;
import java.util.Collection;

import jadex.base.test.TestReport;
import jadex.base.test.Testcase;
import jadex.bridge.ComponentIdentifier;
import jadex.bridge.IComponentIdentifier;
import jadex.bridge.IComponentStep;
import jadex.bridge.IExternalAccess;
import jadex.bridge.IInternalAccess;
import jadex.bridge.IOutputConnection;
import jadex.bridge.ITransportComponentIdentifier;
import jadex.bridge.component.IArgumentsResultsFeature;
import jadex.bridge.component.IExecutionFeature;
import jadex.bridge.service.component.IRequiredServicesFeature;
import jadex.bridge.service.types.message.IMessageService;
import jadex.commons.SReflect;
import jadex.commons.SUtil;
import jadex.commons.Tuple2;
import jadex.commons.future.DelegationResultListener;
import jadex.commons.future.ExceptionDelegationResultListener;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;
import jadex.commons.future.IResultListener;
import jadex.micro.annotation.Agent;
import jadex.micro.annotation.Argument;
import jadex.micro.annotation.Arguments;
import jadex.micro.testcases.TestAgent;

/**
 *  Agent that provides a service with a stream.
 */
@Agent
@Arguments(replace=false, value=@Argument(name="filename", clazz=String.class, defaultvalue="\"jadex/micro/testcases/stream/test.jpg\""))
//@Arguments(@Argument(name="filename", clazz=String.class, defaultvalue="\"jadex/micro/testcases/stream/android-sdk_r07-windows.zip\""))
public class InitiatorAgent extends TestAgent
{
	/**
	 * 
	 */
	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);
				if(SReflect.isAndroid()) 
				{
					tc.setTestCount(1);
					ret.setResult(null);
				} 
				else 
				{
					testRemote(2).addResultListener(agent.getComponentFeature(IExecutionFeature.class).createResultListener(new ExceptionDelegationResultListener(ret)
					{
						public void customResultAvailable(TestReport result)
						{
							tc.addReport(result);
							ret.setResult(null);
						}
					}));
				}
			}
		}));
		
		return ret;
	}
	
	/**
	 * 
	 */
	protected IFuture testLocal(int testno)
	{
		return performTest(agent.getComponentIdentifier().getRoot(), testno);
	}
	
	/**
	 * 
	 */
	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)
			{
				ComponentIdentifier.getTransportIdentifier(platform).addResultListener(new ExceptionDelegationResultListener(ret)
                {
                    public void customResultAvailable(ITransportComponentIdentifier result)
                    { 
                    	performTest(result, testno)
                    		.addResultListener(agent.getComponentFeature(IExecutionFeature.class).createResultListener(new DelegationResultListener(ret)));
                    }
                });
			}
		}));
		
		return ret;
	}
	
	/**
	 *  Perform the test. Consists of the following steps:
	 *  - start a receiver agent
	 *  - create connection
	 */
	protected IFuture performTest(final IComponentIdentifier root, final int testno)
	{
		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 streams work");
				tr.setFailed(exception);
				super.resultAvailable(tr);
			}
		});
		
		final Future>> resfut = new Future>>();
		IResultListener>> reslis = new DelegationResultListener>>(resfut);
		
		createComponent("jadex/micro/testcases/stream/ReceiverAgent.class", root, reslis)
			.addResultListener(new ExceptionDelegationResultListener(ret)
		{
			public void customResultAvailable(final IComponentIdentifier cid) 
			{
				IFuture msfut = agent.getComponentFeature(IRequiredServicesFeature.class).getRequiredService("msgservice");
				msfut.addResultListener(new ExceptionDelegationResultListener(ret)
				{
					public void customResultAvailable(IMessageService ms)
					{
						ms.createOutputConnection(agent.getComponentIdentifier(), cid, null)
							.addResultListener(new ExceptionDelegationResultListener(ret)
						{
							public void customResultAvailable(final IOutputConnection ocon) 
							{
								sendBehavior(testno, ocon, resfut).addResultListener(new DelegationResultListener(ret));
							}
						});
					}
				});
			}
		});
		
		return res;
	}
	
	/**
	 * 
	 */
	protected IFuture sendBehavior(int testno, final IOutputConnection con, IFuture>> resfut)
	{
		final long start = System.currentTimeMillis();
		final long[] filesize = new long[1];

		final Future ret = new Future();
		
		try
		{
			final InputStream is = SUtil.getResource((String)agent.getComponentFeature(IArgumentsResultsFeature.class).getArguments().get("filename"), agent.getClassLoader());
			
			final TestReport tr = new TestReport(""+testno, "Test if file is transferred correctly.");
			
			resfut.addResultListener(new IResultListener>>()
			{
				public void resultAvailable(Collection> results)
				{
					Long fs = (Long)jadex.bridge.modelinfo.Argument.getResult(results, "filesize");
					if(fs!=null)
					{
						if(fs.longValue()==filesize[0])
						{
							long end = System.currentTimeMillis();
							System.out.println("Needed "+(end-start)/1000.0+" seconds for "+filesize[0]/1024+" kbytes.");
							tr.setSucceeded(true);
						}
						else
						{
							tr.setFailed("Wrong file size [expected, received]: "+filesize[0]+" "+fs.longValue());
						}
					}
					else
					{
						tr.setFailed("No target file size reported.");
					}
					ret.setResult(tr);
				}
				public void exceptionOccurred(Exception exception)
				{
					tr.setFailed("Receiver agent had exception: "+exception);
					ret.setResult(tr);
				}
			});
			
			IComponentStep step = new IComponentStep()
			{
				public IFuture execute(IInternalAccess ia)
				{
					try
					{
						final IComponentStep self = this;
						int size = Math.min(200000, is.available());
						filesize[0] += size;
						byte[] buf = new byte[size];
						int read = 0;
						while(read!=buf.length)
						{
							read += is.read(buf, read, buf.length-read);
						}
						con.write(buf);
//						System.out.println("wrote: "+size);
						if(is.available()>0)
						{
							con.waitForReady().addResultListener(new IResultListener()
							{
								public void resultAvailable(Integer result)
								{
									agent.getComponentFeature(IExecutionFeature.class).scheduleStep(self);
//									agent.getComponentFeature(IExecutionFeature.class).waitForDelay(10, self);
								}
								public void exceptionOccurred(Exception exception)
								{
									exception.printStackTrace();
									con.close();
								}
							});
						}
						else
						{
							con.close();
						}
					}
					catch(Exception e)
					{
						e.printStackTrace();
						ret.setException(e);
					}
					
					return IFuture.DONE;
				}
			};
			agent.getComponentFeature(IExecutionFeature.class).scheduleStep(step);
//			con.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			ret.setException(e);
		}
		
		return ret;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy