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.GroupOverviewModel;
17 import at.ac.tuwien.sbc.valesriegler.group.jms.JMSGroupConnector;
18 import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData;
19 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
20 import at.ac.tuwien.sbc.valesriegler.xvsm.GroupAgentXVSM;
23 * The Main class of the Group component.
25 * Start the communication and the group GUI:
27 * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
30 public class GroupAgent {
31 private static final String USAGE = "This application needs exactly 1 parameter: <\"XVSM\"|\"JMS\">";
33 private static final Logger log = LoggerFactory.getLogger(GroupAgent.class);
34 private GroupOverviewModel groupModel;
35 private AbstractGroupConnector groupconn;
36 private DeliveryOverviewModel deliveryOverviewModel;
37 private GroupAgentXVSM xvsm;
40 * Contains the identifiers of the pizzerias. Whenever a new Pizzeria emerges or dies the group agent has to be
41 * notified about this somehow and the list has to be adapted as it is the model of the dropdown in the GUI for
42 * selecting for which pizzeria the customer groups are created
43 * In case of XVSM the identifier string is a port number
45 private Set<String> pizzeriaIdentifiers = Collections.synchronizedSet(new HashSet<String>());
47 public static GroupAgent groupAgent;
49 public static void main(String[] args) {
50 if (args.length != 1) {
51 // throw new IllegalArgumentException(USAGE);
54 String mw = "JMS";// args[0];
55 log.info("Middleware: " + mw);
57 if ("JMS".equalsIgnoreCase(mw)) {
59 } else if ("XVSM".equalsIgnoreCase(mw)) {
62 throw new IllegalArgumentException(USAGE);
65 groupAgent = new GroupAgent();
67 SwingUtilities.invokeLater(new GroupGUI());
71 deliveryOverviewModel = new DeliveryOverviewModel();
72 groupModel = new GroupOverviewModel();
74 groupconn = new JMSGroupConnector();
77 xvsm = new GroupAgentXVSM();
79 xvsm.listenForNewPizzerias();
83 public static GroupAgent getInstance() {
87 public AbstractGroupConnector getGroupcomm() {
91 public Set<String> getPizzeriaIdentifiers() {
92 return pizzeriaIdentifiers;
95 public GroupOverviewModel getGroupModel() {
99 public DeliveryOverviewModel getDeliveryModel() {
100 return deliveryOverviewModel;
103 public void onGroupsCreated(List<Group> newGroups) {
105 List<GroupData> groupData = new ArrayList<>();
107 for (Group group : newGroups) {
108 groupData.add(group.getGroupData());
110 final String pizzeriaId = groupData.get(0).getPizzeriaId();
112 int pizzeriaSpacePort = 0;
114 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
115 } catch (NumberFormatException e) {
116 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
118 xvsm.sendNewGroupsToSpace(groupData, pizzeriaSpacePort);
120 log.info("New normal groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
122 // start the space group in a new thread
123 for (GroupData group : groupData) {
124 new Thread(new SpaceGroup(group.getId(), pizzeriaSpacePort)).start();
130 public void onDeliveryGroupsCreated(List<DeliveryGroup> newDeliveryGroups) {
132 List<DeliveryGroupData> groupData = new ArrayList<>();
133 for (DeliveryGroup group : newDeliveryGroups) {
134 groupData.add(group.getDeliveryGroupData());
136 final String pizzeriaId = groupData.get(0).getPizzeriaId();
138 int pizzeriaSpacePort = 0;
140 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
141 } catch (NumberFormatException e) {
142 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
145 xvsm.sendNewDeliveriesToSpace(groupData, pizzeriaSpacePort);
147 log.info("New delivery groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);