From 7c7807001b1d85980eb4e9aee333984d2142ebfe Mon Sep 17 00:00:00 2001 From: Someone Date: Mon, 10 Jun 2013 00:41:18 +0200 Subject: [PATCH] [JMS] added naming service + integrated with PG. --- .../tuwien/sbc/valesriegler/common/Util.java | 60 +++++++------ .../tuwien/sbc/valesriegler/group/Group.java | 55 ++++++------ .../sbc/valesriegler/group/GroupGUI.java | 8 +- .../gui/GroupCreationDetailsRequest.java | 14 +-- .../group/gui/GroupCreationPanel.java | 89 ++++++++++--------- .../group/jms/GroupJMSNACMsgListener.java | 64 +++++++++++++ .../group/jms/JMSGroupConnector.java | 21 ++++- .../jms/nac/AbstractJMSNACMsgListener.java | 23 +++++ .../sbc/valesriegler/jms/nac/JMSNAC.java | 51 +++++++++++ .../jms/nac/actions/AbstractNACMsg.java | 14 +++ .../jms/nac/actions/AddressInfoRequest.java | 18 ++++ .../jms/nac/actions/AddressInfoResponse.java | 26 ++++++ .../valesriegler/pizzeria/PizzeriaAgent.java | 49 ++++++---- .../pizzeria/jms/JMSPizzeriaConnector.java | 4 + .../jms/PizzeriaJMSNACMsgListener.java | 43 +++++++++ .../sbc/valesriegler/types/GroupData.java | 45 ++++------ 16 files changed, 436 insertions(+), 148 deletions(-) create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/GroupJMSNACMsgListener.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/jms/nac/AbstractJMSNACMsgListener.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/jms/nac/JMSNAC.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/jms/nac/actions/AbstractNACMsg.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/jms/nac/actions/AddressInfoRequest.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/jms/nac/actions/AddressInfoResponse.java create mode 100644 src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/jms/PizzeriaJMSNACMsgListener.java 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 4aa24c6..671dc09 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 @@ -1,18 +1,30 @@ package at.ac.tuwien.sbc.valesriegler.common; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.Insets; +import java.net.URI; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.Set; + +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTable; +import javax.swing.ScrollPaneConstants; +import javax.swing.border.TitledBorder; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import at.ac.tuwien.sbc.valesriegler.group.gui.GroupCreationDetailsRequest; import at.ac.tuwien.sbc.valesriegler.types.Order; import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder; import at.ac.tuwien.sbc.valesriegler.types.PizzaType; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.swing.*; -import javax.swing.border.TitledBorder; -import java.awt.*; -import java.net.URI; -import java.util.*; -import java.util.List; public abstract class Util { private static final Logger log = LoggerFactory.getLogger(Util.class); @@ -42,9 +54,9 @@ public abstract class Util { public static final String DELIVERY_PROGRESS = "deliveryInProgress"; public static final String DELIVERY_DONE = "deliveryDone"; - public static final boolean runSimulation = false; + public static final boolean runSimulation = false; - public static final String NUMBER_DISPLAY_FORMAT = String.format("%%0%dd", 3); + public static final String NUMBER_DISPLAY_FORMAT = String.format("%%0%dd", 3); private static Random random = new Random(); @@ -54,8 +66,6 @@ public abstract class Util { public static final int GROUP_AGENT_PORT = 9876; public static final int DELIVERY_CUSTOMERS_PORT = 9877; - public static final String JMS_NAMING_SERVICE = "tcp://localhost:61616?jms.prefetchPolicy.all=1"; - public static String getId(int id) { return (id != 0 && id != -1) ? String.valueOf(id) : ""; } @@ -115,7 +125,7 @@ public abstract class Util { wrapperPanel.setBorder(new TitledBorder(title)); scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED); wrapperPanel.add(scrollPane, c); -// table.setAutoCreateRowSorter(true); + // table.setAutoCreateRowSorter(true); return table; } @@ -157,15 +167,15 @@ public abstract class Util { System.exit(1); } - public static Order createOrder(HasId group, GroupCreationDetailsRequest gc) { - List pizzaOrders = new ArrayList<>(); - for (PizzaType pt : gc.pizzaTypes) { - final PizzaOrder pizzaOrder = new PizzaOrder(pt); - pizzaOrders.add(pizzaOrder); - } - Order order = new Order(group, pizzaOrders); - order.setNumberOfPizzas(order.getOrderedPizzas().size()); - - return order; - } + public static Order createOrder(HasId group, GroupCreationDetailsRequest gc) { + List pizzaOrders = new ArrayList<>(); + for (PizzaType pt : gc.pizzaTypes) { + final PizzaOrder pizzaOrder = new PizzaOrder(pt); + pizzaOrders.add(pizzaOrder); + } + Order order = new Order(group, pizzaOrders); + order.setNumberOfPizzas(order.getOrderedPizzas().size()); + + return order; + } } 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 9a948f5..4ab8fdf 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 @@ -8,6 +8,7 @@ import at.ac.tuwien.sbc.valesriegler.group.actions.OrderRequest; import at.ac.tuwien.sbc.valesriegler.group.actions.PayRequest; import at.ac.tuwien.sbc.valesriegler.group.actions.TableFree; import at.ac.tuwien.sbc.valesriegler.group.actions.TableRequest; +import at.ac.tuwien.sbc.valesriegler.group.jms.JMSGroupConnector; import at.ac.tuwien.sbc.valesriegler.types.GroupData; import at.ac.tuwien.sbc.valesriegler.types.GroupState; import at.ac.tuwien.sbc.valesriegler.types.Order; @@ -22,7 +23,7 @@ import at.ac.tuwien.sbc.valesriegler.types.Table; */ public class Group implements Runnable, HasId { private static final Logger log = LoggerFactory.getLogger(Group.class); - private static int idNext = 0; // TODO: set to 0 after debugging! + private static int idNext = 0; private GroupData groupData; @@ -41,32 +42,32 @@ public class Group implements Runnable, HasId { 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: - GroupAgent.getInstance().getGroupcomm().send(new OrderRequest(groupData)); - order(); - break; - case ORDER_PENDING: - break; // do nothing. - case ORDERED: - break; // do nothing. - case EATING: // request bill. - GroupAgent.getInstance().getGroupcomm().send(new PayRequest(groupData)); - break; - case PAY:// leave table - GroupAgent.getInstance().getGroupcomm().send(new TableFree(groupData)); - groupData.setState(GroupState.GONE); - groupData.setTable(null); - break; - case GONE: - break; // do nothing. - default: - break; + case NEW : + // TODO : may not happen! + break; + case WAITING : + JMSGroupConnector.getConnector(groupData.getPizzeriaId()).send(new TableRequest(groupData)); + break; + case SITTING : + JMSGroupConnector.getConnector(groupData.getPizzeriaId()).send(new OrderRequest(groupData)); + order(); + break; + case ORDER_PENDING : + break; // do nothing. + case ORDERED : + break; // do nothing. + case EATING : // request bill. + JMSGroupConnector.getConnector(groupData.getPizzeriaId()).send(new PayRequest(groupData)); + break; + case PAY :// leave table + JMSGroupConnector.getConnector(groupData.getPizzeriaId()).send(new TableFree(groupData)); + groupData.setState(GroupState.GONE); + groupData.setTable(null); + break; + case GONE : + break; // do nothing. + default : + break; } } catch (InterruptedException e) { 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 index c3df848..4ee4ec3 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupGUI.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/GroupGUI.java @@ -2,11 +2,15 @@ package at.ac.tuwien.sbc.valesriegler.group; import at.ac.tuwien.sbc.valesriegler.group.gui.GroupFrame; -class GroupGUI implements Runnable { +public class GroupGUI implements Runnable { + private GroupFrame frame; @Override public void run() { - GroupFrame frame = new GroupFrame(GroupAgent.getInstance().getGroupModel(), GroupAgent.getInstance().getDeliveryModel()); + frame = new GroupFrame(GroupAgent.getInstance().getGroupModel(), GroupAgent.getInstance().getDeliveryModel()); frame.pack(); frame.setVisible(true); } + public GroupFrame getFrame() { + return frame; + } } \ No newline at end of file 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 f6ce3ed..93f12ef 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 @@ -8,15 +8,15 @@ public class GroupCreationDetailsRequest { public final int size; public final int numberOfGroups; public final List pizzaTypes; - public final String address; - public final String pizzeria; + public final String address; + public final String pizzeria; - public GroupCreationDetailsRequest(int size, int numberOfGroups, - List pizzaTypes, String address, String pizzeriaIdentifier) { + 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; - } + this.address = address; + pizzeria = pizzeriaIdentifier; + } } 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 9073877..1df226c 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 @@ -1,51 +1,56 @@ package at.ac.tuwien.sbc.valesriegler.group.gui; -import javax.swing.*; -import javax.swing.border.TitledBorder; -import java.awt.*; +import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import javax.swing.JButton; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JSpinner; +import javax.swing.SpinnerNumberModel; +import javax.swing.border.TitledBorder; public class GroupCreationPanel extends JPanel { - private boolean createDeliveryGroups; - - public GroupCreationPanel(final boolean createDeliveryGroups) { - this.createDeliveryGroups = createDeliveryGroups; - - 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); - - // 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(); - GroupCreationPanel.this.createDeliveryGroups = createDeliveryGroups; - final GroupCreationDetailsPanel groupCreationDetailsPanel = new GroupCreationDetailsPanel(numberMembers, GroupCreationPanel.this.createDeliveryGroups); - GroupCreationHandler groupCreationHandler = new GroupCreationHandler(GroupCreationPanel.this, chooseGroupSizePanel, - groupCreationDetailsPanel, GroupCreationPanel.this.createDeliveryGroups); - - groupCreationDetailsPanel.setCreateAndCancelHandler(groupCreationHandler); - groupCreationHandler.showGroupCreationDetailPanel(); - - } - }); - - GridLayout creationPanelLayout = new GridLayout(3, 1); - chooseGroupSizePanel.setLayout(creationPanelLayout); - final String title = createDeliveryGroups ? "Create Delivery Groups" : "Create Normal Groups"; - this.setBorder(new TitledBorder(title)); - - this.add(chooseGroupSizePanel); - chooseGroupSizePanel.add(creationLabel); - chooseGroupSizePanel.add(spinner); - chooseGroupSizePanel.add(next); - - } + private boolean createDeliveryGroups; + + public GroupCreationPanel(final boolean createDeliveryGroups) { + this.createDeliveryGroups = createDeliveryGroups; + + 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); + + // 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(); + GroupCreationPanel.this.createDeliveryGroups = createDeliveryGroups; + final GroupCreationDetailsPanel groupCreationDetailsPanel = new GroupCreationDetailsPanel(numberMembers, + GroupCreationPanel.this.createDeliveryGroups); + GroupCreationHandler groupCreationHandler = new GroupCreationHandler(GroupCreationPanel.this, + chooseGroupSizePanel, groupCreationDetailsPanel, GroupCreationPanel.this.createDeliveryGroups); + + groupCreationDetailsPanel.setCreateAndCancelHandler(groupCreationHandler); + groupCreationHandler.showGroupCreationDetailPanel(); + + } + }); + + GridLayout creationPanelLayout = new GridLayout(3, 1); + chooseGroupSizePanel.setLayout(creationPanelLayout); + final String title = createDeliveryGroups ? "Create Delivery Groups" : "Create Normal Groups"; + setBorder(new TitledBorder(title)); + + this.add(chooseGroupSizePanel); + chooseGroupSizePanel.add(creationLabel); + chooseGroupSizePanel.add(spinner); + chooseGroupSizePanel.add(next); + + } } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/GroupJMSNACMsgListener.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/GroupJMSNACMsgListener.java new file mode 100644 index 0000000..ae63458 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/GroupJMSNACMsgListener.java @@ -0,0 +1,64 @@ +package at.ac.tuwien.sbc.valesriegler.group.jms; + +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.ObjectMessage; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.ac.tuwien.sbc.valesriegler.group.GroupAgent; +import at.ac.tuwien.sbc.valesriegler.jms.nac.AbstractJMSNACMsgListener; +import at.ac.tuwien.sbc.valesriegler.jms.nac.actions.AddressInfoRequest; +import at.ac.tuwien.sbc.valesriegler.jms.nac.actions.AddressInfoResponse; +/** + * Handles the NAC communication for the Group Agent. + * + * @author jan + * + */ +public class GroupJMSNACMsgListener extends AbstractJMSNACMsgListener implements Runnable { + private static final Logger log = LoggerFactory.getLogger(GroupJMSNACMsgListener.class); + + public GroupJMSNACMsgListener() { + Thread th = new Thread(this); + th.setDaemon(true); + th.run(); + } + + @Override + public void onMessage(Message msg) { + try { + if (msg instanceof ObjectMessage) { + ObjectMessage objMsg = (ObjectMessage) msg; + Object obj = objMsg.getObject(); + + if (obj instanceof AddressInfoResponse) { + AddressInfoResponse response = (AddressInfoResponse) obj; + GroupAgent.getInstance().getPizzeriaIdentifiers().add(response.getAddress()); + GroupAgent.getGroupGui().getFrame().repaint(); + JMSGroupConnector.getConnectors().put(response.getAddress(), new JMSGroupConnector(response.getAddress())); + } else { + log.warn("Received unknown Object: " + obj); + } + } else { + log.warn("Received unknown Message: " + msg); + } + } catch (JMSException e) { + log.error("EXCEPTION!", e); + } + } + @Override + public void run() { + while (true) { + try { + getJmsnac().sendNACMsg(new AddressInfoRequest()); + Thread.sleep(10000); + GroupAgent.getInstance().getPizzeriaIdentifiers().clear(); + } catch (Exception e) { + log.warn("EXCEPTION!", e); + } + } + } + +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/JMSGroupConnector.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/JMSGroupConnector.java index fd7965f..360b8b1 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/JMSGroupConnector.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/group/jms/JMSGroupConnector.java @@ -1,5 +1,7 @@ package at.ac.tuwien.sbc.valesriegler.group.jms; +import java.util.HashMap; + import javax.jms.Connection; import javax.jms.DeliveryMode; import javax.jms.JMSException; @@ -35,10 +37,18 @@ import at.ac.tuwien.sbc.valesriegler.waiter.actions.DeliverOrder; */ public class JMSGroupConnector extends AbstractGroupConnector implements MessageListener { private static final Logger log = LoggerFactory.getLogger(JMSGroupConnector.class); - // FIXME: how to communicate?! - public static final String JMS_CONNECTSTRING = "tcp://localhost:61616?jms.prefetchPolicy.all=1"; + private static HashMap connectors = new HashMap(); + + public final String JMS_CONNECTSTRING; private Connection connection; + public JMSGroupConnector(String address) { + if (address == null) + throw new IllegalArgumentException("Address may not be null!"); + JMS_CONNECTSTRING = address; + init(); + } + @Override public void init() { try { @@ -143,4 +153,11 @@ public class JMSGroupConnector extends AbstractGroupConnector implements Message } } + public static HashMap getConnectors() { + return connectors; + } + public static JMSGroupConnector getConnector(String key) { + return connectors.get(key); + } + } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/nac/AbstractJMSNACMsgListener.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/nac/AbstractJMSNACMsgListener.java new file mode 100644 index 0000000..4a90191 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/nac/AbstractJMSNACMsgListener.java @@ -0,0 +1,23 @@ +package at.ac.tuwien.sbc.valesriegler.jms.nac; + +import javax.jms.MessageListener; + +/** + * Abstract "Naming and Control"-msgListener. + * + * @author jan + * + */ +public abstract class AbstractJMSNACMsgListener implements MessageListener { + // private static final Logger log = LoggerFactory.getLogger(AbstractJMSNACMsgListener.class); + private JMSNAC jmsnac = null; + + public AbstractJMSNACMsgListener() { + } + protected void setJmsnac(JMSNAC jmsnac) { + this.jmsnac = jmsnac; + } + public JMSNAC getJmsnac() { + return jmsnac; + } +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/nac/JMSNAC.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/nac/JMSNAC.java new file mode 100644 index 0000000..a70f946 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/nac/JMSNAC.java @@ -0,0 +1,51 @@ +package at.ac.tuwien.sbc.valesriegler.jms.nac; + +import javax.jms.Connection; +import javax.jms.DeliveryMode; +import javax.jms.JMSException; +import javax.jms.MessageConsumer; +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.jms.nac.actions.AbstractNACMsg; + +/** + * JMS "Naming And Control". This class registers the listener and initializes the Broker connection. + * + * @author jan + * + */ +public class JMSNAC { + private static final Logger log = LoggerFactory.getLogger(JMSNAC.class); + public static final String JMS_NAC_SERVICE = "tcp://localhost:61610?jms.prefetchPolicy.all=1"; + public static final String JMS_NAC_TOPIC = "NAC"; + private Connection connection; + + public JMSNAC(AbstractJMSNACMsgListener listener) { + log.info("Initialising Naming and control module."); + listener.setJmsnac(this); + + try { + ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(JMS_NAC_SERVICE); + connection = connectionFactory.createConnection(); + connection.start(); + + Session sessWantToSit = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageConsumer consWantToSit = sessWantToSit.createConsumer(sessWantToSit.createTopic(JMS_NAC_TOPIC)); + consWantToSit.setMessageListener(listener); + } catch (JMSException e) { + log.error("EXCEPTION!", e); + } + } + public void sendNACMsg(AbstractNACMsg msg) throws JMSException { + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageProducer prod = session.createProducer(session.createTopic(JMS_NAC_TOPIC)); + prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + prod.send(session.createObjectMessage(msg)); + session.close(); + } +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/nac/actions/AbstractNACMsg.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/nac/actions/AbstractNACMsg.java new file mode 100644 index 0000000..ce2638d --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/nac/actions/AbstractNACMsg.java @@ -0,0 +1,14 @@ +package at.ac.tuwien.sbc.valesriegler.jms.nac.actions; + +import java.io.Serializable; + +/** + * Abstract NAC Message. + * + * @author jan + * + */ +public abstract class AbstractNACMsg implements Serializable { + public AbstractNACMsg() { + } +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/nac/actions/AddressInfoRequest.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/nac/actions/AddressInfoRequest.java new file mode 100644 index 0000000..252f7b8 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/nac/actions/AddressInfoRequest.java @@ -0,0 +1,18 @@ +package at.ac.tuwien.sbc.valesriegler.jms.nac.actions; + + +/** + * Message hat instructs everyone to reply their Address. + * + * @author jan + * + */ +public class AddressInfoRequest extends AbstractNACMsg { + public AddressInfoRequest() { + } + + @Override + public String toString() { + return "AddressInfoRequest []"; + } +} diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/nac/actions/AddressInfoResponse.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/nac/actions/AddressInfoResponse.java new file mode 100644 index 0000000..d60b73d --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/jms/nac/actions/AddressInfoResponse.java @@ -0,0 +1,26 @@ +package at.ac.tuwien.sbc.valesriegler.jms.nac.actions; + + +/** + * Response to the group's interest in pizza. + * + * @author jan + * + */ +public class AddressInfoResponse extends AbstractNACMsg { + final private String address; + + public AddressInfoResponse(String address) { + this.address = address; + } + + public String getAddress() { + return address; + } + + @Override + public String toString() { + return "AddressInfoResponse [address=" + address + "]"; + } + +} 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 1d4347f..3e8c4d8 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 @@ -1,17 +1,27 @@ package at.ac.tuwien.sbc.valesriegler.pizzeria; +import java.util.List; + +import javax.swing.SwingUtilities; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import at.ac.tuwien.sbc.valesriegler.common.Util; +import at.ac.tuwien.sbc.valesriegler.jms.nac.JMSNAC; import at.ac.tuwien.sbc.valesriegler.pizzeria.actions.TableNew; import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.PizzeriaFrame; -import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.*; +import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.DeliveryDetailsModel; +import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.tablemodels.DeliveryOrdersModel; +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; +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.pizzeria.jms.JMSPizzeriaConnector; +import at.ac.tuwien.sbc.valesriegler.pizzeria.jms.PizzeriaJMSNACMsgListener; import at.ac.tuwien.sbc.valesriegler.types.Table; import at.ac.tuwien.sbc.valesriegler.xvsm.PizzeriaAgentXVSM; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.swing.*; -import java.util.List; /** * The Main class of the Pizzeria compoment. @@ -38,6 +48,7 @@ public class PizzeriaAgent { private PizzeriaAgentXVSM xvsm; private JMSPizzeriaConnector jmspc; + private JMSNAC jmsnac; public static void main(String[] args) { if (args.length != 2) { @@ -50,25 +61,25 @@ public class PizzeriaAgent { case "XVSM" : pizzeriaAgent.createModels(); pizzeriaAgent.initXVSM(args[1]); - if (! Util.runSimulation) { - pizzeriaAgent.initGUI(); - } - Util.useJMS = false; + if (!Util.runSimulation) { + pizzeriaAgent.initGUI(); + } + Util.useJMS = false; break; case "JMS" : pizzeriaAgent.createModels(); pizzeriaAgent.jmspc = new JMSPizzeriaConnector(args[1]); pizzeriaAgent.jmspc.init(); - if (! Util.runSimulation) { - pizzeriaAgent.initGUI(); - } - Util.useJMS = true; + pizzeriaAgent.jmsnac = new JMSNAC(new PizzeriaJMSNACMsgListener()); + if (!Util.runSimulation) { + pizzeriaAgent.initGUI(); + } + Util.useJMS = true; break; default : throw new IllegalArgumentException(USAGE); } } - private void initXVSM(String arg) { int port = 0; try { @@ -162,6 +173,14 @@ public class PizzeriaAgent { return pizzasOfOrderModel; } + public JMSNAC getJmsnac() { + return jmsnac; + } + + public JMSPizzeriaConnector getJmspc() { + return jmspc; + } + public interface TablesCreatedHandler { public void freeTablesCreated(List tables); } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/jms/JMSPizzeriaConnector.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/jms/JMSPizzeriaConnector.java index e2d71d9..2c4e44b 100644 --- a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/jms/JMSPizzeriaConnector.java +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/jms/JMSPizzeriaConnector.java @@ -150,4 +150,8 @@ public class JMSPizzeriaConnector extends AbstractPizzeriaConnector implements M } } + public String getCONNECTSTRING() { + return CONNECTSTRING; + } + } diff --git a/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/jms/PizzeriaJMSNACMsgListener.java b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/jms/PizzeriaJMSNACMsgListener.java new file mode 100644 index 0000000..3c2deb0 --- /dev/null +++ b/src/main/java/at/ac/tuwien/sbc/valesriegler/pizzeria/jms/PizzeriaJMSNACMsgListener.java @@ -0,0 +1,43 @@ +package at.ac.tuwien.sbc.valesriegler.pizzeria.jms; + +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.ObjectMessage; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.ac.tuwien.sbc.valesriegler.jms.nac.AbstractJMSNACMsgListener; +import at.ac.tuwien.sbc.valesriegler.jms.nac.actions.AddressInfoRequest; +import at.ac.tuwien.sbc.valesriegler.jms.nac.actions.AddressInfoResponse; +import at.ac.tuwien.sbc.valesriegler.pizzeria.PizzeriaAgent; +/** + * Handles the NAC communication for the Group Agent. + * + * @author jan + * + */ +public class PizzeriaJMSNACMsgListener extends AbstractJMSNACMsgListener { + private static final Logger log = LoggerFactory.getLogger(PizzeriaJMSNACMsgListener.class); + + @Override + public void onMessage(Message msg) { + try { + if (msg instanceof ObjectMessage) { + ObjectMessage objMsg = (ObjectMessage) msg; + Object obj = objMsg.getObject(); + + if (obj instanceof AddressInfoRequest) { + AddressInfoResponse air = new AddressInfoResponse(PizzeriaAgent.getInstance().getJmspc().getCONNECTSTRING()); + PizzeriaAgent.getInstance().getJmsnac().sendNACMsg(air); + } else { + log.warn("Received unknown Object: " + obj); + } + } else { + log.warn("Received unknown Message: " + msg); + } + } catch (JMSException e) { + log.error("EXCEPTION!", e); + } + } +} 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 9f80b5f..30d2005 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 @@ -1,14 +1,11 @@ package at.ac.tuwien.sbc.valesriegler.types; import java.io.Serializable; -import java.util.ArrayList; -import java.util.List; import org.mozartspaces.capi3.Queryable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import at.ac.tuwien.sbc.valesriegler.common.HasId; import at.ac.tuwien.sbc.valesriegler.common.Util; /** @@ -33,25 +30,25 @@ public class GroupData implements Serializable, HasOrder { private Integer orderWaiter; private Integer servingWaiter; private Integer payingWaiter; - private String pizzeriaId; + private String pizzeriaId; - public GroupData() { - + public GroupData() { } public GroupData(Integer id) { this.id = id; } - + public void setId(Integer id) { this.id = id; } + @Override public int getId() { return Util.getIntSafe(id); } - @Override + @Override public Order getOrder() { return order; } @@ -68,13 +65,13 @@ public class GroupData implements Serializable, HasOrder { return Util.getIntSafe(servingWaiter); } - public String getPizzeriaId() { - return pizzeriaId; - } + public String getPizzeriaId() { + return pizzeriaId; + } - public void setPizzeriaId(String pizzeriaId) { - this.pizzeriaId = pizzeriaId; - } + public void setPizzeriaId(String pizzeriaId) { + this.pizzeriaId = pizzeriaId; + } public int getSize() { return Util.getIntSafe(size); @@ -124,18 +121,10 @@ public class GroupData implements Serializable, HasOrder { this.tableWaiter = tableWaiter; } - @Override - public String toString() { - return "GroupData{" + - "id=" + id + - ", state=" + state + - ", size=" + size + - ", table=" + table + - ", tableWaiter=" + tableWaiter + - ", order=" + order + - ", orderWaiter=" + orderWaiter + - ", servingWaiter=" + servingWaiter + - ", payingWaiter=" + payingWaiter + - '}'; - } + @Override + public String toString() { + return "GroupData{" + "id=" + id + ", state=" + state + ", size=" + size + ", table=" + table + ", tableWaiter=" + + tableWaiter + ", order=" + order + ", orderWaiter=" + orderWaiter + ", servingWaiter=" + servingWaiter + + ", payingWaiter=" + payingWaiter + '}'; + } } -- 2.43.0