1 package at.ac.tuwien.sbc.valesriegler.xvsm;
3 import java.io.Serializable;
5 import java.util.Arrays;
6 import java.util.Collections;
9 import org.mozartspaces.capi3.AnyCoordinator;
10 import org.mozartspaces.capi3.LindaCoordinator;
11 import org.mozartspaces.core.MzsConstants.RequestTimeout;
12 import org.mozartspaces.core.TransactionException;
13 import org.mozartspaces.core.TransactionReference;
14 import org.mozartspaces.notifications.Notification;
15 import org.mozartspaces.notifications.NotificationListener;
16 import org.mozartspaces.notifications.Operation;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 import at.ac.tuwien.sbc.valesriegler.common.Util;
21 import at.ac.tuwien.sbc.valesriegler.types.Pizza;
22 import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder;
23 import at.ac.tuwien.sbc.valesriegler.types.PizzaOrderStatus;
25 public class CookXVSM extends AbstractXVSMConnector {
26 private static final Logger log = LoggerFactory.getLogger(CookXVSM.class);
30 public CookXVSM(int id) {
34 groupsContainer = useContainer(Util.GROUPS_CONTAINER) ;
35 ordersContainer = useContainer(Util.ORDER) ;
36 deliverPizzasContainer = useContainer(Util.DELIVER_PIZZAS);
37 preparePizzasContainer = useContainer(Util.PREPARE_PIZZAS) ;
38 pizzaInProgressContainer = useContainer(Util.PIZZAS_IN_PROGRESS) ;
41 public void listenForPizzas() {
42 NotificationListener pizzasListener = new NotificationListener() {
44 public void entryOperationFinished(final Notification notification, final Operation operation, final List<? extends Serializable> entries) {
46 log.info("New Pizzas to prepare!");
48 List<PizzaOrder> pizzas = castEntries(entries);
49 Collections.rotate(pizzas, cookId);
50 for (PizzaOrder pizzaOrder : pizzas) {
53 TransactionReference tx = capi.createTransaction(9000, URI.create(Util.SERVER_ADDR));
54 String pizzaAlreadyCooked = String.format("Pizza with id %d has already been cooked by another cook", pizzaOrder.getId());
56 // Require the lock for preparing the pizza
57 PizzaOrder order = takeMatchingEntity(new PizzaOrder(pizzaOrder.getId()), preparePizzasContainer, tx, 1000, pizzaAlreadyCooked);
59 // tell the space that you prepare the pizza -> without a transaction!!
60 Pizza pizzaInProgress = Pizza.createPizzaFromPizzaOrder(order, cookId);
61 pizzaInProgress.setStatus(PizzaOrderStatus.IN_PREPARATION);
63 log.info("I say that I now prepare this pizza:");
64 log.info(pizzaInProgress.toString());
65 sendItemsToContainer(Arrays.asList(pizzaInProgress), pizzaInProgressContainer, RequestTimeout.DEFAULT, null);
67 PizzaOrder pizza = createPizza(order);
69 sendItemsToContainer(Arrays.asList(pizza), deliverPizzasContainer, RequestTimeout.DEFAULT, tx);
71 capi.commitTransaction(tx);
72 log.info("I have completed preparing a pizza for order {}!", pizza.getOrderId());
73 } catch (IllegalArgumentException e) {
74 log.info("IllegalArgumentException");
76 } catch (EntityNotFoundByTemplate e) {
77 log.info(e.getMessage());
78 } catch (NullPointerException e) {
79 // for some reason mozartspaces throws a npe but it doesn't seem to mean anything
80 } catch (Exception e) {
81 log.error("AN INNER EXCEPTION");
84 } catch (TransactionException e) {
85 log.info("An unimportant TransactionException has occurred");
86 } catch (Exception e) {
87 log.error("OUTER EXCEPTION");
94 notificationMgr.createNotification(preparePizzasContainer, pizzasListener, Operation.WRITE);
95 log.info("Created pizzasContainer notification for a cook");
96 } catch (Exception e) {
97 handleSpaceErrorAndTerminate(e);
101 private PizzaOrder createPizza(PizzaOrder order) throws InterruptedException {
102 long duration = order.getPizzaType().duration;
103 Thread.sleep(duration * 1000);
105 PizzaOrder pizza = Pizza.createPizzaFromPizzaOrder(order, cookId);
106 pizza.setStatus(PizzaOrderStatus.DONE);