我有一个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;
}
Receiver代理收到消息后会发送确认
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,则理想情况下应阻止该代理并等待确认消息。
问题是我在运行代码时遇到的问题,当Sender
为recieved() == false
时true
被阻止,但是它不会恢复{{ 1}}在收到确认时循环。我做错了吗?
答案 0 :(得分:2)
要阻止代理行为,可以使用多种方法:
receive()
-非阻塞接收,返回队列中的第一条消息;如果队列为空,则返回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);
}
答案 1 :(得分:0)
我将使用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;
}
}