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

jadex.micro.testcases.stream.ReceiverAgent 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.File;
import java.io.FileOutputStream;
import java.util.Collection;

import jadex.bridge.IConnection;
import jadex.bridge.IInputConnection;
import jadex.bridge.IInternalAccess;
import jadex.bridge.component.IArgumentsResultsFeature;
import jadex.bridge.component.IExecutionFeature;
import jadex.bridge.service.types.context.IContextService;
import jadex.commons.future.IIntermediateResultListener;
import jadex.commons.future.ISubscriptionIntermediateFuture;
import jadex.micro.annotation.Agent;
import jadex.micro.annotation.AgentArgument;
import jadex.micro.annotation.AgentService;
import jadex.micro.annotation.AgentStreamArrived;
import jadex.micro.annotation.Argument;
import jadex.micro.annotation.Arguments;
import jadex.micro.annotation.Binding;
import jadex.micro.annotation.RequiredService;
import jadex.micro.annotation.RequiredServices;
import jadex.micro.annotation.Result;
import jadex.micro.annotation.Results;

@Arguments(@Argument(name="filename", clazz=String.class, defaultvalue="\"copy.copy\""))
@Results(@Result(name="filesize", clazz=long.class))
@RequiredServices({
	@RequiredService(name="contextService", type=IContextService.class, binding=@Binding(scope=Binding.SCOPE_PLATFORM))
})
@Agent
public class ReceiverAgent
{
	@Agent
	protected IInternalAccess agent;
	
	@AgentArgument
	protected String filename;
	
	@AgentService
	protected IContextService contextService;
	
	/**
	 * 
	 */
	@AgentStreamArrived
	public void streamArrvied(final IConnection con)
	{
		// todo: how to avoid garbage collection of connection?
//		final IInputConnection con = (IInputConnection)msg.get(SFipa.CONTENT);
//		System.out.println("received: "+con+" "+con.hashCode());
		
		receiveBehavior((IInputConnection)con);
	}

	/**
	 * 
	 */
	public void receiveBehavior(IInputConnection con)
	{
		try
		{
			final long[] cnt = new long[1];
			File f = contextService.getFile(filename).get();
			final FileOutputStream fos = new FileOutputStream(f);
			
			ISubscriptionIntermediateFuture fut = ((IInputConnection)con).aread();
			fut.addResultListener(agent.getComponentFeature(IExecutionFeature.class).createResultListener(new IIntermediateResultListener()
			{
				public void resultAvailable(Collection result)
				{
//					try
//					{
//						for(Iterator it=result.iterator(); it.hasNext(); )
//						{
//							fos.write(it.next().byteValue());
//						}
//					}
//					catch(Exception e)
//					{
//						e.printStackTrace();
//					}
				}
				public void intermediateResultAvailable(byte[] result)
				{
					cnt[0] += result.length;
//					if(cnt[0]%1000==0)
//						System.out.println("bytes: "+cnt[0]);
					try
					{
						fos.write(result);
					}
					catch(Exception e)
					{
						e.printStackTrace();
					}
				}
				public void finished()
				{
					try
					{
//						System.out.println("finished, size: "+cnt[0]);
						fos.close();
						agent.getComponentFeature(IArgumentsResultsFeature.class).getResults().put("filesize", Long.valueOf(cnt[0]));
						agent.killComponent();
					}
					catch(Exception e)
					{
						agent.killComponent();
//						e.printStackTrace();
					}
				}
				public void exceptionOccurred(Exception exception)
				{
//					System.out.println("ex:"+exception);
					agent.killComponent();
				}
			}));
		}
		catch(Exception e)
		{
			e.printStackTrace();
			agent.killComponent();
		}
	}
	
//	/**
//	 * 
//	 */
//	public void receiveBehavior()
//	{
//		final int[] cnt = new int[1];
//		((IInputConnection)con).aread().addResultListener(new IIntermediateResultListener()
//		{
//			public void resultAvailable(Collection result)
//			{
//				System.out.println("Result: "+result);
//				cnt[0] += result.size();
//			}
//			public void intermediateResultAvailable(Byte result)
//			{
//				System.out.println("Intermediate result: "+result+" :"+cnt[0]);
//				cnt[0] += 1;
//				if(cnt[0]==5050)
//					((IInputConnection)con).close();
//			}
//			public void finished()
//			{
//				System.out.println("finished: "+cnt[0]);
//			}
//			public void exceptionOccurred(Exception exception)
//			{
//				System.out.println("ex:"+exception);
//			}
//		});
//		
////		IComponentStep step = new IComponentStep()
////		{
////			public IFuture execute(IInternalAccess ia)
////			{
////				try
////				{
////					byte[] buffer = new byte[2];
////					con.read(buffer);
////					System.out.println("buffer: "+SUtil.arrayToString(buffer));
//////					int res = con.read();
//////					System.out.println("read: "+res);
////				}
////				catch(Exception e)
////				{
////					agent.killComponent()
////					e.printStackTrace();
////				}
////				agent.getComponentFeature(IExecutionFeature.class).waitForDelay(1000, this);
////				return null;
////			}
////		};
////		agent.getComponentFeature(IExecutionFeature.class).waitForDelay(1000, step);
//	}
	
//	@AgentKilled
//	public void killed()
//	{
//		if(con!=null)
//			con.close();
//	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy