From 9c82bb24cba4a24558ad9e9a1a35bb8c0f7c98f1 Mon Sep 17 00:00:00 2001 From: Gregor Riegler Date: Thu, 30 May 2013 16:53:25 +0200 Subject: [PATCH] [XVSM] Waiters, Cooks and Pizzerias can be parameterized with a Space port. Group Agent can create groups for a specific pizzeria. --- .../tuwien/sbc/valesriegler/common/Tuple.java | 12 + .../tuwien/sbc/valesriegler/common/Util.java | 18 +- .../sbc/valesriegler/group/DeliveryGroup.java | 5 + .../sbc/valesriegler/group/GroupAgent.java | 29 +- .../sbc/valesriegler/group/SpaceGroup.java | 5 +- .../group/gui/GroupCreationDetailsPanel.java | 4 +- .../gui/GroupCreationDetailsRequest.java | 6 +- .../group/gui/GroupCreationHandler.java | 54 ++- .../valesriegler/pizzeria/PizzeriaAgent.java | 36 +- .../sbc/valesriegler/types/GroupData.java | 2 +- .../tuwien/sbc/valesriegler/types/Order.java | 7 +- .../xvsm/AbstractXVSMConnector.java | 370 +++++++++--------- .../sbc/valesriegler/xvsm/CookXVSM.java | 7 +- .../sbc/valesriegler/xvsm/DriverXVSM.java | 4 +- .../sbc/valesriegler/xvsm/GroupAgentXVSM.java | 14 +- .../sbc/valesriegler/xvsm/GroupXVSM.java | 11 +- .../valesriegler/xvsm/PizzeriaAgentXVSM.java | 15 +- .../sbc/valesriegler/xvsm/WaiterXVSM.java | 45 +-- .../sbc/valesriegler/xvsm/cook/Cook.java | 30 +- .../sbc/valesriegler/xvsm/driver/Driver.java | 19 +- .../sbc/valesriegler/xvsm/waiter/Waiter.java | 28 +- 21 files changed, 370 insertions(+), 351 deletions(-) create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/common/Tuple.java diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/common/Tuple.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/common/Tuple.java new file mode 100644 index 0000000..e27e031 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/common/Tuple.java @@ -0,0 +1,12 @@ +package at.ac.tuwien.sbc.valesriegler.common; + + +public class Tuple { + public final T snd; + public final T fst; + + public Tuple(T fst, T snd) { + this.fst = fst; + this.snd = snd; + } +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/common/Util.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/common/Util.java index fe2bc4f..3226328 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/common/Util.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/common/Util.java @@ -22,9 +22,10 @@ import javax.swing.border.TitledBorder; public abstract class Util { private static final Logger log = LoggerFactory.getLogger(Util.class); - // TODO: solve the switch between mom by command-line arguments public static boolean useJMS = true; + + public static final String TABLE_ASSIGNED = "tables"; public static final String GROUPS_CONTAINER = "groups"; public static final String ASSIGN_TABLE = "assignTable"; @@ -44,7 +45,8 @@ public abstract class Util { public static final long SPACE_TRANSACTION_TIMEOUT = 1500; - public static final String SERVER_ADDR = "xvsm://localhost:9876"; + public static final String SERVER_ADDR = "xvsm://localhost:%d"; + public static final int GROUP_AGENT_PORT = 9876; public static final String JMS_CONNECTSTRING = "tcp://localhost:61616?jms.prefetchPolicy.all=1"; @@ -56,6 +58,10 @@ public abstract class Util { return nr == null ? 0 : nr; } + public static URI createURI(int port) { + return URI.create(String.format(SERVER_ADDR, port)); + } + public static Map intoMapById(List hasIds) { if (hasIds == null) { return Collections.emptyMap(); @@ -72,19 +78,21 @@ public abstract class Util { return random.nextInt(max - min + 1) + min; } - public static int parseId(String[] args, String usage) { - if (args.length != 1) { + public static Tuple parseIdAndSpacePort(String[] args, String usage) { + if (args.length != 2) { throw new IllegalArgumentException(usage); } int parsedId = 0; + int port = 0; try { parsedId = Integer.parseInt(args[0]); + port = Integer.parseInt(args[1]); } catch (NumberFormatException e) { throw new IllegalArgumentException(usage); } - return parsedId; + return new Tuple(parsedId, port); } public static JTable createTableInTitledPanel(JPanel wrapperPanel, TableModel model, String title) { diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/DeliveryGroup.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/DeliveryGroup.java index f2b726d..36d5ea9 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/DeliveryGroup.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/DeliveryGroup.java @@ -8,9 +8,14 @@ import org.slf4j.LoggerFactory; public class DeliveryGroup implements Runnable, HasId { private static final Logger log = LoggerFactory.getLogger(DeliveryGroup.class); + private static int idNext = 0; // TODO: set to 0 after debugging! private DeliveryGroupData deliveryGroupData; + public DeliveryGroup() { + this.deliveryGroupData = new DeliveryGroupData(++idNext); + } + public DeliveryGroup(int id) { this.deliveryGroupData = new DeliveryGroupData(id); } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupAgent.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupAgent.java index 216f7ac..bcf2251 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupAgent.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupAgent.java @@ -11,6 +11,7 @@ import org.slf4j.LoggerFactory; import javax.swing.*; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -34,7 +35,7 @@ public class GroupAgent { /* Contains the identifiers of the pizzerias. Whenever a new Pizzeria emerges or dies the group agent has to be notified about this somehow and the list has to be adapted as it is the model of the dropdown in the GUI for selecting for which pizzeria the customer groups are created - In case of XVSM the identifier string is a space URI*/ + In case of XVSM the identifier string is a port number*/ private List pizzeriaIdentifiers = Collections.synchronizedList(new ArrayList()); public static GroupAgent groupAgent; @@ -48,23 +49,22 @@ public class GroupAgent { log.info("Middleware: " + mw); if ("JMS".equalsIgnoreCase(mw)) { - groupAgent = new GroupAgent(new JMSGroupConnector()); Util.useJMS = true; } else if ("XVSM".equalsIgnoreCase(mw)) { - // TODO: XVSM Connector? Util.useJMS = false; - groupAgent = new GroupAgent(null); } else { throw new IllegalArgumentException(USAGE); } + groupAgent = new GroupAgent(); + SwingUtilities.invokeLater(new GroupGUI()); } - public GroupAgent(AbstractGroupConnector groupconn) { + public GroupAgent() { groupModel = new GroupOverviewModel(); if (Util.useJMS) { - this.groupconn = groupconn; + this.groupconn = new JMSGroupConnector(); groupconn.init(); } else { xvsm = new GroupAgentXVSM(); @@ -97,24 +97,35 @@ public class GroupAgent { return deliveryOverviewModel; } - public void onGroupsCreated(List newGroups, boolean isDeliveryGroup) { + public void onGroupsCreated(List newGroups) { if (!Util.useJMS) { List groupData = new ArrayList<>(); for (Group group : newGroups) { groupData.add(group.getGroupData()); } - xvsm.sendNewGroupsToSpace(groupData); + final String pizzeriaId = groupData.get(0).getPizzeriaId(); + + int pizzeriaSpacePort = 0; + try { + pizzeriaSpacePort = Integer.parseInt(pizzeriaId); + } catch (NumberFormatException e) { + log.error("Pizzeria Identifier should be an integer in the XVSM version!"); + } + xvsm.sendNewGroupsToSpace(groupData, pizzeriaSpacePort); log.info("New Groups were sent to the space"); // start the space group in a new thread for (GroupData group : groupData) { - new Thread(new SpaceGroup(group.getId())).start(); + new Thread(new SpaceGroup(group.getId(), pizzeriaSpacePort)).start(); } } } + public void onDeliveryGroupsCreated(List newDeliveryGroups) { + //To change body of created methods use File | Settings | File Templates. + } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/SpaceGroup.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/SpaceGroup.java index 0f0ee39..00a2fc5 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/SpaceGroup.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/SpaceGroup.java @@ -5,8 +5,9 @@ import at.ac.tuwien.sbc.valesriegler.xvsm.GroupXVSM; public class SpaceGroup implements Runnable { private GroupXVSM xvsm; - public SpaceGroup(int groupId) { - xvsm = new GroupXVSM(groupId); + public SpaceGroup(int groupId, int pizzeriaSpacePort) { + // TODO add space port of pizzeria! + xvsm = new GroupXVSM(groupId, pizzeriaSpacePort); } @Override diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationDetailsPanel.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationDetailsPanel.java index ad5ca71..d60a965 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationDetailsPanel.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationDetailsPanel.java @@ -28,6 +28,7 @@ public class GroupCreationDetailsPanel extends JPanel { private JSpinner numberOfGroupsSpinner; private List combos = new ArrayList(); private JTextField addressField = new JTextField(); + private final JComboBox pizzeriaDropdown = new JComboBox(); public GroupCreationDetailsPanel(int numberMembers, boolean withAddressField) { this.numberMembers = numberMembers; @@ -53,7 +54,6 @@ public class GroupCreationDetailsPanel extends JPanel { this.add(numberOfGroupsSpinner); final JLabel pizzeriaLabel = new JLabel("Pizzeria: "); - final JComboBox pizzeriaDropdown = new JComboBox(); final List pizzeriaIdentifiers = GroupAgent.getInstance().getPizzeriaIdentifiers(); for (String pizzeriaIdentifier : pizzeriaIdentifiers) { @@ -99,7 +99,7 @@ public class GroupCreationDetailsPanel extends JPanel { for(JComboBox combo : combos) { types.add((PizzaType) combo.getSelectedItem()); } - GroupCreationDetailsRequest gcr = new GroupCreationDetailsRequest(numberMembers, (int) numberOfGroupsSpinner.getValue(), types, addressField.getText()); + GroupCreationDetailsRequest gcr = new GroupCreationDetailsRequest(numberMembers, (int) numberOfGroupsSpinner.getValue(), types, addressField.getText(), (String) pizzeriaDropdown.getSelectedItem()); handler.handleGroupCreation(gcr); } }); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationDetailsRequest.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationDetailsRequest.java index a9b6057..f6ce3ed 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationDetailsRequest.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationDetailsRequest.java @@ -9,12 +9,14 @@ public class GroupCreationDetailsRequest { public final int numberOfGroups; public final List pizzaTypes; public final String address; + public final String pizzeria; - public GroupCreationDetailsRequest(int size, int numberOfGroups, - List pizzaTypes, String address) { + public GroupCreationDetailsRequest(int size, int numberOfGroups, + List pizzaTypes, String address, String pizzeriaIdentifier) { this.size = size; this.numberOfGroups = numberOfGroups; this.pizzaTypes = pizzaTypes; this.address = address; + this.pizzeria = pizzeriaIdentifier; } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationHandler.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationHandler.java index f3864f1..19f4c5e 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationHandler.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationHandler.java @@ -1,5 +1,7 @@ package at.ac.tuwien.sbc.valesriegler.group.gui; +import at.ac.tuwien.sbc.valesriegler.common.HasId; +import at.ac.tuwien.sbc.valesriegler.group.DeliveryGroup; import at.ac.tuwien.sbc.valesriegler.group.Group; import at.ac.tuwien.sbc.valesriegler.group.GroupAgent; import at.ac.tuwien.sbc.valesriegler.types.Order; @@ -42,27 +44,45 @@ class GroupCreationHandler { } public void createGroups(GroupCreationDetailsRequest gc) { - List newGroups = new ArrayList<>(); + List newDeliveryGroups = new ArrayList<>(); + List newNormalGroups = new ArrayList<>(); + for (int i = 0; i < gc.numberOfGroups; i++) { - Group group = new Group(); - group.getGroupData().setSize(gc.size); - List pizzaOrders = new ArrayList<>(); - for (PizzaType pt : gc.pizzaTypes) { - pizzaOrders.add(new PizzaOrder(pt)); + if(isDeliveryGroup) { + DeliveryGroup group = new DeliveryGroup(); + final Order order = createOrder(group, gc); + group.getDeliveryGroupData().setOrder(order); + group.getDeliveryGroupData().setPizzeriaId(gc.pizzeria); + newDeliveryGroups.add(group); + } else { + Group group = new Group(); + final Order order = createOrder(group, gc); + group.getGroupData().setOrder(order); + group.getGroupData().setSize(gc.size); + group.getGroupData().setPizzeriaId(gc.pizzeria); + newNormalGroups.add(group); } - Order order = new Order(group, pizzaOrders); -// the order id has to be set by the waiter - /*for (PizzaOrder pizzaOrder : order.getOrderedPizzas()) { - pizzaOrder.setOrderId(order.getId()); - }*/ - group.getGroupData().setOrder(order); - order.setNumberOfPizzas(order.getOrderedPizzas().size()); + } + if(isDeliveryGroup) { + DeliveryOverviewModel deliveryOverviewModel = GroupAgent.getInstance().getDeliveryModel(); + deliveryOverviewModel.addItems(newDeliveryGroups); + GroupAgent.getInstance().onDeliveryGroupsCreated(newDeliveryGroups); + } else { + GroupOverviewModel groupModel = GroupAgent.getInstance().getGroupModel(); + groupModel.addItems(newNormalGroups); + GroupAgent.getInstance().onGroupsCreated(newNormalGroups); + } + } - newGroups.add(group); + private Order createOrder(HasId group, GroupCreationDetailsRequest gc) { + List pizzaOrders = new ArrayList<>(); + for (PizzaType pt : gc.pizzaTypes) { + pizzaOrders.add(new PizzaOrder(pt)); } - GroupOverviewModel groupModel = GroupAgent.getInstance().getGroupModel(); - groupModel.addItems(newGroups); - GroupAgent.getInstance().onGroupsCreated(newGroups, isDeliveryGroup); + Order order = new Order(group, pizzaOrders); + order.setNumberOfPizzas(order.getOrderedPizzas().size()); + + return order; } public void handleCancel() { diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/PizzeriaAgent.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/PizzeriaAgent.java index 6e3c6d7..9568130 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/PizzeriaAgent.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/PizzeriaAgent.java @@ -24,7 +24,7 @@ import at.ac.tuwien.sbc.valesriegler.xvsm.PizzeriaAgentXVSM; * @author jan */ public class PizzeriaAgent { - private static final String USAGE = "This application needs exactly 1 parameter: <\"XVSM\"|\"JMS\">"; + private static final String USAGE = "This application needs 2 parameters: <\"XVSM\"|\"JMS\"> "; private static final Logger log = LoggerFactory.getLogger(PizzeriaAgent.class); private static PizzeriaAgent pizzeriaAgent; @@ -42,7 +42,7 @@ public class PizzeriaAgent { private JMSPizzeriaConnector jmspc; public static void main(String[] args) { - if (args.length != 1) { + if (args.length != 2) { throw new IllegalArgumentException(USAGE); } String mw = args[0]; @@ -51,7 +51,7 @@ public class PizzeriaAgent { switch (mw) { case "XVSM": pizzeriaAgent.createModels(); - pizzeriaAgent.initXVSM(); + pizzeriaAgent.initXVSM(args[1]); pizzeriaAgent.initGUI(); Util.useJMS = false; break; @@ -71,19 +71,27 @@ public class PizzeriaAgent { jmspc.init(); } - private void initXVSM() { - xvsm = new PizzeriaAgentXVSM(); + private void initXVSM(String arg) { + int port = 0; + try { + port = Integer.parseInt(arg); + } catch (NumberFormatException e) { + log.error("The XVSM-Space-Identifier needs to be an integer port!"); + System.exit(1); + } + + xvsm = new PizzeriaAgentXVSM(port); xvsm.initializeOrderId(); - xvsm.listenForFreeTables(); - xvsm.listenForTakenOrders(); - xvsm.listenForWaitingGroups(); - xvsm.listenForPizzasInPreparation(); - xvsm.listenForDeliveredOrders(); - xvsm.listenForPreparedPizzas(); - xvsm.listenForPayment(); - xvsm.listenForOccupiedTables(); - } + xvsm.listenForFreeTables(); + xvsm.listenForTakenOrders(); + xvsm.listenForWaitingGroups(); + xvsm.listenForPizzasInPreparation(); + xvsm.listenForDeliveredOrders(); + xvsm.listenForPreparedPizzas(); + xvsm.listenForPayment(); + xvsm.listenForOccupiedTables(); + } private void initGUI() { PizzeriaGUI gui = new PizzeriaGUI(); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/GroupData.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/GroupData.java index 27f40b0..094da95 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/GroupData.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/GroupData.java @@ -76,7 +76,7 @@ public class GroupData implements Serializable, HasId { } public int getSize() { - return size; + return Util.getIntSafe(size); } public GroupState getState() { diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Order.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Order.java index 465a785..4d2de24 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Order.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Order.java @@ -39,12 +39,13 @@ public class Order implements Serializable, HasId { this.groupId = groupId; status = OrderStatus.NEW; this.orderedPizzas = orderedPizzas; - cookedPizzas = null; + cookedPizzas = new ArrayList(); } - public Order(Group group, List orderedPizzas) { + public Order(HasId group, List orderedPizzas) { + // TODO don't set the id here but let the waiter set it id = ++idNext; - groupId = group.getGroupData().getId(); + groupId = group.getId(); status = OrderStatus.NEW; this.orderedPizzas = orderedPizzas; cookedPizzas = new ArrayList(); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/AbstractXVSMConnector.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/AbstractXVSMConnector.java index d05c675..95b68fd 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/AbstractXVSMConnector.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/AbstractXVSMConnector.java @@ -1,200 +1,204 @@ package at.ac.tuwien.sbc.valesriegler.xvsm; -import java.io.Serializable; -import java.net.URI; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Date; -import java.util.List; -import java.util.concurrent.atomic.AtomicLong; - +import at.ac.tuwien.sbc.valesriegler.common.HasId; +import at.ac.tuwien.sbc.valesriegler.common.Util; +import at.ac.tuwien.sbc.valesriegler.types.GroupData; import org.mozartspaces.capi3.Coordinator; import org.mozartspaces.capi3.CountNotMetException; import org.mozartspaces.capi3.FifoCoordinator; import org.mozartspaces.capi3.LindaCoordinator; import org.mozartspaces.capi3.LindaCoordinator.LindaSelector; -import org.mozartspaces.core.Capi; -import org.mozartspaces.core.CapiUtil; -import org.mozartspaces.core.ContainerReference; -import org.mozartspaces.core.DefaultMzsCore; -import org.mozartspaces.core.Entry; -import org.mozartspaces.core.MzsConstants; -import org.mozartspaces.core.MzsCore; -import org.mozartspaces.core.MzsCoreException; -import org.mozartspaces.core.MzsTimeoutException; -import org.mozartspaces.core.TransactionReference; +import org.mozartspaces.core.*; +import org.mozartspaces.core.config.CommonsXmlConfiguration; +import org.mozartspaces.core.config.Configuration; import org.mozartspaces.notifications.NotificationManager; import org.mozartspaces.notifications.Operation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import at.ac.tuwien.sbc.valesriegler.common.HasId; -import at.ac.tuwien.sbc.valesriegler.common.Util; -import at.ac.tuwien.sbc.valesriegler.types.GroupData; +import java.io.Serializable; +import java.net.URI; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.List; +import java.util.concurrent.atomic.AtomicLong; public abstract class AbstractXVSMConnector { - public static Object lockObject = new Object(); - public static AtomicLong timeOflastOperation = new AtomicLong(new Date().getTime()); - - private static final Logger log = LoggerFactory.getLogger(AbstractXVSMConnector.class); - - protected ContainerReference tableAssignedContainer; - protected ContainerReference assignTableContainer; - protected ContainerReference takeOrderContainer; - protected ContainerReference orderTakenContainer; - protected ContainerReference preparePizzasContainer; - protected ContainerReference deliverPizzasContainer; - protected ContainerReference paymentRequestContainer; - protected ContainerReference freeTablesContainer; - protected ContainerReference pizzaInProgressContainer; - protected ContainerReference orderCompleteContainer; - protected ContainerReference isEatingContainer; - protected ContainerReference paymentDoneContainer; - protected ContainerReference infoContainer; - protected Capi capi; - protected NotificationManager notificationMgr; - - public AbstractXVSMConnector() { - initSpaceCommunication(); - } - - public void initSpaceCommunication() { - try { - MzsCore core = DefaultMzsCore.newInstanceWithoutSpace(); - capi = new Capi(core); - notificationMgr = new NotificationManager(core); - } catch (Exception e) { - log.error("Space connection could not be established! Have you started the Space Server?"); - handleSpaceErrorAndTerminate(e); - } - } - - protected ContainerReference useContainer(String containerName) { - try { - return CapiUtil.lookupOrCreateContainer(containerName, URI.create(Util.SERVER_ADDR), createCoordinators(new FifoCoordinator(), new LindaCoordinator(false)), null, capi); - } catch (MzsCoreException e) { - handleSpaceErrorAndTerminate(e); - } - - throw new RuntimeException("Could not Create container " + containerName); - } - - protected ContainerReference useContainer(String containerName, List coordinators) { - try { - return CapiUtil.lookupOrCreateContainer(containerName, URI.create(Util.SERVER_ADDR), coordinators, null, capi); - } catch (MzsCoreException e) { - handleSpaceErrorAndTerminate(e); - } - - throw new RuntimeException("Could not Create container " + containerName); - } - - protected List createCoordinators(Coordinator... coordinator) { - return Arrays.asList(coordinator); - } - - protected void handleSpaceErrorAndTerminate(Exception e) { - log.error(e.getMessage()); - e.printStackTrace(); - System.exit(1); - } - - protected void createNotification(SpaceListener listener, - ContainerReference cref) { - listener.startHandlingAbsenceOfNotifications(); - try { - notificationMgr.createNotification(cref, listener, Operation.WRITE); - } catch (Exception e) { - handleSpaceErrorAndTerminate(e); - } - } - - protected void sendItemsToContainer( - List items, ContainerReference cref, long timeout, TransactionReference tx) { - - try { - List entries = new ArrayList<>(); - for (Serializable item : items) { - entries.add(new Entry(item)); - } - capi.write(entries, cref, timeout, tx); - } catch (Exception e) { - log.info(e.getMessage()); - e.printStackTrace(); - } - } - - @SuppressWarnings("unchecked") - protected T takeMatchingEntity( - T entity, ContainerReference ref, TransactionReference tx, long timeout, String errorMsg) - throws MzsCoreException { - try { - LindaSelector sel = LindaCoordinator.newSelector(entity, 1); - - ArrayList entities = capi.take(ref, sel, timeout, tx); - - return (T) CapiUtil.getSingleEntry(entities); - } catch (CountNotMetException e) { - capi.rollbackTransaction(tx); - - throw new EntityNotFoundByTemplate(errorMsg); - } catch(MzsTimeoutException e) { - capi.rollbackTransaction(tx); - - throw new EntityNotFoundByTemplate(errorMsg); - } - } - - protected List takeMatchingEntities( - T entity, ContainerReference ref, TransactionReference tx, long timeout, String errorMsg) - throws MzsCoreException { - try { - LindaSelector sel = LindaCoordinator.newSelector(entity, MzsConstants.Selecting.COUNT_MAX); - - ArrayList entities = capi.take(ref, sel, timeout, tx); - - return (List) entities; - } catch (CountNotMetException e) { - capi.rollbackTransaction(tx); - - throw new EntityNotFoundByTemplate(errorMsg); - } catch(MzsTimeoutException e) { - capi.rollbackTransaction(tx); - - throw new EntityNotFoundByTemplate(errorMsg); - } - } - - protected List castEntries(List entries) { - List newList = new ArrayList(); - if(entries.size() == 0) return newList; - - Serializable firstEntry = entries.get(0); - if (firstEntry instanceof Entry) { - - List newEntries = (List) entries; - for (Entry entry : newEntries) { - newList.add((T) entry.getValue()); - } - return newList; - } else { - return (List) entries; - } - } - - - protected T getSingleEntity(final List entities) { - if(entities.size() != 1) { - throw new RuntimeException("Only one entity was expected!"); + public static Object lockObject = new Object(); + public static AtomicLong timeOflastOperation = new AtomicLong(new Date().getTime()); + + private static final Logger log = LoggerFactory.getLogger(AbstractXVSMConnector.class); + + protected ContainerReference tableAssignedContainer; + protected ContainerReference assignTableContainer; + protected ContainerReference takeOrderContainer; + protected ContainerReference orderTakenContainer; + protected ContainerReference preparePizzasContainer; + protected ContainerReference deliverPizzasContainer; + protected ContainerReference paymentRequestContainer; + protected ContainerReference freeTablesContainer; + protected ContainerReference pizzaInProgressContainer; + protected ContainerReference orderCompleteContainer; + protected ContainerReference isEatingContainer; + protected ContainerReference paymentDoneContainer; + protected ContainerReference infoContainer; + protected Capi capi; + protected NotificationManager notificationMgr; + + protected int port; + + public AbstractXVSMConnector(int port) { + this.port = port; + initSpaceCommunication(port); + } + + public void initSpaceCommunication(int port) { + try { +// Configuration config = CommonsXmlConfiguration.load(0); +// config.setEmbeddedSpace(false); + MzsCore core = DefaultMzsCore.newInstanceWithoutSpace(); + capi = new Capi(core); + notificationMgr = new NotificationManager(core); + } catch (Exception e) { + log.error("Space connection could not be established! Have you started the Space Server?"); + handleSpaceErrorAndTerminate(e); + } + } + + protected ContainerReference useContainer(String containerName) { + return useContainerOfSpaceWithPort(containerName, port); + } + + protected ContainerReference useContainerOfSpaceWithPort(String containerName, int spacePort) { + try { + final String address = String.format(Util.SERVER_ADDR, spacePort); + return CapiUtil.lookupOrCreateContainer(containerName, URI.create(address), createCoordinators(new FifoCoordinator(), new LindaCoordinator(false)), null, capi); + } catch (MzsCoreException e) { + handleSpaceErrorAndTerminate(e); } - return entities.get(0); - } - - protected GroupData getSingleGroup(final List entities) { - List groups = castEntries(entities); - if(groups.size() != 1) { - throw new RuntimeException("Only one group was expected!"); + + throw new RuntimeException("Could not Create container " + containerName); + } + + protected List createCoordinators(Coordinator... coordinator) { + return Arrays.asList(coordinator); + } + + protected void handleSpaceErrorAndTerminate(Exception e) { + log.error(e.getMessage()); + e.printStackTrace(); + System.exit(1); + } + + protected void createNotification(SpaceListener listener, + ContainerReference cref) { + listener.startHandlingAbsenceOfNotifications(); + try { + notificationMgr.createNotification(cref, listener, Operation.WRITE); + } catch (Exception e) { + handleSpaceErrorAndTerminate(e); } - return groups.get(0); - } + } + + protected void sendItemsToContainer( + List items, ContainerReference cref, long timeout, TransactionReference tx) { + + try { + List entries = new ArrayList<>(); + for (Serializable item : items) { + entries.add(new Entry(item)); + } + capi.write(entries, cref, timeout, tx); + } catch (Exception e) { + log.info(e.getMessage()); + e.printStackTrace(); + } + } + + @SuppressWarnings("unchecked") + /** + * Searches for one entity matching the given template object by linda selection. + */ + protected T takeMatchingEntity( + T entity, ContainerReference ref, TransactionReference tx, long timeout, String errorMsg) + throws MzsCoreException { + try { + LindaSelector sel = LindaCoordinator.newSelector(entity, 1); + + ArrayList entities = capi.take(ref, sel, timeout, tx); + + return (T) CapiUtil.getSingleEntry(entities); + } catch (CountNotMetException e) { + capi.rollbackTransaction(tx); + + throw new EntityNotFoundByTemplate(errorMsg); + } catch (MzsTimeoutException e) { + capi.rollbackTransaction(tx); + + throw new EntityNotFoundByTemplate(errorMsg); + } + } + + /** + * Searches for all entities matching the given template object by linda selection. + */ + protected List takeMatchingEntities( + T entity, ContainerReference ref, TransactionReference tx, long timeout, String errorMsg) + throws MzsCoreException { + try { + LindaSelector sel = LindaCoordinator.newSelector(entity, MzsConstants.Selecting.COUNT_MAX); + + ArrayList entities = capi.take(ref, sel, timeout, tx); + + return (List) entities; + } catch (CountNotMetException e) { + capi.rollbackTransaction(tx); + + throw new EntityNotFoundByTemplate(errorMsg); + } catch (MzsTimeoutException e) { + capi.rollbackTransaction(tx); + + throw new EntityNotFoundByTemplate(errorMsg); + } + } + + protected List castEntries(List entries) { + List newList = new ArrayList(); + if (entries.size() == 0) return newList; + + Serializable firstEntry = entries.get(0); + if (firstEntry instanceof Entry) { + + List newEntries = (List) entries; + for (Entry entry : newEntries) { + newList.add((T) entry.getValue()); + } + return newList; + } else { + return (List) entries; + } + } + + + protected T getSingleEntity(final List entities) { + if (entities.size() != 1) { + throw new RuntimeException("Only one entity was expected!"); + } + return entities.get(0); + } + + protected GroupData getSingleGroup(final List entities) { + List groups = castEntries(entities); + if (groups.size() != 1) { + throw new RuntimeException("Only one group was expected!"); + } + return groups.get(0); + } + + protected TransactionReference getDefaultTransaction() throws MzsCoreException { + return capi.createTransaction( + Util.SPACE_TRANSACTION_TIMEOUT, + URI.create(String.format(Util.SERVER_ADDR, port))); + } } \ No newline at end of file diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/CookXVSM.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/CookXVSM.java index 5845222..525cea5 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/CookXVSM.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/CookXVSM.java @@ -7,7 +7,6 @@ import java.util.Collections; import java.util.List; import org.mozartspaces.capi3.FifoCoordinator; -import org.mozartspaces.capi3.LindaCoordinator; import org.mozartspaces.core.MzsConstants; import org.mozartspaces.core.MzsConstants.RequestTimeout; import org.mozartspaces.core.TransactionReference; @@ -24,8 +23,8 @@ public class CookXVSM extends AbstractXVSMConnector { private int cookId; - public CookXVSM(int id) { - super(); + public CookXVSM(int id, int port) { + super(port); this.cookId = id; orderTakenContainer = useContainer(Util.ORDER_TAKEN) ; @@ -47,7 +46,7 @@ public class CookXVSM extends AbstractXVSMConnector { for (PizzaOrder pizzaOrder : pizzas) { - TransactionReference tx = capi.createTransaction(9000, URI.create(Util.SERVER_ADDR)); + TransactionReference tx = capi.createTransaction(9000, URI.create(String.format(Util.SERVER_ADDR, port))); String pizzaAlreadyCooked = String.format("Pizza with id %d has already been cooked by another cook", pizzaOrder.getId()); try { diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/DriverXVSM.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/DriverXVSM.java index 5e954f2..69a6393 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/DriverXVSM.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/DriverXVSM.java @@ -9,8 +9,8 @@ public class DriverXVSM extends AbstractXVSMConnector { private int driverId; - public DriverXVSM(int id) { - super(); + public DriverXVSM(int id, int port) { + super(port); this.driverId = id; diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupAgentXVSM.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupAgentXVSM.java index a348677..2bb5f4c 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupAgentXVSM.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupAgentXVSM.java @@ -5,16 +5,10 @@ import java.util.List; import javax.swing.SwingUtilities; -import org.mozartspaces.capi3.FifoCoordinator; -import org.mozartspaces.capi3.LindaCoordinator; import org.mozartspaces.core.MzsConstants.RequestTimeout; -import org.mozartspaces.notifications.Notification; -import org.mozartspaces.notifications.NotificationListener; -import org.mozartspaces.notifications.Operation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import at.ac.tuwien.sbc.valesriegler.common.HasId; import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.group.GroupAgent; import at.ac.tuwien.sbc.valesriegler.types.GroupData; @@ -24,8 +18,8 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { private static final Logger log = LoggerFactory.getLogger(GroupAgentXVSM.class); public GroupAgentXVSM() { - super(); - assignTableContainer = useContainer(Util.ASSIGN_TABLE); + super(Util.GROUP_AGENT_PORT); +// assignTableContainer = useContainer(Util.ASSIGN_TABLE); orderCompleteContainer = useContainer(Util.ORDER_COMPLETE) ; paymentRequestContainer = useContainer(Util.PAYMENT_REQUEST) ; paymentDoneContainer = useContainer(Util.PAYMENT_DONE); @@ -130,8 +124,8 @@ public class GroupAgentXVSM extends AbstractXVSMConnector { createNotification(tableAssignmentListener, orderTakenContainer); } - public void sendNewGroupsToSpace(List groupData) { - sendItemsToContainer(groupData, assignTableContainer, RequestTimeout.DEFAULT, null); + public void sendNewGroupsToSpace(List groupData, int pizzeriaSpacePort) { + sendItemsToContainer(groupData, useContainerOfSpaceWithPort(Util.ASSIGN_TABLE, pizzeriaSpacePort), RequestTimeout.DEFAULT, null); } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupXVSM.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupXVSM.java index bca5363..6a2b0d8 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupXVSM.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupXVSM.java @@ -5,10 +5,6 @@ import java.util.Arrays; import java.util.List; import java.util.Random; -import javax.swing.SwingUtilities; - -import org.mozartspaces.capi3.AnyCoordinator; -import org.mozartspaces.capi3.LindaCoordinator; import org.mozartspaces.core.MzsConstants.RequestTimeout; import org.mozartspaces.notifications.Notification; import org.mozartspaces.notifications.NotificationListener; @@ -19,7 +15,6 @@ import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.types.GroupData; import at.ac.tuwien.sbc.valesriegler.types.Order; -import at.ac.tuwien.sbc.valesriegler.types.Table; public class GroupXVSM extends AbstractXVSMConnector { private static final Logger log = LoggerFactory.getLogger(GroupXVSM.class); @@ -28,8 +23,8 @@ public class GroupXVSM extends AbstractXVSMConnector { private Random random = new Random(); - public GroupXVSM(int groupId) { - super(); + public GroupXVSM(int groupId, int port) { + super(port); this.groupId = groupId; paymentRequestContainer = useContainer(Util.PAYMENT_REQUEST) ; @@ -39,7 +34,7 @@ public class GroupXVSM extends AbstractXVSMConnector { } public void waitForMyOrder() { - log.info("Thred started for group {}", groupId); + log.info("Thread started for group {}", groupId); NotificationListener deliveredOrders = new NotificationListener() { @Override public void entryOperationFinished(final Notification notification, final Operation operation, final List entries) { diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/PizzeriaAgentXVSM.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/PizzeriaAgentXVSM.java index 84b1d3d..b5824ab 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/PizzeriaAgentXVSM.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/PizzeriaAgentXVSM.java @@ -1,19 +1,12 @@ package at.ac.tuwien.sbc.valesriegler.xvsm; import java.io.Serializable; -import java.net.URI; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; import javax.swing.SwingUtilities; import at.ac.tuwien.sbc.valesriegler.common.OrderId; -import org.mozartspaces.capi3.AnyCoordinator; -import org.mozartspaces.capi3.FifoCoordinator; -import org.mozartspaces.capi3.LindaCoordinator; -import org.mozartspaces.core.MzsCoreException; -import org.mozartspaces.core.TransactionReference; import org.mozartspaces.core.MzsConstants.RequestTimeout; import org.mozartspaces.notifications.Notification; import org.mozartspaces.notifications.NotificationListener; @@ -24,8 +17,6 @@ import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.common.Util; import at.ac.tuwien.sbc.valesriegler.pizzeria.PizzeriaAgent; import at.ac.tuwien.sbc.valesriegler.types.GroupData; -import at.ac.tuwien.sbc.valesriegler.types.GroupState; -import at.ac.tuwien.sbc.valesriegler.types.Order; import at.ac.tuwien.sbc.valesriegler.types.Pizza; import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder; import at.ac.tuwien.sbc.valesriegler.types.Table; @@ -33,8 +24,8 @@ import at.ac.tuwien.sbc.valesriegler.types.Table; public class PizzeriaAgentXVSM extends AbstractXVSMConnector { private static final Logger log = LoggerFactory.getLogger(PizzeriaAgentXVSM.class); - public PizzeriaAgentXVSM() { - super(); + public PizzeriaAgentXVSM(int port) { + super(port); tableAssignedContainer = useContainer(Util.TABLE_ASSIGNED) ; freeTablesContainer = useContainer(Util.FREE_TABLES); pizzaInProgressContainer = useContainer(Util.PIZZAS_IN_PROGRESS) ; @@ -270,6 +261,6 @@ public void listenForTakenOrders() { public void initializeOrderId() { - sendItemsToContainer(Arrays.asList(new OrderId(1)), infoContainer, RequestTimeout.DEFAULT, null); + sendItemsToContainer(Arrays.asList(new OrderId(0)), infoContainer, RequestTimeout.DEFAULT, null); } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/WaiterXVSM.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/WaiterXVSM.java index 760c314..9e50245 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/WaiterXVSM.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/WaiterXVSM.java @@ -1,26 +1,14 @@ package at.ac.tuwien.sbc.valesriegler.xvsm; import java.io.Serializable; -import java.net.URI; import java.util.Arrays; -import java.util.Collections; -import java.util.Iterator; import java.util.List; import at.ac.tuwien.sbc.valesriegler.common.OrderId; import at.ac.tuwien.sbc.valesriegler.types.*; -import org.mozartspaces.capi3.AnyCoordinator; -import org.mozartspaces.capi3.FifoCoordinator; -import org.mozartspaces.capi3.LindaCoordinator; -import org.mozartspaces.core.ContainerReference; -import org.mozartspaces.core.MzsConstants; import org.mozartspaces.core.MzsConstants.RequestTimeout; import org.mozartspaces.core.MzsCoreException; -import org.mozartspaces.core.TransactionException; import org.mozartspaces.core.TransactionReference; -import org.mozartspaces.notifications.Notification; -import org.mozartspaces.notifications.NotificationListener; -import org.mozartspaces.notifications.Operation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -30,8 +18,8 @@ public class WaiterXVSM extends AbstractXVSMConnector { private static final Logger log = LoggerFactory.getLogger(WaiterXVSM.class); private final int waiterId; - public WaiterXVSM(int waiterId) { - super(); + public WaiterXVSM(int waiterId, int port) { + super(port); this.waiterId = waiterId; @@ -58,9 +46,7 @@ public class WaiterXVSM extends AbstractXVSMConnector { for (Table table : tables) { - TransactionReference tx = capi.createTransaction( - Util.SPACE_TRANSACTION_TIMEOUT, - URI.create(Util.SERVER_ADDR)); + TransactionReference tx = getDefaultTransaction(); // Acquire a lock for the free table in the // FreeTableContainer @@ -103,9 +89,7 @@ public class WaiterXVSM extends AbstractXVSMConnector { for (GroupData group : groups) { - TransactionReference tx = capi.createTransaction( - Util.SPACE_TRANSACTION_TIMEOUT, - URI.create(Util.SERVER_ADDR)); + TransactionReference tx = getDefaultTransaction(); // Acquire a lock for the group in the // AssignTableContainer @@ -133,7 +117,7 @@ public class WaiterXVSM extends AbstractXVSMConnector { createNotification(listener, assignTableContainer); } - public void listenForPaymentRequest() { + public void listenForPaymentRequest() { SpaceListener paymentListener = new SpaceListenerImpl(capi, paymentRequestContainer) { @Override @@ -143,9 +127,7 @@ public class WaiterXVSM extends AbstractXVSMConnector { List groups = castEntries(entries); for (GroupData groupData : groups) { - TransactionReference tx = capi.createTransaction( - Util.SPACE_TRANSACTION_TIMEOUT, - URI.create(Util.SERVER_ADDR)); + TransactionReference tx = getDefaultTransaction(); GroupData entity = new GroupData(groupData.getId()); // Acquire the lock so that another waiter can't do the same @@ -182,9 +164,7 @@ public class WaiterXVSM extends AbstractXVSMConnector { for (GroupData groupData : groups) { - TransactionReference tx = capi.createTransaction( - Util.SPACE_TRANSACTION_TIMEOUT, - URI.create(Util.SERVER_ADDR)); + TransactionReference tx = getDefaultTransaction(); GroupData entity = new GroupData(groupData.getId()); entity.setState(GroupState.SITTING); @@ -204,12 +184,13 @@ public class WaiterXVSM extends AbstractXVSMConnector { update the space */ final OrderId orderId = takeMatchingEntity(new OrderId(null), infoContainer, tx, RequestTimeout.INFINITE, "The Order id object could not be taken from the space!"); final int id = orderId.getId(); - order.setId(id); + final int nextId = id+1; + order.setId(nextId); final List orderedPizzas = order.getOrderedPizzas(); for (PizzaOrder orderedPizza : orderedPizzas) { - orderedPizza.setId(id); + orderedPizza.setOrderId(nextId); } - sendItemsToContainer(Arrays.asList(new OrderId(id+1)), infoContainer, RequestTimeout.DEFAULT, tx); + sendItemsToContainer(Arrays.asList(new OrderId(nextId)), infoContainer, RequestTimeout.DEFAULT, tx); // send the order as a whole to the space sendItemsToContainer(Arrays.asList(groupData), @@ -250,9 +231,7 @@ public class WaiterXVSM extends AbstractXVSMConnector { for (Pizza pizza : pizzas) { int orderId = pizza.getOrderId(); - TransactionReference tx = capi.createTransaction( - Util.SPACE_TRANSACTION_TIMEOUT, - URI.create(Util.SERVER_ADDR)); + TransactionReference tx = getDefaultTransaction(); try { GroupData entity = new GroupData(); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/cook/Cook.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/cook/Cook.java index 9347184..2f15e90 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/cook/Cook.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/cook/Cook.java @@ -1,44 +1,38 @@ package at.ac.tuwien.sbc.valesriegler.xvsm.cook; +import at.ac.tuwien.sbc.valesriegler.common.Tuple; +import at.ac.tuwien.sbc.valesriegler.common.Util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.xvsm.CookXVSM; public class Cook { - private static final String USAGE = "Cook needs exactly one parameter: ID of type Integer"; + private static final String USAGE = "Cook needs exactly two integer parameters: COOKID, PIZZERIA-SPACE-PORT"; private static final Logger log = LoggerFactory.getLogger(Cook.class); - - private int id; + + private final int pizzeriaPort; + private final int id; private CookXVSM xvsm; public static void main(String[] args) { - if(args.length != 1) { - throw new IllegalArgumentException(USAGE); - } - - int parsedId = 0; - try { - parsedId = Integer.parseInt(args[0]); - } catch (NumberFormatException e) { - log.error(USAGE); - return; - } - - Cook cook = new Cook(parsedId); + final Tuple cookIdAndSpacePort = Util.parseIdAndSpacePort(args, USAGE); + + Cook cook = new Cook(cookIdAndSpacePort.fst, cookIdAndSpacePort.snd); cook.start(); } private void start() { - xvsm = new CookXVSM(id); + xvsm = new CookXVSM(id, pizzeriaPort); xvsm.listenForPizzas(); } - public Cook(int id) { + public Cook(int id, int pizzeriaPort) { this.id = id; + this.pizzeriaPort = pizzeriaPort; log.info("I AM A Cook WITH ID {}", id); } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/driver/Driver.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/driver/Driver.java index ba14e48..3ce9980 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/driver/Driver.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/driver/Driver.java @@ -1,33 +1,34 @@ package at.ac.tuwien.sbc.valesriegler.xvsm.driver; +import at.ac.tuwien.sbc.valesriegler.common.Tuple; import at.ac.tuwien.sbc.valesriegler.common.Util; -import at.ac.tuwien.sbc.valesriegler.xvsm.CookXVSM; import at.ac.tuwien.sbc.valesriegler.xvsm.DriverXVSM; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Driver { - private static final String USAGE = "Driver needs exactly one parameter: ID of type Integer"; + private static final String USAGE = "Driver needs exactly two integer parameters: DRIVERID, PIZZERIA-SPACE-PORT"; private static final Logger log = LoggerFactory.getLogger(Driver.class); - private int id; + private final int port; + private final int id; private DriverXVSM xvsm; public static void main(String[] args) { - final int parsedId = Util.parseId(args, USAGE); + final Tuple driverIdAndSpacePort = Util.parseIdAndSpacePort(args, USAGE); - Driver cook = new Driver(parsedId); - cook.start(); + Driver driver = new Driver(driverIdAndSpacePort.fst, driverIdAndSpacePort.snd); + driver.start(); } private void start() { - xvsm = new DriverXVSM(id); - + xvsm = new DriverXVSM(id, port); } - public Driver(int id) { + public Driver(int id, int port) { this.id = id; + this.port = port; log.info("I AM A Driver WITH ID {}", id); } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/waiter/Waiter.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/waiter/Waiter.java index 71a867e..9a553b6 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/waiter/Waiter.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/waiter/Waiter.java @@ -2,6 +2,8 @@ package at.ac.tuwien.sbc.valesriegler.xvsm.waiter; import java.io.Serializable; +import at.ac.tuwien.sbc.valesriegler.common.Tuple; +import at.ac.tuwien.sbc.valesriegler.common.Util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -14,31 +16,22 @@ import at.ac.tuwien.sbc.valesriegler.xvsm.WaiterXVSM; * */ public class Waiter implements Serializable { - private static final String USAGE = "Waiter needs exactly one parameter: ID of type Integer"; + private static final String USAGE = "Waiter needs exactly two integer parameters: WAITERID, PIZZERIA-SPACE-PORT"; private static final Logger log = LoggerFactory.getLogger(Waiter.class); - private int id; + private final int port; + private final int id; private WaiterXVSM xvsm; public static void main(String[] args) { - if (args.length != 1) { - throw new IllegalArgumentException(USAGE); - } - - int parsedId = 0; - try { - parsedId = Integer.parseInt(args[0]); - } catch (NumberFormatException e) { - log.error(USAGE); - return; - } - - Waiter waiter = new Waiter(parsedId); + final Tuple waiterIdAndSpacePort = Util.parseIdAndSpacePort(args, USAGE); + + Waiter waiter = new Waiter(waiterIdAndSpacePort.fst, waiterIdAndSpacePort.snd); waiter.start(); } private void start() { - xvsm = new WaiterXVSM(id); + xvsm = new WaiterXVSM(id, port); xvsm.listenForOrderRequests(); @@ -54,8 +47,9 @@ public class Waiter implements Serializable { } - public Waiter(int id) { + public Waiter(int id, int port) { this.id = id; + this.port = port; log.info("I AM A WAITER WITH ID {}", id); } -- 2.43.0