From d4b4bcf67b9a03aa149752e91cfe280a1f5efca6 Mon Sep 17 00:00:00 2001 From: Jan Vales Date: Sun, 5 May 2013 19:10:43 +0200 Subject: [PATCH] =?utf8?q?gro=C3=9Fer=20Brocken=20an=20GruppenGui-=C3=84nd?= =?utf8?q?erungen=20kombiniert=20mit=20ersten=20Teil=20des=20Bestellworkfl?= =?utf8?q?ows.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- queues | 22 ++++ .../sbc/valesriegler/DEBUG_CreateTable.java | 69 +++++++++++ .../sbc/valesriegler/DEP_GroupDataMin.java | 30 +++++ .../tuwien/sbc/valesriegler/group/Group.java | 116 ++++++++++++------ .../sbc/valesriegler/group/GroupAgent.java | 38 +++--- .../valesriegler/group/GroupConnector.java | 87 +++++++++++++ .../sbc/valesriegler/group/GroupData.java | 95 +++++++++++++- .../sbc/valesriegler/group/GroupGUI.java | 12 ++ .../group/actions/AbstractAction.java | 17 +++ .../group/actions/TableRequest.java | 12 ++ .../group/actions/TableResponse.java | 26 ++++ .../group/gui/GroupCreationPanel.java | 2 +- .../group/gui/GroupCreationRequest.java | 2 +- .../valesriegler/group/gui/GroupFrame.java | 92 +++++++------- .../group/gui/GroupOverviewModel.java | 23 +++- .../sbc/valesriegler/jms/cook/Cook.java | 4 +- .../sbc/valesriegler/jms/waiter/Waiter.java | 71 ----------- .../valesriegler/pizzeria/PizzeriaAgent.java | 6 +- .../gui/{Table.java => DEP_Table.java} | 5 +- .../sbc/valesriegler/pizzeria/gui/Order.java | 7 +- .../valesriegler/pizzeria/gui/PizzaOrder.java | 2 +- .../pizzeria/gui/PizzeriaFrame.java | 4 +- .../gui/tablemodels/GroupsOverviewModel.java | 6 +- .../gui/tablemodels/TablesOverviewModel.java | 20 +-- .../{group => types}/GroupState.java | 2 +- .../valesriegler/{common => types}/Pizza.java | 4 +- .../{common => types}/PizzaType.java | 2 +- .../tuwien/sbc/valesriegler/types/Table.java | 21 ++++ .../sbc/valesriegler/waiter/Waiter.java | 60 +++++++++ .../jms/messageListeners/WantToOrder.java | 74 +++++++++++ .../messageListeners/WantToSitAtTable.java | 88 +++++++++++++ .../sbc/valesriegler/xvsm/XVSMConnector.java | 10 +- .../sbc/valesriegler/xvsm/waiter/Waiter.java | 4 +- src/main/resources/log4j.properties | 12 -- src/main/resources/logback.xml | 77 +++--------- 35 files changed, 827 insertions(+), 295 deletions(-) create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/DEBUG_CreateTable.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/DEP_GroupDataMin.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupConnector.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupGUI.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/AbstractAction.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/TableRequest.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/TableResponse.java delete mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/jms/waiter/Waiter.java rename src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/{Table.java => DEP_Table.java} (89%) rename src/main/java/at/ac/tuwien/sbc/valesriegler/{group => types}/GroupState.java (74%) rename src/main/java/at/ac/tuwien/sbc/valesriegler/{common => types}/Pizza.java (84%) rename src/main/java/at/ac/tuwien/sbc/valesriegler/{common => types}/PizzaType.java (86%) create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/types/Table.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/Waiter.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToOrder.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToSitAtTable.java delete mode 100644 src/main/resources/log4j.properties diff --git a/queues b/queues index 2583525..d556238 100644 --- a/queues +++ b/queues @@ -1,3 +1,25 @@ +Liste aller Messagequeues: + GroupConnector + Da es Angabe ist, dass Gruppen einzelne Threads sein müssen, sind diese nun Fix mit + dem GroupAgent verknüpft. + Daten, die einzelne Gruppen senden, wollen wieder zurück an die Gruppen gelangen. + Da ich keine einzelnen Queues/Gruppe machen wollte, gibt es den GroupConnector, + welcher alle Messages empfängt und die Gruppen entsprechend aktualisiert und in der GUI anzeigt. + + PizzariaConnector + Selbiges wie GroupConnector: eingehende Informationen über Tische, Gruppen, Pizzen, ... + + TablesFree + Leere Tische, die zugewiesen werden können. + + WantToSitAtTable + Gruppen, die auf einen Tisch warten. + + WantToOrder + Gruppen, die eine Bestellungen, die von Waitern bearbeitet werden sollen. + + +---------------------- Wartende Gruppen Freie Tische diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/DEBUG_CreateTable.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/DEBUG_CreateTable.java new file mode 100644 index 0000000..4663c7d --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/DEBUG_CreateTable.java @@ -0,0 +1,69 @@ +package at.ac.tuwien.sbc.valesriegler; + +import javax.jms.Connection; +import javax.jms.DeliveryMode; +import javax.jms.MessageProducer; +import javax.jms.Session; + +import org.apache.activemq.ActiveMQConnectionFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.ac.tuwien.sbc.valesriegler.types.PizzaType; +import at.ac.tuwien.sbc.valesriegler.types.Table; + +public class DEBUG_CreateTable { + private static final Logger log = LoggerFactory.getLogger(DEBUG_CreateTable.class); + private static int nextID = 0; + final private int id; + + public static void main(String[] args) throws Exception { + new DEBUG_CreateTable(++nextID); + } + + public DEBUG_CreateTable(int id) { + this.id = id; + log.info("I AM A DEBUG_CreateTable WITH ID {}", this.id); + produce(PizzaType.CARDINALE); + } + + public void produce(PizzaType pizzatype) { + try { + // Connecting to the Broker and to the output queue + ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); + Connection connection = connectionFactory.createConnection(); + connection.start(); + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + + MessageProducer tableprod = session.createProducer(session.createQueue("TablesFree")); + tableprod.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + + // Create Tables + for (int i = 0; i < 1; i++) { + tableprod.send(session.createObjectMessage(new Table(i))); + } + + // // Create Groups + // for (int i = 0; i < 1; i++) { + // Group g = new Group(); + // g.setId(i); + // g.setSize(4); + // ArrayList p = new ArrayList(); + // p.add(PizzaType.CARDINALE); + // p.add(PizzaType.MARGHERITA); + // p.add(PizzaType.SALAMI); + // p.add(PizzaType.MARGHERITA); + // Order o = new Order(p); + // g.setOrder(o); + // g.goGrabSomeFood(); // start the workflow. + // } + + // Clean up + session.close(); + connection.close(); + } catch (Exception e) { + log.error("Caught: ", e); + e.printStackTrace(); + } + } +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/DEP_GroupDataMin.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/DEP_GroupDataMin.java new file mode 100644 index 0000000..c4e3f6b --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/DEP_GroupDataMin.java @@ -0,0 +1,30 @@ +package at.ac.tuwien.sbc.valesriegler; + +import java.io.Serializable; + +/** + * A group bean which only has an id and a size. + * + * @author Gregor Riegler + */ +@Deprecated +public class DEP_GroupDataMin implements Serializable { + private int id; + private int size; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public int getSize() { + return size; + } + + public void setSize(int size) { + this.size = size; + } +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/Group.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/Group.java index 1a1446f..6b5cd61 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/Group.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/Group.java @@ -1,62 +1,102 @@ package at.ac.tuwien.sbc.valesriegler.group; -import java.util.ArrayList; -import java.util.List; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import at.ac.tuwien.sbc.valesriegler.common.PizzaType; +import at.ac.tuwien.sbc.valesriegler.group.actions.TableRequest; +import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.Order; +import at.ac.tuwien.sbc.valesriegler.types.GroupState; +import at.ac.tuwien.sbc.valesriegler.types.Table; -// TODO: move all data to GroupData. Angabe: Jede gruppe muss in einem eigenen Thread laufen. -// Use this class as local datastore only and GroupData as serializable datastore. +// class doing all the Group related work as required. public class Group implements Runnable { private static final Logger log = LoggerFactory.getLogger(Group.class); - private GroupData groupData = new GroupData(); - - private List pizzas = new ArrayList(); - private GroupState state = GroupState.NEW; - - public int getId() { - return groupData.getId(); - } + private static int idNext = -1; // TODO: set to 0 after debugging! - public void setId(int id) { - groupData.setId(id); - } - - public int getSize() { - return groupData.getSize(); - } + final private GroupData groupData; - public void setSize(int size) { - groupData.setSize(size); + public GroupData getGroupData() { + return groupData; } - public List getPizzas() { - return pizzas; + public Group() { + groupData = new GroupData(++idNext); } - public void setPizzas(List pizzas) { - this.pizzas = pizzas; + @Override + public void run() { + log.debug("Thread started for:" + this); + try { + Thread.sleep((long) (Math.random() * 10000)); + + switch (groupData.getState()) { + case NEW: + // TODO : may not happen! + break; + case WAITING: + GroupAgent.getInstance().getGroupcomm().send(new TableRequest(groupData)); + break; + case SITTING: + // MessageProducer wantToOrder = + // session.createProducer(session.createQueue("WantToOrder")); + // wantToOrder.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + // wantToOrder.send(session.createObjectMessage(this)); + break; + case ORDERED: + break; // do nothing. + case EATING:// just wait some more and request bill. + // MessageProducer wantToPay = + // session.createProducer(session.createQueue("WantToPay")); + // wantToPay.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + // wantToPay.send(session.createObjectMessage(this)); + break; + case PAY:// leave table + // MessageProducer wantToLeave = + // session.createProducer(session.createQueue("LeftPizzaria")); + // wantToLeave.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + // wantToLeave.send(session.createObjectMessage(this)); + break; + case GONE: + break; // do nothing. + default: + break; + } + + } catch (InterruptedException e) { + e.printStackTrace(); + } } - public GroupState getState() { - return state; + // lets go to the pizzaria. + public void goGrabSomeFood() { + log.debug("goGrabSomeFood():" + this); + groupData.setState(GroupState.WAITING); + new Thread(this).start(); } - public void setState(GroupState state) { - this.state = state; + // get a table assigned by a waiter. + public void assignTable(Table t, int waiterID) { + groupData.setState(GroupState.SITTING); + groupData.setTable(t); + groupData.setTableWaiter(waiterID); + new Thread(this).start(); + log.debug("assignTable():" + this); } - public void goGrabSomeFood() { - state = GroupState.WAITING; - log.info("goGrabSomeFood()"); + // ask the group to send their order. + public void askForOrder(int waiter) { + groupData.setState(GroupState.ORDERED); + groupData.setOrderWaiter(waiter); + new Thread(this).start(); + log.debug("askForOrder():" + this); } - @Override - public void run() { - // TODO Auto-generated method stub - + // pizzas are here! nom pizzas! + public void bringOrderedPizzas(Order order, int waiter) { + groupData.setState(GroupState.EATING); + groupData.setOrder(order); + groupData.setPizzaWaiter(waiter); + new Thread(this).start(); + log.debug("bringOrderedPizzas():" + this); } } 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 75bdfb8..3c731bf 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 @@ -2,7 +2,7 @@ package at.ac.tuwien.sbc.valesriegler.group; import javax.swing.SwingUtilities; -import at.ac.tuwien.sbc.valesriegler.group.gui.GroupFrame; +import at.ac.tuwien.sbc.valesriegler.group.gui.GroupOverviewModel; /** * The Main class of the Group component. @@ -10,24 +10,34 @@ import at.ac.tuwien.sbc.valesriegler.group.gui.GroupFrame; * Start the communication and the group GUI: * * @author Gregor Riegler - * + * */ public class GroupAgent { - + public static GroupAgent groupAgent; + private GroupOverviewModel groupModel; + private GroupConnector groupconn; + public static void main(String[] args) { - // TODO initiate communication - + // TODO: use jms and xvsm manager here. + groupAgent = new GroupAgent(new GroupConnector()); SwingUtilities.invokeLater(new GroupGUI()); - } - - static class GroupGUI implements Runnable { - @Override - public void run() { - GroupFrame frame = new GroupFrame(); - frame.pack(); - frame.setVisible(true); - } + + public GroupAgent(GroupConnector groupconn) { + groupModel = new GroupOverviewModel(); + this.groupconn = groupconn; + groupconn.init();// TODO: move somewhere else! + } + + public static GroupAgent getInstance() { + return groupAgent; + } + + public GroupConnector getGroupcomm() { + return groupconn; } + public GroupOverviewModel getGroupModel() { + return groupModel; + } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupConnector.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupConnector.java new file mode 100644 index 0000000..6f30697 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupConnector.java @@ -0,0 +1,87 @@ +package at.ac.tuwien.sbc.valesriegler.group; + +import javax.jms.Connection; +import javax.jms.DeliveryMode; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageConsumer; +import javax.jms.MessageListener; +import javax.jms.MessageProducer; +import javax.jms.ObjectMessage; +import javax.jms.Session; + +import org.apache.activemq.ActiveMQConnectionFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.ac.tuwien.sbc.valesriegler.group.actions.AbstractAction; +import at.ac.tuwien.sbc.valesriegler.group.actions.TableResponse; + +// This handles lower-level communication between processes. +// put messages into queues, put data into space. +// not sure if it should be refactored to be an interface with xvsm + jms classes implementing this. + +//TODO +public class GroupConnector implements MessageListener { + private static final Logger log = LoggerFactory.getLogger(GroupConnector.class); + + private Connection connection; + + public void init() { + try { + connection = new ActiveMQConnectionFactory("tcp://localhost:61616").createConnection(); + connection.start(); + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageConsumer consWantToSit = session.createConsumer(session.createQueue("GroupConnector")); + consWantToSit.setMessageListener(this); + } catch (JMSException e) { + log.error("EXCEPTION!", e); + } + } + + public Group findGroup(GroupData gd) { + return null; + } + + @Override + public void onMessage(Message msg) { + try { + msg.acknowledge(); + if (msg instanceof ObjectMessage) { + ObjectMessage objMsg = (ObjectMessage) msg; + Object obj = objMsg.getObject(); + + if (obj instanceof TableResponse) { + TableResponse tablerresponse = (TableResponse) obj; + log.debug("Received: " + tablerresponse); + Group g = GroupAgent.getInstance().getGroupModel().getGroupByID(tablerresponse.getGroupdata().getId()); + g.assignTable(tablerresponse.getTable(), tablerresponse.getWaiterId()); + return; + } + } + + log.warn("Unknown messagetype received!"); + } catch (JMSException e) { + log.error("EXCEPTION!", e); + } + } + + public void send(AbstractAction request) { + try { + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageProducer wantToSitAtTable = session.createProducer(session.createQueue("WantToSitAtTable")); + wantToSitAtTable.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + wantToSitAtTable.send(session.createObjectMessage(request)); + session.close(); + + session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageProducer informPizzariaGui = session.createProducer(session.createQueue("PizzariaConnector")); + informPizzariaGui.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + informPizzariaGui.send(session.createObjectMessage(request)); + session.close(); + } catch (JMSException e) { + log.error("EXCEPTION!", e); + } + } + +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupData.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupData.java index 00b936c..8ba2a81 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupData.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupData.java @@ -1,26 +1,109 @@ package at.ac.tuwien.sbc.valesriegler.group; import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.Order; +import at.ac.tuwien.sbc.valesriegler.types.GroupState; +import at.ac.tuwien.sbc.valesriegler.types.PizzaType; +import at.ac.tuwien.sbc.valesriegler.types.Table; /** - * A group bean which only has an id and a size. + * This class represents all of a group's data, that can be relevant to send + * across the net. + * + * @author jan * - * @author Gregor Riegler */ public class GroupData implements Serializable { - private int id; + private static final Logger log = LoggerFactory.getLogger(GroupData.class); + + final private int id; + private GroupState state = GroupState.NEW; private int size; - + + private Table table; + private int tableWaiter; + private Order order; + private int orderWaiter; + private int pizzaWaiter; + + @Deprecated + private List pizzas = new ArrayList(); + + public GroupData(int id) { + this.id = id; + } + public int getId() { return id; } - public void setId(int id) { - this.id = id; + + public Order getOrder() { + return order; + } + + public int getOrderWaiter() { + return orderWaiter; + } + + public List getPizzas() { + return pizzas; + } + + public int getPizzaWaiter() { + return pizzaWaiter; } + public int getSize() { return size; } + + public GroupState getState() { + return state; + } + + public Table getTable() { + return table; + } + + public int getTableWaiter() { + return tableWaiter; + } + + public void setOrder(Order order) { + this.order = order; + } + + public void setOrderWaiter(int orderWaiter) { + this.orderWaiter = orderWaiter; + } + + public void setPizzas(List pizzas) { + this.pizzas = pizzas; + } + + public void setPizzaWaiter(int pizzaWaiter) { + this.pizzaWaiter = pizzaWaiter; + } + public void setSize(int size) { this.size = size; } + + public void setState(GroupState state) { + this.state = state; + } + + public void setTable(Table table) { + this.table = table; + } + + public void setTableWaiter(int tableWaiter) { + this.tableWaiter = tableWaiter; + } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupGUI.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupGUI.java new file mode 100644 index 0000000..f99a01d --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupGUI.java @@ -0,0 +1,12 @@ +package at.ac.tuwien.sbc.valesriegler.group; + +import at.ac.tuwien.sbc.valesriegler.group.gui.GroupFrame; + +class GroupGUI implements Runnable { + @Override + public void run() { + GroupFrame frame = new GroupFrame(GroupAgent.getInstance().getGroupModel()); + frame.pack(); + frame.setVisible(true); + } +} \ No newline at end of file diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/AbstractAction.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/AbstractAction.java new file mode 100644 index 0000000..9940805 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/AbstractAction.java @@ -0,0 +1,17 @@ +package at.ac.tuwien.sbc.valesriegler.group.actions; + +import java.io.Serializable; + +import at.ac.tuwien.sbc.valesriegler.group.GroupData; + +public abstract class AbstractAction implements Serializable { + private final GroupData groupdata; + + public AbstractAction(GroupData groupdata) { + this.groupdata = groupdata; + } + + public GroupData getGroupdata() { + return groupdata; + } +} \ No newline at end of file diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/TableRequest.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/TableRequest.java new file mode 100644 index 0000000..f4a8076 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/TableRequest.java @@ -0,0 +1,12 @@ +package at.ac.tuwien.sbc.valesriegler.group.actions; + +import java.io.Serializable; + +import at.ac.tuwien.sbc.valesriegler.group.GroupData; + +public class TableRequest extends AbstractAction implements Serializable { + public TableRequest(GroupData groupdata) { + super(groupdata); + } + +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/TableResponse.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/TableResponse.java new file mode 100644 index 0000000..71c69c9 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/actions/TableResponse.java @@ -0,0 +1,26 @@ +package at.ac.tuwien.sbc.valesriegler.group.actions; + +import java.io.Serializable; + +import at.ac.tuwien.sbc.valesriegler.group.GroupData; +import at.ac.tuwien.sbc.valesriegler.types.Table; + +public class TableResponse extends AbstractAction implements Serializable { + private final Table table; + private final int waiterId; + + public TableResponse(GroupData groupdata, Table table, int waiterId) { + super(groupdata); + this.table = table; + this.waiterId = waiterId; + } + + public Table getTable() { + return table; + } + + public int getWaiterId() { + return waiterId; + } + +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationPanel.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationPanel.java index 3183ab9..ee36f08 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationPanel.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationPanel.java @@ -13,8 +13,8 @@ import javax.swing.JPanel; import javax.swing.JSpinner; import javax.swing.SpinnerNumberModel; -import at.ac.tuwien.sbc.valesriegler.common.PizzaType; import at.ac.tuwien.sbc.valesriegler.group.gui.GroupFrame.GroupCreationHandler; +import at.ac.tuwien.sbc.valesriegler.types.PizzaType; /** * Panel which allows the selection of pizzas and the number of groups to create diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationRequest.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationRequest.java index 1c24d8c..ee88ab9 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationRequest.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationRequest.java @@ -2,7 +2,7 @@ package at.ac.tuwien.sbc.valesriegler.group.gui; import java.util.List; -import at.ac.tuwien.sbc.valesriegler.common.PizzaType; +import at.ac.tuwien.sbc.valesriegler.types.PizzaType; public class GroupCreationRequest { public final int size; diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupFrame.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupFrame.java index 200cccf..5239b4a 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupFrame.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupFrame.java @@ -8,44 +8,41 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.atomic.AtomicInteger; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; -import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JSpinner; import javax.swing.JTable; +import javax.swing.ScrollPaneConstants; import javax.swing.SpinnerNumberModel; import javax.swing.border.TitledBorder; import at.ac.tuwien.sbc.valesriegler.group.Group; +import at.ac.tuwien.sbc.valesriegler.group.GroupAgent; /** * Base Frame of the Group UI * * @author Gregor Riegler - * + * */ +@SuppressWarnings("serial") public class GroupFrame extends JFrame { - private int idCounter = 1; - - private GroupOverviewModel groupModel; - - public GroupFrame() { + public GroupFrame(GroupOverviewModel groupModel) { super("Groups"); JPanel wrapper = new JPanel(); GridLayout wrapperLayout = new GridLayout(2, 1); wrapper.setLayout(wrapperLayout); - + // Create Wizard-like panel for the creation of groups initGroupCreation(wrapper); - + // Create the panel for the group overview table - initGroupOverview(wrapper); - + initGroupOverview(wrapper, groupModel); + setContentPane(wrapper); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } @@ -55,46 +52,46 @@ public class GroupFrame extends JFrame { final JPanel chooseGroupSizePanel = new JPanel(); JLabel creationLabel = new JLabel("How many members should the group have?"); JButton next = new JButton("Next"); - SpinnerNumberModel model = new SpinnerNumberModel(1,1,4,1); - final JSpinner spinner = new JSpinner(model); + SpinnerNumberModel model = new SpinnerNumberModel(1, 1, 4, 1); + final JSpinner spinner = new JSpinner(model); - // When 'next' is clicked the second, final panel of the Group Creation Wizard should be shown - next.addActionListener(new ActionListener() { + // When 'next' is clicked the second, final panel of the Group Creation + // Wizard should be shown + next.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int numberMembers = (int) spinner.getValue(); final GroupCreationPanel groupCreationPanel = new GroupCreationPanel(numberMembers); - GroupCreationHandler groupCreationHandler = new GroupCreationHandler(creationPanel, chooseGroupSizePanel, groupCreationPanel); - + GroupCreationHandler groupCreationHandler = new GroupCreationHandler(creationPanel, chooseGroupSizePanel, + groupCreationPanel); + groupCreationPanel.setCreateAndCancelHandler(groupCreationHandler); groupCreationHandler.showGroupCreationDetailPanel(); - - + } }); - - GridLayout creationPanelLayout = new GridLayout(3,1); - chooseGroupSizePanel.setLayout(creationPanelLayout); - creationPanel.setBorder(new TitledBorder("Create Groups")); - - creationPanel.add(chooseGroupSizePanel); + + GridLayout creationPanelLayout = new GridLayout(3, 1); + chooseGroupSizePanel.setLayout(creationPanelLayout); + creationPanel.setBorder(new TitledBorder("Create Groups")); + + creationPanel.add(chooseGroupSizePanel); chooseGroupSizePanel.add(creationLabel); chooseGroupSizePanel.add(spinner); chooseGroupSizePanel.add(next); - + wrapper.add(creationPanel); } - private void initGroupOverview(JPanel wrapper) { + private void initGroupOverview(JPanel wrapper, GroupOverviewModel groupModel) { JPanel overviewPanel = new JPanel(new GridBagLayout()); - - groupModel = new GroupOverviewModel(); + JTable overviewTable = new JTable(groupModel); JScrollPane scrollPane = new JScrollPane(overviewTable); - + overviewPanel.setBorder(new TitledBorder("Group Overview")); - scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); - + scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED); + GridBagConstraints c = new GridBagConstraints(); c.gridx = 0; c.weightx = 1; @@ -104,13 +101,12 @@ public class GroupFrame extends JFrame { overviewPanel.add(scrollPane, c); wrapper.add(overviewPanel); } - - - /** + + /** * Handles the Wizard-like creation of groups. *

- * Give an instance of this class as a callback to the Panel from which - * a group creation request should be issued + * Give an instance of this class as a callback to the Panel from which a + * group creation request should be issued */ class GroupCreationHandler { private JPanel creationPanel; @@ -122,7 +118,7 @@ public class GroupFrame extends JFrame { this.chooseGroupSizePanel = chooseGroupSizePanel; this.groupCreationPanel = groupCreationPanel; } - + public void showGroupCreationDetailPanel() { chooseGroupSizePanel.setVisible(false); creationPanel.add(groupCreationPanel); @@ -132,22 +128,20 @@ public class GroupFrame extends JFrame { chooseGroupSizePanel.setVisible(true); creationPanel.remove(groupCreationPanel); creationPanel.repaint(); - + createGroups(gc); } - - private void createGroups(GroupCreationRequest gc) { + + public void createGroups(GroupCreationRequest gc) { List newGroups = new ArrayList<>(); - for(int i=0; i { private static final Logger log = LoggerFactory.getLogger(GroupOverviewModel.class); @@ -20,6 +20,11 @@ public class GroupOverviewModel extends TableModel { private static final String ID = "ID"; private static final String[] COLUMNS = new String[] { ID, SIZE, PIZZAS, STATE }; + public GroupOverviewModel() { + super(); + super.items.add(new Group()); // TODO: remove, debuggroup! + } + @Override protected String[] getColumns() { return COLUMNS; @@ -29,23 +34,31 @@ public class GroupOverviewModel extends TableModel { return items; } + public Group getGroupByID(int id) { + for (Group group : items) { + if (group.getGroupData().getId() == id) + return group; + } + return null; + } + @Override public Object getValueAt(int rowIndex, int columnIndex) { Group group = items.get(rowIndex); String wantedColumn = COLUMNS[columnIndex]; switch (wantedColumn) { case ID: - int groupId = group.getId(); + int groupId = group.getGroupData().getId(); log.info("This is the group id : {}", groupId); return groupId; case SIZE: - int groupSize = group.getSize(); + int groupSize = group.getGroupData().getSize(); log.info("This is the size : {}", groupSize); return groupSize; case PIZZAS: - return pizzaDisplay(group.getPizzas()); + return pizzaDisplay(group.getGroupData().getPizzas()); case STATE: - return group.getState(); + return group.getGroupData().getState(); default: throw new RuntimeException(UNHANDLEDCOLUMN); } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/cook/Cook.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/cook/Cook.java index 80e277f..9208824 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/cook/Cook.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/cook/Cook.java @@ -12,8 +12,8 @@ import org.apache.activemq.ActiveMQConnectionFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import at.ac.tuwien.sbc.valesriegler.common.Pizza; -import at.ac.tuwien.sbc.valesriegler.common.PizzaType; +import at.ac.tuwien.sbc.valesriegler.types.Pizza; +import at.ac.tuwien.sbc.valesriegler.types.PizzaType; public class Cook implements Serializable { private static final Logger log = LoggerFactory.getLogger(Cook.class); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/waiter/Waiter.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/waiter/Waiter.java deleted file mode 100644 index e236ff1..0000000 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/waiter/Waiter.java +++ /dev/null @@ -1,71 +0,0 @@ -package at.ac.tuwien.sbc.valesriegler.jms.waiter; - -import java.io.Serializable; - -import javax.jms.Connection; -import javax.jms.JMSException; -import javax.jms.Message; -import javax.jms.MessageConsumer; -import javax.jms.ObjectMessage; -import javax.jms.Session; - -import org.apache.activemq.ActiveMQConnectionFactory; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import at.ac.tuwien.sbc.valesriegler.common.Pizza; - -public class Waiter implements Serializable { - private static final Logger log = LoggerFactory.getLogger(Waiter.class); - private static int nextID = 0; - final private int id; - - public static void main(String[] args) { - new Waiter(++nextID); - } - - public Waiter(int id) { - this.id = id; - log.info("I AM A WAITER WITH ID {}", id); - deliver(); - } - - public void deliver() { - try { - // Connecting to the Broker and to the output queue - ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); - Connection connection = connectionFactory.createConnection(); - connection.start(); - Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - - MessageConsumer consumer = session.createConsumer(session.createQueue("CookedPizzas")); - - // Wait for a Pizza - Message message = consumer.receive(10000); - - if (message instanceof ObjectMessage) { - ObjectMessage pizzaMessage = (ObjectMessage) message; - Object data = pizzaMessage.getObject(); - if (data instanceof Pizza) { - Pizza pizza = (Pizza) data; - System.out.println("Received: " + pizza); - } else { - System.out.println("Received unknown Object: " + data); - } - } else { - System.out.println("Received unknown Message: " + message); - } - - consumer.close(); - session.close(); - connection.close(); - } catch (Exception e) { - log.error("Caught: ", e); - e.printStackTrace(); - } - } - - public synchronized void onException(JMSException ex) { - System.out.println("JMS Exception occured. Shutting down client."); - } -} 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 a45bf22..027bece 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 @@ -19,7 +19,7 @@ import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.common.SpaceUtil; import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.PizzeriaFrame; -import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.Table; +import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.DEP_Table; import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.GroupsOverviewModel; import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.OrdersOverviewModel; import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.PizzasOfOrderModel; @@ -91,7 +91,7 @@ public class PizzeriaAgent { frame.setOnTablesCreatedHandler(new TablesCreatedHandler() { @Override - public void freeTablesCreated(List tables) { + public void freeTablesCreated(List tables) { xvsm.sendFreeTablesToSpace(tables); } @@ -111,6 +111,6 @@ public class PizzeriaAgent { } public interface TablesCreatedHandler { - public void freeTablesCreated(List
tables); + public void freeTablesCreated(List tables); } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/Table.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/DEP_Table.java similarity index 89% rename from src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/Table.java rename to src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/DEP_Table.java index 90fdf42..3ae422d 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/Table.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/DEP_Table.java @@ -2,12 +2,13 @@ package at.ac.tuwien.sbc.valesriegler.pizzeria.gui; import java.io.Serializable; -public class Table implements Serializable { +@Deprecated +public class DEP_Table implements Serializable { private int id; private TableStatus status; private int groupId; - + public int getId() { return id; } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/Order.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/Order.java index 2ef2dc7..91beea2 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/Order.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/Order.java @@ -2,19 +2,20 @@ package at.ac.tuwien.sbc.valesriegler.pizzeria.gui; import java.util.List; +//TODO: cahnge me! public class Order { private int id; private int tableId; private int groupId; private OrderStatus status; - + private int waiterIdOfTableAssignment; private int waiterIdOfOrder; private int waiterIdOfServing; private int waiterIdOfPayment; - + private List pizzaOrders; - + public int getId() { return id; } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/PizzaOrder.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/PizzaOrder.java index 852865e..ce6b90b 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/PizzaOrder.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/PizzaOrder.java @@ -1,6 +1,6 @@ package at.ac.tuwien.sbc.valesriegler.pizzeria.gui; -import at.ac.tuwien.sbc.valesriegler.common.PizzaType; +import at.ac.tuwien.sbc.valesriegler.types.PizzaType; public class PizzaOrder { private PizzaType pizzaType; diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/PizzeriaFrame.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/PizzeriaFrame.java index 4783393..7a99e0e 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/PizzeriaFrame.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/PizzeriaFrame.java @@ -20,7 +20,6 @@ import javax.swing.event.ListSelectionListener; import javax.swing.event.TableModelEvent; import javax.swing.event.TableModelListener; -import at.ac.tuwien.sbc.valesriegler.common.PizzaType; import at.ac.tuwien.sbc.valesriegler.common.TableModel; import at.ac.tuwien.sbc.valesriegler.pizzeria.PizzeriaAgent; import at.ac.tuwien.sbc.valesriegler.pizzeria.PizzeriaAgent.TablesCreatedHandler; @@ -30,6 +29,7 @@ import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.OrdersOverviewMode import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.PizzasOfOrderModel; import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.TablesOverviewModel; import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.WaitersOfOrderModel; +import at.ac.tuwien.sbc.valesriegler.types.PizzaType; /** * Base Frame of the Pizzeria GUI @@ -115,7 +115,7 @@ public class PizzeriaFrame extends JFrame { // When the tables get created in the first place, the handler is informed int numberOfTables = getNumberOfTables(); - List
freeTablesCreated = tablesModel.createFreeTables(numberOfTables); + List freeTablesCreated = tablesModel.createFreeTables(numberOfTables); onTablesCreatedHandler.freeTablesCreated(freeTablesCreated); } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/GroupsOverviewModel.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/GroupsOverviewModel.java index 4427ae8..498e820 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/GroupsOverviewModel.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/GroupsOverviewModel.java @@ -1,9 +1,9 @@ package at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels; +import at.ac.tuwien.sbc.valesriegler.DEP_GroupDataMin; import at.ac.tuwien.sbc.valesriegler.common.TableModel; -import at.ac.tuwien.sbc.valesriegler.group.GroupData; -public class GroupsOverviewModel extends TableModel { +public class GroupsOverviewModel extends TableModel { private static final String ID = "ID"; private static final String SIZE = "Size"; @@ -18,7 +18,7 @@ public class GroupsOverviewModel extends TableModel { @Override public Object getValueAt(int rowIndex, int columnIndex) { - GroupData group = items.get(rowIndex); + DEP_GroupDataMin group = items.get(rowIndex); String wantedColumn = COLUMNS[columnIndex]; switch(wantedColumn) { case ID : return group.getId(); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/TablesOverviewModel.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/TablesOverviewModel.java index 1bf9c3f..2b810ea 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/TablesOverviewModel.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/gui/tablemodels/TablesOverviewModel.java @@ -4,10 +4,10 @@ import java.util.ArrayList; import java.util.List; import at.ac.tuwien.sbc.valesriegler.common.TableModel; -import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.Table; -import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.Table.TableStatus; +import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.DEP_Table; +import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.DEP_Table.TableStatus; -public class TablesOverviewModel extends TableModel
{ +public class TablesOverviewModel extends TableModel { private static final String TABLE_ID = "ID"; private static final String STATUS = "Status"; private static final String GROUP_ID = "Group ID"; @@ -23,7 +23,7 @@ public class TablesOverviewModel extends TableModel
{ @Override public Object getValueAt(int rowIndex, int columnIndex) { - Table table = items.get(rowIndex); + DEP_Table table = items.get(rowIndex); String wantedColumn = COLUMNS[columnIndex]; switch(wantedColumn) { case TABLE_ID : return table.getId(); @@ -41,7 +41,7 @@ public class TablesOverviewModel extends TableModel
{ List ids = new ArrayList<>(); synchronized(items) { - for(Table table : items) { + for(DEP_Table table : items) { if(table.getStatus() == TableStatus.FREE) { ids.add(table.getId()); } @@ -51,11 +51,11 @@ public class TablesOverviewModel extends TableModel
{ return ids; } - public List
createFreeTables(int numberOfTables) { + public List createFreeTables(int numberOfTables) { synchronized(items) { - List
tables = new ArrayList
(); + List tables = new ArrayList(); for(int i=0; i { } } - private Table createFreeTable() { - Table table = new Table(); + private DEP_Table createFreeTable() { + DEP_Table table = new DEP_Table(); table.setId(idCounter); idCounter = idCounter + 1; table.setStatus(TableStatus.FREE); diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupState.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/GroupState.java similarity index 74% rename from src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupState.java rename to src/main/java/at/ac/tuwien/sbc/valesriegler/types/GroupState.java index f564a8c..c0fa2bf 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupState.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/GroupState.java @@ -1,4 +1,4 @@ -package at.ac.tuwien.sbc.valesriegler.group; +package at.ac.tuwien.sbc.valesriegler.types; import java.io.Serializable; diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/common/Pizza.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Pizza.java similarity index 84% rename from src/main/java/at/ac/tuwien/sbc/valesriegler/common/Pizza.java rename to src/main/java/at/ac/tuwien/sbc/valesriegler/types/Pizza.java index 7b5dff6..9990c39 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/common/Pizza.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Pizza.java @@ -1,9 +1,9 @@ -package at.ac.tuwien.sbc.valesriegler.common; +package at.ac.tuwien.sbc.valesriegler.types; import java.io.Serializable; import at.ac.tuwien.sbc.valesriegler.jms.cook.Cook; -import at.ac.tuwien.sbc.valesriegler.jms.waiter.Waiter; +import at.ac.tuwien.sbc.valesriegler.waiter.Waiter; public class Pizza implements Serializable { final private PizzaType typ; diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/common/PizzaType.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/PizzaType.java similarity index 86% rename from src/main/java/at/ac/tuwien/sbc/valesriegler/common/PizzaType.java rename to src/main/java/at/ac/tuwien/sbc/valesriegler/types/PizzaType.java index 9e44555..453ffee 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/common/PizzaType.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/PizzaType.java @@ -1,4 +1,4 @@ -package at.ac.tuwien.sbc.valesriegler.common; +package at.ac.tuwien.sbc.valesriegler.types; import java.io.Serializable; diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Table.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Table.java new file mode 100644 index 0000000..62b03c2 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/types/Table.java @@ -0,0 +1,21 @@ +package at.ac.tuwien.sbc.valesriegler.types; + +import java.io.Serializable; + +public class Table implements Serializable { + private final int id; + + public Table(int id) { + this.id = id; + } + + @Override + public String toString() { + return "Table [id=" + id + "]"; + } + + public int getId() { + return id; + } + +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/Waiter.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/Waiter.java new file mode 100644 index 0000000..5fdd224 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/Waiter.java @@ -0,0 +1,60 @@ +package at.ac.tuwien.sbc.valesriegler.waiter; + +import java.io.Serializable; + +import javax.jms.Connection; +import javax.jms.MessageConsumer; +import javax.jms.Session; + +import org.apache.activemq.ActiveMQConnectionFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.ac.tuwien.sbc.valesriegler.waiter.jms.messageListeners.WantToOrder; +import at.ac.tuwien.sbc.valesriegler.waiter.jms.messageListeners.WantToSitAtTable; + +//TODO: needs a WaiterAgent and abstraction to support mzs + jms! +// Currently this is jms only! +public class Waiter implements Serializable { + private static final Logger log = LoggerFactory.getLogger(Waiter.class); + private static int nextID = 0; + final private int id; + + public static void main(String[] args) { + new Waiter(++nextID); + } + + public Waiter(int id) { + this.id = id; + log.info("I AM A WAITER WITH ID {}", id); + deliver(); + } + + @Override + public String toString() { + return "Waiter [id=" + id + "]"; + } + + public int getId() { + return id; + } + + public void deliver() { + try { + // Connecting to the Broker and to the output queue + ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); + Connection connection = connectionFactory.createConnection(); + connection.start(); + + Session sessWantToSit = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageConsumer consWantToSit = sessWantToSit.createConsumer(sessWantToSit.createQueue("WantToSitAtTable")); + consWantToSit.setMessageListener(new WantToSitAtTable(this)); + + Session sessWantToOrder = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageConsumer consWantToOrder = sessWantToOrder.createConsumer(sessWantToOrder.createQueue("WantToOrder")); + consWantToOrder.setMessageListener(new WantToOrder(this)); + } catch (Exception e) { + log.error("Caught: ", e); + } + } +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToOrder.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToOrder.java new file mode 100644 index 0000000..88a67d9 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToOrder.java @@ -0,0 +1,74 @@ +package at.ac.tuwien.sbc.valesriegler.waiter.jms.messageListeners; + +import javax.jms.Message; +import javax.jms.MessageListener; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.ac.tuwien.sbc.valesriegler.waiter.Waiter; + +public class WantToOrder implements MessageListener { + private static final Logger log = LoggerFactory.getLogger(WantToOrder.class); + private final Waiter waiter; + + public WantToOrder(Waiter waiter) { + this.waiter = waiter; + } + + @Override + public void onMessage(Message m) { + // try { + // m.acknowledge(); + // if (m instanceof ObjectMessage) { + // ObjectMessage pizzaMessage = (ObjectMessage) m; + // Object data = pizzaMessage.getObject(); + // + // if (data instanceof Group) { + // Group group = (Group) data; + log.debug("Received: " + m); + // + // // generate random delay + // Thread.sleep((long) (Math.random() * 10000)); + // + // ActiveMQConnectionFactory connectionFactory = new + // ActiveMQConnectionFactory("tcp://localhost:61616"); + // Connection connection = connectionFactory.createConnection(); + // connection.start(); + // Session session = connection.createSession(false, + // Session.AUTO_ACKNOWLEDGE); + // + // MessageProducer prodOP = + // session.createProducer(session.createQueue("OrderedPizzas")); + // prodOP.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + // + // // Create the queue for the cooks. + // Order o = group.askForOrder(waiter); + // for (PizzaType p : o.getOrdered()) { + // ObjectMessage message = session.createObjectMessage(p); + // prodOP.send(message); + // } + // + // // Also put the order in the Pending order queue for the waiter to + // // check. + // MessageProducer prodPending = + // session.createProducer(session.createQueue("PendingOrders")); + // prodPending.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + // ObjectMessage message = session.createObjectMessage(o); + // prodPending.send(message); + // + // session.close(); + // connection.close(); + // } else { + // log.warn("Received unknown Object: " + data); + // } + // } else { + // log.warn("Received unknown Message: " + m); + // } + // } catch (JMSException e) { + // log.error("EXCEPTION!", e); + // } catch (InterruptedException e) { + // log.error("EXCEPTION!", e); + // } + } +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToSitAtTable.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToSitAtTable.java new file mode 100644 index 0000000..9b1487e --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/waiter/jms/messageListeners/WantToSitAtTable.java @@ -0,0 +1,88 @@ +package at.ac.tuwien.sbc.valesriegler.waiter.jms.messageListeners; + +import javax.jms.Connection; +import javax.jms.DeliveryMode; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageConsumer; +import javax.jms.MessageListener; +import javax.jms.MessageProducer; +import javax.jms.ObjectMessage; +import javax.jms.Session; + +import org.apache.activemq.ActiveMQConnectionFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.ac.tuwien.sbc.valesriegler.group.actions.TableRequest; +import at.ac.tuwien.sbc.valesriegler.group.actions.TableResponse; +import at.ac.tuwien.sbc.valesriegler.types.Table; +import at.ac.tuwien.sbc.valesriegler.waiter.Waiter; + +public class WantToSitAtTable implements MessageListener { + private static final Logger log = LoggerFactory.getLogger(WantToSitAtTable.class); + private final Waiter waiter; + + public WantToSitAtTable(Waiter waiter) { + this.waiter = waiter; + } + + @Override + public void onMessage(Message msg) { + try { + msg.acknowledge(); + if (msg instanceof ObjectMessage) { + ObjectMessage objMsg = (ObjectMessage) msg; + Object obj = objMsg.getObject(); + + if (obj instanceof TableRequest) { + TableRequest tablerequest = (TableRequest) obj; + log.debug("Received: " + tablerequest); + + // generate random delay + Thread.sleep((long) (Math.random() * 10000)); + + ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); + Connection connection = connectionFactory.createConnection(); + connection.start(); + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + + MessageConsumer consumer = session.createConsumer(session.createQueue("TablesFree")); + Message trm = consumer.receive(); + if (trm instanceof ObjectMessage) { + ObjectMessage tom = (ObjectMessage) trm; + Object tabledata = tom.getObject(); + + if (tabledata instanceof Table) { + session.close(); + Table table = (Table) tabledata; + System.out.println("Received: " + table); + + TableResponse tr = new TableResponse(tablerequest.getGroupdata(), table, waiter.getId()); + session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageProducer ret = session.createProducer(session.createQueue("GroupConnector")); + ret.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + ret.send(session.createObjectMessage(tr)); + + } else { + log.warn("Received unknown Object: " + tabledata); + } + } else { + log.warn("Received unknown Message: " + trm); + } + session.close(); + connection.close(); + } else { + log.warn("Received unknown Object: " + obj); + } + } else { + log.warn("Received unknown Message: " + msg); + } + } catch (JMSException e) { + log.error("EXCEPTION!", e); + } catch (InterruptedException e) { + log.error("EXCEPTION!", e); + } + } + +} 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 465b315..43b24c5 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 @@ -17,7 +17,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import at.ac.tuwien.sbc.valesriegler.common.SpaceUtil; -import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.Table; +import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.DEP_Table; /** * Responsible for XVSM Communication @@ -38,10 +38,10 @@ public class XVSMConnector { tablesContainer = SpaceUtil.getOrCreateNamedContainer(SpaceUtil.SERVER_ADDR, SpaceUtil.TABLES_CONTAINER, capi, Arrays.asList((Coordinator) new AnyCoordinator())); } - public void sendFreeTablesToSpace(List
tables) { + public void sendFreeTablesToSpace(List tables) { try { List entries = new ArrayList<>(); - for (Table table : tables) { + for (DEP_Table table : tables) { entries.add(new Entry(table)); } capi.write(entries, tablesContainer); @@ -52,8 +52,8 @@ public class XVSMConnector { } } - public List
readTables() { - ArrayList
tables = new ArrayList<>(); + public List readTables() { + ArrayList tables = new ArrayList<>(); try { tables = capi.take(tablesContainer); } catch (MzsCoreException e) { 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 f16e843..b9fa1ce 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 @@ -7,7 +7,7 @@ import org.mozartspaces.core.MzsCoreException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.Table; +import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.DEP_Table; import at.ac.tuwien.sbc.valesriegler.xvsm.XVSMConnector; /** @@ -43,7 +43,7 @@ public class Waiter implements Serializable { private void start() { initSpaceCommunication(); - List
tables = xvsm.readTables(); + List tables = xvsm.readTables(); log.info("Number of free tables received: {}", tables.size()); diff --git a/src/main/resources/log4j.properties b/src/main/resources/log4j.properties deleted file mode 100644 index 101a101..0000000 --- a/src/main/resources/log4j.properties +++ /dev/null @@ -1,12 +0,0 @@ -# Set root logger level to DEBUG and its only appender to A1. -log4j.rootLogger=WARN, A1 - -# A1 is set to be a ConsoleAppender. -log4j.appender.A1=org.apache.log4j.ConsoleAppender - -# A1 uses PatternLayout. -log4j.appender.A1.layout=org.apache.log4j.PatternLayout -log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n - -log4j.category.org.apache.wicket=WARN -log4j.category.org.mozartspaces=WARN \ No newline at end of file diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml index 552fd62..21d6cda 100644 --- a/src/main/resources/logback.xml +++ b/src/main/resources/logback.xml @@ -1,67 +1,22 @@ - + + + + + + - - - - - - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - - + + + %-30(%d{HH:mm:ss.SSS} [%thread]) %-5level %logger{32} - %msg%n + - + + + - - - - - - + + + - - - - - - -- 2.43.0