]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/DriverXVSM.java
[XVSM] Remove some containers of the Group Agent by introducing notifications of...
[pub/jan/sbc.git] / src / main / java / at / ac / tuwien / sbc / valesriegler / xvsm / DriverXVSM.java
1 package at.ac.tuwien.sbc.valesriegler.xvsm;
2
3
4 import at.ac.tuwien.sbc.valesriegler.common.Util;
5 import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData;
6 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
7 import at.ac.tuwien.sbc.valesriegler.types.Order;
8 import at.ac.tuwien.sbc.valesriegler.types.Pizza;
9 import org.mozartspaces.core.MzsConstants;
10 import org.mozartspaces.core.TransactionReference;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13
14 import java.io.Serializable;
15 import java.util.Arrays;
16 import java.util.List;
17
18 public class DriverXVSM extends AbstractXVSMConnector {
19     private static final Logger log = LoggerFactory.getLogger(DriverXVSM.class);
20
21     private int driverId;
22
23     public DriverXVSM(int id, int port) {
24         super(port);
25
26         this.driverId = id;
27
28         this.deliverDeliveryPizzasContainer = useContainer(Util.DELIVER_DELIVERY_PIZZAS);
29
30     }
31
32     public void listenForPreparedPizzas() {
33         /**
34          * A waiter gets informed when a new pizza is complete. He takes the
35          * orderId of the pizza and looks up the corresponding order from which
36          * he gets the number of necessary pizzas of the order. He then tries to
37          * fetch all pizzas with the corresponding orderId and compares the
38          * number of those pizzas with the number of necessary pizzas. If all
39          * pizzas of an order are complete he then delivers them!
40          */
41         SpaceListener preparedPizzasListener = new SpaceListenerImpl(capi, deliverDeliveryPizzasContainer) {
42
43             @Override
44             void onEntriesWritten(List<? extends Serializable> entries)
45                     throws Exception {
46
47                 List<Pizza> pizzas = castEntries(entries);
48
49                 for (Pizza pizza : pizzas) {
50                     int orderId = pizza.getOrderId();
51                     Order order = new Order();
52                     order.setId(orderId);
53
54                     TransactionReference tx = getDefaultTransaction();
55                     final boolean isDeliveryPizza = pizza.isDeliveryPizza();
56
57                     try {
58                         DeliveryGroupData entity = new DeliveryGroupData();
59                         entity.setDeliveryStatus(null);
60                         entity.setOrder(order);
61
62                         DeliveryGroupData groupData = takeMatchingEntity(entity,
63                                 orderTakenContainer, tx, MzsConstants.RequestTimeout.DEFAULT,
64                                 "Another driver just checks if the delivery order is complete");
65                         int groupId = groupData.getId();
66                         int numberOfPizzas = order.getNumberOfPizzas();
67
68                         Pizza pizzaTemplate = new Pizza();
69                         pizzaTemplate.setOrderId(orderId);
70
71                         List<Pizza> pizzasOfOrder = takeMatchingEntities(
72                                 pizzaTemplate, deliverPizzasContainer, tx,
73                                 MzsConstants.RequestTimeout.DEFAULT,
74                                 "Cannot take the pizzas from the deliverDeliveryPizzasContainer");
75
76                         if (pizzasOfOrder.size() == numberOfPizzas) {
77                             DeliveryGroupData group = new DeliveryGroupData();
78                             group.setDriverId(driverId);
79                             Order completeOrder = new Order();
80                             completeOrder.setId(orderId);
81                             completeOrder.setGroupId(groupId);
82                             group.setOrder(completeOrder);
83                             final List<DeliveryGroupData> groupsWithCompleteOrder = Arrays.asList(group);
84                             sendItemsToContainer(groupsWithCompleteOrder,
85                                     orderCompleteContainer, MzsConstants.RequestTimeout.DEFAULT,
86                                     tx);
87
88                             capi.commitTransaction(tx);
89                         } else {
90                             log.info("Not yet all pizzas prepared! Order with id "
91                                     + orderId + " has " + numberOfPizzas
92                                     + " pizzas, but only " + pizzasOfOrder.size()
93                                     + " pizzas are ready by now!");
94                             capi.rollbackTransaction(tx);
95                         }
96                     } catch (NullPointerException e) {
97
98                     } catch (Exception e) {
99                         capi.rollbackTransaction(tx);
100                     }
101                 }
102             }
103         };
104
105         createNotification(preparedPizzasListener, deliverDeliveryPizzasContainer);
106     }
107 }