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.GroupCreationDetailsRequest;
6 import at.ac.tuwien.sbc.valesriegler.group.gui.GroupOverviewModel;
7 import at.ac.tuwien.sbc.valesriegler.group.jms.GroupJMSNACMsgListener;
8 import at.ac.tuwien.sbc.valesriegler.jms.nac.JMSNAC;
9 import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData;
10 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
11 import at.ac.tuwien.sbc.valesriegler.types.Order;
12 import at.ac.tuwien.sbc.valesriegler.types.PizzaType;
13 import at.ac.tuwien.sbc.valesriegler.xvsm.GroupAgentXVSM;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
21 * The Main class of the Group component.
23 * Start the communication and the group GUI:
25 * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
28 public class GroupAgent {
29 private static final String USAGE = "This application needs exactly 1 parameter: <\"XVSM\"|\"JMS\">";
31 private static final Logger log = LoggerFactory.getLogger(GroupAgent.class);
32 private GroupOverviewModel groupModel;
33 private DeliveryOverviewModel deliveryOverviewModel;
34 private GroupAgentXVSM xvsm;
35 static private JMSNAC jmsnac;
36 static private GroupGUI groupGui;
38 * Contains the identifiers of the pizzerias. Whenever a new Pizzeria emerges or dies the group agent has to be
39 * notified about this somehow and the list has to be adapted as it is the model of the dropdown in the GUI for
40 * selecting for which pizzeria the customer groups are created
41 * In case of XVSM the identifier string is a port number
43 private Set<String> pizzeriaIdentifiers = Collections.synchronizedSet(new HashSet<String>());
45 public static GroupAgent groupAgent;
47 public static void main(String[] args) {
48 if (args.length < 1) {
49 throw new IllegalArgumentException(USAGE);
53 log.info("Middleware: " + mw);
55 if ("JMS".equalsIgnoreCase(mw)) {
57 } else if ("XVSM".equalsIgnoreCase(mw)) {
60 throw new IllegalArgumentException(USAGE);
63 groupAgent = new GroupAgent();
64 if (! Util.runSimulation) {
65 groupGui = new GroupGUI();
66 SwingUtilities.invokeLater(groupGui);
71 deliveryOverviewModel = new DeliveryOverviewModel();
72 groupModel = new GroupOverviewModel();
74 GroupJMSNACMsgListener tmp = new GroupJMSNACMsgListener();
75 jmsnac = new JMSNAC(tmp);
78 xvsm = new GroupAgentXVSM();
80 xvsm.listenForNewPizzerias();
82 if (Util.runSimulation) {
83 xvsm.listenToPizzeria("9875");
84 xvsm.listenToPizzeria("9874");
90 public List<DeliveryGroup> createGroups(List<PizzaType> pizzaTypes1, String pizzeria, int number) {
91 List<DeliveryGroup> newDeliveryGroups = new ArrayList<>();
92 final String fakeAddress = "address";
93 final GroupCreationDetailsRequest gc = new GroupCreationDetailsRequest(number, 100, pizzaTypes1, fakeAddress,
96 for (int i = 0; i < gc.numberOfGroups; i++) {
97 DeliveryGroup group = new DeliveryGroup();
98 final Order order = Util.createOrder(group, gc);
99 group.getDeliveryGroupData().setOrder(order);
100 group.getDeliveryGroupData().setPizzeriaId(gc.pizzeria);
101 group.getDeliveryGroupData().setAddress(gc.address);
102 newDeliveryGroups.add(group);
105 return newDeliveryGroups;
108 public static GroupAgent getInstance() {
112 public Set<String> getPizzeriaIdentifiers() {
113 return pizzeriaIdentifiers;
116 public GroupOverviewModel getGroupModel() {
120 public static GroupGUI getGroupGui() {
124 public static JMSNAC getJmsnac() {
128 public DeliveryOverviewModel getDeliveryModel() {
129 return deliveryOverviewModel;
132 public void onGroupsCreated(List<Group> newGroups) {
134 List<GroupData> groupData = new ArrayList<>();
136 for (Group group : newGroups) {
137 groupData.add(group.getGroupData());
139 final String pizzeriaId = groupData.get(0).getPizzeriaId();
141 int pizzeriaSpacePort = 0;
143 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
144 } catch (NumberFormatException e) {
145 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
147 xvsm.sendNewGroupsToSpace(groupData, pizzeriaSpacePort);
149 log.info("New normal groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
151 // start the space group in a new thread
152 for (GroupData group : groupData) {
153 new Thread(new SpaceGroup(group.getId(), pizzeriaSpacePort)).start();
159 public void onDeliveryGroupsCreated(List<DeliveryGroup> newDeliveryGroups) {
161 List<DeliveryGroupData> groupData = new ArrayList<>();
162 for (DeliveryGroup group : newDeliveryGroups) {
163 groupData.add(group.getDeliveryGroupData());
165 final String pizzeriaId = groupData.get(0).getPizzeriaId();
167 int pizzeriaSpacePort = 0;
169 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
170 } catch (NumberFormatException e) {
171 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
174 xvsm.sendNewDeliveriesToSpace(groupData, pizzeriaSpacePort);
176 log.info("New delivery groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
178 if (!Util.runSimulation) {
179 for (DeliveryGroupData deliveryGroupData : groupData) {
180 new Thread(new SpaceDeliveryGroup(deliveryGroupData.getId(), pizzeriaSpacePort,
181 deliveryGroupData.getAddress())).start();