📄 ams.java
字号:
/**
Put a DeadAgent event in the AMS event queue
*
public void deadAgent(PlatformEvent ev) {
if (logger.isLoggable(Logger.CONFIG))
logger.log(Logger.CONFIG, ev.toString());
ContainerID cid = ev.getContainer();
AID agentID = ev.getAgent();
DeadAgent da = new DeadAgent();
da.setAgent(agentID);
da.setWhere(cid);
if (ev.getContainerRemoved()) {
da.setContainerRemoved(new Boolean(true));
}
EventRecord er = new EventRecord(da, here());
er.setWhen(ev.getTime());
eventQueue.put(er);
}*/
/**
Put a SuspendedAgent event in the AMS event queue
*
public void suspendedAgent(PlatformEvent ev) {
if (logger.isLoggable(Logger.CONFIG))
logger.log(Logger.CONFIG, ev.toString());
ContainerID cid = ev.getContainer();
AID name = ev.getAgent();
SuspendedAgent sa = new SuspendedAgent();
sa.setAgent(name);
sa.setWhere(cid);
EventRecord er = new EventRecord(sa, here());
er.setWhen(ev.getTime());
eventQueue.put(er);
}*/
/**
Put a ResumedAgent event in the AMS event queue
*
public void resumedAgent(PlatformEvent ev) {
if (logger.isLoggable(Logger.CONFIG))
logger.log(Logger.CONFIG, ev.toString());
ContainerID cid = ev.getContainer();
AID name = ev.getAgent();
ResumedAgent ra = new ResumedAgent();
ra.setAgent(name);
ra.setWhere(cid);
EventRecord er = new EventRecord(ra, here());
er.setWhen(ev.getTime());
eventQueue.put(er);
}*/
/**
Put a FrozenAgent event in the AMS event queue
*
public void frozenAgent(PlatformEvent ev) {
if (logger.isLoggable(Logger.CONFIG))
logger.log(Logger.CONFIG, ev.toString());
ContainerID cid = ev.getContainer();
AID name = ev.getAgent();
ContainerID bufferContainer = ev.getNewContainer();
FrozenAgent fa = new FrozenAgent();
fa.setAgent(name);
fa.setWhere(cid);
fa.setBufferContainer(bufferContainer);
EventRecord er = new EventRecord(fa, here());
er.setWhen(ev.getTime());
eventQueue.put(er);
}*/
/**
Put a ThawedAgent event in the AMS event queue
*
public void thawedAgent(PlatformEvent ev) {
if (logger.isLoggable(Logger.CONFIG))
logger.log(Logger.CONFIG, ev.toString());
ContainerID cid = ev.getContainer();
AID name = ev.getAgent();
ContainerID bufferContainer = ev.getNewContainer();
ThawedAgent ta = new ThawedAgent();
ta.setAgent(name);
ta.setWhere(cid);
ta.setBufferContainer(bufferContainer);
EventRecord er = new EventRecord(ta, here());
er.setWhen(ev.getTime());
eventQueue.put(er);
}*/
/**
Put a MovedAgent event in the AMS event queue
*
public void movedAgent(PlatformEvent ev) {
if (logger.isLoggable(Logger.CONFIG))
logger.log(Logger.CONFIG, ev.toString());
ContainerID from = ev.getContainer();
ContainerID to = ev.getNewContainer();
AID agentID = ev.getAgent();
MovedAgent ma = new MovedAgent();
ma.setAgent(agentID);
ma.setFrom(from);
ma.setTo(to);
EventRecord er = new EventRecord(ma, here());
er.setWhen(ev.getTime());
eventQueue.put(er);
}*/
/**
Put a ChangedAgentOwnership event in the AMS event queue
*
public void changedAgentPrincipal(PlatformEvent ev) {
if (logger.isLoggable(Logger.CONFIG))
logger.log(Logger.CONFIG, ev.toString());
ContainerID cid = ev.getContainer();
AID name = ev.getAgent();
ChangedAgentOwnership cao = new ChangedAgentOwnership();
cao.setAgent(name);
cao.setWhere(cid);
cao.setFrom(ev.getOldOwnership());
cao.setTo(ev.getNewOwnership());
EventRecord er = new EventRecord(cao, here());
er.setWhen(ev.getTime());
eventQueue.put(er);
}*/
/**
Put an AddedContainer event in the AMS event queue
*
public void addedContainer(PlatformEvent ev) {
if (logger.isLoggable(Logger.CONFIG))
logger.log(Logger.CONFIG, ev.toString());
ContainerID cid = ev.getContainer();
String name = cid.getName();
AddedContainer ac = new AddedContainer();
ac.setContainer(cid);
EventRecord er = new EventRecord(ac, here());
er.setWhen(ev.getTime());
eventQueue.put(er);
}*/
/**
Put a RemovedContainer event in the AMS event queue
*
public void removedContainer(PlatformEvent ev) {
if (logger.isLoggable(Logger.CONFIG))
logger.log(Logger.CONFIG, ev.toString());
ContainerID cid = ev.getContainer();
String name = cid.getName();
RemovedContainer rc = new RemovedContainer();
rc.setContainer(cid);
EventRecord er = new EventRecord(rc, here());
er.setWhen(ev.getTime());
eventQueue.put(er);
}*/
/**
Put a XXX event in the AMS event queue
*
public synchronized void changedContainerPrincipal(PlatformEvent ev) {
if (logger.isLoggable(Logger.CONFIG))
logger.log(Logger.CONFIG, ev.toString());
// FIXME: There is no element in the IntrospectionOntology
// corresponding to this event
}*/
/**
Put a AddedMTP event in the AMS event queue
*
public synchronized void addedMTP(MTPEvent ev) {
if (logger.isLoggable(Logger.CONFIG))
logger.log(Logger.CONFIG, "MTPEvent [added MTP]");
Channel ch = ev.getChannel();
ContainerID cid = ev.getPlace();
String proto = ch.getProtocol();
String address = ch.getAddress();
// Generate a suitable AMS event
AddedMTP amtp = new AddedMTP();
amtp.setAddress(address);
amtp.setProto(proto);
amtp.setWhere(cid);
EventRecord er = new EventRecord(amtp, here());
er.setWhen(ev.getTime());
eventQueue.put(er);
// The PlatformDescription has changed --> Generate a suitable event
PlatformDescription ap = new PlatformDescription();
ap.setPlatform(getDescriptionAction(null));
er = new EventRecord(ap, here());
er.setWhen(ev.getTime());
eventQueue.put(er);
}*/
/**
Put a RemovedMTP event in the AMS event queue
*
public synchronized void removedMTP(MTPEvent ev) {
if (logger.isLoggable(Logger.CONFIG))
logger.log(Logger.CONFIG, "MTPEvent [removed MTP]");
Channel ch = ev.getChannel();
ContainerID cid = ev.getPlace();
String proto = ch.getProtocol();
String address = ch.getAddress();
RemovedMTP rmtp = new RemovedMTP();
rmtp.setAddress(address);
rmtp.setProto(proto);
rmtp.setWhere(cid);
EventRecord er = new EventRecord(rmtp, here());
er.setWhen(ev.getTime());
eventQueue.put(er);
// The PlatformDescription has changed --> Generate a suitable event
PlatformDescription ap = new PlatformDescription();
ap.setPlatform(getDescriptionAction(null));
er = new EventRecord(ap, here());
er.setWhen(ev.getTime());
eventQueue.put(er);
}*/
/*
public void messageIn(MTPEvent ev) {
if (logger.isLoggable(Logger.CONFIG))
logger.log(Logger.CONFIG, "MTPEvent [message in]");
}
public void messageOut(MTPEvent ev) {
if (logger.isLoggable(Logger.CONFIG))
logger.log(Logger.CONFIG, "MTPEvent [message out]");
}*/
//////////////////////////////////////////////////
// Utility methods
//////////////////////////////////////////////////
/**
Return the ownership of a container
*/
private String getContainerOwnership(ContainerID container) {
// FIXME: should use AgentManager to do that
return JADEPrincipal.NONE;
}
/**
Return the ownership of an agent
*/
private String getAgentOwnership(AID agent) {
String ownership = null;
try {
AMSAgentDescription amsd = myPlatform.getAMSDescription(agent);
ownership = amsd.getOwnership();
} catch (Exception e) {
// Do nothing
}
return (ownership != null ? ownership : JADEPrincipal.NONE);
}
/**
Write the AP description in a text file
*/
private void writeAPDescription(APDescription description) {
//Write the APDescription file.
try {
FileWriter f = new FileWriter(getProperty(Profile.FILE_DIR, "") + "APDescription.txt");
f.write(description.toString());
f.write('\n');
f.flush();
f.close();
} catch (java.io.IOException ioe) {
ioe.printStackTrace();
}
}
/**
Return a list of all MTPs in the platform
*/
private List platformMTPs() {
List mtps = new ArrayList();
ContainerID[] cc = myPlatform.containerIDs();
for (int i = 0; i < cc.length; ++i) {
try {
List l = myPlatform.containerMTPs(cc[i]);
Iterator it = l.iterator();
while (it.hasNext()) {
mtps.add(it.next());
}
} catch (NotFoundException nfe) {
// The container has died while we were looping --> ignore it
}
}
return mtps;
}
/**
Store a notification message to be sent at a later time.
Package-scoped as it is called by the AMSJadeAgentManagementBehaviour
*/
void storeNotification(Concept action, Object key, ACLMessage notification) {
if (action instanceof CreateAgent) {
pendingNewAgents.put(key, notification);
}
if (action instanceof KillAgent) {
pendingDeadAgents.put(key, notification);
} else if (action instanceof CloneAction) {
pendingClonedAgents.put(key, notification);
} else if (action instanceof MoveAction) {
pendingMovedAgents.put(key, notification);
} else if (action instanceof KillContainer) {
pendingRemovedContainers.put(key, notification);
}
}
/**
This method is executed by an auxiliary thread started by the AMS
to perform certain actions (createAgent, killContainer) asynchronously
*/
private void sendFailureNotification(final Concept action, final Object key, final FIPAException fe) {
addBehaviour(new OneShotBehaviour(this) {
public void action() {
ACLMessage notification = null;
if (action instanceof CreateAgent) {
notification = (ACLMessage) pendingNewAgents.remove(key);
} else if (action instanceof KillContainer) {
notification = (ACLMessage) pendingRemovedContainers.remove(key);
}
if (notification != null) {
notification.setPerformative(ACLMessage.FAILURE);
// Compose the content
Action slAction = new Action(getAID(), action);
ContentElementList cel = new ContentElementList();
cel.add(slAction);
cel.add(fe);
try {
getContentManager().fillContent(notification, cel);
} catch (Exception e) {
// Should never happen
e.printStackTrace();
notification.setContent("(" + fe.getMessage() + ")");
}
send(notification);
}
}
});
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -