jadex.base.service.chat.ChatService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jadex-platform-base Show documentation
Show all versions of jadex-platform-base Show documentation
The Jadex platform base package contains
functionality useful for constructing platforms.
The newest version!
package jadex.base.service.chat;
import jadex.bridge.IComponentIdentifier;
import jadex.bridge.IConnection;
import jadex.bridge.IInputConnection;
import jadex.bridge.IInternalAccess;
import jadex.bridge.IOutputConnection;
import jadex.bridge.ServiceCall;
import jadex.bridge.service.BasicService;
import jadex.bridge.service.RequiredServiceInfo;
import jadex.bridge.service.annotation.Reference;
import jadex.bridge.service.annotation.Service;
import jadex.bridge.service.annotation.ServiceComponent;
import jadex.bridge.service.annotation.ServiceShutdown;
import jadex.bridge.service.annotation.ServiceStart;
import jadex.bridge.service.types.chat.ChatEvent;
import jadex.bridge.service.types.chat.IChatGuiService;
import jadex.bridge.service.types.chat.IChatService;
import jadex.bridge.service.types.chat.TransferInfo;
import jadex.bridge.service.types.remote.ServiceInputConnection;
import jadex.bridge.service.types.settings.ISettingsService;
import jadex.commons.Base64;
import jadex.commons.IPropertiesProvider;
import jadex.commons.Properties;
import jadex.commons.Property;
import jadex.commons.SUtil;
import jadex.commons.Tuple3;
import jadex.commons.future.CollectionResultListener;
import jadex.commons.future.DelegationResultListener;
import jadex.commons.future.ExceptionDelegationResultListener;
import jadex.commons.future.Future;
import jadex.commons.future.FutureTerminatedException;
import jadex.commons.future.IFuture;
import jadex.commons.future.IIntermediateFuture;
import jadex.commons.future.IIntermediateResultListener;
import jadex.commons.future.IResultListener;
import jadex.commons.future.ISubscriptionIntermediateFuture;
import jadex.commons.future.ITerminableFuture;
import jadex.commons.future.ITerminableIntermediateFuture;
import jadex.commons.future.IntermediateDefaultResultListener;
import jadex.commons.future.IntermediateDelegationResultListener;
import jadex.commons.future.IntermediateFuture;
import jadex.commons.future.SubscriptionIntermediateFuture;
import jadex.commons.future.TerminableFuture;
import jadex.commons.future.TerminableIntermediateFuture;
import jadex.commons.future.TerminationCommand;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Chat service implementation.
*/
@Service
public class ChatService implements IChatService, IChatGuiService
{
//-------- attributes --------
/** The agent. */
@ServiceComponent
protected IInternalAccess agent;
/** The service identifier. */
// @ServiceIdentifier
// protected IServiceIdentifier sid;
/** The futures of active subscribers. */
protected Set> subscribers;
/** The local nick name. */
protected String nick;
/** The currently managed file transfers. */
protected Map, IInputConnection>> transfers;
protected Map, IConnection>> transfers2;
/** Flag to avoid duplicate initialization/shutdown due to duplicate use of implementation. */
protected boolean running;
/** The image. */
protected byte[] image;
//-------- initialization methods --------
/**
* Called on startup.
*/
@ServiceStart
public IFuture start()
{
final Future ret = new Future();
if(!running)
{
running = true;
final PropProvider pp = new PropProvider();
agent.getServiceContainer().searchService(ISettingsService.class, RequiredServiceInfo.SCOPE_PLATFORM)
.addResultListener(new IResultListener()
{
public void resultAvailable(ISettingsService settings)
{
settings.registerPropertiesProvider(agent.getComponentIdentifier().getLocalName(), pp)
.addResultListener(new DelegationResultListener(ret)
{
public void customResultAvailable(Void result)
{
// pp.isCalled().addResultListener(new DelegationResultListener(ret)
// {
// public void customResultAvailable(Void result)
// {
proceed();
// }
// });
}
});
}
public void exceptionOccurred(Exception exception)
{
// No settings service: ignore.
proceed();
}
public void proceed()
{
if(nick==null)
nick = SUtil.createUniqueId("user", 3);
transfers = new LinkedHashMap, IInputConnection>>();
transfers2 = new LinkedHashMap, IConnection>>();
// Search and post status in background for not delaying platform startup.
IIntermediateFuture chatfut = agent.getServiceContainer().getRequiredServices("chatservices");
chatfut.addResultListener(new IntermediateDefaultResultListener()
{
public void intermediateResultAvailable(IChatService chat)
{
chat.status(nick, STATE_IDLE, null);
}
public void finished()
{
// ignore...
}
public void exceptionOccurred(Exception exception)
{
// ignore...
}
});
ret.setResult(null);
}
});
}
else
{
ret.setResult(null);
}
return ret;
}
/**
* Called on shutdown.
*/
@ServiceShutdown
public IFuture shutdown()
{
if(!running)
{
return IFuture.DONE;
}
else
{
running = false;
final Future ret = new Future();
if(subscribers!=null)
{
for(SubscriptionIntermediateFuture fut: subscribers)
{
fut.terminate();
}
}
IIntermediateFuture chatfut = agent.getServiceContainer().getRequiredServices("chatservices");
chatfut.addResultListener(new IntermediateDefaultResultListener()
{
public void intermediateResultAvailable(IChatService chat)
{
// Hack!!! change local id from rms to chat agent.
IComponentIdentifier id = IComponentIdentifier.LOCAL.get();
IComponentIdentifier.LOCAL.set(agent.getComponentIdentifier());
chat.status(nick, STATE_DEAD, null);
IComponentIdentifier.LOCAL.set(id);
}
public void finished()
{
ret.setResult(null);
}
public void exceptionOccurred(Exception exception)
{
ret.setResult(null);
}
});
return ret;
}
}
//-------- IChatService interface --------
/**
* Post a message
* @param text The text message.
*/
public IFuture message(String nick, String text, boolean privatemessage)
{
// System.out.println("Timeout: "+ServiceCall.getInstance().getTimeout()+", "+ServiceCall.getInstance().isRealtime());
boolean published = publishEvent(ChatEvent.TYPE_MESSAGE, nick, ServiceCall.getInstance().getCaller(), text, privatemessage, null);
return published ? IFuture.DONE : new Future(new RuntimeException("No GUI, message was discarded."));
}
/**
* Post a status change.
* @param status The new status.
*/
public IFuture status(String nick, String status, byte[] image)
{
publishEvent(ChatEvent.TYPE_STATECHANGE, nick, ServiceCall.getInstance().getCaller(), status, false, image);
return IFuture.DONE;
}
/**
* Send a file.
*
* @param nick The sender's nick name.
* @param filename The filename.
* @param size The size of the file.
* @param id An optional id to identify the transfer (e.g. for resume after error).
* @param con The connection.
*
* @return The returned future publishes updates about the total number of bytes received.
* Exception messages of the returned future correspond to file transfer states (aborted vs. error vs. rejected).
*/
public ITerminableIntermediateFuture sendFile(final String nick, String filename, long size, String id, final IInputConnection con)
{
final ServiceCall call = ServiceCall.getInstance();
// Hack!!! always assume real time for chat interaction.
final TransferInfo ti = new TransferInfo(true, id, filename, null, call.getCaller(), size, System.currentTimeMillis() + call.getTimeout());
ti.setState(TransferInfo.STATE_WAITING);
final TerminableIntermediateFuture ret = new TerminableIntermediateFuture(new TerminationCommand()
{
public void terminated(Exception reason)
{
ti.setState(TransferInfo.STATE_ABORTED);
publishEvent(ChatEvent.TYPE_FILE, nick, call.getCaller(), ti);
transfers2.remove(ti.getId());
}
});
transfers.put(ti.getId(), new Tuple3, IInputConnection>(ti, ret, con));
publishEvent(ChatEvent.TYPE_FILE, nick, call.getCaller(), ti);
return ret;
}
/**
* Send a file. Alternative method signature.
*
* @param nick The sender's nick name.
* @param filename The filename.
* @param size The size of the file.
* @param id An optional id to identify the transfer (e.g. for resume after error).
*
* @return When the upload is accepted, the output connection for sending the file is returned.
*/
public ITerminableFuture startUpload(final String nick, String filename, long size, String id)
{
final ServiceCall call = ServiceCall.getInstance();
final TransferInfo ti = new TransferInfo(true, id, filename, null, call.getCaller(), size, System.currentTimeMillis() + call.getTimeout());
ti.setState(TransferInfo.STATE_WAITING);
TerminableFuture ret = new TerminableFuture(new TerminationCommand()
{
public void terminated(Exception reason)
{
ti.setState(TransferInfo.STATE_ABORTED);
publishEvent(ChatEvent.TYPE_FILE, nick, call.getCaller(), ti);
transfers2.remove(ti.getId());
}
});
transfers2.put(ti.getId(), new Tuple3, IConnection>(ti, ret, null));
publishEvent(ChatEvent.TYPE_FILE, nick, call.getCaller(), ti);
return ret;
}
//-------- IChatGuiService interface --------
/**
* Set the user name.
*/
public IFuture setNickName(String nick)
{
this.nick = nick;
// Publish new nickname
status(null, null, new IComponentIdentifier[0]);
return IFuture.DONE;
}
/**
* Get the user name.
*/
public IFuture getNickName()
{
return new Future(nick);
}
/**
* Set the image.
*/
public IFuture setImage(byte[] image)
{
this.image = image;
// Publish new image
status(null, image, new IComponentIdentifier[0]);
return IFuture.DONE;
}
/**
* Get the image.
*/
public IFuture getImage()
{
return new Future(image);
}
/**
* Subscribe to events from the chat service.
* @return A future publishing chat events as intermediate results.
*/
public ISubscriptionIntermediateFuture subscribeToEvents()
{
if(subscribers==null)
{
subscribers = new LinkedHashSet>();
}
SubscriptionIntermediateFuture ret = new SubscriptionIntermediateFuture();
subscribers.add(ret);
return ret;
}
/**
* Search for available chat services.
* @return The currently available remote services.
*/
public IIntermediateFuture findUsers()
{
IIntermediateFuture ret = agent.getServiceContainer().getRequiredServices("chatservices");
return ret;
}
/**
* Post a message.
* Searches for available chat services and posts the message to all.
* @param text The text message.
* @return The remote services, to which the message was successfully posted.
*/
public IIntermediateFuture message(final String text, final IComponentIdentifier[] receivers, boolean self)
{
final IntermediateFuture ret = new IntermediateFuture();
if(receivers.length>0)
{
boolean foundself = false;
if(self)
{
for(int i=0; i lis = new CollectionResultListener(
cnt, true, new IResultListener>()
{
public void resultAvailable(Collection result)
{
ret.setFinished();
}
public void exceptionOccurred(Exception exception)
{
ret.setFinished();
}
});
for(int i=0; i()
{
public void resultAvailable(IChatService result)
{
ret.addIntermediateResultIfUndone(result); // Might return after later exception in service search!?
lis.resultAvailable(result);
}
public void exceptionOccurred(Exception exception)
{
lis.exceptionOccurred(exception);
}
});
}
if(self && !foundself)
{
sendTo(text, agent.getComponentIdentifier(), true).addResultListener(new IResultListener()
{
public void resultAvailable(IChatService result)
{
ret.addIntermediateResultIfUndone(result); // Might return after later exception in service search!?
lis.resultAvailable(result);
}
public void exceptionOccurred(Exception exception)
{
lis.exceptionOccurred(exception);
}
});
}
}
else //if(receivers.length==0)
{
final IIntermediateFuture ifut = agent.getServiceContainer().getRequiredServices("chatservices");
ifut.addResultListener(new IntermediateDelegationResultListener(ret)
{
boolean finished;
int cnt;
public void intermediateResultAvailable(final IChatService chat)
{
cnt++;
chat.message(nick, text, false).addResultListener(new IResultListener()
{
public void resultAvailable(Void result)
{
ret.addIntermediateResultIfUndone(chat); // Might be called after concurrent exception in service search!
if(--cnt==0 && finished)
{
ret.setFinished();
}
}
public void exceptionOccurred(Exception exception)
{
if(--cnt==0 && finished)
{
ret.setFinished();
}
}
});
}
public void finished()
{
finished = true;
if(finished && cnt==0)
{
ret.setFinished();
}
}
});
}
return ret;
}
/**
* Helper method for sending message to cid.
*/
protected IFuture sendTo(final String text, IComponentIdentifier rec, final boolean privatemessage)
{
final Future ret = new Future();
agent.getServiceContainer().getService(IChatService.class, rec)
.addResultListener(new DelegationResultListener(ret)
{
public void customResultAvailable(final IChatService chat)
{
// ret.setResult(chat);
chat.message(nick, text, privatemessage).addResultListener(new ExceptionDelegationResultListener(ret)
{
public void customResultAvailable(Void result)
{
ret.setResult(chat);
}
});
}
});
return ret;
}
/**
* Post a status change.
* @param status The new status or null for no change.
* @param image The new avatar iamge or null for no change.
*/
public IIntermediateFuture status(final String status, final byte[] image, IComponentIdentifier[] receivers)
{
final IntermediateFuture ret = new IntermediateFuture();
if(receivers.length>0)
{
boolean foundself = false;
for(int i=0; i lis = new CollectionResultListener(
cnt, true, new IResultListener>()
{
public void resultAvailable(Collection result)
{
ret.setFinished();
}
public void exceptionOccurred(Exception exception)
{
ret.setFinished();
}
});
for(int i=0; i()
{
public void resultAvailable(IChatService result)
{
ret.addIntermediateResultIfUndone(result); // Might return after later exception in service search!?
lis.resultAvailable(result);
}
public void exceptionOccurred(Exception exception)
{
lis.exceptionOccurred(exception);
}
});
}
if(!foundself)
{
statusTo(nick, status, image, agent.getComponentIdentifier()).addResultListener(new IResultListener()
{
public void resultAvailable(IChatService result)
{
ret.addIntermediateResultIfUndone(result); // Might return after later exception in service search!?
lis.resultAvailable(result);
}
public void exceptionOccurred(Exception exception)
{
lis.exceptionOccurred(exception);
}
});
}
}
else //if(receivers.length==0)
{
final IIntermediateFuture ifut = agent.getServiceContainer().getRequiredServices("chatservices");
ifut.addResultListener(new IntermediateDelegationResultListener(ret)
{
boolean finished;
int cnt = 0;
public void intermediateResultAvailable(final IChatService chat)
{
cnt++;
chat.status(nick, status, image).addResultListener(new IResultListener()
{
public void resultAvailable(Void result)
{
ret.addIntermediateResultIfUndone(chat); // Might be called after concurrent exception in service search!
if(--cnt==0 && finished)
{
ret.setFinished();
}
}
public void exceptionOccurred(Exception exception)
{
if(--cnt==0 && finished)
{
ret.setFinished();
}
}
});
}
public void finished()
{
finished = true;
if(finished && cnt==0)
{
ret.setFinished();
}
}
});
}
return ret;
}
/**
* Helper method for posting status to cid.
*/
protected IFuture statusTo(final String nick, final String status, final byte[] image, IComponentIdentifier rec)
{
final Future ret = new Future();
agent.getServiceContainer().getService(IChatService.class, rec)
.addResultListener(new DelegationResultListener(ret)
{
public void customResultAvailable(final IChatService chat)
{
// ret.setResult(chat);
chat.status(nick, status, image).addResultListener(new ExceptionDelegationResultListener(ret)
{
public void customResultAvailable(Void result)
{
ret.setResult(chat);
}
});
}
});
return ret;
}
/**
* Get a snapshot of the currently managed file transfers.
*/
public IIntermediateFuture getFileTransfers()
{
List ret = new ArrayList();
for(Tuple3, IInputConnection> tup: transfers.values())
{
ret.add(tup.getFirstEntity());
}
for(Tuple3, IConnection> tup: transfers2.values())
{
ret.add(tup.getFirstEntity());
}
return new IntermediateFuture(ret);
}
/**
* Accept a waiting file transfer.
* @param id The transfer id.
* @param filepath The location of the file (possibly changed by user).
*/
public IFuture acceptFile(String id, String filepath)
{
IFuture ret;
Tuple3, IInputConnection> tup = transfers.get(id);
Tuple3, IConnection> tup2 = transfers2.get(id);
if(tup!=null)
{
TransferInfo ti = tup.getFirstEntity();
if(TransferInfo.STATE_WAITING.equals(ti.getState()))
{
ti.setFileName(new File(filepath).getName());
ti.setFilePath(filepath);
doDownload(ti, tup.getSecondEntity(), tup.getThirdEntity());
ret = IFuture.DONE;
}
else if(TransferInfo.STATE_REJECTED.equals(ti.getState()))
{
ret = new Future(new RuntimeException("Transfer already rejected."));
}
else
{
// Already accepted -> ignore.
ret = IFuture.DONE;
}
}
else if(tup2!=null)
{
TransferInfo ti = tup2.getFirstEntity();
if(TransferInfo.STATE_WAITING.equals(ti.getState()))
{
ServiceInputConnection sic = new ServiceInputConnection();
((Future)tup2.getSecondEntity()).setResultIfUndone(sic.getOutputConnection());
ti.setFileName(new File(filepath).getName());
ti.setFilePath(filepath);
doDownload(ti, null, sic);
ret = IFuture.DONE;
}
else if(TransferInfo.STATE_REJECTED.equals(ti.getState()))
{
ret = new Future(new RuntimeException("Transfer already rejected."));
}
else
{
// Already accepted -> ignore.
ret = IFuture.DONE;
}
}
else
{
ret = new Future(new RuntimeException("No such file transfer."));
}
return ret;
}
/**
* Reject a waiting file transfer.
* @param id The transfer id.
*/
public IFuture rejectFile(String id)
{
IFuture ret;
Tuple3, IInputConnection> tup = transfers.get(id);
Tuple3, IConnection> tup2 = transfers2.get(id);
if(tup!=null)
{
TransferInfo ti = tup.getFirstEntity();
if(TransferInfo.STATE_WAITING.equals(ti.getState()))
{
ti.setState(TransferInfo.STATE_REJECTED);
publishEvent(ChatEvent.TYPE_FILE, null, ti.getOther(), ti);
tup.getSecondEntity().setException(new RuntimeException(TransferInfo.STATE_REJECTED));
transfers.remove(id);
ret = IFuture.DONE;
}
else if(TransferInfo.STATE_REJECTED.equals(ti.getState()))
{
// Already rejected -> ignore.
ret = IFuture.DONE;
}
else
{
ret = new Future(new RuntimeException("Transfer already accepted."));
}
}
else if(tup2!=null)
{
TransferInfo ti = tup2.getFirstEntity();
if(TransferInfo.STATE_WAITING.equals(ti.getState()))
{
ti.setState(TransferInfo.STATE_REJECTED);
publishEvent(ChatEvent.TYPE_FILE, null, ti.getOther(), ti);
((Future>)tup2.getSecondEntity()).setException(new RuntimeException(TransferInfo.STATE_REJECTED));
transfers2.remove(id);
ret = IFuture.DONE;
}
else if(TransferInfo.STATE_REJECTED.equals(ti.getState()))
{
// Already rejected -> ignore.
ret = IFuture.DONE;
}
else
{
ret = new Future(new RuntimeException("Transfer already accepted."));
}
}
else
{
ret = new Future(new RuntimeException("No such file transfer."));
}
return ret;
}
/**
* Cancel an ongoing file transfer.
* @param id The transfer id.
*/
public IFuture cancelTransfer(String id)
{
IFuture ret;
Tuple3, IInputConnection> tup = transfers.get(id);
Tuple3, IConnection> tup2 = transfers2.get(id);
if(tup!=null)
{
TransferInfo ti = tup.getFirstEntity();
if(TransferInfo.STATE_TRANSFERRING.equals(ti.getState()) || TransferInfo.STATE_WAITING.equals(ti.getState()))
{
ti.setState(TransferInfo.STATE_CANCELLING);
publishEvent(ChatEvent.TYPE_FILE, null, null, ti);
tup.getSecondEntity().terminate();
ret = IFuture.DONE;
}
else if(TransferInfo.STATE_COMPLETED.equals(ti.getState()))
{
ret = new Future(new RuntimeException("Transfer already completed."));
}
else
{
// Already aborted -> ignore.
ret = IFuture.DONE;
}
}
else if(tup2!=null)
{
TransferInfo ti = tup2.getFirstEntity();
if(TransferInfo.STATE_WAITING.equals(ti.getState()))
{
ti.setState(TransferInfo.STATE_CANCELLING);
publishEvent(ChatEvent.TYPE_FILE, null, null, ti);
tup2.getSecondEntity().terminate();
ret = IFuture.DONE;
}
else if(TransferInfo.STATE_TRANSFERRING.equals(ti.getState()))
{
ti.setState(TransferInfo.STATE_CANCELLING);
publishEvent(ChatEvent.TYPE_FILE, null, null, ti);
tup2.getThirdEntity().close();
ret = IFuture.DONE;
}
else if(TransferInfo.STATE_COMPLETED.equals(ti.getState()))
{
ret = new Future(new RuntimeException("Transfer already completed."));
}
else
{
// Already aborted -> ignore.
ret = IFuture.DONE;
}
}
else
{
ret = new Future(new RuntimeException("No such file transfer."));
}
return ret;
}
/**
* Send a local file to the target component.
* @param filepath The file path, local to the chat component.
* @param cid The id of a remote chat component.
*/
public IFuture sendFile(final String filepath, final IComponentIdentifier cid)
{
final Future ret = new Future();
IFuture fut = agent.getServiceContainer().getService(IChatService.class, cid);
fut.addResultListener(new ExceptionDelegationResultListener(ret)
{
public void customResultAvailable(IChatService cs)
{
final File file = new File(filepath);
final long size = file.length();
// // Call chat service of receiver
// final ServiceOutputConnection ocon = new ServiceOutputConnection();
// final IInputConnection icon = ocon.getInputConnection();
// ITerminableIntermediateFuture fut = cs.sendFile(nick, file.getName(), size, fi.getId(), icon);
//
// // Receives notifications how many bytes were received.
// fut.addResultListener(new IntermediateExceptionDelegationResultListener(ret)
// {
// boolean started;
//
// public void intermediateResultAvailable(Long result)
// {
//// System.out.println("rec: "+result);
// // Start sending after first intermediate result was received
// if(!started)
// {
// started = true;
// ret.setResult(null);
// doUpload(fi, ocon, cid);
// }
// }
//
// public void finished()
// {
// fi.setState(TransferInfo.STATE_COMPLETED);
// publishEvent(ChatEvent.TYPE_FILE, null, cid, fi);
// }
//
// public void customResultAvailable(Collection result)
// {
// fi.setState(TransferInfo.STATE_COMPLETED);
// publishEvent(ChatEvent.TYPE_FILE, null, cid, fi);
// }
//
// public void exceptionOccurred(Exception exception)
// {
// if(exception instanceof FutureTerminatedException || TransferInfo.STATE_ABORTED.equals(exception.getMessage()))
// {
// fi.setState(TransferInfo.STATE_ABORTED);
// }
// else if(TransferInfo.STATE_REJECTED.equals(exception.getMessage()))
// {
// fi.setState(TransferInfo.STATE_REJECTED);
// }
// else
// {
// fi.setState(TransferInfo.STATE_ERROR);
// }
// publishEvent(ChatEvent.TYPE_FILE, null, cid, fi);
// }
// });
// Call chat service of receiver (alternative interface)
final TransferInfo fi = new TransferInfo(false, null, file.getName(), filepath, cid, file.length(), System.currentTimeMillis() + // Hack!!! assume real time timeout.
(cid.getRoot().equals(agent.getComponentIdentifier().getRoot()) ? BasicService.DEFAULT_LOCAL : BasicService.DEFAULT_REMOTE)); // Todo: actual timeout of method!?
fi.setState(TransferInfo.STATE_WAITING);
ITerminableFuture fut = cs.startUpload(nick, file.getName(), size, fi.getId());
transfers2.put(fi.getId(), new Tuple3, IConnection>(fi, fut, null));
publishEvent(ChatEvent.TYPE_FILE, null, cid, fi);
fut.addResultListener(new ExceptionDelegationResultListener(ret)
{
public void customResultAvailable(IOutputConnection ocon)
{
doUpload(fi, ocon, cid);
ret.setResult(null);
}
public void exceptionOccurred(Exception exception)
{
if(exception instanceof FutureTerminatedException || TransferInfo.STATE_ABORTED.equals(exception.getMessage())
|| TransferInfo.STATE_CANCELLING.equals(fi.getState()))
{
fi.setState(TransferInfo.STATE_ABORTED);
}
else if(TransferInfo.STATE_REJECTED.equals(exception.getMessage()))
{
fi.setState(TransferInfo.STATE_REJECTED);
}
else
{
fi.setState(TransferInfo.STATE_ERROR);
}
transfers2.remove(fi.getId());
publishEvent(ChatEvent.TYPE_FILE, null, cid, fi);
}
});
}
});
return ret;
}
//-------- helper methods --------
/**
* Post an event to registered subscribers.
* @param type The event type.
* @param nick The nick name.
* @param cid The component ID.
* @param value The event value.
*/
protected boolean publishEvent(String type, String nick, IComponentIdentifier cid, Object value)
{
return publishEvent(type, nick, cid, value, false, null);
}
/**
* Post an event to registered subscribers.
* @param type The event type.
* @param nick The nick name.
* @param cid The component ID.
* @param value The event value.
*/
protected boolean publishEvent(String type, String nick, IComponentIdentifier cid, Object value, boolean privatemessage, byte[] image)
{
boolean ret = false;
if(subscribers!=null)
{
ChatEvent ce = new ChatEvent(type, nick, cid, value, privatemessage, image);
for(Iterator> it=subscribers.iterator(); it.hasNext(); )
{
if(it.next().addIntermediateResultIfUndone(ce))
{
ret = true;
}
else
{
it.remove();
}
}
if(subscribers.isEmpty())
{
subscribers = null;
}
}
return ret;
}
/**
* Perform a download.
*/
protected void doDownload(final TransferInfo ti, final TerminableIntermediateFuture ret, final IInputConnection con)
{
assert TransferInfo.STATE_WAITING.equals(ti.getState());
ti.setState(TransferInfo.STATE_TRANSFERRING);
Tuple3, IConnection> tup2 = transfers2.get(ti.getId());
if(tup2!=null)
transfers2.put(ti.getId(), new Tuple3, IConnection>(ti, tup2.getSecondEntity(), con));
publishEvent(ChatEvent.TYPE_FILE, null, ti.getOther(), ti);
try
{
// Enable sending
if(ret!=null)
{
ret.addIntermediateResult(new Long(0));
}
final FileOutputStream fos = new FileOutputStream(ti.getFilePath());
final ITerminableIntermediateFuture fut = con.writeToOutputStream(fos, agent.getExternalAccess());
fut.addResultListener(new IIntermediateResultListener()
{
public void resultAvailable(Collection result)
{
finished();
}
public void intermediateResultAvailable(Long filesize)
{
if(TransferInfo.STATE_ABORTED.equals(ti.getState()))
{
fut.terminate();
}
if(ret!=null)
{
ret.addIntermediateResult(new Long(filesize));
}
if(ti.update(filesize))
{
publishEvent(ChatEvent.TYPE_FILE, null, ti.getOther(), ti);
}
}
public void finished()
{
try
{
fos.close();
}
catch(Exception e)
{
}
con.close();
ti.setState(ti.getSize()==ti.getDone() ? TransferInfo.STATE_COMPLETED : TransferInfo.STATE_ABORTED);
transfers.remove(ti.getId());
transfers2.remove(ti.getId());
publishEvent(ChatEvent.TYPE_FILE, null, ti.getOther(), ti);
}
public void exceptionOccurred(Exception exception)
{
try
{
fos.close();
}
catch(Exception e)
{
}
con.close();
ti.setState(TransferInfo.STATE_CANCELLING.equals(ti.getState()) ? TransferInfo.STATE_ABORTED : TransferInfo.STATE_ERROR);
transfers.remove(ti.getId());
transfers2.remove(ti.getId());
publishEvent(ChatEvent.TYPE_FILE, null, ti.getOther(), ti);
}
});
}
catch(Exception e)
{
ti.setState(TransferInfo.STATE_ERROR);
transfers.remove(ti.getId());
transfers2.remove(ti.getId());
publishEvent(ChatEvent.TYPE_FILE, null, ti.getOther(), ti);
if(ret!=null)
{
ret.setExceptionIfUndone(new RuntimeException(TransferInfo.STATE_ERROR, e));
}
}
}
/**
* Perform an upload.
* Called from file sender.
* Writes bytes from file input stream to output connection.
*/
protected void doUpload(final TransferInfo ti, final IOutputConnection ocon, final IComponentIdentifier receiver)
{
assert TransferInfo.STATE_WAITING.equals(ti.getState()) : ti.getState();
ti.setState(TransferInfo.STATE_TRANSFERRING);
Tuple3, IConnection> tup2 = transfers2.get(ti.getId());
if(tup2!=null)
transfers2.put(ti.getId(), new Tuple3, IConnection>(ti, tup2.getSecondEntity(), ocon));
publishEvent(ChatEvent.TYPE_FILE, null, ti.getOther(), ti);
try
{
final FileInputStream fis = new FileInputStream(new File(ti.getFilePath()));
final ITerminableIntermediateFuture fut = ocon.writeFromInputStream(fis, agent.getExternalAccess());
fut.addResultListener(new IIntermediateResultListener()
{
public void resultAvailable(Collection result)
{
finished();
}
public void intermediateResultAvailable(Long filesize)
{
if(TransferInfo.STATE_ABORTED.equals(ti.getState()))
{
ocon.close();
fut.terminate();
}
if(ti.update(filesize))
{
publishEvent(ChatEvent.TYPE_FILE, null, ti.getOther(), ti);
}
}
public void finished()
{
try
{
fis.close();
}
catch(Exception e)
{
}
ocon.close();
ti.setState(TransferInfo.STATE_COMPLETED);
transfers.remove(ti.getId());
transfers2.remove(ti.getId());
publishEvent(ChatEvent.TYPE_FILE, null, ti.getOther(), ti);
}
public void exceptionOccurred(Exception exception)
{
try
{
fis.close();
}
catch(Exception e)
{
}
ocon.close();
ti.setState(TransferInfo.STATE_CANCELLING.equals(ti.getState()) ? TransferInfo.STATE_ABORTED : TransferInfo.STATE_ERROR);
transfers.remove(ti.getId());
transfers2.remove(ti.getId());
publishEvent(ChatEvent.TYPE_FILE, null, ti.getOther(), ti);
}
});
}
catch(Exception e)
{
ti.setState(TransferInfo.STATE_ERROR);
transfers.remove(ti.getId());
transfers2.remove(ti.getId());
publishEvent(ChatEvent.TYPE_FILE, null, ti.getOther(), ti);
}
}
//-------- IPropertiesProvider interface --------
/**
*
*/
@Reference
public class PropProvider implements IPropertiesProvider
{
protected Future called = new Future();
/**
*
*/
public IFuture isCalled()
{
return called;
}
/**
* Update from given properties.
*/
public IFuture setProperties(Properties props)
{
String tmp = props.getStringProperty("nickname");
if(tmp!=null)
setNickName(tmp);
tmp = props.getStringProperty("image");
if(tmp!=null)
setImage(Base64.decode(tmp.getBytes()));
called.setResultIfUndone(null);
return IFuture.DONE;
}
/**
* Write current state into properties.
*/
public IFuture getProperties()
{
Properties props = new Properties();
// Only save as executing when in normal mode.
props.addProperty(new Property("nickname", nick));
if(image!=null)
props.addProperty(new Property("image", new String(Base64.encode(image))));
return new Future(props);
}
}
}