]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/CookXVSM.java
Make cook and waiter look around for work to do if they are idle
[pub/jan/sbc.git] / src / main / java / at / ac / tuwien / sbc / valesriegler / xvsm / CookXVSM.java
1 package at.ac.tuwien.sbc.valesriegler.xvsm;
2
3 import java.io.Serializable;
4 import java.net.URI;
5 import java.util.Arrays;
6 import java.util.Collections;
7 import java.util.List;
8
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;
19
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;
24
25 public class CookXVSM extends AbstractXVSMConnector {
26         private static final Logger log = LoggerFactory.getLogger(CookXVSM.class);
27
28         private int cookId;
29
30         public CookXVSM(int id) {
31                 super();
32                 
33                 this.cookId = 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) ;
38         }
39
40         public void listenForPizzas() {
41                 SpaceListener pizzasListener = new SpaceListenerImpl(capi, preparePizzasContainer) {
42                         
43                         @Override
44                         void onEntriesWritten(List<? extends Serializable> entries)
45                                         throws Exception {
46          
47                                 List<PizzaOrder> pizzas = castEntries(entries);
48                                 Collections.rotate(pizzas, cookId);
49                                 
50                                 for (PizzaOrder pizzaOrder : pizzas) {
51                 
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());
54                                         
55                                         // Require the lock for preparing the pizza
56                                         PizzaOrder order = takeMatchingEntity(new PizzaOrder(pizzaOrder.getId()), preparePizzasContainer, tx, 1000, pizzaAlreadyCooked);
57                                         
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);
61                                         
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);
65                                         
66                                         PizzaOrder pizza = createPizza(order);
67                                         
68                                         sendItemsToContainer(Arrays.asList(pizza), deliverPizzasContainer, RequestTimeout.DEFAULT, tx); 
69                                         
70                                         capi.commitTransaction(tx);
71                                         log.info("I have completed preparing a pizza for order {}!", pizza.getOrderId());
72                         
73                                 }
74             }
75         };
76         createNotification(pizzasListener, preparePizzasContainer);
77  
78         }
79
80         private PizzaOrder createPizza(PizzaOrder order) throws InterruptedException {
81                 long duration = order.getPizzaType().duration;
82                 Thread.sleep(duration * 1000);
83                 
84                 PizzaOrder pizza = Pizza.createPizzaFromPizzaOrder(order, cookId);
85                 pizza.setStatus(PizzaOrderStatus.DONE);
86                 return pizza;
87         }
88
89 }