Skip to content

Commit

Permalink
Assignment 4 schedule and aggregation of order #12
Browse files Browse the repository at this point in the history
added queue position request and updated documentation accordingly
  • Loading branch information
Dr4gonbl4de committed Nov 26, 2018
1 parent 91261f8 commit 339188c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
16 changes: 15 additions & 1 deletion docs/messages/scheduler.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
* Receiver: SchedulerAgent
* Content: String -> "NO NEW ORDER"

* Get Queue Position:
* Type: REQUEST
* Sender: Any agent
* Receiver: SchedulerAgent
* ConversationId: queue request
* Content: String -> {orderId}

## Messages sent:
* Confirm Schedule:
* Type: CONFIRM
Expand All @@ -41,4 +48,11 @@
* Type: PROPAGATE
* Sender: SchedulerAgent
* Receiver: All Agents
* Content: JSONArray -> sorted List of accepted Orders
* Content: JSONArray -> sorted List of accepted Orders

* Queue request reply:
* Type: INFORM
* Sender: SchedulerAgent
* Receiver: Any agent
* ConversationId: queue request
* Content: String -> Order position if order in queue else -1
31 changes: 31 additions & 0 deletions src/main/java/org/team_pjt/agents/SchedulerAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.team_pjt.objects.Location;
import org.team_pjt.objects.Order;
import org.team_pjt.objects.Product;
import sun.plugin2.message.Message;

import javax.swing.*;
import java.util.*;
Expand All @@ -42,6 +43,7 @@ protected void setup(){
scheduledOrders = new HashMap<>();

addBehaviour(new isNewOrderChecker());
addBehaviour(new QueueRequestServer());

System.out.println("SchedulerAgent is ready");
}
Expand Down Expand Up @@ -200,7 +202,36 @@ private class QueueRequestServer extends CyclicBehaviour {
// TODO
@Override
public void action() {
MessageTemplate mtQueueRequest = MessageTemplate.and(MessageTemplate.MatchConversationId("queue request"),
MessageTemplate.MatchPerformative(ACLMessage.REQUEST));
ACLMessage queue_request = myAgent.receive(mtQueueRequest);
if(queue_request != null) {
String order_id = queue_request.getContent();
int pos = 0;

Iterator<Integer> order_date_iterator = scheduledOrders.keySet().iterator();
boolean found = false;
while(order_date_iterator.hasNext()) {
int day = order_date_iterator.next();
if(scheduledOrders.get(day).getGuid().equals(order_id)) {
found = true;
break;
}
pos++;
}
ACLMessage reply = queue_request.createReply();
reply.setPerformative(ACLMessage.INFORM);
if(found) {
reply.setContent(Integer.toString(pos));
}
else {
reply.setContent(Integer.toString(-1));
}
sendMessage(reply);
}
else {
block();
}
}
}

Expand Down

0 comments on commit 339188c

Please sign in to comment.