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();
72 xvsm.listenForTableAssigned();
73 xvsm.listenForOrdersTaken();
74 xvsm.listenForDeliveredOrders();
75 xvsm.listenForPaymentRequest();
76 xvsm.listenForPaymentDone();
80 public static GroupAgent getInstance() {
84 public AbstractGroupConnector getGroupcomm() {
88 public Set<String> getPizzeriaIdentifiers() {
89 return pizzeriaIdentifiers;
92 public GroupOverviewModel getGroupModel() {
96 public DeliveryOverviewModel getDeliveryModel() {
97 return deliveryOverviewModel;
100 public void onGroupsCreated(List<Group> newGroups) {
102 List<GroupData> groupData = new ArrayList<>();
104 for (Group group : newGroups) {
105 groupData.add(group.getGroupData());
107 final String pizzeriaId = groupData.get(0).getPizzeriaId();
109 int pizzeriaSpacePort = 0;
111 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
112 } catch (NumberFormatException e) {
113 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
115 xvsm.sendNewGroupsToSpace(groupData, pizzeriaSpacePort);
117 log.info("New normal groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
119 // start the space group in a new thread
120 for (GroupData group : groupData) {
121 new Thread(new SpaceGroup(group.getId(), pizzeriaSpacePort)).start();
128 public void onDeliveryGroupsCreated(List<DeliveryGroup> newDeliveryGroups) {
130 List<DeliveryGroupData> groupData = new ArrayList<>();
131 for (DeliveryGroup group : newDeliveryGroups) {
132 groupData.add(group.getDeliveryGroupData());
134 final String pizzeriaId = groupData.get(0).getPizzeriaId();
136 int pizzeriaSpacePort = 0;
138 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
139 } catch (NumberFormatException e) {
140 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
143 xvsm.sendNewDeliveriesToSpace(groupData, pizzeriaSpacePort);
145 log.info("New delivery groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);