Details
diff --git a/network-resilience/src/br/ufrgs/inf/bdinetr/agent/RateLimiterCapability.java b/network-resilience/src/br/ufrgs/inf/bdinetr/agent/RateLimiterCapability.java
index 7a50c79..eb76c0b 100644
--- a/network-resilience/src/br/ufrgs/inf/bdinetr/agent/RateLimiterCapability.java
+++ b/network-resilience/src/br/ufrgs/inf/bdinetr/agent/RateLimiterCapability.java
@@ -42,9 +42,9 @@ import br.ufrgs.inf.bdinetr.agent.RouterAgent.RootCapability;
import br.ufrgs.inf.bdinetr.domain.Flow;
import br.ufrgs.inf.bdinetr.domain.Ip;
import br.ufrgs.inf.bdinetr.domain.Link;
+import br.ufrgs.inf.bdinetr.domain.PReSETRole.RoleType;
import br.ufrgs.inf.bdinetr.domain.PReSETRouter;
import br.ufrgs.inf.bdinetr.domain.RateLimiter;
-import br.ufrgs.inf.bdinetr.domain.PReSETRole.RoleType;
import br.ufrgs.inf.bdinetr.domain.logic.FlowPreposition.FlowRateLimited;
import br.ufrgs.inf.bdinetr.domain.logic.FlowPreposition.Threat;
import br.ufrgs.inf.bdinetr.domain.logic.FlowPreposition.ThreatResponded;
@@ -55,7 +55,6 @@ import br.ufrgs.inf.bdinetr.domain.logic.IpPreposition.RateLimited;
import br.ufrgs.inf.bdinetr.domain.logic.IpPreposition.Restricted;
import br.ufrgs.inf.bdinetr.domain.logic.LinkProposition.AttackPrevented;
import br.ufrgs.inf.bdinetr.domain.logic.LinkProposition.FullyOperational;
-import br.ufrgs.inf.bdinetr.domain.logic.LinkProposition.OverUsage;
import br.ufrgs.inf.bdinetr.domain.logic.LinkProposition.RegularUsage;
/**
@@ -255,14 +254,7 @@ public class RateLimiterCapability extends RouterAgentCapability implements
limitLinkRate = new DefaultPlan(
GoalTemplateFactory.hasBeliefOfTypeWithValue(
AttackPrevented.class, Boolean.TRUE),
- LimitLinkRatePlan.class) {
- public boolean isContextApplicable(Goal goal) {
- BeliefGoal<AttackPrevented> bg = (BeliefGoal<AttackPrevented>) goal;
- PropositionalBelief<OverUsage> overUsage = (PropositionalBelief<OverUsage>) getBeliefBase()
- .getBelief(new OverUsage(bg.getBeliefName().getLink()));
- return (overUsage != null && overUsage.getValue());
- };
- };
+ LimitLinkRatePlan.class);
restoreIpRate = new DefaultPlan(
GoalTemplateFactory.hasBeliefOfTypeWithValue(Restricted.class,
Boolean.FALSE), RestoreIPRatePlan.class) {
diff --git a/network-resilience/src/br/ufrgs/inf/bdinetr/agent/RequestBeliefGoalPlanBody.java b/network-resilience/src/br/ufrgs/inf/bdinetr/agent/RequestBeliefGoalPlanBody.java
index 2f92702..5c2dd27 100644
--- a/network-resilience/src/br/ufrgs/inf/bdinetr/agent/RequestBeliefGoalPlanBody.java
+++ b/network-resilience/src/br/ufrgs/inf/bdinetr/agent/RequestBeliefGoalPlanBody.java
@@ -39,8 +39,8 @@ import br.ufrgs.inf.bdinetr.domain.PReSETRole.RoleType;
*/
public class RequestBeliefGoalPlanBody extends BeliefGoalPlanBody {
- public static final int MSG_TIME_OUT = 10000;
- public static final int ANSWER_TIME_OUT = 30000;
+ public static final int MSG_TIME_OUT = 10;
+ public static final int ANSWER_TIME_OUT = 100;
private enum State {
Resquesting, ReceivingResponses, Selecting, AchievingGoal, Ended;
@@ -76,9 +76,10 @@ public class RequestBeliefGoalPlanBody extends BeliefGoalPlanBody {
.MatchConversationId(msg.getConversationId()),
MessageTemplate.MatchInReplyTo(msg.getReplyWith()));
this.positiveAnswers = new HashSet<>();
+ this.state = State.ReceivingResponses;
break;
case ReceivingResponses:
- ACLMessage reply = myAgent.blockingReceive(mt, MSG_TIME_OUT);
+ ACLMessage reply = myAgent.receive(mt);
if (reply != null) {
Object content = reply.getContentObject();
if (content instanceof Boolean) {
@@ -86,11 +87,16 @@ public class RequestBeliefGoalPlanBody extends BeliefGoalPlanBody {
+ content);
if ((Boolean) content) {
positiveAnswers.add(reply);
+ log.info("Answers: " + positiveAnswers);
}
}
+ } else {
+ block(MSG_TIME_OUT);
}
if ((System.currentTimeMillis() - requestTime) > ANSWER_TIME_OUT) {
this.state = State.Selecting;
+ } else {
+ log.info("Waiting for more answers...");
}
break;
case Selecting:
@@ -100,26 +106,38 @@ public class RequestBeliefGoalPlanBody extends BeliefGoalPlanBody {
this.state = State.Ended;
return;
} else {
- ACLMessage chosenMsg = positiveAnswers.iterator().next(); // TODO
- // Better
- // selection
- // process.
+ // TODO Better selection process.
+ ACLMessage chosenMsg = positiveAnswers.iterator().next();
for (ACLMessage answer : positiveAnswers) {
reply = answer.createReply();
- reply.setPerformative((answer == chosenMsg) ? ACLMessage.ACCEPT_PROPOSAL
- : ACLMessage.REJECT_PROPOSAL);
+ if (answer == chosenMsg) {
+ reply.setPerformative(ACLMessage.ACCEPT_PROPOSAL);
+ reply.setReplyWith("cfp"
+ + System.currentTimeMillis());
+ reply.setContentObject(getGoal());
+ this.mt = MessageTemplate.and(MessageTemplate
+ .MatchConversationId(reply
+ .getConversationId()),
+ MessageTemplate.MatchInReplyTo(reply
+ .getReplyWith()));
+ log.info("Accepted proposal of agent: "
+ + answer.getSender());
+ } else {
+ reply.setPerformative(ACLMessage.REJECT_PROPOSAL);
+ log.info("Rejected proposal of agent: "
+ + answer.getSender());
+ }
this.myAgent.send(reply);
}
this.state = State.AchievingGoal;
}
break;
case AchievingGoal:
- reply = myAgent.blockingReceive(mt);
+ reply = myAgent.receive(mt);
if (reply != null) {
Object content = reply.getContentObject();
if (content instanceof Belief) {
- getCapability().getWholeCapability().getBeliefBase()
- .addBelief((Belief<?, ?>) content);
+ getBeliefBase().addBelief((Belief<?, ?>) content);
assert ((BeliefGoal<?>) getGoal())
.isAchieved(getBeliefBase());
log.info("Goal " + getGoal() + " achieved.");
@@ -127,6 +145,8 @@ public class RequestBeliefGoalPlanBody extends BeliefGoalPlanBody {
setEndState(EndState.FAILED);
}
this.state = State.Ended;
+ } else {
+ block();
}
break;
case Ended:
diff --git a/network-resilience/src/br/ufrgs/inf/bdinetr/agent/RespondBeliefGoalPlanBody.java b/network-resilience/src/br/ufrgs/inf/bdinetr/agent/RespondBeliefGoalPlanBody.java
index 9b5b2c7..72ea425 100644
--- a/network-resilience/src/br/ufrgs/inf/bdinetr/agent/RespondBeliefGoalPlanBody.java
+++ b/network-resilience/src/br/ufrgs/inf/bdinetr/agent/RespondBeliefGoalPlanBody.java
@@ -25,7 +25,10 @@ import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import bdi4jade.annotation.Parameter;
import bdi4jade.annotation.Parameter.Direction;
+import bdi4jade.belief.Belief;
+import bdi4jade.event.GoalEvent;
import bdi4jade.goal.BeliefGoal;
+import bdi4jade.goal.GoalStatus;
import bdi4jade.plan.Plan.EndState;
import bdi4jade.plan.planbody.AbstractPlanBody;
@@ -43,37 +46,69 @@ public class RespondBeliefGoalPlanBody extends AbstractPlanBody {
private ACLMessage beliefGoalMsg;
private MessageTemplate mt;
private State state;
+ private ACLMessage incomingMsg;
+ private ACLMessage outcomingMsg;
+ private BeliefGoal<?> beliefGoal;
@Override
public void action() {
try {
switch (state) {
case SendingResponse:
- ACLMessage reply = beliefGoalMsg.createReply();
- Object content = reply.getContent();
+ outcomingMsg = beliefGoalMsg.createReply();
+ Object content = beliefGoalMsg.getContentObject();
if (content instanceof BeliefGoal) {
- Boolean canAchieve = getCapability().canAchieve(
- (BeliefGoal<?>) content);
- reply.setContentObject(canAchieve);
+ beliefGoal = (BeliefGoal<?>) content;
+ Boolean canAchieve = getCapability().canAchieve(beliefGoal);
+ outcomingMsg.setContentObject(canAchieve);
log.info("Agent " + myAgent + " can achieve " + content
+ ": " + canAchieve);
} else {
- reply.setContentObject(Boolean.FALSE);
+ outcomingMsg.setContentObject(Boolean.FALSE);
}
- this.myAgent.send(reply);
+ outcomingMsg.setReplyWith("cfp" + System.currentTimeMillis());
+ this.myAgent.send(outcomingMsg);
this.mt = MessageTemplate.and(MessageTemplate
- .MatchConversationId(reply.getConversationId()),
- MessageTemplate.MatchInReplyTo(reply.getReplyWith()));
+ .MatchConversationId(outcomingMsg.getConversationId()),
+ MessageTemplate.MatchInReplyTo(outcomingMsg
+ .getReplyWith()));
this.state = State.ReceivingReply;
break;
case ReceivingReply:
-
+ incomingMsg = myAgent.receive(mt);
+ if (incomingMsg != null) {
+ if (ACLMessage.ACCEPT_PROPOSAL == incomingMsg
+ .getPerformative()) {
+ dispatchSubgoalAndListen(beliefGoal);
+ this.state = State.AchievingBeliefGoal;
+ } else {
+ setEndState(EndState.SUCCESSFULL);
+ log.info("Proposal rejected.");
+ this.state = State.Ended;
+ return;
+ }
+ } else {
+ block();
+ }
break;
case AchievingBeliefGoal:
-
+ GoalEvent event = getGoalEvent();
+ if (event != null) {
+ outcomingMsg = incomingMsg.createReply();
+ outcomingMsg.setPerformative(ACLMessage.INFORM);
+ if (GoalStatus.ACHIEVED.equals(event.getStatus())) {
+ Belief<?, ?> belief = (getBeliefBase()
+ .getBelief(beliefGoal.getBeliefName()));
+ outcomingMsg.setContentObject((Belief<?, ?>) belief
+ .clone());
+ } else {
+ outcomingMsg.setContentObject(null);
+ }
+ this.myAgent.send(outcomingMsg);
+ this.state = State.Ended;
+ }
break;
case Ended:
-
break;
}
} catch (Exception exc) {
diff --git a/network-resilience/src/br/ufrgs/inf/bdinetr/BDINetRApp.java b/network-resilience/src/br/ufrgs/inf/bdinetr/BDINetRApp.java
index 3949d1c..a802cd0 100644
--- a/network-resilience/src/br/ufrgs/inf/bdinetr/BDINetRApp.java
+++ b/network-resilience/src/br/ufrgs/inf/bdinetr/BDINetRApp.java
@@ -34,6 +34,9 @@ import jade.wrapper.AgentContainer;
import jade.wrapper.AgentController;
import jade.wrapper.PlatformController;
+import java.awt.GridLayout;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
@@ -41,6 +44,11 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
+import javax.swing.JButton;
+import javax.swing.JFrame;
+import javax.swing.JPanel;
+import javax.swing.SwingUtilities;
+
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.PropertyConfigurator;
@@ -90,7 +98,27 @@ public class BDINetRApp {
public static void main(String[] args) {
new BDINetRApp();
- NETWORK.run();
+
+ JPanel panel = new JPanel(new GridLayout(1, 1));
+ JButton button = new JButton("Run!");
+ button.addActionListener(new ActionListener() {
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ NETWORK.run();
+ }
+ });
+ panel.add(button);
+ final JFrame frame = new JFrame();
+ frame.setTitle(BDINetRApp.class.getSimpleName());
+ frame.setContentPane(panel);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ frame.pack();
+ SwingUtilities.invokeLater(new Runnable() {
+ @Override
+ public void run() {
+ frame.setVisible(true);
+ }
+ });
}
private ProfileImpl bootProfile;
diff --git a/network-resilience/src/br/ufrgs/inf/bdinetr/Network.java b/network-resilience/src/br/ufrgs/inf/bdinetr/Network.java
index f6f3eba..cbc564f 100644
--- a/network-resilience/src/br/ufrgs/inf/bdinetr/Network.java
+++ b/network-resilience/src/br/ufrgs/inf/bdinetr/Network.java
@@ -26,7 +26,6 @@ import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import bdi4jade.examples.BDI4JADEExamplesPanel;
import br.ufrgs.inf.bdinetr.domain.Link;
import br.ufrgs.inf.bdinetr.domain.LinkMonitor;
import br.ufrgs.inf.bdinetr.domain.Observer;