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;
34 * Contains the identifiers of the pizzerias. Whenever a new Pizzeria emerges or dies the group agent has to be
35 * notified about this somehow and the list has to be adapted as it is the model of the dropdown in the GUI for
36 * selecting for which pizzeria the customer groups are created
37 * In case of XVSM the identifier string is a port number
39 private Set<String> pizzeriaIdentifiers = Collections.synchronizedSet(new HashSet<String>());
41 public static GroupAgent groupAgent;
43 public static void main(String[] args) {
44 if (args.length != 1) {
45 // throw new IllegalArgumentException(USAGE);
48 String mw = "XVSM";// args[0];
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 deliveryOverviewModel = new DeliveryOverviewModel();
66 groupModel = new GroupOverviewModel();
68 groupconn = new JMSGroupConnector();
71 xvsm = new GroupAgentXVSM();
73 xvsm.listenForNewPizzerias();
77 public static GroupAgent getInstance() {
81 public AbstractGroupConnector getGroupcomm() {
85 public Set<String> getPizzeriaIdentifiers() {
86 return pizzeriaIdentifiers;
89 public GroupOverviewModel getGroupModel() {
93 public DeliveryOverviewModel getDeliveryModel() {
94 return deliveryOverviewModel;
97 public void onGroupsCreated(List<Group> newGroups) {
99 List<GroupData> groupData = new ArrayList<>();
101 for (Group group : newGroups) {
102 groupData.add(group.getGroupData());
104 final String pizzeriaId = groupData.get(0).getPizzeriaId();
106 int pizzeriaSpacePort = 0;
108 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
109 } catch (NumberFormatException e) {
110 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
112 xvsm.sendNewGroupsToSpace(groupData, pizzeriaSpacePort);
114 log.info("New normal groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
116 // start the space group in a new thread
117 for (GroupData group : groupData) {
118 new Thread(new SpaceGroup(group.getId(), pizzeriaSpacePort)).start();
124 public void onDeliveryGroupsCreated(List<DeliveryGroup> newDeliveryGroups) {
126 List<DeliveryGroupData> groupData = new ArrayList<>();
127 for (DeliveryGroup group : newDeliveryGroups) {
128 groupData.add(group.getDeliveryGroupData());
130 final String pizzeriaId = groupData.get(0).getPizzeriaId();
132 int pizzeriaSpacePort = 0;
134 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
135 } catch (NumberFormatException e) {
136 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
139 xvsm.sendNewDeliveriesToSpace(groupData, pizzeriaSpacePort);
141 log.info("New delivery groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
143 for (DeliveryGroupData deliveryGroupData : groupData) {
144 new Thread(new SpaceDeliveryGroup(deliveryGroupData.getId(), pizzeriaSpacePort, deliveryGroupData.getAddress())).start();