📄 agentmanager.java
字号:
package SOMA.agent.mobility;
import SOMA.agent.*;
import SOMA.agent.classLoading.*;
import SOMA.naming.*;
import SOMA.explorer.*;
import SOMA.Environment;
import SOMA.utility.IndexHashtable;
import java.util.*;
import java.io.*;
import java.io.PrintStream;
/** Gestore degli {@link SOMA.agent.Agent agenti} di un {@link SOMA.Environment place}.
*
* @author Livio Profiri
*/
public class AgentManager
{
Environment env;
AgentSystem agentSystem;
public ClassManager agentClassManager;
public ClassManager cacheClassManager;
/** Timeout standard: 1 minuto. */
//public WaitAndTimeoutStore waitAndTimeoutStore = new WaitAndTimeoutStore( 60000, 0 );
public IndexHashtable indexStore = new IndexHashtable();
StringExplorerItem path, cache;
public int AgentIDCounter = 1;
/** Memorizza i {@link SOMA.agent.AgentWorker worker}
* degli {@link SOMA.agent.Agent agenti}.
*/
public AgentWorkerStore agentWorkerStore = new AgentWorkerStore();
public AgentPositionStore agentPositionStore = new AgentPositionStore();
//Hashtable agentPosition = new Hashtable();
DirExplorerItem agentManagerDir;
/** Numero massimo di tentativi di ricerca di un agente
* per recapitargli un messaggio.
*/
public final int MAX_MESSAGE_ATTEMPTS = 6;
public AgentManager ()
{
}
/** Costruttore.
* @param env L'environment del place.
*/
public AgentManager( Environment env )
{
this.env = env;
agentSystem = new AgentSystem( env );
agentManagerDir = new DirExplorerItem( "agentManager" );
env.dir.addItem( agentManagerDir );
// Entrambe NON creano la directory se non esiste gia'
try
{
agentClassManager = new ClassManager( System.getProperty( "user.dir" ) + File.separator + "agents", false );
cacheClassManager = new ClassManager( System.getProperty( "user.dir" ) + File.separator + "cache", false );
}
catch( Exception e )
{
e.printStackTrace( env.err );
}
path = new ClassManagerExplorerItem( agentClassManager )
{
public void update( ClassManager classManager )
{
agentClassManager = classManager;
}
};
cache = new ClassManagerExplorerItem( cacheClassManager )
{
public void update( ClassManager classManager )
{
cacheClassManager = classManager;
}
};
agentManagerDir.addItem( "path", path );
agentManagerDir.addItem( "cache", cache );
agentManagerDir.addItem( "store", new ObjectExplorerItem( indexStore ) );
agentManagerDir.addItem( "list", new ExplorerItem( "List of agent workers" )
{
public Object Execute( Collection Parameters, PrintStream out )
{
agentWorkerStore.printWorkers( out );
return null;
}
});
agentManagerDir.addItem( "a" , new AgentWorkerExplorerItem( env ) );
agentManagerDir.addItem( "pos" , new ExplorerItem( "List of agents positions." )
{
public Object Execute( Collection Parameters, PrintStream out )
{
agentPositionStore.printPositions( out );
return null;
}
});
agentManagerDir.addItem( "death", new ExplorerItem( "\"Agent ID\"" )
{
public Object Execute( Collection Parameters, PrintStream out )
{
try
{
agentDeath( new AgentID( (String)Parameters.iterator().next() ) );
out.println( "DONE!" );
}
catch( Exception e )
{
e.printStackTrace();
}
return null;
}
public String Help( PrintStream out )
{
out.println( "Notifies an agent's death to his home place." );
out.println( " Use it only if the agent is really dead!" );
return "Notifies an agent's death to his home place.";
}
});
agentManagerDir.addItem( "save", new ExplorerItem( "Save agents to disk." )
{
public Object Execute( java.util.Collection Parameters, PrintStream out )
{
try
{
save();
out.println( "Saved!" );
}
catch( Exception e )
{
e.printStackTrace( out );
}
return null;
}
} );
agentManagerDir.addItem( "load", new ExplorerItem( "Load agents from disk." )
{
public Object Execute( java.util.Collection Parameters, PrintStream out )
{
try
{
load();
out.println( "Loaded!" );
}
catch( Exception e )
{
e.printStackTrace( out );
}
return null;
}
} );
/*
agentManagerDir.addItem( "launchSys" , new ExplorerItem( "<Agent> { <Arg1> <Arg2> ... }" )
{
public Object Execute( Collection Parameters, PrintStream out )
{
int NumParameters = Parameters.size();
if( NumParameters > 0 )
{
Iterator i = Parameters.iterator();
String agentName = (String)i.next();
String[] ParametersArray = new String[NumParameters - 1];
out.println( "Creating agent: " + agentName + " Parameters: " );
for( int j = 0; j < NumParameters - 1; j++ )
{
ParametersArray[j] = (String)i.next();
out.println( " " + j + ") " + ParametersArray[j] );
}
// true --> System agent!
AgentID created = createAgent( agentName, ParametersArray, true, true );
if( created == null )
out.println( "ERROR: can't create agent." );
return created;
}
out.println( "incorrect number of parameters" );
return null;
}
});
agentManagerDir.addItem( "launchNoTra" , new ExplorerItem( "<Agent> { <Arg1> <Arg2> ... }" )
{
public Object Execute( Collection Parameters, PrintStream out )
{
int NumParameters = Parameters.size();
if( NumParameters > 0 )
{
Iterator i = Parameters.iterator();
String agentName = (String)i.next();
String[] ParametersArray = new String[NumParameters - 1];
out.println( "Creating agent: " + agentName + " Parameters: " );
for( int j = 0; j < NumParameters - 1; j++ )
{
ParametersArray[j] = (String)i.next();
out.println( " " + j + ") " + ParametersArray[j] );
}
// false --> System agent!
// false --> Not traceable!
AgentID created = createAgent( agentName, ParametersArray, false, false );
if( created == null )
out.println( "ERROR: can't create agent." );
return created;
}
out.println( "incorrect number of parameters" );
return null;
}
});
ExplorerItem launch = new ExplorerItem( "<Agent> { <Arg1> <Arg2> ... }" )
{
public Object Execute( Collection Parameters, PrintStream out )
{
int NumParameters = Parameters.size();
if( NumParameters > 0 )
{
Iterator i = Parameters.iterator();
String agentName = (String)i.next();
String[] ParametersArray = new String[NumParameters - 1];
out.println( "Creating agent: " + agentName + " Parameters: " );
for( int j = 0; j < NumParameters - 1; j++ )
{
ParametersArray[j] = (String)i.next();
out.println( " " + j + ") " + ParametersArray[j] );
}
// false --> NON System agent!
AgentID created = createAgent( agentName, ParametersArray, false, true );
if( created == null )
out.println( "ERROR: can't create agent." );
return created;
}
out.println( "incorrect number of parameters" );
return null;
}
};
*/
ExplorerItem launch = new AgentLauncherExplorerItem( env );
agentManagerDir.addItem( "launch", launch );
env.dir.addItem( "launch", launch );
}
/** Creazione di un agente di sistema.
*
* @param agentName Nome dell'agente.
* @param argument Parametro di inizializxzazione, vedi
* {@link SOMA.agent.Agent#putArgument( Object obj )}.
*/
/*
public AgentID createSystemAgent( String agentName, Object argument )
{
AgentID returnID = null;
try
{
Agent agent = (Agent)Class.forName( agentName ).newInstance();
agent.setID( newAgentID() );
agent.putArgument( argument );
//env.out.println( "AgentManager.createAgent: agent " + agent + "CREATED" );
returnID = startAgent( agent );
}
catch( Exception e )
{
e.printStackTrace( env.err );
}
return returnID;
}*/
/** Creazione di un agente.
*
* @param agentName Nome dell'agente.
* @param argument Parametro di inizializzazione, vedi
* {@link SOMA.agent.Agent#putArgument( Object obj )}.
* @param isSystemAgent Se a true si forza l'utilizzo del classloader di sistema
* @param traceable Se a true l'agente ha un
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -