Discussion:
[jade-develop] Jade Gateway - Communicating back from Agent to Servlet
zibrahim
2014-07-23 14:49:06 UTC
Permalink
Hello everyone,

After seeing many posts on Jade Gateway, I finally started adding it to my
project. Following Giovanni's advice to several others (and after I failed
to get Keleman's example to work), I decided to use simple communication
messages to implement the functionality. I will first post the relevant
pieces of code as I have them then will tell you about my problem.


Servlet:
-------
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

pin = (String) req.getAttribute("pin");

MainContainerAgentsRetriever retriever;

try {
retriever = new MainContainerAgentsRetriever();
JadeGateway.execute(retriever);
List agents = retriever.getAgents();
if (agents != null) {
for (int i = 0; i < agents.size(); ++i) {
System.out.println("- "+ ((AID) agents.get(i)).getLocalName());
if((((AID) agents.get(i)).getLocalName()).equals("gatewayAgent")){
pin = (String) req.getAttribute("pin");
final ACLMessage msg = new ACLMessage();
msg.addReceiver(((AID) agents.get(i)));
msg.setContent(pin);
msg.setPerformative(ACLMessage.REQUEST);
JadeGateway.execute(new OneShotBehaviour() {
public void action() {
myAgent.send(msg);
MessageTemplate mt =
MessageTemplate.MatchPerformative(
ACLMessage.INFORM);
ACLMessage msg = myAgent.receive();
while(msg == null){
}


}
} );

}
}
}
}
catch (Exception e) {
e.printStackTrace();
}


GatewayAgent:
---------------
public void setup()
{
System.out.println(" HIIII i'm gateway Initialised by backbone ");
// Waiting for the answer

addBehaviour(new CyclicBehaviour(this)
{
public void action() {
ACLMessage msg = receive();
System.out.println(" listening ");
if(msg != null){
int patient = Integer.parseInt(msg.getContent());

ACLMessage request = new ACLMessage(ACLMessage.REQUEST);

int next_sdq = Schedule.getInstance().nextSDQ(patient);
boolean due_now = Schedule.getInstance().sdqDue(patient);

String reply_txt = next_sdq + " "+due_now;
//replying to servlet!!!


ACLMessage reply = request.createReply();
reply.setPerformative(ACLMessage.INFORM);
reply.setContent(reply_txt);

send(reply);

releaseCommand(this);
}
else block();
}
});

super.setup();
}

}



My Problem:
-----------
The message gets sent from the servlet to the agent. The agent receives the
value (patient) and it's valid and okay.

My problem is the communication back from the agent to the servlet. It never
happens! In the agent code, here's the relevant snip:


ACLMessage reply = request.createReply();
reply.setPerformative(ACLMessage.INFORM);
reply.setContent(reply_txt);

And in the servlet, here's the relevant code:

myAgent.send(msg);
MessageTemplate mt = MessageTemplate.MatchPerformative(ACLMessage.INFORM);
ACLMessage msg = myAgent.receive();
while(msg == null){
}


The problem is that the serlvet never gets the message back! (i.e. the while
loop will always be true) and the servlet freezes. Everything's running on
loca host. Any help will be more than appreciated. Thank you in advance.





--
View this message in context: http://jade.17737.x6.nabble.com/Jade-Gateway-Communicating-back-from-Agent-to-Servlet-tp5002656.html
Sent from the JADE - Dev mailing list archive at Nabble.com.
Caire Giovanni
2014-07-24 21:32:22 UTC
Permalink
Hi,

Pay attention:
myAgent.send(msg);
MessageTemplate mt = MessageTemplate.MatchPerformative(
ACLMessage.INFORM);
ACLMessage msg = myAgent.receive();
while(msg == null){
}

This code does not work: receive() is called just after send --> For sure the reply is not yet there --> it returns null. Then
while(msg == null){
} }
loops forever.

The right code should be something like

jadeGateway.execute(new SimpleBehaviour() {
private boolean finished = false;
public void onStart() {
// Send the message
}

public void action() {
ACLMessage response = myAgent.receive(template);
if (response != null) {
// Response got!
finished= true;
}
else {
block();
}
}

public Boolean done() {
return finished;
}
});

bye,

Giovanni


-----Messaggio originale-----
Da: jade-develop [mailto:jade-develop-bounces at avalon.tilab.com] Per conto di zibrahim
Inviato: mercoledì 23 luglio 2014 16:49
A: jade-develop at avalon.tilab.com
Oggetto: [jade-develop] Jade Gateway - Communicating back from Agent to Servlet

Hello everyone,

After seeing many posts on Jade Gateway, I finally started adding it to my project. Following Giovanni's advice to several others (and after I failed to get Keleman's example to work), I decided to use simple communication messages to implement the functionality. I will first post the relevant pieces of code as I have them then will tell you about my problem.


Servlet:
-------
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

pin = (String) req.getAttribute("pin");

MainContainerAgentsRetriever retriever;

try {
retriever = new MainContainerAgentsRetriever();
JadeGateway.execute(retriever);
List agents = retriever.getAgents();
if (agents != null) {
for (int i = 0; i < agents.size(); ++i) {
System.out.println("- "+ ((AID) agents.get(i)).getLocalName());
if((((AID) agents.get(i)).getLocalName()).equals("gatewayAgent")){
pin = (String) req.getAttribute("pin");
final ACLMessage msg = new ACLMessage();
msg.addReceiver(((AID) agents.get(i)));
msg.setContent(pin);
msg.setPerformative(ACLMessage.REQUEST);
JadeGateway.execute(new OneShotBehaviour() {
public void action() {
myAgent.send(msg);
MessageTemplate mt =
MessageTemplate.MatchPerformative(
ACLMessage.INFORM);
ACLMessage msg = myAgent.receive();
while(msg == null){
}


}
} );

}
}
}
}
catch (Exception e) {
e.printStackTrace();
}


GatewayAgent:
---------------
public void setup()
{
System.out.println(" HIIII i'm gateway Initialised by backbone ");
// Waiting for the answer

addBehaviour(new CyclicBehaviour(this)
{
public void action() {
ACLMessage msg = receive();
System.out.println(" listening ");
if(msg != null){
int patient = Integer.parseInt(msg.getContent());

ACLMessage request = new ACLMessage(ACLMessage.REQUEST);

int next_sdq = Schedule.getInstance().nextSDQ(patient);
boolean due_now = Schedule.getInstance().sdqDue(patient);

String reply_txt = next_sdq + " "+due_now;
//replying to servlet!!!


ACLMessage reply = request.createReply();
reply.setPerformative(ACLMessage.INFORM);
reply.setContent(reply_txt);

send(reply);

releaseCommand(this);
}
else block();
}
});

super.setup();
}

}



My Problem:
-----------
The message gets sent from the servlet to the agent. The agent receives the value (patient) and it's valid and okay.

My problem is the communication back from the agent to the servlet. It never happens! In the agent code, here's the relevant snip:


ACLMessage reply = request.createReply();
reply.setPerformative(ACLMessage.INFORM);
reply.setContent(reply_txt);

And in the servlet, here's the relevant code:

myAgent.send(msg);
MessageTemplate mt = MessageTemplate.MatchPerformative(ACLMessage.INFORM);
ACLMessage msg = myAgent.receive();
while(msg == null){
}


The problem is that the serlvet never gets the message back! (i.e. the while loop will always be true) and the servlet freezes. Everything's running on loca host. Any help will be more than appreciated. Thank you in advance.





--
View this message in context: http://jade.17737.x6.nabble.com/Jade-Gateway-Communicating-back-from-Agent-to-Servlet-tp5002656.html
Sent from the JADE - Dev mailing list archive at Nabble.com.
_______________________________________________
jade-develop mailing list
jade-develop at avalon.tilab.com
http://jade.tilab.com/mailman/listinfo/jade-develop
UNSUBSCRIBE INSTRUCTIONS AT
http://jade.tilab.com/community-mailinglist.htm

Loading...