agentmanagementservice.java
来自「JADE(JAVA Agent开发框架)是一个完全由JAVA语言开发的软件,它简」· Java 代码 · 共 966 行 · 第 1/3 页
JAVA
966 行
try {
mainSlice.deadAgent(target, cmd);
}
catch(IMTPException imtpe) {
// Try to get a newer slice and repeat...
mainSlice = (AgentManagementSlice)getFreshSlice(MAIN_SLICE);
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
String jarName = className.replace('.', '_') + ".jar";
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();
}
}
}
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);
}
}
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();
myContainer.releaseLocalAgent(agentID);
}
private void changeAgentState(AID agentID, int newState) throws IMTPException, NotFoundException {
Agent a = myContainer.acquireLocalAgent(agentID);
if(a == null)
throw new NotFoundException("Change-Agent-State failed to find " + agentID);
if(newState == Agent.AP_SUSPENDED) {
a.doSuspend();
}
else if(newState == Agent.AP_WAITING) {
a.doWait();
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?