|JADE+Web Service| Communication entre un agent et un web service
Bonjour,
Le but : faire comunicer un web service et un agent.
1. on donne une valeur en paramètre du web service
2. on envoit cette valeur a l'agent
3. l'agent fait ces traitement
4. on remonte les résultat de l'agent au web service
Le problème:
Mon problème lorsque j'arrive a envoyer est que mon agent recoive le message la page du web service tourne a l'infini
et je n'arrive pas a récupérer la réponce de l'agent.
Et lorsque la page charge correctement mon agent ne recoi pas le message.
Cela fait maintenant une semeine que je bloque sur ce problème et que je n'ai toujours aucune piste pour la solution.
Voici un de mes essai:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
package lu.list.webservice;
import java.util.Vector;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import jade.content.onto.basic.Result;
import jade.core.AID;
import jade.core.Profile;
import jade.domain.FIPANames;
import jade.domain.JADEAgentManagement.JADEManagementOntology;
import jade.lang.acl.ACLMessage;
import jade.proto.AchieveREInitiator;
import jade.util.leap.Properties;
import jade.wrapper.gateway.JadeGateway;
@RestController
public class GreetingController {
private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();
@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
//gateway
Properties pp = new Properties();
pp.setProperty(Profile.MAIN_HOST, "localhost");
pp.setProperty(Profile.MAIN_PORT, "1099");
JadeGateway.init(null, pp);
try {
MainContainerAgentsRetriever retriever = new MainContainerAgentsRetriever(name);
JadeGateway.execute(retriever);
}
catch (Exception e) {
e.printStackTrace();
}
return new Greeting(counter.incrementAndGet(), String.format(template, name));
}
private static class MainContainerAgentsRetriever extends AchieveREInitiator {
private static final long serialVersionUID = 1L;
private String contenue;
private String reply;
public MainContainerAgentsRetriever(String name) {
super(null, null);
this.contenue=name;
}
public void onStart() {
super.onStart();
}
@Override
protected Vector<ACLMessage> prepareRequests(ACLMessage initialMsg) {
Vector<ACLMessage> v = null;
System.out.println("initialMsg : "+initialMsg);
ACLMessage request = new ACLMessage(ACLMessage.REQUEST);
request.addReceiver(new AID("m" , AID.ISLOCALNAME));
request.setOntology(JADEManagementOntology.getInstance().getName());
request.setLanguage(FIPANames.ContentLanguage.FIPA_SL);
request.setContent(contenue);
System.out.println("request : "+request);
try {
v = new Vector<ACLMessage>(1);
v.add(request);
}
catch (Exception e) {
e.printStackTrace();
}
return v;
}
@Override
protected void handleInform(ACLMessage inform) {
try {
Result result = (Result) myAgent.getContentManager().extractContent(inform);
reply = (String) result.getValue();
System.out.println("reply : " + reply);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
} |
Solution a mes problèmes de comunication
Bonjour,
J'ai enfin trouver une solution pour mes problèmes de communication ( seulement ceux informatiques (pas pour ceux humain)).
Voici ma solution:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
package lu.list.webservice;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import jade.core.AID;
import jade.core.Profile;
import jade.core.behaviours.SimpleBehaviour;
import jade.domain.FIPANames;
import jade.domain.JADEAgentManagement.JADEManagementOntology;
import jade.lang.acl.ACLMessage;
import jade.util.leap.Properties;
import jade.wrapper.ControllerException;
import jade.wrapper.StaleProxyException;
import jade.wrapper.gateway.JadeGateway;
@RestController
public class GreetingController {
private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();
@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
String content = "";
Properties pp = new Properties();
pp.setProperty(Profile.MAIN_HOST, "localhost");
pp.setProperty(Profile.MAIN_PORT, "1099");
JadeGateway.init(null, pp);
try {
MyBehaviour beha = new MyBehaviour(name);
JadeGateway.execute(beha);
content = beha.getContent();
} catch (StaleProxyException e) {e.printStackTrace();
} catch (ControllerException e) {e.printStackTrace();
} catch (InterruptedException e) {e.printStackTrace();}
return new Greeting(counter.incrementAndGet(), String.format(template, content));
}
class MyBehaviour extends SimpleBehaviour{
private static final long serialVersionUID = 1L;
private boolean finished = false;
private String content="";
public MyBehaviour(String name) {
this.content=name;
}
public String getContent() { return content; }
public void setContent(String content) { this.content = content; }
public void onStart() {
ACLMessage request = new ACLMessage(ACLMessage.REQUEST);
request.addReceiver(new AID("m" , AID.ISLOCALNAME));
request.setOntology(JADEManagementOntology.getInstance().getName());
request.setLanguage(FIPANames.ContentLanguage.FIPA_SL);
request.setContent(content);
myAgent.send(request);
}
public void action() {
ACLMessage response = myAgent.receive();
if (response != null) {
setContent(response.getContent());
finished= true;
}
else { block(); }
}
public boolean done() { return finished; }
}
} |