代理之间的确认消息
Confirmation msg between Agents
我有一个 Sender
从文件读取并将每一行发送给 Receiver
代理。
通常,我希望我的 Receiver
代理为接收到的每一行发送确认,并且 Sender
代理被阻止(或等待)直到收到该确认。
这是 Sender
代理的快照代码
private volatile boolean recieved = false;
// Codes and
while ((line = bufferedReader.readLine()) != null) {
// SENDING LINES ...
String Agentname="Sender";
ACLMessage aclMessage= new ACLMessage(ACLMessage.CONFIRM);
aclMessage.addReceiver(new AID(Agentname,AID.ISLOCALNAME));
aclMessage.setContent(line);
send(aclMessage);
if (isRecieved() == false ) { block(); return; }
System.out.println("Value after "+isRecieved());
}
用于接收 ACLMessages:
if (msg == null) {
block();
return;
}
else {
switch (msg.getPerformative()) {
// Other cases
case ACLMessage.INFORM:
System.out.println("Confirmation");
setRecieved(true);
break;
default:
break;
}
接收方代理在收到消息时发送确认
case ACLMessage.CONFIRM:
// Process the results
String result = doNormalize (msg.getContent());
if (result!=null) {
//send the results to the writer
ACLMessage aclMessage= new ACLMessage(ACLMessage.CONFIRM);
aclMessage.addReceiver(new AID("Writer",AID.ISLOCALNAME));
aclMessage.setContent(result);
send(aclMessage);
// Sends a confirmation to using the method sendConfirmation
sendConfirmation(msg);
}
break;
default:
break;
}
// Code ..
// ..
// ..
// ..
public void sendConfirmation(ACLMessage aclMessage) {
ACLMessage message = aclMessage.createReply();
message.setPerformative(ACLMessage.INFORM);
send(message);
}
所以发件人使用标志值来帮助确定是否收到确认。为此,它使用 isRecieved()
方法,如果它是 false,理想情况下应该阻止代理并等待确认消息。
问题是当我运行代码时,recieved() == false
是true
时Sender
被阻塞,但它在收到确认后不会恢复 while
循环。我做错了吗?
我会用 switch 语句来做这个行为,像这样:
public class ReadBehaviour extends Behaviour {
private boolean isDone = false;
private int step = 0;
private BufferedReader bufferedReader;
@Override
public void action() {
switch (step){
case 0:
// TODO initialize your bufferedReader
step++;
break;
case 1:
try {
String line = bufferedReader.readLine();
if(line == null){
isDone = true;
} else {
ACLMessage aclMessage= new ACLMessage(ACLMessage.CONFIRM);
aclMessage.addReceiver(new AID("Sender", AID.ISLOCALNAME));
aclMessage.setContent(line);
myAgent.send(aclMessage);
step++;
}
} catch (IOException e) {
isDone = true;
}
break;
case 2:
if(isRecieved()){
step--;
} else {
block();
}
}
}
@Override
public boolean done() {
return isDone;
}
}
要阻止特工的行为有多种方法:
receive()
- 非阻塞接收,returns 队列中的第一条消息或 null
如果队列为空
receive(MessageTemplate pattern)
- 与前一个行为类似,但您还可以指定消息的模式,例如特定的发件人 AID、会话 ID 以及组合。
blockingReceive()
- 阻塞接收,阻塞代理直到消息出现在队列中
blockingReceive(MessageTemplate pattern)
- 阻塞接收,模式
因此添加 blockingReceive()
或 blockingReceive(MessageTemplate pattern)
会阻止代理 Behaviour
直到收到特定消息。看起来像这样:
while ((line = bufferedReader.readLine()) != null) {
// SENDING EVENT LINES ...
String Agentname="Reciever";
ACLMessage aclMessage= new ACLMessage(ACLMessage.CONFIRM);
aclMessage.addReceiver(new AID(Agentname,AID.ISLOCALNAME));
aclMessage.setContent(line);
send(aclMessage);
// Block an Agent until a confirmation is received..
ACLMessage aclMessage2 = blockingReceive(mt);
}
我有一个 Sender
从文件读取并将每一行发送给 Receiver
代理。
通常,我希望我的 Receiver
代理为接收到的每一行发送确认,并且 Sender
代理被阻止(或等待)直到收到该确认。
这是 Sender
代理的快照代码
private volatile boolean recieved = false;
// Codes and
while ((line = bufferedReader.readLine()) != null) {
// SENDING LINES ...
String Agentname="Sender";
ACLMessage aclMessage= new ACLMessage(ACLMessage.CONFIRM);
aclMessage.addReceiver(new AID(Agentname,AID.ISLOCALNAME));
aclMessage.setContent(line);
send(aclMessage);
if (isRecieved() == false ) { block(); return; }
System.out.println("Value after "+isRecieved());
}
用于接收 ACLMessages:
if (msg == null) {
block();
return;
}
else {
switch (msg.getPerformative()) {
// Other cases
case ACLMessage.INFORM:
System.out.println("Confirmation");
setRecieved(true);
break;
default:
break;
}
接收方代理在收到消息时发送确认
case ACLMessage.CONFIRM:
// Process the results
String result = doNormalize (msg.getContent());
if (result!=null) {
//send the results to the writer
ACLMessage aclMessage= new ACLMessage(ACLMessage.CONFIRM);
aclMessage.addReceiver(new AID("Writer",AID.ISLOCALNAME));
aclMessage.setContent(result);
send(aclMessage);
// Sends a confirmation to using the method sendConfirmation
sendConfirmation(msg);
}
break;
default:
break;
}
// Code ..
// ..
// ..
// ..
public void sendConfirmation(ACLMessage aclMessage) {
ACLMessage message = aclMessage.createReply();
message.setPerformative(ACLMessage.INFORM);
send(message);
}
所以发件人使用标志值来帮助确定是否收到确认。为此,它使用 isRecieved()
方法,如果它是 false,理想情况下应该阻止代理并等待确认消息。
问题是当我运行代码时,recieved() == false
是true
时Sender
被阻塞,但它在收到确认后不会恢复 while
循环。我做错了吗?
我会用 switch 语句来做这个行为,像这样:
public class ReadBehaviour extends Behaviour {
private boolean isDone = false;
private int step = 0;
private BufferedReader bufferedReader;
@Override
public void action() {
switch (step){
case 0:
// TODO initialize your bufferedReader
step++;
break;
case 1:
try {
String line = bufferedReader.readLine();
if(line == null){
isDone = true;
} else {
ACLMessage aclMessage= new ACLMessage(ACLMessage.CONFIRM);
aclMessage.addReceiver(new AID("Sender", AID.ISLOCALNAME));
aclMessage.setContent(line);
myAgent.send(aclMessage);
step++;
}
} catch (IOException e) {
isDone = true;
}
break;
case 2:
if(isRecieved()){
step--;
} else {
block();
}
}
}
@Override
public boolean done() {
return isDone;
}
}
要阻止特工的行为有多种方法:
receive()
- 非阻塞接收,returns 队列中的第一条消息或null
如果队列为空receive(MessageTemplate pattern)
- 与前一个行为类似,但您还可以指定消息的模式,例如特定的发件人 AID、会话 ID 以及组合。blockingReceive()
- 阻塞接收,阻塞代理直到消息出现在队列中blockingReceive(MessageTemplate pattern)
- 阻塞接收,模式
因此添加 blockingReceive()
或 blockingReceive(MessageTemplate pattern)
会阻止代理 Behaviour
直到收到特定消息。看起来像这样:
while ((line = bufferedReader.readLine()) != null) {
// SENDING EVENT LINES ...
String Agentname="Reciever";
ACLMessage aclMessage= new ACLMessage(ACLMessage.CONFIRM);
aclMessage.addReceiver(new AID(Agentname,AID.ISLOCALNAME));
aclMessage.setContent(line);
send(aclMessage);
// Block an Agent until a confirmation is received..
ACLMessage aclMessage2 = blockingReceive(mt);
}