]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/CookXVSM.java
Space waiters pay and overall remaining space workflow refactoring
[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                 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) ;
39         }
40
41         public void listenForPizzas() {
42                 NotificationListener pizzasListener = new NotificationListener() {
43             @Override
44             public void entryOperationFinished(final Notification notification, final Operation operation, final List<? extends Serializable> entries) {
45                
46                                 log.info("New Pizzas to prepare!");
47                                 
48                                 List<PizzaOrder> pizzas = castEntries(entries);
49                                 Collections.rotate(pizzas, cookId);
50                                 for (PizzaOrder pizzaOrder : pizzas) {
51                                         try {
52                                                 try {
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());
55                                                         
56                                                         // Require the lock for preparing the pizza
57                                                         PizzaOrder order = takeMatchingEntity(new PizzaOrder(pizzaOrder.getId()), preparePizzasContainer, tx, 1000, pizzaAlreadyCooked);
58                                                         
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);
62                                                         
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);
66                                                         
67                                                         PizzaOrder pizza = createPizza(order);
68                                                         
69                                                         sendItemsToContainer(Arrays.asList(pizza), deliverPizzasContainer, RequestTimeout.DEFAULT, tx); 
70                                                         
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");
75                                                         e.printStackTrace();
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");
82                                                         e.printStackTrace();
83                                                 }
84                                         } catch (TransactionException e) {
85                                                 log.info("An unimportant TransactionException has occurred");
86                                         } catch (Exception e) {
87                                                 log.error("OUTER EXCEPTION");
88                                         }
89                                 }
90                                 
91             }
92         };
93         try {
94                         notificationMgr.createNotification(preparePizzasContainer, pizzasListener, Operation.WRITE);
95                         log.info("Created pizzasContainer notification for a cook");
96         } catch (Exception e) {
97                         handleSpaceErrorAndTerminate(e);
98                 }
99         }
100
101         private PizzaOrder createPizza(PizzaOrder order) throws InterruptedException {
102                 long duration = order.getPizzaType().duration;
103                 Thread.sleep(duration * 1000);
104                 
105                 PizzaOrder pizza = Pizza.createPizzaFromPizzaOrder(order, cookId);
106                 pizza.setStatus(PizzaOrderStatus.DONE);
107                 return pizza;
108         }
109
110 }