📄 agentmobilityservice.java
字号:
}
}
catch(Exception e) {
// Link failure: abort transaction
//log("Link failure!", 2);
if(myLogger.isLoggable(Logger.WARNING))
myLogger.log(Logger.WARNING,"Link failure!");
return false;
}
finally {
impl.releaseAgentDescriptor(agentID);
}
}
else {
throw new NotFoundException("Agent agentID not found");
}
}
else {
// Do nothing for now, but could also use another slice as transaction coordinator...
//log("Not a main!", 2);
if(myLogger.isLoggable(Logger.WARNING))
myLogger.log(Logger.WARNING,"Not a main!");
return false;
}
}
//#J2ME_EXCLUDE_BEGIN
private void handleCloneCodeLocatorEntry(AID oldAgentID, AID newAgentID) throws ServiceException, IMTPException, NotFoundException {
AgentManagementService amSrv = (AgentManagementService) myFinder.findService(AgentManagementService.NAME);
CodeLocator codeLocator = amSrv.getCodeLocator();
if (codeLocator.isRegistered(oldAgentID)) {
if(myLogger.isLoggable(Logger.FINE)) {
myLogger.log(Logger.FINE," adding clone " + newAgentID.getName() + " to code locator.");
}
codeLocator.cloneAgent(oldAgentID, newAgentID);
}
}
private void handleRemoveCodeLocatorEntry(AID agentID) throws IMTPException, ServiceException {
if(myLogger.isLoggable(Logger.FINE)) {
myLogger.log(Logger.FINE,"Target sink consuming command REMOVE_CODE_LOCATOR_ENTRY");
}
// Remove entry from CodeLocator.
AgentManagementService amSrv = (AgentManagementService) myFinder.findService(AgentManagementService.NAME);
CodeLocator codeLocator = amSrv.getCodeLocator();
codeLocator.removeAgent(agentID);
}
//#J2ME_EXCLUDE_END
} // End of ServiceComponent class
/**
* Inner class Deserializer
*/
private class Deserializer extends ObjectInputStream {
private String agentName;
private String classSiteName;
private ServiceFinder finder;
/**
*/
public Deserializer(InputStream inner, String an, String sliceName, ServiceFinder sf) throws IOException {
super(inner);
agentName = an;
classSiteName = sliceName;
finder = sf;
}
/**
*/
protected Class resolveClass(ObjectStreamClass v) throws IOException, ClassNotFoundException {
String key = createClassLoaderKey(agentName, classSiteName);
MobileAgentClassLoader cl = (MobileAgentClassLoader)loaders.get(key);
if (cl == null) {
try {
cl = new MobileAgentClassLoader(agentName, classSiteName, finder, AgentMobilityService.this.getClass().getClassLoader());
loaders.put(key, cl);
}
catch (IMTPException imtpe) {
// We are loading an incoming agent --> Should never happen
imtpe.printStackTrace();
throw new ClassNotFoundException("Error creating MobileAgent ClassLoader. "+imtpe.getMessage());
}
catch (ServiceException se) {
// We are loading an incoming agent --> Should never happen
se.printStackTrace();
throw new ClassNotFoundException("Error creating MobileAgent ClassLoader. "+se.getMessage());
}
}
//#J2ME_EXCLUDE_BEGIN
Class c = Class.forName(v.getName(), true, cl);
//#J2ME_EXCLUDE_END
/*#J2ME_INCLUDE_BEGIN
Class c = cl.loadClass(v.getName());
#J2ME_INCLUDE_END*/
return c;
}
private String createClassLoaderKey(String agentName, String classSiteName) {
return agentName+'#'+classSiteName;
}
} // END of inner class Deserializer
// This Map holds the mapping between a container/agent pair and the class loader
// that can retrieve agent classes from that container.
private final Map loaders = new HashMap();
// This Map holds the mapping between an agent that arrived on this
// container and the service slice where its classes can be found
private final Map sites = new HashMap();
// The concrete agent container, providing access to LADT, etc.
private AgentContainer myContainer;
// The local slice for this service
private final ServiceComponent localSlice = new ServiceComponent();
/**
Inner class AgentMobilityHelperImpl.
The actual implementation of the AgentMobilityHelper interface.
*/
private class AgentMobilityHelperImpl implements AgentMobilityHelper {
private Agent myAgent;
private Movable myMovable;
public void init(Agent a) {
myAgent = a;
}
public void registerMovable(Movable m) {
myMovable = m;
}
public void move(Location destination) {
myAgent.changeStateTo(new TransitLifeCycle(destination, myMovable, AgentMobilityService.this));
}
public void clone(Location destination, String newName) {
myAgent.changeStateTo(new CopyLifeCycle(destination, newName, myMovable, AgentMobilityService.this));
}
//#J2ME_EXCLUDE_BEGIN
public ClassLoader getContainerClassLoader(String codeSourceContainer, ClassLoader parent) throws ServiceException {
try {
return new MobileAgentClassLoader(null, codeSourceContainer, AgentMobilityService.this.myFinder, parent);
}
catch (IMTPException imtpe) {
throw new ServiceException("Communication error retrieving code source container slice.", imtpe);
}
}
//#J2ME_EXCLUDE_END
} // END of inner class AgentMobilityHelperImpl
/**
Inner class TransitLifeCycle
*/
private static class TransitLifeCycle extends LifeCycle {
private Location myDestination;
private Movable myMovable;
private transient AgentMobilityService myService;
private Logger myLogger;
private boolean firstTime = true;
private boolean messageAware = false;
private TransitLifeCycle(Location l, Movable m, AgentMobilityService s) {
super(AP_TRANSIT);
myDestination = l;
myMovable = m;
myService = s;
myLogger = Logger.getMyLogger(myService.getName());
}
public void init() {
myAgent.restoreBufferedState();
if (myMovable != null) {
myMovable.afterMove();
}
}
public void execute() throws JADESecurityException, InterruptedException, InterruptedIOException {
try {
// Call beforeMove() and issue an INFORM_MOVED vertical command
if (firstTime) {
firstTime = false;
if (myMovable != null) {
messageAware = true;
myMovable.beforeMove();
messageAware = false;
}
informMoved(myAgent.getAID(), myDestination);
}
}
catch (Exception e) {
if (myAgent.getState() == myState) {
// Something went wrong during the transfer. Rollback
myAgent.restoreBufferedState();
myDestination = null;
if (e instanceof JADESecurityException) {
// Will be caught together with all other JADESecurityException-s
throw (JADESecurityException) e;
}
else {
e.printStackTrace();
}
}
else {
throw new Interrupted();
}
}
}
public void end() {
if(myLogger.isLoggable(Logger.SEVERE))
myLogger.log(Logger.SEVERE,"*** Agent " + myAgent.getName() + " moved in a forbidden situation ***");
myAgent.clean(true);
}
public boolean transitionTo(LifeCycle newLF) {
int s = newLF.getState();
return (s == AP_GONE || s == Agent.AP_ACTIVE || s == Agent.AP_DELETED);
}
public boolean isMessageAware() {
return messageAware;
}
public void informMoved(AID agentID, Location where) throws ServiceException, JADESecurityException, NotFoundException, IMTPException {
GenericCommand cmd = new GenericCommand(AgentMobilityHelper.INFORM_MOVED, AgentMobilitySlice.NAME, null);
cmd.addParam(agentID);
cmd.addParam(where);
// Set the credentials of the moving agent
myService.initCredentials(cmd, agentID);
Object lastException = myService.submit(cmd);
if(lastException != null) {
if(lastException instanceof JADESecurityException) {
throw (JADESecurityException)lastException;
}
if(lastException instanceof NotFoundException) {
throw (NotFoundException)lastException;
}
if(lastException instanceof IMTPException) {
throw (IMTPException)lastException;
}
}
}
} // END of inner class TransitLifeCycle
/**
Inner class CopyLifeCycle
*/
private static class CopyLifeCycle extends LifeCycle {
private Location myDestination;
private String myNewName;
private Movable myMovable;
private transient AgentMobilityService myService;
private Logger myLogger;
private boolean firstTime = true;
private boolean messageAware = false;
private CopyLifeCycle(Location l, String newName, Movable m, AgentMobilityService s) {
super(AP_COPY);
myDestination = l;
myNewName = newName;
myMovable = m;
myService = s;
myLogger = Logger.getMyLogger(myService.getName());
}
public void init() {
myAgent.restoreBufferedState();
if (myMovable != null) {
myMovable.afterClone();
}
}
public void execute() throws JADESecurityException, InterruptedException, InterruptedIOException {
try {
// Call beforeClone() and issue an INFORM_CLONED vertical command
if (firstTime) {
firstTime = false;
if (myMovable != null) {
messageAware = true;
myMovable.beforeClone();
messageAware = false;
}
informCloned(myAgent.getAID(), myDestination, myNewName);
}
}
catch (Exception e) {
if (myAgent.getState() == myState) {
// Something went wrong during the clonation. Rollback
myDestination = null;
myNewName = null;
myAgent.restoreBufferedState();
if (e instanceof JADESecurityException) {
// Will be catched together with all other JADESecurityException-s
throw (JADESecurityException) e;
}
else {
e.printStackTrace();
return;
}
}
else {
throw new Interrupted();
}
}
// Once cloned go back to the previous state
myAgent.restoreBufferedState();
}
public boolean transitionTo(LifeCycle newLF) {
int s = newLF.getState();
return (s == Agent.AP_ACTIVE || s == Agent.AP_DELETED);
}
public boolean isMessageAware() {
return messageAware;
}
public void end() {
//System.err.println("*** Agent " + myAgent.getName() + " cloned in a forbidden situation ***");
if(myLogger.isLoggable(Logger.SEVERE))
myLogger.log(Logger.SEVERE,"*** Agent " + myAgent.getName() + " cloned in a forbidden situation ***");
myAgent.clean(true);
}
public void informCloned(AID agentID, Location where, String newName) throws ServiceException, JADESecurityException, IMTPException, NotFoundException, NameClashException {
GenericCommand cmd = new GenericCommand(AgentMobilityHelper.INFORM_CLONED, AgentMobilitySlice.NAME, null);
cmd.addParam(agentID);
cmd.addParam(where);
cmd.addParam(newName);
// Set the credentials of the cloning agent
myService.initCredentials(cmd, agentID);
Object lastException = myService.submit(cmd);
if(lastException != null) {
if(lastException instanceof JADESecurityException) {
throw (JADESecurityException)lastException;
}
if(lastException instanceof NotFoundException) {
throw (NotFoundException)lastException;
}
if(lastException instanceof IMTPException) {
throw (IMTPException)lastException;
}
if(lastException instanceof NameClashException) {
throw (NameClashException)lastException;
}
}
}
} // END of inner class CopyLifeCycle
// The command sink, source side
private final CommandSourceSink senderSink = new CommandSourceSink();
// The command sink, target side
private final CommandTargetSink receiverSink = new CommandTargetSink();
//#J2ME_EXCLUDE_BEGIN
// Filter for outgoing commands
private final Filter _outFilter = new CommandOutgoingFilter();
//#J2ME_EXCLUDE_END
// Work-around for PJAVA compilation
protected Service.Slice getFreshSlice(String name) throws ServiceException {
return super.getFreshSlice(name);
}
private void initCredentials(Command cmd, AID id) {
Agent agent = myContainer.acquireLocalAgent(id);
if (agent != null) {
try {
CredentialsHelper ch = (CredentialsHelper) agent.getHelper("jade.core.security.Security");
cmd.setPrincipal(ch.getPrincipal());
cmd.setCredentials(ch.getCredentials());
}
catch (ServiceException se) {
// The security plug-in is not there. Just ignore it
}
}
myContainer.releaseLocalAgent(id);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -