要實現局域網內不同機器上的Agent進行通信,需要注意這樣兩個問題:
1。防火牆內關於文件與打印機共享的協議必須打開,保證機器名的解析。
2。通信時ACLMessage添加Receiver的時候指定合法的AID,這個AID一要有完整的名稱,二要有合法的Addresses。
比如下面的例子
import jade.core.AID;
import jade.domain.AMSService;
import jade.domain.FIPAAgentManagement.*;
public class Sender2 extends Agent {
protected void setup()
{
AMSAgentDescription [] agents = null;
AID Rams= new AID("ams@gjpc:1099/JADE");//獲取gjpc機器上的ams
Rams.addAddresses("http://gjpc:7778/acc");
try {
SearchConstraints c = new SearchConstraints();
c.setMaxResults (new Long(-1));
//agents = AMSService.search( this, new AMSAgentDescription (), c );
//下面查找rams裡面的agents
agents = AMSService.search( this, Rams, new AMSAgentDescription (), c );
}
catch (Exception e) {
System.out.println( "Problem searching AMS: " + e );
e.printStackTrace();
}
ACLMessage msg = new ACLMessage(ACLMessage.INFORM);
msg.setContent( "this is from guojie 's Ping" );//本地機器名為guojie
//逐個向他們發送消息
for (int i=0; i<agents.length;i++){
if ( agents[i].getName().equals(getAID()) ){
continue;
}
System.out.println(agents[i].getName().toString());
agents[i].getName().addAddresses("http://gjpc:7778/acc");
//這一句必須要有
msg.addReceiver( agents[i].getName() );
}
System.out.println("wait fro a minute...");
send(msg);
}
}