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.FifoCoordinator;
10 import org.mozartspaces.capi3.LindaCoordinator;
11 import org.mozartspaces.core.MzsConstants;
12 import org.mozartspaces.core.MzsConstants.RequestTimeout;
13 import org.mozartspaces.core.TransactionReference;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
17 import at.ac.tuwien.sbc.valesriegler.common.Util;
18 import at.ac.tuwien.sbc.valesriegler.types.Pizza;
19 import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder;
20 import at.ac.tuwien.sbc.valesriegler.types.PizzaOrderStatus;
22 public class CookXVSM extends AbstractXVSMConnector {
23 private static final Logger log = LoggerFactory.getLogger(CookXVSM.class);
27 public CookXVSM(int id) {
31 orderTakenContainer = useContainer(Util.ORDER_TAKEN) ;
32 deliverPizzasContainer = useContainer(Util.DELIVER_PIZZAS);
33 preparePizzasContainer = useContainer(Util.PREPARE_PIZZAS, createCoordinators(new FifoCoordinator(), new LindaCoordinator(false)));
34 pizzaInProgressContainer = useContainer(Util.PIZZAS_IN_PROGRESS) ;
37 public void listenForPizzas() {
38 SpaceListener pizzasListener = new SpaceListenerImpl(capi, preparePizzasContainer, 15000) {
41 void onEntriesWritten(List<? extends Serializable> entries)
44 List<PizzaOrder> pizzas = castEntries(entries);
46 if(inNotification.get()) Collections.shuffle(pizzas);
48 for (PizzaOrder pizzaOrder : pizzas) {
50 TransactionReference tx = capi.createTransaction(9000, URI.create(Util.SERVER_ADDR));
51 String pizzaAlreadyCooked = String.format("Pizza with id %d has already been cooked by another cook", pizzaOrder.getId());
54 // Require the lock for preparing the pizza
55 PizzaOrder order = takeMatchingEntity(new PizzaOrder(pizzaOrder.getId()), preparePizzasContainer, tx, RequestTimeout.ZERO, pizzaAlreadyCooked);
57 // tell the space that you prepare the pizza -> without a transaction!!
58 Pizza pizzaInProgress = Pizza.createPizzaFromPizzaOrder(order, cookId);
59 pizzaInProgress.setStatus(PizzaOrderStatus.IN_PREPARATION);
62 log.info("I say that I now prepare a pizza for order {}", pizzaInProgress.getOrderId());
63 sendItemsToContainer(Arrays.asList(pizzaInProgress), pizzaInProgressContainer, RequestTimeout.DEFAULT, null);
65 PizzaOrder pizza = createPizza(order);
67 sendItemsToContainer(Arrays.asList(pizza), deliverPizzasContainer, RequestTimeout.DEFAULT, tx);
69 capi.commitTransaction(tx);
70 log.info("I have completed preparing a pizza for order {}!", pizza.getOrderId());
71 } catch (NullPointerException e) {
72 // the strange nullpointer exception from the space
73 } catch (Exception e) {
74 log.info(e.getMessage());
81 List<? extends Serializable> getEntries() throws Exception {
82 return capi.read(preparePizzasContainer, FifoCoordinator
83 .newSelector(MzsConstants.Selecting.COUNT_ALL),
84 RequestTimeout.DEFAULT, null);
87 createNotification(pizzasListener, preparePizzasContainer);
91 private PizzaOrder createPizza(PizzaOrder order) throws InterruptedException {
92 long duration = order.getPizzaType().duration;
93 Thread.sleep(duration * 1000);
95 PizzaOrder pizza = Pizza.createPizzaFromPizzaOrder(order, cookId);
96 pizza.setStatus(PizzaOrderStatus.DONE);