Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

DialogueHelper improvements #20

Merged
merged 3 commits into from

1 participant

@esteban-aliverti
Collaborator

No description provided.

esteban-aliverti added some commits
@esteban-aliverti esteban-aliverti DialogueHelper:
	- Implemented invokeQueryRef()
9f149f6
@esteban-aliverti esteban-aliverti DialogueHelper:
	- Added more logs
	- TimeoutException if the responses don't arrive
763b81e
@esteban-aliverti esteban-aliverti - Improved Messages ids for concurrent scenarios f5d6141
@esteban-aliverti esteban-aliverti merged commit 3692219 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 20, 2012
  1. @esteban-aliverti

    DialogueHelper:

    esteban-aliverti authored
    	- Implemented invokeQueryRef()
  2. @esteban-aliverti

    DialogueHelper:

    esteban-aliverti authored
    	- Added more logs
    	- TimeoutException if the responses don't arrive
  3. @esteban-aliverti
This page is out of date. Refresh to see the latest.
View
44 drools-mas-generic-client/src/main/java/org/drools/mas/helpers/DialogueHelper.java
@@ -10,6 +10,7 @@
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
+import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.namespace.QName;
@@ -21,6 +22,7 @@
import org.drools.mas.body.acts.Inform;
import org.drools.mas.body.acts.InformIf;
import org.drools.mas.body.content.Action;
+import org.drools.mas.body.content.Query;
import org.drools.mas.util.ACLMessageFactory;
import org.drools.mas.util.MessageContentEncoder;
import org.drools.mas.util.MessageContentFactory;
@@ -215,6 +217,22 @@ protected String doQueryIf(String sender, String receiver, Object proposition, D
return qryif.getId();
}
+
+
+ public String invokeQueryRef(String sender, String receiver, Query query, DialogueHelperCallback callback) {
+ return this.doQueryRef(sender, receiver, query, callback);
+ }
+
+ protected String doQueryRef(String sender, String receiver, Query query, DialogueHelperCallback callback) {
+
+ ACLMessageFactory factory = new ACLMessageFactory(Encodings.XML);
+ ACLMessage qryRef = factory.newQueryRefMessage(sender, receiver, query);
+
+ this.tell(qryRef, callback, callback != null);
+
+ return qryRef.getId();
+
+ }
public String invokeInform(String sender, String receiver, Object proposition, DialogueHelperCallback callback) {
return this.doInform(sender, receiver, proposition, callback);
@@ -376,18 +394,19 @@ private AsyncDroolsAgentService getAsyncDroolsAgentService(){
}
protected void tell(final ACLMessage message, DialogueHelperCallback callback, final boolean waitForAnswers){
-
final DialogueHelperCallback finalCallback = callback != null? callback : this.defaultDialogueHelperCallback;
+ Logger.getLogger(DialogueHelper.class.getName()).log(Level.INFO, "Preparing tell command for {0} ", message.getId());
+ Logger.getLogger(DialogueHelper.class.getName()).log(Level.CONFIG, "Message:\n{0} ", message);
Runnable runnable = new Runnable() {
- private AsyncDroolsAgentService asyncDroolsAgentService;
+ AsyncDroolsAgentService asyncDroolsAgentService = getAsyncDroolsAgentService();
public void run() {
try{
- asyncDroolsAgentService = getAsyncDroolsAgentService();
+ Logger.getLogger(DialogueHelper.class.getName()).log(Level.INFO, "Telling the agent about {0} - START", message.getId());
asyncDroolsAgentService.tell(message);
-
+ Logger.getLogger(DialogueHelper.class.getName()).log(Level.INFO, "Telling the agent about {0} - DONE", message.getId());
if (waitForAnswers){
List<ACLMessage> results = this.waitForAnswers(message.getId(), finalCallback.getExpectedResponsesNumber(), finalCallback.getMinimumWaitTimeForResponses(), finalCallback.getTimeoutForResponses());
finalCallback.onSuccess(results);
@@ -397,23 +416,34 @@ public void run() {
}
}
- private List<ACLMessage> waitForAnswers( String id, int expectedMessagesNumber, long minimumWaitTime, long timeout) {
+ private List<ACLMessage> waitForAnswers( String id, int expectedMessagesNumber, long minimumWaitTime, long timeout) throws TimeoutException {
+ //could be the case that the client is not waiting for any answer.
+ //In this case there's no need to invoke the agent to get any response.
+ if (expectedMessagesNumber == 0){
+ return new ArrayList<ACLMessage>();
+ }
List<ACLMessage> answers = new ArrayList<ACLMessage>();
long waitTime = minimumWaitTime;
do {
try {
- Logger.getLogger(DialogueHelper.class.getName()).log(Level.FINER, "Answer for {0} is not ready, wait... ", id);
+ Logger.getLogger(DialogueHelper.class.getName()).log(Level.INFO, "Answer for {0} is not ready, wait... ", id);
Thread.sleep( waitTime );
} catch ( InterruptedException ex ) {
Logger.getLogger(DialogueHelper.class.getName()).log(Level.WARNING, "Thread could not be put to sleep", ex);
}
List<ACLMessage> incomingAnswers = asyncDroolsAgentService.getResponses(id);
answers.addAll( incomingAnswers );
-
+
+ Logger.getLogger(DialogueHelper.class.getName()).log(Level.INFO, "Answers for {0}: {1} (waitTime= {2}, timeout= {3}, # responsed expected= {4})",new Object[]{ id, answers.size(), waitTime, timeout, expectedMessagesNumber});
waitTime *= 2;
} while ( answers.size() != expectedMessagesNumber && waitTime < timeout );
+
+ if (answers.size() < expectedMessagesNumber){
+ throw new TimeoutException("Expecting "+expectedMessagesNumber+" messages for message "+id+" but only received "+answers.size()+" in "+timeout+"ms");
+ }
+
return answers;
}
View
26 drools-mas-util/src/main/java/org/drools/mas/util/ACLMessageFactory.java
@@ -22,6 +22,8 @@
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.atomic.AtomicLong;
import org.drools.mas.ACLMessage;
import org.drools.mas.Act;
import org.drools.mas.AgentID;
@@ -60,9 +62,9 @@
*/
public class ACLMessageFactory implements Serializable {
- private static long idCounter = 0;
- private static long convoCounter = 0;
-
+ private static AtomicLong idCounter = new AtomicLong();
+ private static AtomicLong convCounter = new AtomicLong();
+
private static ACLMessageFactory instance;
public static ACLMessageFactory getInstance(){
@@ -72,12 +74,12 @@ public static ACLMessageFactory getInstance(){
return instance;
}
- private String newId() {
- return "" + (idCounter++);
+ private long newId() {
+ return idCounter.incrementAndGet();
}
- private String newConversationId() {
- return "" + (convoCounter++);
+ private long newConversationId() {
+ return convCounter.incrementAndGet();
}
private Encodings defaultEncoding = Encodings.XML;
@@ -94,20 +96,20 @@ public ACLMessageFactory(Encodings defEncoding) {
}
public ACLMessage newMessage() {
- return new ACLMessage( newId() );
+ return new ACLMessage( UUID.randomUUID().toString()+"-"+newId() );
}
protected ACLMessage newMessage( String sender, String receiver ) {
ACLMessage msg = new ACLMessage();
- msg.setConversationId( newConversationId() );
-
AgentID senderAgent = new AgentID();
senderAgent.setName( sender );
msg.setSender( senderAgent );
- msg.setId( newId() + senderAgent.toString() );
+ msg.setConversationId( senderAgent.toString() +"-"+ newConversationId());
+
+ msg.setId( senderAgent.toString() +"-"+ newId());
List<AgentID> recSet = msg.getReceiver();
AgentID receiverAgent = new AgentID();
@@ -121,7 +123,7 @@ protected ACLMessage newMessage( String sender, String receiver ) {
protected ACLMessage createReply(ACLMessage inMsg, AgentID sender) {
- ACLMessage msg = new ACLMessage(newId());
+ ACLMessage msg = newMessage();
msg.setEncoding(inMsg.getEncoding());
msg.setSender(sender);
Something went wrong with that request. Please try again.