From 426099ee58ff639d6bdc6e78aab7cf2d52ab1bb3 Mon Sep 17 00:00:00 2001 From: Gregor Riegler Date: Sun, 5 May 2013 22:25:58 +0200 Subject: [PATCH] Implement handleWaitingGroup+handleOrderRequest for XVSM waiter --- .../sbc/valesriegler/common/SpaceUtil.java | 1 + .../sbc/valesriegler/group/GroupAgent.java | 19 ++ .../valesriegler/pizzeria/PizzeriaAgent.java | 18 +- .../valesriegler/pizzeria/gui/DEP_Table.java | 4 +- .../sbc/valesriegler/types/GroupData.java | 12 +- .../sbc/valesriegler/xvsm/XVSMConnector.java | 168 +++++++++++++++++- .../sbc/valesriegler/xvsm/cook/Cook.java | 57 ++++++ .../sbc/valesriegler/xvsm/waiter/Waiter.java | 28 +-- 8 files changed, 269 insertions(+), 38 deletions(-) create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/cook/Cook.java diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/common/SpaceUtil.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/common/SpaceUtil.java index 81f7bf3..64a1439 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/common/SpaceUtil.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/common/SpaceUtil.java @@ -12,6 +12,7 @@ import org.mozartspaces.core.MzsCoreException; public abstract class SpaceUtil { public static final String TABLES_CONTAINER = "tables"; + public static final String GROUPS_CONTAINER = "groups"; public static final String SERVER_ADDR = "xvsm://localhost:9876"; public static ContainerReference getOrCreateNamedContainer(final String spaceUri, final String containerName, final Capi capi, final List coordinators) throws MzsCoreException { 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 e15dd1d..b09cdc9 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 @@ -1,9 +1,14 @@ package at.ac.tuwien.sbc.valesriegler.group; +import java.util.ArrayList; +import java.util.List; + import javax.swing.SwingUtilities; import at.ac.tuwien.sbc.valesriegler.group.gui.GroupOverviewModel; import at.ac.tuwien.sbc.valesriegler.group.jms.JMSGroupConnector; +import at.ac.tuwien.sbc.valesriegler.types.GroupData; +import at.ac.tuwien.sbc.valesriegler.xvsm.XVSMConnector; /** * The Main class of the Group component. @@ -18,10 +23,12 @@ public class GroupAgent { public static GroupAgent groupAgent; private GroupOverviewModel groupModel; private AbstractGroupConnector groupconn; + private XVSMConnector xvsm; public static void main(String[] args) { // TODO: use jms and xvsm manager here. groupAgent = new GroupAgent(new JMSGroupConnector()); + SwingUtilities.invokeLater(new GroupGUI()); } @@ -29,6 +36,10 @@ public class GroupAgent { groupModel = new GroupOverviewModel(); this.groupconn = groupconn; groupconn.init(); + +// xvsm = new XVSMConnector(); +// xvsm.initSpaceCommunication(); +// xvsm.useGroupsContainer(); } public static GroupAgent getInstance() { @@ -42,4 +53,12 @@ public class GroupAgent { public GroupOverviewModel getGroupModel() { return groupModel; } + + public void onGroupsCreated(List newGroups) { + List groupData = new ArrayList<>(); + for (Group group : newGroups) { + groupData.add(group.getGroupData()); + } + xvsm.createGroups(groupData); + } } 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 027bece..8c26c5e 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 @@ -4,6 +4,8 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import javax.management.Notification; +import javax.management.NotificationListener; import javax.swing.SwingUtilities; import org.mozartspaces.capi3.AnyCoordinator; @@ -34,7 +36,7 @@ import at.ac.tuwien.sbc.valesriegler.xvsm.XVSMConnector; * @author Gregor Riegler * */ -public class PizzeriaAgent { +public class PizzeriaAgent implements NotificationListener { private static final Logger log = LoggerFactory.getLogger(PizzeriaAgent.class); private static final String USAGE = "Usage: java PizzeriaAgent XVSM|JMS"; @@ -68,15 +70,8 @@ public class PizzeriaAgent { } private void initXVSM() { - try { xvsm = new XVSMConnector(); xvsm.initSpaceCommunication(); - - } catch (MzsCoreException e) { - log.error(e.getMessage()); - log.error("The Pizzeria has no Space connection! Have you started the Space Server?"); - System.exit(1); - } } private void initGUI() { @@ -92,7 +87,7 @@ public class PizzeriaAgent { @Override public void freeTablesCreated(List tables) { - xvsm.sendFreeTablesToSpace(tables); + xvsm.sendTablesToSpace(tables); } }); @@ -113,4 +108,9 @@ public class PizzeriaAgent { public interface TablesCreatedHandler { public void freeTablesCreated(List tables); } + + @Override + public void handleNotification(Notification notification, Object handback) { + // handle space notifications + } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/DEP_Table.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/DEP_Table.java index 3ae422d..d400184 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/DEP_Table.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/DEP_Table.java @@ -5,9 +5,9 @@ import java.io.Serializable; @Deprecated public class DEP_Table implements Serializable { - private int id; + private Integer id; private TableStatus status; - private int groupId; + private Integer groupId; public int getId() { return id; 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 04891bc..551746a 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 @@ -17,20 +17,20 @@ import org.slf4j.LoggerFactory; public class GroupData implements Serializable { private static final Logger log = LoggerFactory.getLogger(GroupData.class); - final private int id; + final private Integer id; private GroupState state = GroupState.NEW; - private int size; + private Integer size; private Table table; - private int tableWaiter; + private Integer tableWaiter; private Order order; - private int orderWaiter; - private int pizzaWaiter; + private Integer orderWaiter; + private Integer pizzaWaiter; @Deprecated private List pizzas = new ArrayList(); - public GroupData(int id) { + public GroupData(Integer id) { this.id = id; } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/XVSMConnector.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/XVSMConnector.java index 43b24c5..e9e7186 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/XVSMConnector.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/XVSMConnector.java @@ -1,23 +1,32 @@ 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 org.mozartspaces.capi3.AnyCoordinator; import org.mozartspaces.capi3.Coordinator; +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.ContainerReference; import org.mozartspaces.core.DefaultMzsCore; import org.mozartspaces.core.Entry; +import org.mozartspaces.core.MzsConstants.RequestTimeout; import org.mozartspaces.core.MzsCore; import org.mozartspaces.core.MzsCoreException; +import org.mozartspaces.core.TransactionReference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.common.SpaceUtil; import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.DEP_Table; +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.Table; /** * Responsible for XVSM Communication @@ -29,16 +38,47 @@ public class XVSMConnector { private static final Logger log = LoggerFactory.getLogger(XVSMConnector.class); private ContainerReference tablesContainer; + private ContainerReference groupsContainer; +// private ContainerReference notificationContainer; private Capi capi; - public void initSpaceCommunication() throws MzsCoreException { - MzsCore core = DefaultMzsCore.newInstanceWithoutSpace(); - capi = new Capi(core); - tablesContainer = SpaceUtil.getOrCreateNamedContainer(SpaceUtil.SERVER_ADDR, SpaceUtil.TABLES_CONTAINER, capi, Arrays.asList((Coordinator) new AnyCoordinator())); + public void initSpaceCommunication() { + try { + MzsCore core = DefaultMzsCore.newInstanceWithoutSpace(); + capi = new Capi(core); + } catch (Exception e) { + log.error("Space connection could not be established! Have you started the Space Server?"); + handleSpaceErrorAndTerminate(e); + } + } + + public void useTablesContainer() { + try { + tablesContainer = SpaceUtil.getOrCreateNamedContainer(SpaceUtil.SERVER_ADDR, SpaceUtil.TABLES_CONTAINER, capi, createCoordinators(new AnyCoordinator())); + } catch (MzsCoreException e) { + handleSpaceErrorAndTerminate(e); + } + } + + public void useGroupsContainer() { + try { + groupsContainer = SpaceUtil.getOrCreateNamedContainer(SpaceUtil.SERVER_ADDR, SpaceUtil.GROUPS_CONTAINER, capi, createCoordinators(new AnyCoordinator(), new LindaCoordinator())); + } catch (MzsCoreException e) { + handleSpaceErrorAndTerminate(e); + } + } + + private List createCoordinators(Coordinator... coordinator) { + return Arrays.asList(coordinator); + } + + private void handleSpaceErrorAndTerminate(Exception e) { + log.error(e.getMessage()); + System.exit(1); } - public void sendFreeTablesToSpace(List tables) { + public void sendTablesToSpace(List tables) { try { List entries = new ArrayList<>(); for (DEP_Table table : tables) { @@ -46,9 +86,9 @@ public class XVSMConnector { } capi.write(entries, tablesContainer); - log.info("Wrote Free tables to Space!"); + log.info("Wrote tables to Space!"); } catch (MzsCoreException e) { - e.printStackTrace(); + log.info(e.getMessage()); } } @@ -64,4 +104,116 @@ public class XVSMConnector { return tables; } + public void createGroups(List newGroups) { + try { + List entries = new ArrayList<>(); + for (GroupData group : newGroups) { + entries.add(new Entry(group)); + } + capi.write(entries, groupsContainer); + + log.info("Wrote new Groups to Space!"); + } catch (MzsCoreException e) { + log.info(e.getMessage()); + } + } + + public void handleWaitingGroup(int id) { + try { + TransactionReference tx = capi.createTransaction(RequestTimeout.INFINITE, URI.create(SpaceUtil.SERVER_ADDR)); + DEP_Table table = null; + try { + ArrayList tables = capi.take(tablesContainer, FifoCoordinator.newSelector(), RequestTimeout.ZERO, tx); + table = tables.get(0); + } catch (Exception e) { + log.info("There is no free table"); + capi.rollbackTransaction(tx); + return; + } + + GroupData groupData = new GroupData(null); + groupData.setState(GroupState.WAITING); + LindaSelector sel = LindaCoordinator.newSelector(groupData, 1); + ArrayList waitingGroups = null; + + try { + waitingGroups = capi.take(groupsContainer, sel, RequestTimeout.ZERO, tx); + } catch (Exception e) { + log.info("There is no waiting group"); + capi.rollbackTransaction(tx); + return; + } + + GroupData group = waitingGroups.get(0); + group.setTable(new Table(table.getGroupId())); + group.setTableWaiter(id); + group.setState(GroupState.SITTING); + + try { + capi.write(groupsContainer, RequestTimeout.TRY_ONCE, tx, new Entry(group)); + } catch (Exception e) { + e.printStackTrace(); + capi.rollbackTransaction(tx); + return; + } + + try { + capi.write(new Entry(table), tablesContainer, RequestTimeout.TRY_ONCE, tx); + } catch (Exception e) { + e.printStackTrace(); + capi.rollbackTransaction(tx); + return; + } + + capi.commitTransaction(tx); + } catch (MzsCoreException e) { + e.printStackTrace(); + } + } + + public void handleOrderRequest(int id) { + try { + TransactionReference tx = capi.createTransaction(RequestTimeout.INFINITE, URI.create(SpaceUtil.SERVER_ADDR)); + + GroupData groupData = new GroupData(null); + groupData.setState(GroupState.SITTING); + LindaSelector sel = LindaCoordinator.newSelector(groupData, 1); + ArrayList sittingGroups = null; + + try { + sittingGroups = capi.take(groupsContainer, sel, RequestTimeout.ZERO, tx); + } catch (Exception e) { + log.info("There is no sitting group"); + capi.rollbackTransaction(tx); + return; + } + GroupData sittingGroup = sittingGroups.get(0); + Order order = sittingGroup.getOrder(); + sittingGroup.setOrderWaiter(id); + sittingGroup.setState(GroupState.ORDERED); + + try { + capi.write(new Entry(sittingGroup), groupsContainer, RequestTimeout.TRY_ONCE, tx); + } catch (Exception e) { + e.printStackTrace(); + capi.rollbackTransaction(tx); + return; + } + + capi.commitTransaction(tx); + + } catch (MzsCoreException e) { + e.printStackTrace(); + } + } + + + + public void handlePizzaDistribution() { + } + + public void handlePaymentRequest() { + } + + } 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 new file mode 100644 index 0000000..05f07b2 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/cook/Cook.java @@ -0,0 +1,57 @@ +package at.ac.tuwien.sbc.valesriegler.xvsm.cook; + +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.DEP_Table; +import at.ac.tuwien.sbc.valesriegler.xvsm.XVSMConnector; +import at.ac.tuwien.sbc.valesriegler.xvsm.waiter.Waiter; + +public class Cook { + private static final String USAGE = "Cook needs exactly one parameter: ID of type Integer"; + private static final Logger log = LoggerFactory.getLogger(Cook.class); + + private int id; + private XVSMConnector 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); + cook.start(); + } + + private void start() { + initSpaceCommunication(); + + + + } + + private void initSpaceCommunication() { + xvsm = new XVSMConnector(); + xvsm.initSpaceCommunication(); + + log.info("Space Connection established!"); + + + } + + public Cook(int id) { + this.id = id; + log.info("I AM A Cook 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 b9fa1ce..c759889 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 @@ -43,22 +43,24 @@ public class Waiter implements Serializable { private void start() { initSpaceCommunication(); - List tables = xvsm.readTables(); - - log.info("Number of free tables received: {}", tables.size()); - + while(true) { + xvsm.handleWaitingGroup(id); + + xvsm.handleOrderRequest(id); + + xvsm.handlePaymentRequest(); + + xvsm.handlePizzaDistribution(); + } } + private void initSpaceCommunication() { - try { - xvsm = new XVSMConnector(); - xvsm.initSpaceCommunication(); - } catch (MzsCoreException e) { - log.error(e.getMessage()); - log.error("The Waiter has no Space connection! Have you started the Space Server?"); - System.exit(1); - } - + xvsm = new XVSMConnector(); + xvsm.initSpaceCommunication(); + xvsm.useTablesContainer(); + xvsm.useGroupsContainer(); + log.info("Space Connection established!"); -- 2.43.0