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.group.jms.JMSGroupConnector;
9 import at.ac.tuwien.sbc.valesriegler.jms.nac.JMSNAC;
10 import at.ac.tuwien.sbc.valesriegler.jms.nac.actions.BenchmarkStart;
11 import at.ac.tuwien.sbc.valesriegler.jms.nac.actions.BenchmarkStop;
12 import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData;
13 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
14 import at.ac.tuwien.sbc.valesriegler.types.Order;
15 import at.ac.tuwien.sbc.valesriegler.types.PizzaType;
16 import at.ac.tuwien.sbc.valesriegler.xvsm.GroupAgentXVSM;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 import javax.jms.JMSException;
22 import java.lang.management.ManagementFactory;
24 import java.util.concurrent.atomic.AtomicLong;
27 * The Main class of the Group component.
29 * Start the communication and the group GUI:
31 * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
34 public class GroupAgent {
35 private static final String USAGE = "This application needs exactly 1 parameter: <\"XVSM\"|\"JMS\">";
37 private static final Logger log = LoggerFactory.getLogger(GroupAgent.class);
38 private GroupOverviewModel groupModel;
39 private DeliveryOverviewModel deliveryOverviewModel;
40 private GroupAgentXVSM xvsm;
41 static private JMSNAC jmsnac;
42 static private GroupGUI groupGui;
44 * Contains the identifiers of the pizzerias. Whenever a new Pizzeria emerges or dies the group agent has to be
45 * notified about this somehow and the list has to be adapted as it is the model of the dropdown in the GUI for
46 * selecting for which pizzeria the customer groups are created
47 * In case of XVSM the identifier string is a port number
49 private Set<String> pizzeriaIdentifiers = Collections.synchronizedSet(new HashSet<String>());
51 public static GroupAgent groupAgent;
56 public static AtomicLong startTime = new AtomicLong();
58 public static void main(String[] args) {
59 if (args.length < 1) {
60 throw new IllegalArgumentException(USAGE);
64 log.warn("Middleware: " + mw);
65 log.warn("JVM: " + ManagementFactory.getRuntimeMXBean().getName());
66 log.warn("THREAD: " + Thread.currentThread().getName());
68 if ("JMS".equalsIgnoreCase(mw)) {
70 } else if ("XVSM".equalsIgnoreCase(mw)) {
73 throw new IllegalArgumentException(USAGE);
76 groupAgent = new GroupAgent();
77 if (!Util.runSimulation) {
78 groupGui = new GroupGUI();
79 SwingUtilities.invokeLater(groupGui);
80 } else if (Util.useJMS) {
81 JMSGroupConnector.getConnectors().put("tcp://localhost:61621?jms.prefetchPolicy.all=1",
82 new JMSGroupConnector("tcp://localhost:61621?jms.prefetchPolicy.all=1"));
83 JMSGroupConnector.getConnectors().put("tcp://localhost:61622?jms.prefetchPolicy.all=1",
84 new JMSGroupConnector("tcp://localhost:61622?jms.prefetchPolicy.all=1"));
86 List<PizzaType> pizzaTypes1 = Arrays.asList(PizzaType.CARDINALE, PizzaType.SALAMI, PizzaType.MARGHERITA,
87 PizzaType.MARGHERITA);
88 List<PizzaType> pizzaTypes2 = Arrays.asList(PizzaType.CARDINALE, PizzaType.SALAMI, PizzaType.MARGHERITA);
89 List<PizzaType> pizzaTypes3 = Arrays.asList(PizzaType.SALAMI, PizzaType.MARGHERITA);
91 final String pizzeria1 = "tcp://localhost:61621?jms.prefetchPolicy.all=1";
92 final String pizzeria2 = "tcp://localhost:61622?jms.prefetchPolicy.all=1";
93 List<DeliveryGroup> groups1 = GroupAgent.getInstance().createGroups(pizzaTypes1, pizzeria1, 4);
94 List<DeliveryGroup> groups2 = GroupAgent.getInstance().createGroups(pizzaTypes2, pizzeria2, 3);
95 List<DeliveryGroup> groups3 = GroupAgent.getInstance().createGroups(pizzaTypes3, pizzeria2, 2);
97 final DeliveryOverviewModel deliveryModel = GroupAgent.getInstance().getDeliveryModel();
98 deliveryModel.addItems(groups1);
99 deliveryModel.addItems(groups2);
100 deliveryModel.addItems(groups3);
102 GroupAgent.getInstance().onDeliveryGroupsCreated(groups2);
103 GroupAgent.getInstance().onDeliveryGroupsCreated(groups3);
104 GroupAgent.getInstance().onDeliveryGroupsCreated(groups1);
106 log.warn("Starting benchmark in 20 sec");
108 startTime.set(new Date().getTime());
109 jmsnac.sendNACMsg(new BenchmarkStart());
111 jmsnac.sendNACMsg(new BenchmarkStop());
112 log.warn("Benchmark Stop Signal sent.");
113 } catch (InterruptedException | JMSException e) {
114 log.warn("EXCEPTION!", e);
119 public GroupAgent() {
120 deliveryOverviewModel = new DeliveryOverviewModel();
121 groupModel = new GroupOverviewModel();
123 GroupJMSNACMsgListener tmp = new GroupJMSNACMsgListener();
124 jmsnac = new JMSNAC(tmp);
127 xvsm = new GroupAgentXVSM();
129 xvsm.listenForNewPizzerias();
131 if (Util.runSimulation) {
132 xvsm.listenToPizzeria("9875");
133 xvsm.listenToPizzeria("9874");
134 xvsm.runSimulation();
139 public List<DeliveryGroup> createGroups(List<PizzaType> pizzaTypes1, String pizzeria, int number) {
140 List<DeliveryGroup> newDeliveryGroups = new ArrayList<>();
141 final String fakeAddress = "address";
142 final GroupCreationDetailsRequest gc = new GroupCreationDetailsRequest(number, 100, pizzaTypes1, fakeAddress,
145 for (int i = 0; i < gc.numberOfGroups; i++) {
146 DeliveryGroup group = new DeliveryGroup();
147 final Order order = Util.createOrder(group, gc, true);
148 group.getDeliveryGroupData().setOrder(order);
149 group.getDeliveryGroupData().setPizzeriaId(gc.pizzeria);
150 group.getDeliveryGroupData().setAddress(gc.address);
151 newDeliveryGroups.add(group);
154 return newDeliveryGroups;
157 public static GroupAgent getInstance() {
161 public Set<String> getPizzeriaIdentifiers() {
162 return pizzeriaIdentifiers;
165 public GroupOverviewModel getGroupModel() {
169 public static GroupGUI getGroupGui() {
173 public static JMSNAC getJmsnac() {
177 public DeliveryOverviewModel getDeliveryModel() {
178 return deliveryOverviewModel;
181 public void onGroupsCreated(List<Group> newGroups) {
183 List<GroupData> groupData = new ArrayList<>();
185 for (Group group : newGroups) {
186 groupData.add(group.getGroupData());
188 final String pizzeriaId = groupData.get(0).getPizzeriaId();
190 int pizzeriaSpacePort = 0;
192 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
193 } catch (NumberFormatException e) {
194 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
196 xvsm.sendNewGroupsToSpace(groupData, pizzeriaSpacePort);
198 log.info("New normal groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
200 // start the space group in a new thread
201 for (GroupData group : groupData) {
202 new Thread(new SpaceGroup(group.getId(), pizzeriaSpacePort)).start();
208 public void onDeliveryGroupsCreated(List<DeliveryGroup> newDeliveryGroups) {
210 List<DeliveryGroupData> groupData = new ArrayList<>();
211 for (DeliveryGroup group : newDeliveryGroups) {
212 groupData.add(group.getDeliveryGroupData());
214 final String pizzeriaId = groupData.get(0).getPizzeriaId();
216 int pizzeriaSpacePort = 0;
218 pizzeriaSpacePort = Integer.parseInt(pizzeriaId);
219 } catch (NumberFormatException e) {
220 log.error("Pizzeria Identifier should be an integer in the XVSM version!");
223 xvsm.sendNewDeliveriesToSpace(groupData, pizzeriaSpacePort);
225 log.info("New delivery groups were sent to the pizzeria space of port {}", pizzeriaSpacePort);
227 if (!Util.runSimulation) {
228 for (DeliveryGroupData deliveryGroupData : groupData) {
229 new Thread(new SpaceDeliveryGroup(deliveryGroupData.getId(), pizzeriaSpacePort,
230 deliveryGroupData.getAddress())).start();