]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/CookXVSM.java
Some space refactoring. Doing away with unnecessary containers. Auto-Reloading of...
[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                 NotificationListener pizzasListener = new NotificationListener() {
42             @Override
43             public void entryOperationFinished(final Notification notification, final Operation operation, final List<? extends Serializable> entries) {
44                
45                                 log.info("New Pizzas to prepare!");
46                                 
47                                 List<PizzaOrder> pizzas = castEntries(entries);
48                                 Collections.rotate(pizzas, cookId);
49                                 for (PizzaOrder pizzaOrder : pizzas) {
50                                         try {
51                                                 try {
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                                                 } catch (IllegalArgumentException e) {
73                                                         log.info("IllegalArgumentException");
74                                                         e.printStackTrace();
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");
81                                                         e.printStackTrace();
82                                                 }
83                                         } catch (TransactionException e) {
84                                                 log.info("An unimportant TransactionException has occurred");
85                                         } catch (Exception e) {
86                                                 log.error("OUTER EXCEPTION");
87                                         }
88                                 }
89                                 
90             }
91         };
92         try {
93                         notificationMgr.createNotification(preparePizzasContainer, pizzasListener, Operation.WRITE);
94                         log.info("Created pizzasContainer notification for a cook");
95         } catch (Exception e) {
96                         handleSpaceErrorAndTerminate(e);
97                 }
98         }
99
100         private PizzaOrder createPizza(PizzaOrder order) throws InterruptedException {
101                 long duration = order.getPizzaType().duration;
102                 Thread.sleep(duration * 1000);
103                 
104                 PizzaOrder pizza = Pizza.createPizzaFromPizzaOrder(order, cookId);
105                 pizza.setStatus(PizzaOrderStatus.DONE);
106                 return pizza;
107         }
108
109 }