📄 agentmanagementservice.java
字号:
mainSlice.deadAgent(target, cmd);
}
}
private void handleInformStateChanged(VerticalCommand cmd) {
Object[] params = cmd.getParams();
AID target = (AID)params[0];
AgentState from = (AgentState)params[1];
AgentState to = (AgentState)params[2];
if (to.equals(jade.domain.FIPAAgentManagement.AMSAgentDescription.SUSPENDED)) {
try {
// Notify the main container through its slice
AgentManagementSlice mainSlice = (AgentManagementSlice)getSlice(MAIN_SLICE);
try {
mainSlice.suspendedAgent(target);
}
catch(IMTPException imtpe) {
// Try to get a newer slice and repeat...
mainSlice = (AgentManagementSlice)getFreshSlice(MAIN_SLICE);
mainSlice.suspendedAgent(target);
}
}
catch(IMTPException re) {
re.printStackTrace();
}
catch(NotFoundException nfe) {
nfe.printStackTrace();
}
catch(ServiceException se) {
se.printStackTrace();
}
}
else if (from.equals(jade.domain.FIPAAgentManagement.AMSAgentDescription.SUSPENDED)) {
try {
// Notify the main container through its slice
AgentManagementSlice mainSlice = (AgentManagementSlice)getSlice(MAIN_SLICE);
try {
mainSlice.resumedAgent(target);
}
catch(IMTPException imtpe) {
// Try to get a newer slice and repeat...
mainSlice = (AgentManagementSlice)getFreshSlice(MAIN_SLICE);
mainSlice.resumedAgent(target);
}
}
catch(IMTPException re) {
re.printStackTrace();
}
catch(NotFoundException nfe) {
nfe.printStackTrace();
}
catch(ServiceException se) {
se.printStackTrace();
}
}
}
private void handleKillContainer(VerticalCommand cmd) throws IMTPException, ServiceException, NotFoundException {
Object[] params = cmd.getParams();
ContainerID cid = (ContainerID)params[0];
if(myLogger.isLoggable(Logger.CONFIG)) {
myLogger.log(Logger.CONFIG,"Source Sink consuming command KILL_CONTAINER. Container is "+cid.getName());
}
// Forward to the correct slice
AgentManagementSlice targetSlice = (AgentManagementSlice)getSlice(cid.getName());
try {
try {
targetSlice.exitContainer();
}
catch(IMTPException imtpe) {
// Try to get a newer slice and repeat...
targetSlice = (AgentManagementSlice)getFreshSlice(cid.getName());
targetSlice.exitContainer();
}
}
catch (NullPointerException npe) {
// targetSlice not found --> The container does not exist
throw new NotFoundException("Container "+cid.getName()+" not found");
}
}
private void handleAddTool(VerticalCommand cmd) {
Object[] params = cmd.getParams();
AID tool = (AID)params[0];
MainContainer impl = myContainer.getMain();
if(impl != null) {
impl.toolAdded(tool);
}
else {
// Do nothing for now, but could also route the command to the main slice, thus enabling e.g. AMS replication
}
}
private void handleRemoveTool(VerticalCommand cmd) {
Object[] params = cmd.getParams();
AID tool = (AID)params[0];
MainContainer impl = myContainer.getMain();
if(impl != null) {
impl.toolRemoved(tool);
}
else {
// Do nothing for now, but could also route the command to the main slice, thus enabling e.g. AMS replication
}
}
} // End of CommandSourceSink class
private class CommandTargetSink implements Sink {
public void consume(VerticalCommand cmd) {
try {
String name = cmd.getName();
if(name.equals(AgentManagementSlice.REQUEST_CREATE)) {
handleRequestCreate(cmd);
}
else if(name.equals(AgentManagementSlice.REQUEST_KILL)) {
handleRequestKill(cmd);
}
else if(name.equals(AgentManagementSlice.REQUEST_STATE_CHANGE)) {
handleRequestStateChange(cmd);
}
else if(name.equals(AgentManagementSlice.INFORM_KILLED)) {
handleInformKilled(cmd);
}
else if(name.equals(AgentManagementSlice.INFORM_STATE_CHANGED)) {
handleInformStateChanged(cmd);
}
else if(name.equals(AgentManagementSlice.INFORM_CREATED)) {
handleInformCreated(cmd);
}
else if(name.equals(AgentManagementSlice.KILL_CONTAINER)) {
handleKillContainer(cmd);
}
}
catch (Throwable t) {
cmd.setReturnValue(t);
}
}
// Vertical command handler methods
private void handleRequestCreate(VerticalCommand cmd) throws IMTPException, JADESecurityException, NotFoundException, NameClashException, ServiceException {
Object[] params = cmd.getParams();
AID agentID = (AID)params[0];
String className = (String)params[1];
Object[] arguments = (Object[])params[2];
JADEPrincipal owner = (JADEPrincipal)params[3];
Credentials initialCredentials = (Credentials)params[4];
boolean startIt = ((Boolean) params[5]).booleanValue();
//log("Target sink consuming command REQUEST_CREATE: Name is "+agentID.getName(), 2);
if(myLogger.isLoggable(Logger.FINE))
myLogger.log(Logger.FINE,"Target sink consuming command REQUEST_CREATE: Name is "+agentID.getName());
createAgent(agentID, className, arguments, owner, initialCredentials, startIt);
}
private void handleRequestKill(VerticalCommand cmd) throws IMTPException, JADESecurityException, NotFoundException, ServiceException {
Object[] params = cmd.getParams();
AID agentID = (AID)params[0];
//log("Target sink consuming command REQUEST_KILL: Name is "+agentID.getName(), 2);
if(myLogger.isLoggable(Logger.FINE))
myLogger.log(Logger.FINE,"Target sink consuming command REQUEST_KILL: Name is "+agentID.getName());
killAgent(agentID);
}
private void handleRequestStateChange(VerticalCommand cmd) throws IMTPException, JADESecurityException, NotFoundException, ServiceException {
Object[] params = cmd.getParams();
AID agentID = (AID)params[0];
int newState = ((Integer)params[1]).intValue();
changeAgentState(agentID, newState);
}
private void handleInformCreated(VerticalCommand cmd) throws NotFoundException, NameClashException, ServiceException {
Object[] params = cmd.getParams();
AID agentID = (AID)params[0];
ContainerID cid = (ContainerID)params[1];
if(myLogger.isLoggable(Logger.FINE)) {
myLogger.log(Logger.FINE,"Target sink consuming command INFORM_CREATED: Name is "+agentID.getName());
}
bornAgent(agentID, cid, cmd.getPrincipal(), cmd.getCredentials());
}
private void handleInformKilled(VerticalCommand cmd) throws NotFoundException, ServiceException {
Object[] params = cmd.getParams();
AID agentID = (AID)params[0];
//log("Target sink consuming command INFORM_KILLED: Name is "+agentID.getName(), 2);
if(myLogger.isLoggable(Logger.FINE))
myLogger.log(Logger.FINE,"Target sink consuming command INFORM_KILLED: Name is "+agentID.getName());
deadAgent(agentID);
}
private void handleInformStateChanged(VerticalCommand cmd) throws NotFoundException {
Object[] params = cmd.getParams();
AID agentID = (AID)params[0];
String newState = (String)params[1];
String oldState = (String)params[2];
if (newState.equals(jade.domain.FIPAAgentManagement.AMSAgentDescription.SUSPENDED)) {
suspendedAgent(agentID);
}
else if(newState.equals(jade.domain.FIPAAgentManagement.AMSAgentDescription.ACTIVE)) {
resumedAgent(agentID);
}
}
private void handleKillContainer(VerticalCommand cmd) {
if(myLogger.isLoggable(Logger.FINE)) {
myLogger.log(Logger.FINE,"Target sink consuming command KILL_CONTAINER");
}
exitContainer();
}
private void createAgent(AID agentID, String className, Object arguments[], JADEPrincipal owner, Credentials initialCredentials, boolean startIt) throws IMTPException, NotFoundException, NameClashException, JADESecurityException {
Agent agent = null;
try {
//#J2ME_EXCLUDE_BEGIN
Properties pp = getClassProperties(className);
String jarName = pp.getProperty(CLASS_CODE);
className = pp.getProperty(CLASS_NAME);
boolean warnIfJarNotFound = true;
if (jarName == null) {
jarName = className.replace('.', '_') + ".jar";
warnIfJarNotFound = false;
}
jarName = agentsPath + File.separator + jarName;
File file = new File(jarName);
try {
if (file.exists()) {
JarClassLoader loader = new JarClassLoader(file, getClass().getClassLoader());
agent = (Agent) Class.forName(className, true, loader).newInstance();
try {
codeLocator.registerAgent(agentID, loader);
}
catch (Exception e) {
myLogger.log(Logger.WARNING, "Error registering jar file "+file.getPath()+" to CodeLocator. Agent "+agentID.getName()+" may not be able to move properly");
e.printStackTrace();
}
}
else if (warnIfJarNotFound) {
myLogger.log(Logger.WARNING, "Jar file "+jarName+" for class "+className+" does not exist");
}
}
catch (IOException ioe) {
myLogger.log(Logger.WARNING, "File "+file.getPath()+" is not a valid Jar file. Try to laod agent "+agentID.getName()+" from the classpath");
}
//#J2ME_EXCLUDE_END
if (agent == null) {
agent = (Agent)Class.forName(new String(className)).newInstance();
}
agent.setArguments(arguments);
AID oldAgentID = agentID;
myContainer.initAgent(agentID, agent, owner, initialCredentials);
//#J2ME_EXCLUDE_BEGIN
// initAgent() may change the agent name.
codeLocator.changeAgentName(oldAgentID, agentID);
//#J2ME_EXCLUDE_END
if (startIt) {
myContainer.powerUpLocalAgent(agentID);
}
}
catch(ClassNotFoundException cnfe) {
throw new IMTPException("Class " + className + " for agent " + agentID + " not found", cnfe);
}
catch(InstantiationException ie) {
throw new IMTPException("Class " + className + " for agent " + agentID + " cannot be instantiated", ie);
}
catch(IllegalAccessException iae) {
throw new IMTPException("Illegal access exception in createAgent()", iae);
}
}
//#J2ME_EXCLUDE_BEGIN
/**
* A class specification may have the form foo.bar[key1=value1;key2=value2...]
* Parse it and fill a Properties object
*/
private Properties getClassProperties(String str) {
Properties pp = new Properties();
int index = str.indexOf('[');
if (index < 0) {
pp.setProperty(CLASS_NAME, str);
}
else {
pp.setProperty(CLASS_NAME, str.substring(0, index));
int index1 = str.indexOf(']');
String propsStr = str.substring(index+1, index1);
Vector propsList = Specifier.parseList(propsStr, ';');
for (int i = 0; i < propsList.size(); ++i) {
String ps = (String) propsList.get(i);
int k = ps.indexOf('=');
if (k > 0) {
String name = ps.substring(0, k);
String value = ps.substring(k+1);
pp.setProperty(name, value);
}
}
}
return pp;
}
//#J2ME_EXCLUDE_END
private void killAgent(AID agentID) throws IMTPException, NotFoundException {
Agent a = myContainer.acquireLocalAgent(agentID);
if(a == null)
throw new NotFoundException("Kill-Agent failed to find " + agentID);
a.doDelete();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -