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.GroupData;
8 import at.ac.tuwien.sbc.valesriegler.xvsm.GroupAgentXVSM;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.Collections;
16 import java.util.List;
19 * The Main class of the Group component.
21 * Start the communication and the group GUI:
23 * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
26 public class GroupAgent {
27 private static final String USAGE = "This application needs exactly 1 parameter: <\"XVSM\"|\"JMS\">";
29 private static final Logger log = LoggerFactory.getLogger(GroupAgent.class);
30 private GroupOverviewModel groupModel;
31 private AbstractGroupConnector groupconn;
32 private DeliveryOverviewModel deliveryOverviewModel;
33 private GroupAgentXVSM xvsm;
35 /* Contains the identifiers of the pizzerias. Whenever a new Pizzeria emerges or dies the group agent has to be
36 notified about this somehow and the list has to be adapted as it is the model of the dropdown in the GUI for
37 selecting for which pizzeria the customer groups are created
38 In case of XVSM the identifier string is a port number*/
39 private List<String> pizzeriaIdentifiers = Collections.synchronizedList(new ArrayList<String>());
41 public static GroupAgent groupAgent;
43 public static void main(String[] args) {
44 if (args.length != 1) {
45 throw new IllegalArgumentException(USAGE);
49 log.info("Middleware: " + mw);
51 if ("JMS".equalsIgnoreCase(mw)) {
53 } else if ("XVSM".equalsIgnoreCase(mw)) {
56 throw new IllegalArgumentException(USAGE);
59 groupAgent = new GroupAgent();
61 SwingUtilities.invokeLater(new GroupGUI());
65 groupModel = new GroupOverviewModel();
67 this.groupconn = new JMSGroupConnector();
70 xvsm = new GroupAgentXVSM();
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 List<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 Groups were sent to the space");
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) {
129 //To change body of created methods use File | Settings | File Templates.