]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/DriverXVSM.java
[XVSM] Some fixes for making sure that Cook only prepares normal pizzas when there...
[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.Bill;
5 import at.ac.tuwien.sbc.valesriegler.common.Util;
6 import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData;
7 import at.ac.tuwien.sbc.valesriegler.types.DeliveryStatus;
8 import at.ac.tuwien.sbc.valesriegler.types.Order;
9 import at.ac.tuwien.sbc.valesriegler.types.Pizza;
10 import at.ac.tuwien.sbc.valesriegler.xvsm.spacehelpers.SpaceAction;
11 import org.mozartspaces.capi3.EntryLockedException;
12 import org.mozartspaces.core.ContainerReference;
13 import org.mozartspaces.core.MzsConstants;
14 import org.mozartspaces.core.MzsCoreException;
15 import org.mozartspaces.core.TransactionReference;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 import java.io.Serializable;
20 import java.net.URI;
21 import java.util.Arrays;
22 import java.util.List;
23
24 public class DriverXVSM extends AbstractXVSMConnector {
25     private static final Logger log = LoggerFactory.getLogger(DriverXVSM.class);
26
27     private int driverId;
28
29     public DriverXVSM(int id, int port) {
30         super(port);
31
32         this.driverId = id;
33
34         preparedDeliveryPizzasContainer = useContainer(Util.DELIVER_DELIVERY_PIZZAS);
35         deliveryOrderTakenContainer = useContainer(Util.DELIVERY_ORDER_TAKEN);
36         deliverDeliveryOrderContainer = useContainer(Util.DELIVER_DELIVERY_ORDER);
37         pizzeriaDeliveryContainer = useContainer(Util.PIZZERIA_DELIVERY);
38
39     }
40
41
42     public void listenForPreparedDeliveryOrders() {
43         getDefaultBuilder("listenForPreparedDeliveryOrders").setCref(deliverDeliveryOrderContainer).setLookaround(true).setSpaceAction(new SpaceAction() {
44
45             @Override
46             public void onEntriesWritten(List<? extends Serializable> entries) throws Exception {
47                 handleDeliveries(entries);
48
49             }
50         }).createSpaceListenerImpl();
51     }
52
53     private void handleDeliveries(List<? extends Serializable> entries) throws MzsCoreException {
54         final List<DeliveryGroupData> deliveries = castEntries(entries);
55
56         for (DeliveryGroupData delivery : deliveries) {
57             DeliveryGroupData template = new DeliveryGroupData(delivery.getId());
58             template.setDeliveryStatus(DeliveryStatus.ORDERED);
59
60             final TransactionReference tx = capi.createTransaction(
61                     7000,
62                     URI.create(String.format(Util.SERVER_ADDR, port)));
63             final String error = "Another driver wants to deliver this order!";
64
65             try {
66                 // Get lock for delivering this delivery order
67                 takeMatchingEntity(template, deliverDeliveryOrderContainer, tx, MzsConstants.RequestTimeout.ZERO, error);
68                 final DeliveryGroupData group = takeMatchingEntity(template, pizzeriaDeliveryContainer, tx, MzsConstants.RequestTimeout.DEFAULT, "Cannot get the delivery order!");
69                 group.setDriverId(driverId);
70                 group.setDeliveryStatus(DeliveryStatus.IN_PROGRESS);
71
72                 final List<DeliveryGroupData> groups = Arrays.asList(group);
73                         sendItemsToContainer(groups,
74                                 pizzeriaDeliveryContainer, MzsConstants.RequestTimeout.ZERO, null);
75
76                 if (!Util.runSimulation) {
77                     Thread.sleep(3000);
78                 }
79
80                 final String address = String.format(Util.SERVER_ADDR, Util.DELIVERY_CUSTOMERS_PORT);
81                 ContainerReference cref = null;
82                 boolean success = false;
83                 try {
84                     cref = capi.lookupContainer(group.getAddress(), URI.create(address), MzsConstants.RequestTimeout.ZERO, null);
85                     log.debug("Looked up container {} successfully!", address);
86                     success = true;
87
88                 } catch (Exception ex) {
89                     log.info("Could not find container {}", address);
90                 }
91                 if (success) {
92                     group.setDeliveryStatus(DeliveryStatus.DELIVERED);
93                     if (!Util.runSimulation) {
94                         sendItemsToContainer(Arrays.asList(new Bill(1000, group.getId())), cref, MzsConstants.RequestTimeout.ZERO, null);
95                     }
96                 } else {
97                     group.setDeliveryStatus(DeliveryStatus.DELIVERY_FAILED);
98                 }
99                 sendItemsToContainer(groups, pizzeriaDeliveryContainer, MzsConstants.RequestTimeout.ZERO, tx);
100                 capi.commitTransaction(tx);
101
102
103             } catch(EntryLockedException e) {
104                 capi.rollbackTransaction(tx);
105             } catch (Exception e) {
106                 log.info("DRiverXVSM exception");
107                 log.info(e.getMessage());
108                 e.printStackTrace();
109             }
110         }
111     }
112
113     public void listenForPreparedDeliveryPizzas() {
114         getDefaultBuilder("listenForPreparedDeliveryPizzas").setCref(preparedDeliveryPizzasContainer).setLookaround(true).setSpaceAction(new SpaceAction() {
115
116             @Override
117             public void onEntriesWritten(List<? extends Serializable> entries)
118                     throws Exception {
119
120                 List<Pizza> pizzas = castEntries(entries);
121
122                 for (Pizza pizza : pizzas) {
123                     int orderId = pizza.getOrderId();
124                     Order order = new Order();
125                     order.setId(orderId);
126
127                     TransactionReference tx = getDefaultTransaction();
128
129                     try {
130                         DeliveryGroupData entity = new DeliveryGroupData();
131                         entity.setDeliveryStatus(null);
132                         entity.setOrder(order);
133
134                         final DeliveryGroupData groupData = takeMatchingEntity(entity,
135                                 deliveryOrderTakenContainer, tx, MzsConstants.RequestTimeout.DEFAULT,
136                                 "Another driver just checks if the delivery order is complete");
137                         int numberOfPizzas = groupData.getOrder().getNumberOfPizzas();
138
139                         Pizza pizzaTemplate = new Pizza();
140                         pizzaTemplate.setOrderId(orderId);
141
142                         List<Pizza> pizzasOfOrder = takeMatchingEntities(
143                                 pizzaTemplate, preparedDeliveryPizzasContainer, tx,
144                                 MzsConstants.RequestTimeout.DEFAULT,
145                                 "Cannot take the pizzas from the preparedDeliveryPizzasContainer");
146
147                         if (pizzasOfOrder.size() == numberOfPizzas) {
148                             final List<DeliveryGroupData> groupsWithCompleteOrder = Arrays.asList(groupData);
149                             sendItemsToContainer(groupsWithCompleteOrder,
150                                     deliverDeliveryOrderContainer, MzsConstants.RequestTimeout.DEFAULT,
151                                     tx);
152
153                             capi.commitTransaction(tx);
154                         } else {
155                             log.info("Not yet all pizzas prepared! Order with id "
156                                     + orderId + " has " + numberOfPizzas
157                                     + " pizzas, but only " + pizzasOfOrder.size()
158                                     + " pizzas are ready by now!");
159                             capi.rollbackTransaction(tx);
160                         }
161                     } catch (NullPointerException e) {
162
163                     } catch (Exception e) {
164                         capi.rollbackTransaction(tx);
165                     }
166                 }
167             }
168         }).createSpaceListenerImpl();
169     }
170 }