1 package at.ac.tuwien.sbc.valesriegler.group;
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Collections;
6 import java.util.HashSet;
10 import javax.jms.JMSException;
11 import javax.swing.SwingUtilities;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
16 import at.ac.tuwien.sbc.valesriegler.common.Util;
17 import at.ac.tuwien.sbc.valesriegler.group.gui.DeliveryOverviewModel;
18 import at.ac.tuwien.sbc.valesriegler.group.gui.GroupCreationDetailsRequest;
19 import at.ac.tuwien.sbc.valesriegler.group.gui.GroupOverviewModel;
20 import at.ac.tuwien.sbc.valesriegler.group.jms.GroupJMSNACMsgListener;
21 import at.ac.tuwien.sbc.valesriegler.group.jms.JMSGroupConnector;
22 import at.ac.tuwien.sbc.valesriegler.jms.nac.JMSNAC;
23 import at.ac.tuwien.sbc.valesriegler.jms.nac.actions.BenchmarkStart;
24 import at.ac.tuwien.sbc.valesriegler.jms.nac.actions.BenchmarkStop;
25 import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData;
26 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
27 import at.ac.tuwien.sbc.valesriegler.types.Order;
28 import at.ac.tuwien.sbc.valesriegler.types.PizzaType;
29 import at.ac.tuwien.sbc.valesriegler.xvsm.GroupAgentXVSM;
32 * The Main class of the Group component.
34 * Start the communication and the group GUI:
36 * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
39 public class GroupAgent {
40 private static final String USAGE = "This application needs exactly 1 parameter: <\"XVSM\"|\"JMS\">";
42 private static final Logger log = LoggerFactory.getLogger(GroupAgent.class);
43 private GroupOverviewModel groupModel;
44 private DeliveryOverviewModel deliveryOverviewModel;
45 private GroupAgentXVSM xvsm;
46 static private JMSNAC jmsnac;
47 static private GroupGUI groupGui;
49 * Contains the identifiers of the pizzerias. Whenever a new Pizzeria emerges or dies the group agent has to be
50 * notified about this somehow and the list has to be adapted as it is the model of the dropdown in the GUI for
51 * selecting for which pizzeria the customer groups are created
52 * In case of XVSM the identifier string is a port number
54 private Set<String> pizzeriaIdentifiers = Collections.synchronizedSet(new HashSet<String>());
56 public static GroupAgent groupAgent;
58 public static void main(String[] args) {
59 if (args.length < 1) {
60 throw new IllegalArgumentException(USAGE);
64 log.info("Middleware: " + mw);
66 if ("JMS".equalsIgnoreCase(mw)) {
68 } else if ("XVSM".equalsIgnoreCase(mw)) {
71 throw new IllegalArgumentException(USAGE);
74 groupAgent = new GroupAgent();
75 if (!Util.runSimulation) {
76 groupGui = new GroupGUI();
77 SwingUtilities.invokeLater(groupGui);
79 JMSGroupConnector.getConnectors().put("tcp://localhost:61621?jms.prefetchPolicy.all=1",
80 new JMSGroupConnector("tcp://localhost:61621?jms.prefetchPolicy.all=1"));
81 JMSGroupConnector.getConnectors().put("tcp://localhost:61622?jms.prefetchPolicy.all=1",
82 new JMSGroupConnector("tcp://localhost:61622?jms.prefetchPolicy.all=1"));
84 List<PizzaType> pizzaTypes1 = Arrays.asList(PizzaType.CARDINALE, PizzaType.SALAMI, PizzaType.MARGHERITA,
85 PizzaType.MARGHERITA);
86 List<PizzaType> pizzaTypes2 = Arrays.asList(PizzaType.CARDINALE, PizzaType.SALAMI, PizzaType.MARGHERITA);
87 List<PizzaType> pizzaTypes3 = Arrays.asList(PizzaType.SALAMI, PizzaType.MARGHERITA);
89 final String pizzeria1 = "tcp://localhost:61621?jms.prefetchPolicy.all=1";
90 final String pizzeria2 = "tcp://localhost:61622?jms.prefetchPolicy.all=1";
91 List<DeliveryGroup> groups1 = GroupAgent.getInstance().createGroups(pizzaTypes1, pizzeria1, 4);
92 List<DeliveryGroup> groups2 = GroupAgent.getInstance().createGroups(pizzaTypes2, pizzeria2, 3);
93 List<DeliveryGroup> groups3 = GroupAgent.getInstance().createGroups(pizzaTypes3, pizzeria2, 2);
95 final DeliveryOverviewModel deliveryModel = GroupAgent.getInstance().getDeliveryModel();
96 deliveryModel.addItems(groups1);
97 deliveryModel.addItems(groups2);
98 deliveryModel.addItems(groups3);
100 GroupAgent.getInstance().onDeliveryGroupsCreated(groups2);
101 GroupAgent.getInstance().onDeliveryGroupsCreated(groups3);
102 GroupAgent.getInstance().onDeliveryGroupsCreated(groups1);
104 log.warn("Starting benchmark in 20 sec");
106 jmsnac.sendNACMsg(new BenchmarkStart());
108 jmsnac.sendNACMsg(new BenchmarkStop());
109 log.warn("Benchmark Stop Signal sent.");
110 } catch (InterruptedException | JMSException e) {
111 log.warn("EXCEPTION!", e);
116 public GroupAgent() {
117 deliveryOverviewModel = new DeliveryOverviewModel();
118 groupModel = new GroupOverviewModel();
120 GroupJMSNACMsgListener tmp = new GroupJMSNACMsgListener();
121 jmsnac = new JMSNAC(tmp);
124 xvsm = new GroupAgentXVSM();
126 xvsm.listenForNewPizzerias();
128 if (Util.runSimulation) {
129 xvsm.listenToPizzeria("9875");
130 xvsm.listenToPizzeria("9874");
131 xvsm.runSimulation();
136 public List<DeliveryGroup> createGroups(List<PizzaType> pizzaTypes1, String pizzeria, int number) {
137 List<DeliveryGroup> newDeliveryGroups = new ArrayList<>();
138 final String fakeAddress = "address";
139 final GroupCreationDetailsRequest gc = new GroupCreationDetailsRequest(number, 1000, pizzaTypes1, fakeAddress,
142 for (int i = 0; i < gc.numberOfGroups; i++) {
143 DeliveryGroup group = new DeliveryGroup();
144 final Order order = Util.createOrder(group, gc);
145 group.getDeliveryGroupData().setOrder(order);
146 group.getDeliveryGroupData().setPizzeriaId(gc.pizzeria);
147 group.getDeliveryGroupData().setAddress(gc.address);
148 newDeliveryGroups.add(group);
151 return newDeliveryGroups;
154 public static GroupAgent getInstance() {
158 public Set<String> getPizzeriaIdentifiers() {
159 return pizzeriaIdentifiers;
162 public GroupOverviewModel getGroupModel() {
166 public static GroupGUI getGroupGui() {
170 public static JMSNAC getJmsnac() {
174 public DeliveryOverviewModel getDeliveryModel() {
175 return deliveryOverviewModel;
178 public void onGroupsCreated(List<Group> newGroups) {
180 List<GroupData> groupData = new ArrayList<>();
182 for (Group group : newGroups) {
183 groupData.add(group.getGroupData());
185 final String pizzeriaId = groupData.get(0).getPizzeriaId();
187 int pizzeriaSpacePort = 0;
189 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
190 } catch (NumberFormatException e) {
191 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
193 xvsm.sendNewGroupsToSpace(groupData, pizzeriaSpacePort);
195 log.info("New normal groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
197 // start the space group in a new thread
198 for (GroupData group : groupData) {
199 new Thread(new SpaceGroup(group.getId(), pizzeriaSpacePort)).start();
205 public void onDeliveryGroupsCreated(List<DeliveryGroup> newDeliveryGroups) {
207 List<DeliveryGroupData> groupData = new ArrayList<>();
208 for (DeliveryGroup group : newDeliveryGroups) {
209 groupData.add(group.getDeliveryGroupData());
211 final String pizzeriaId = groupData.get(0).getPizzeriaId();
213 int pizzeriaSpacePort = 0;
215 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
216 } catch (NumberFormatException e) {
217 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
220 xvsm.sendNewDeliveriesToSpace(groupData, pizzeriaSpacePort);
222 log.info("New delivery groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
224 if (!Util.runSimulation) {
225 for (DeliveryGroupData deliveryGroupData : groupData) {
226 new Thread(new SpaceDeliveryGroup(deliveryGroupData.getId(), pizzeriaSpacePort,
227 deliveryGroupData.getAddress())).start();