1 package at.ac.tuwien.sbc.valesriegler.group;
3 import at.ac.tuwien.sbc.valesriegler.common.Util;
4 import at.ac.tuwien.sbc.valesriegler.group.gui.DeliveryOverviewModel;
5 import at.ac.tuwien.sbc.valesriegler.group.gui.GroupOverviewModel;
6 import at.ac.tuwien.sbc.valesriegler.group.jms.JMSGroupConnector;
7 import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData;
8 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
9 import at.ac.tuwien.sbc.valesriegler.xvsm.GroupAgentXVSM;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
17 * The Main class of the Group component.
19 * Start the communication and the group GUI:
21 * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
24 public class GroupAgent {
25 private static final String USAGE = "This application needs exactly 1 parameter: <\"XVSM\"|\"JMS\">";
27 private static final Logger log = LoggerFactory.getLogger(GroupAgent.class);
28 private GroupOverviewModel groupModel;
29 private AbstractGroupConnector groupconn;
30 private DeliveryOverviewModel deliveryOverviewModel;
31 private GroupAgentXVSM xvsm;
33 /* Contains the identifiers of the pizzerias. Whenever a new Pizzeria emerges or dies the group agent has to be
34 notified about this somehow and the list has to be adapted as it is the model of the dropdown in the GUI for
35 selecting for which pizzeria the customer groups are created
36 In case of XVSM the identifier string is a port number*/
37 private Set<String> pizzeriaIdentifiers = Collections.synchronizedSet(new HashSet<String>());
39 public static GroupAgent groupAgent;
41 public static void main(String[] args) {
42 if (args.length != 1) {
43 throw new IllegalArgumentException(USAGE);
47 log.info("Middleware: " + mw);
49 if ("JMS".equalsIgnoreCase(mw)) {
51 } else if ("XVSM".equalsIgnoreCase(mw)) {
54 throw new IllegalArgumentException(USAGE);
57 groupAgent = new GroupAgent();
59 SwingUtilities.invokeLater(new GroupGUI());
63 deliveryOverviewModel = new DeliveryOverviewModel();
64 groupModel = new GroupOverviewModel();
66 this.groupconn = new JMSGroupConnector();
69 xvsm = new GroupAgentXVSM();
71 xvsm.listenForNewPizzerias();
75 public static GroupAgent getInstance() {
79 public AbstractGroupConnector getGroupcomm() {
83 public Set<String> getPizzeriaIdentifiers() {
84 return pizzeriaIdentifiers;
87 public GroupOverviewModel getGroupModel() {
91 public DeliveryOverviewModel getDeliveryModel() {
92 return deliveryOverviewModel;
95 public void onGroupsCreated(List<Group> newGroups) {
97 List<GroupData> groupData = new ArrayList<>();
99 for (Group group : newGroups) {
100 groupData.add(group.getGroupData());
102 final String pizzeriaId = groupData.get(0).getPizzeriaId();
104 int pizzeriaSpacePort = 0;
106 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
107 } catch (NumberFormatException e) {
108 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
110 xvsm.sendNewGroupsToSpace(groupData, pizzeriaSpacePort);
112 log.info("New normal groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
114 // start the space group in a new thread
115 for (GroupData group : groupData) {
116 new Thread(new SpaceGroup(group.getId(), pizzeriaSpacePort)).start();
123 public void onDeliveryGroupsCreated(List<DeliveryGroup> newDeliveryGroups) {
125 List<DeliveryGroupData> groupData = new ArrayList<>();
126 for (DeliveryGroup group : newDeliveryGroups) {
127 groupData.add(group.getDeliveryGroupData());
129 final String pizzeriaId = groupData.get(0).getPizzeriaId();
131 int pizzeriaSpacePort = 0;
133 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
134 } catch (NumberFormatException e) {
135 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
138 xvsm.sendNewDeliveriesToSpace(groupData, pizzeriaSpacePort);
140 log.info("New delivery groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);