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 orderTakenContainer = useContainer(Util.ORDER_TAKEN) ;
35 deliverPizzasContainer = useContainer(Util.DELIVER_PIZZAS);
36 preparePizzasContainer = useContainer(Util.PREPARE_PIZZAS) ;
37 pizzaInProgressContainer = useContainer(Util.PIZZAS_IN_PROGRESS) ;
40 public void listenForPizzas() {
41 NotificationListener pizzasListener = new NotificationListener() {
43 public void entryOperationFinished(final Notification notification, final Operation operation, final List<? extends Serializable> entries) {
45 log.info("New Pizzas to prepare!");
47 List<PizzaOrder> pizzas = castEntries(entries);
48 Collections.rotate(pizzas, cookId);
49 for (PizzaOrder pizzaOrder : pizzas) {
52 TransactionReference tx = capi.createTransaction(9000, URI.create(Util.SERVER_ADDR));
53 String pizzaAlreadyCooked = String.format("Pizza with id %d has already been cooked by another cook", pizzaOrder.getId());
55 // Require the lock for preparing the pizza
56 PizzaOrder order = takeMatchingEntity(new PizzaOrder(pizzaOrder.getId()), preparePizzasContainer, tx, 1000, pizzaAlreadyCooked);
58 // tell the space that you prepare the pizza -> without a transaction!!
59 Pizza pizzaInProgress = Pizza.createPizzaFromPizzaOrder(order, cookId);
60 pizzaInProgress.setStatus(PizzaOrderStatus.IN_PREPARATION);
62 log.info("I say that I now prepare this pizza:");
63 log.info(pizzaInProgress.toString());
64 sendItemsToContainer(Arrays.asList(pizzaInProgress), pizzaInProgressContainer, RequestTimeout.DEFAULT, null);
66 PizzaOrder pizza = createPizza(order);
68 sendItemsToContainer(Arrays.asList(pizza), deliverPizzasContainer, RequestTimeout.DEFAULT, tx);
70 capi.commitTransaction(tx);
71 log.info("I have completed preparing a pizza for order {}!", pizza.getOrderId());
72 } catch (IllegalArgumentException e) {
73 log.info("IllegalArgumentException");
75 } catch (EntityNotFoundByTemplate e) {
76 log.info(e.getMessage());
77 } catch (NullPointerException e) {
78 // for some reason mozartspaces throws a npe but it doesn't seem to mean anything
79 } catch (Exception e) {
80 log.error("AN INNER EXCEPTION");
83 } catch (TransactionException e) {
84 log.info("An unimportant TransactionException has occurred");
85 } catch (Exception e) {
86 log.error("OUTER EXCEPTION");
93 notificationMgr.createNotification(preparePizzasContainer, pizzasListener, Operation.WRITE);
94 log.info("Created pizzasContainer notification for a cook");
95 } catch (Exception e) {
96 handleSpaceErrorAndTerminate(e);
100 private PizzaOrder createPizza(PizzaOrder order) throws InterruptedException {
101 long duration = order.getPizzaType().duration;
102 Thread.sleep(duration * 1000);
104 PizzaOrder pizza = Pizza.createPizzaFromPizzaOrder(order, cookId);
105 pizza.setStatus(PizzaOrderStatus.DONE);