I have a Sender
that reads from a file and sends each line to a Receiver
agent.
Typically, I want my Receiver
agent to send a confirmation for every line received line and the Sender
Agent is blocked (or waits) until that confirmation is received.
Here is a snap-code of the Sender
agent
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());
}
for recieving the ACLMessages:
if (msg == null) {
block();
return;
}
else {
switch (msg.getPerformative()) {
// Other cases
case ACLMessage.INFORM:
System.out.println("Confirmation");
setRecieved(true);
break;
default:
break;
}
The Receiver agent when it receives the message it sends a confirmation
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);
}
So the sender uses a flag value that helps determine whether a confirmation is received or not. For this, it uses the isRecieved()
method, if it is false, Ideally the agent should be blocked and wait for the confirmation message.
The problem is when I run the code it, the Sender
is blocked when recieved() == false
is true
, but it doesn't resume the while
loop when it receives the confirmation. Am I doing this wrong ?