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.JMSGroupConnector;
8 import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData;
9 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
10 import at.ac.tuwien.sbc.valesriegler.types.Order;
11 import at.ac.tuwien.sbc.valesriegler.types.PizzaType;
12 import at.ac.tuwien.sbc.valesriegler.xvsm.GroupAgentXVSM;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
20 * The Main class of the Group component.
22 * Start the communication and the group GUI:
24 * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
27 public class GroupAgent {
28 private static final String USAGE = "This application needs exactly 1 parameter: <\"XVSM\"|\"JMS\">";
30 private static final Logger log = LoggerFactory.getLogger(GroupAgent.class);
31 private GroupOverviewModel groupModel;
32 private AbstractGroupConnector groupconn;
33 private DeliveryOverviewModel deliveryOverviewModel;
34 private GroupAgentXVSM xvsm;
37 * Contains the identifiers of the pizzerias. Whenever a new Pizzeria emerges or dies the group agent has to be
38 * notified about this somehow and the list has to be adapted as it is the model of the dropdown in the GUI for
39 * selecting for which pizzeria the customer groups are created
40 * In case of XVSM the identifier string is a port number
42 private Set<String> pizzeriaIdentifiers = Collections.synchronizedSet(new HashSet<String>());
44 public static GroupAgent groupAgent;
46 public static void main(String[] args) {
47 if (args.length != 1) {
48 // throw new IllegalArgumentException(USAGE);
51 String mw = "JMS";// args[0];
52 log.info("Middleware: " + mw);
54 if ("JMS".equalsIgnoreCase(mw)) {
56 } else if ("XVSM".equalsIgnoreCase(mw)) {
59 throw new IllegalArgumentException(USAGE);
62 groupAgent = new GroupAgent();
64 if (! Util.runSimulation) {
65 SwingUtilities.invokeLater(new GroupGUI());
70 deliveryOverviewModel = new DeliveryOverviewModel();
71 groupModel = new GroupOverviewModel();
73 groupconn = new JMSGroupConnector();
77 xvsm = new GroupAgentXVSM();
79 xvsm.listenForNewPizzerias();
81 if (Util.runSimulation) {
82 xvsm.listenToPizzeria("9875");
83 xvsm.listenToPizzeria("9874");
89 public List<DeliveryGroup> createGroups(List<PizzaType> pizzaTypes1, String pizzeria, int number) {
90 List<DeliveryGroup> newDeliveryGroups = new ArrayList<>();
91 final String fakeAddress = "address";
92 final GroupCreationDetailsRequest gc = new GroupCreationDetailsRequest(number, 100, pizzaTypes1, fakeAddress, pizzeria);
94 for (int i = 0; i < gc.numberOfGroups; i++) {
95 DeliveryGroup group = new DeliveryGroup();
96 final Order order = Util.createOrder(group, gc);
97 group.getDeliveryGroupData().setOrder(order);
98 group.getDeliveryGroupData().setPizzeriaId(gc.pizzeria);
99 group.getDeliveryGroupData().setAddress(gc.address);
100 newDeliveryGroups.add(group);
103 return newDeliveryGroups;
106 public static GroupAgent getInstance() {
110 public AbstractGroupConnector getGroupcomm() {
114 public Set<String> getPizzeriaIdentifiers() {
115 return pizzeriaIdentifiers;
118 public GroupOverviewModel getGroupModel() {
122 public DeliveryOverviewModel getDeliveryModel() {
123 return deliveryOverviewModel;
126 public void onGroupsCreated(List<Group> newGroups) {
128 List<GroupData> groupData = new ArrayList<>();
130 for (Group group : newGroups) {
131 groupData.add(group.getGroupData());
133 final String pizzeriaId = groupData.get(0).getPizzeriaId();
135 int pizzeriaSpacePort = 0;
137 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
138 } catch (NumberFormatException e) {
139 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
141 xvsm.sendNewGroupsToSpace(groupData, pizzeriaSpacePort);
143 log.info("New normal groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
145 // start the space group in a new thread
146 for (GroupData group : groupData) {
147 new Thread(new SpaceGroup(group.getId(), pizzeriaSpacePort)).start();
153 public void onDeliveryGroupsCreated(List<DeliveryGroup> newDeliveryGroups) {
155 List<DeliveryGroupData> groupData = new ArrayList<>();
156 for (DeliveryGroup group : newDeliveryGroups) {
157 groupData.add(group.getDeliveryGroupData());
159 final String pizzeriaId = groupData.get(0).getPizzeriaId();
161 int pizzeriaSpacePort = 0;
163 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
164 } catch (NumberFormatException e) {
165 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
168 xvsm.sendNewDeliveriesToSpace(groupData, pizzeriaSpacePort);
170 log.info("New delivery groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
172 if (!Util.runSimulation) {
173 for (DeliveryGroupData deliveryGroupData : groupData) {
174 new Thread(new SpaceDeliveryGroup(deliveryGroupData.getId(), pizzeriaSpacePort, deliveryGroupData.getAddress())).start();