1 package at.ac.tuwien.sbc.valesriegler.group;
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashSet;
9 import javax.swing.SwingUtilities;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
14 import at.ac.tuwien.sbc.valesriegler.common.Util;
15 import at.ac.tuwien.sbc.valesriegler.group.gui.DeliveryOverviewModel;
16 import at.ac.tuwien.sbc.valesriegler.group.gui.GroupCreationDetailsRequest;
17 import at.ac.tuwien.sbc.valesriegler.group.gui.GroupOverviewModel;
18 import at.ac.tuwien.sbc.valesriegler.group.jms.GroupJMSNACMsgListener;
19 import at.ac.tuwien.sbc.valesriegler.jms.nac.JMSNAC;
20 import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData;
21 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
22 import at.ac.tuwien.sbc.valesriegler.types.Order;
23 import at.ac.tuwien.sbc.valesriegler.types.PizzaType;
24 import at.ac.tuwien.sbc.valesriegler.xvsm.GroupAgentXVSM;
27 * The Main class of the Group component.
29 * Start the communication and the group GUI:
31 * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
34 public class GroupAgent {
35 private static final String USAGE = "This application needs exactly 1 parameter: <\"XVSM\"|\"JMS\">";
37 private static final Logger log = LoggerFactory.getLogger(GroupAgent.class);
38 private GroupOverviewModel groupModel;
39 private DeliveryOverviewModel deliveryOverviewModel;
40 private GroupAgentXVSM xvsm;
41 static private JMSNAC jmsnac;
42 static private GroupGUI groupGui;
44 * Contains the identifiers of the pizzerias. Whenever a new Pizzeria emerges or dies the group agent has to be
45 * notified about this somehow and the list has to be adapted as it is the model of the dropdown in the GUI for
46 * selecting for which pizzeria the customer groups are created
47 * In case of XVSM the identifier string is a port number
49 private Set<String> pizzeriaIdentifiers = Collections.synchronizedSet(new HashSet<String>());
51 public static GroupAgent groupAgent;
53 public static void main(String[] args) {
54 if (args.length != 1) {
55 throw new IllegalArgumentException(USAGE);
59 log.info("Middleware: " + mw);
61 if ("JMS".equalsIgnoreCase(mw)) {
63 } else if ("XVSM".equalsIgnoreCase(mw)) {
66 throw new IllegalArgumentException(USAGE);
69 groupAgent = new GroupAgent();
70 if (!Util.runSimulation) {
71 groupGui = new GroupGUI();
72 SwingUtilities.invokeLater(groupGui);
77 deliveryOverviewModel = new DeliveryOverviewModel();
78 groupModel = new GroupOverviewModel();
80 GroupJMSNACMsgListener tmp = new GroupJMSNACMsgListener();
81 jmsnac = new JMSNAC(tmp);
84 xvsm = new GroupAgentXVSM();
86 xvsm.listenForNewPizzerias();
88 if (Util.runSimulation) {
89 xvsm.listenToPizzeria("9875");
90 xvsm.listenToPizzeria("9874");
96 public List<DeliveryGroup> createGroups(List<PizzaType> pizzaTypes1, String pizzeria, int number) {
97 List<DeliveryGroup> newDeliveryGroups = new ArrayList<>();
98 final String fakeAddress = "address";
99 final GroupCreationDetailsRequest gc = new GroupCreationDetailsRequest(number, 100, pizzaTypes1, fakeAddress,
102 for (int i = 0; i < gc.numberOfGroups; i++) {
103 DeliveryGroup group = new DeliveryGroup();
104 final Order order = Util.createOrder(group, gc);
105 group.getDeliveryGroupData().setOrder(order);
106 group.getDeliveryGroupData().setPizzeriaId(gc.pizzeria);
107 group.getDeliveryGroupData().setAddress(gc.address);
108 newDeliveryGroups.add(group);
111 return newDeliveryGroups;
114 public static GroupAgent getInstance() {
118 public Set<String> getPizzeriaIdentifiers() {
119 return pizzeriaIdentifiers;
122 public GroupOverviewModel getGroupModel() {
126 public static GroupGUI getGroupGui() {
130 public static JMSNAC getJmsnac() {
134 public DeliveryOverviewModel getDeliveryModel() {
135 return deliveryOverviewModel;
138 public void onGroupsCreated(List<Group> newGroups) {
140 List<GroupData> groupData = new ArrayList<>();
142 for (Group group : newGroups) {
143 groupData.add(group.getGroupData());
145 final String pizzeriaId = groupData.get(0).getPizzeriaId();
147 int pizzeriaSpacePort = 0;
149 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
150 } catch (NumberFormatException e) {
151 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
153 xvsm.sendNewGroupsToSpace(groupData, pizzeriaSpacePort);
155 log.info("New normal groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
157 // start the space group in a new thread
158 for (GroupData group : groupData) {
159 new Thread(new SpaceGroup(group.getId(), pizzeriaSpacePort)).start();
165 public void onDeliveryGroupsCreated(List<DeliveryGroup> newDeliveryGroups) {
167 List<DeliveryGroupData> groupData = new ArrayList<>();
168 for (DeliveryGroup group : newDeliveryGroups) {
169 groupData.add(group.getDeliveryGroupData());
171 final String pizzeriaId = groupData.get(0).getPizzeriaId();
173 int pizzeriaSpacePort = 0;
175 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
176 } catch (NumberFormatException e) {
177 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
180 xvsm.sendNewDeliveriesToSpace(groupData, pizzeriaSpacePort);
182 log.info("New delivery groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
184 if (!Util.runSimulation) {
185 for (DeliveryGroupData deliveryGroupData : groupData) {
186 new Thread(new SpaceDeliveryGroup(deliveryGroupData.getId(), pizzeriaSpacePort,
187 deliveryGroupData.getAddress())).start();