zibrahim
2014-07-23 14:49:06 UTC
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.
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.