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);
105 jmsnac.sendNACMsg(new BenchmarkStart());
107 jmsnac.sendNACMsg(new BenchmarkStop());
108 } catch (InterruptedException | JMSException e) {
109 log.warn("EXCEPTION!", e);
114 public GroupAgent() {
115 deliveryOverviewModel = new DeliveryOverviewModel();
116 groupModel = new GroupOverviewModel();
118 GroupJMSNACMsgListener tmp = new GroupJMSNACMsgListener();
119 jmsnac = new JMSNAC(tmp);
122 xvsm = new GroupAgentXVSM();
124 xvsm.listenForNewPizzerias();
126 if (Util.runSimulation) {
127 xvsm.listenToPizzeria("9875");
128 xvsm.listenToPizzeria("9874");
129 xvsm.runSimulation();
134 public List<DeliveryGroup> createGroups(List<PizzaType> pizzaTypes1, String pizzeria, int number) {
135 List<DeliveryGroup> newDeliveryGroups = new ArrayList<>();
136 final String fakeAddress = "address";
137 final GroupCreationDetailsRequest gc = new GroupCreationDetailsRequest(number, 100, pizzaTypes1, fakeAddress,
140 for (int i = 0; i < gc.numberOfGroups; i++) {
141 DeliveryGroup group = new DeliveryGroup();
142 final Order order = Util.createOrder(group, gc);
143 group.getDeliveryGroupData().setOrder(order);
144 group.getDeliveryGroupData().setPizzeriaId(gc.pizzeria);
145 group.getDeliveryGroupData().setAddress(gc.address);
146 newDeliveryGroups.add(group);
149 return newDeliveryGroups;
152 public static GroupAgent getInstance() {
156 public Set<String> getPizzeriaIdentifiers() {
157 return pizzeriaIdentifiers;
160 public GroupOverviewModel getGroupModel() {
164 public static GroupGUI getGroupGui() {
168 public static JMSNAC getJmsnac() {
172 public DeliveryOverviewModel getDeliveryModel() {
173 return deliveryOverviewModel;
176 public void onGroupsCreated(List<Group> newGroups) {
178 List<GroupData> groupData = new ArrayList<>();
180 for (Group group : newGroups) {
181 groupData.add(group.getGroupData());
183 final String pizzeriaId = groupData.get(0).getPizzeriaId();
185 int pizzeriaSpacePort = 0;
187 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
188 } catch (NumberFormatException e) {
189 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
191 xvsm.sendNewGroupsToSpace(groupData, pizzeriaSpacePort);
193 log.info("New normal groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
195 // start the space group in a new thread
196 for (GroupData group : groupData) {
197 new Thread(new SpaceGroup(group.getId(), pizzeriaSpacePort)).start();
203 public void onDeliveryGroupsCreated(List<DeliveryGroup> newDeliveryGroups) {
205 List<DeliveryGroupData> groupData = new ArrayList<>();
206 for (DeliveryGroup group : newDeliveryGroups) {
207 groupData.add(group.getDeliveryGroupData());
209 final String pizzeriaId = groupData.get(0).getPizzeriaId();
211 int pizzeriaSpacePort = 0;
213 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
214 } catch (NumberFormatException e) {
215 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
218 xvsm.sendNewDeliveriesToSpace(groupData, pizzeriaSpacePort);
220 log.info("New delivery groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
222 if (!Util.runSimulation) {
223 for (DeliveryGroupData deliveryGroupData : groupData) {
224 new Thread(new SpaceDeliveryGroup(deliveryGroupData.getId(), pizzeriaSpacePort,
225 deliveryGroupData.getAddress())).start();