1 package at.ac.tuwien.sbc.valesriegler.xvsm;
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.*;
7 import at.ac.tuwien.sbc.valesriegler.xvsm.spacehelpers.SpaceAction;
8 import org.mozartspaces.capi3.EntryLockedException;
9 import org.mozartspaces.core.ContainerReference;
10 import org.mozartspaces.core.MzsConstants;
11 import org.mozartspaces.core.MzsCoreException;
12 import org.mozartspaces.core.TransactionReference;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
16 import java.io.Serializable;
18 import java.util.Arrays;
19 import java.util.List;
21 public class DriverXVSM extends AbstractXVSMConnector {
22 private static final Logger log = LoggerFactory.getLogger(DriverXVSM.class);
26 public DriverXVSM(int id, int port) {
31 preparedDeliveryPizzasContainer = useContainer(Util.DELIVER_DELIVERY_PIZZAS);
32 deliveryOrderTakenContainer = useContainer(Util.DELIVERY_ORDER_TAKEN);
33 deliverDeliveryOrderContainer = useContainer(Util.DELIVER_DELIVERY_ORDER);
34 pizzeriaDeliveryContainer = useContainer(Util.PIZZERIA_DELIVERY);
39 public void listenForPreparedDeliveryOrders() {
40 getDefaultBuilder("listenForPreparedDeliveryOrders").setCref(deliverDeliveryOrderContainer).setLookaround(true).setSpaceAction(new SpaceAction() {
43 public void onEntriesWritten(List<? extends Serializable> entries) throws Exception {
44 handleDeliveries(entries);
47 }).createSpaceListenerImpl();
50 private void handleDeliveries(List<? extends Serializable> entries) throws MzsCoreException {
51 final List<DeliveryGroupData> deliveries = castEntries(entries);
53 for (DeliveryGroupData delivery : deliveries) {
54 DeliveryGroupData template = new DeliveryGroupData(delivery.getId());
55 template.setDeliveryStatus(DeliveryStatus.ORDERED);
57 final TransactionReference tx = capi.createTransaction(
59 URI.create(String.format(Util.SERVER_ADDR, port)));
60 final String error = "Another driver wants to deliver this order!";
63 // Get lock for delivering this delivery order
64 takeMatchingEntity(template, deliverDeliveryOrderContainer, tx, MzsConstants.RequestTimeout.ZERO, error);
65 final DeliveryGroupData group = takeMatchingEntity(template, pizzeriaDeliveryContainer, tx, MzsConstants.RequestTimeout.DEFAULT, "Cannot get the delivery order!");
66 group.setDriverId(driverId);
67 group.setDeliveryStatus(DeliveryStatus.IN_PROGRESS);
69 final List<DeliveryGroupData> groups = Arrays.asList(group);
70 sendItemsToContainer(groups,
71 pizzeriaDeliveryContainer, MzsConstants.RequestTimeout.ZERO, null);
73 if (!Util.runSimulation) {
77 final String address = String.format(Util.SERVER_ADDR, Util.DELIVERY_CUSTOMERS_PORT);
78 ContainerReference cref = null;
79 boolean success = false;
81 cref = capi.lookupContainer(group.getAddress(), URI.create(address), MzsConstants.RequestTimeout.ZERO, null);
82 log.debug("Looked up container {} successfully!", address);
85 } catch (Exception ex) {
86 log.info("Could not find container {}", address);
89 group.setDeliveryStatus(DeliveryStatus.DELIVERED);
90 if (!Util.runSimulation) {
91 double sum = computeSum(group.getOrder());
92 sendItemsToContainer(Arrays.asList(new Bill(sum, group.getId())), cref, MzsConstants.RequestTimeout.ZERO, null);
95 group.setDeliveryStatus(DeliveryStatus.DELIVERY_FAILED);
97 sendItemsToContainer(groups, pizzeriaDeliveryContainer, MzsConstants.RequestTimeout.ZERO, tx);
98 capi.commitTransaction(tx);
101 } catch(EntryLockedException e) {
102 capi.rollbackTransaction(tx);
103 } catch (Exception e) {
104 log.info("DRiverXVSM exception");
105 log.info(e.getMessage());
111 private double computeSum(Order order) {
112 final List<PizzaOrder> orderedPizzas = order.getOrderedPizzas();
114 for (PizzaOrder pizza : orderedPizzas) {
115 final PizzaType pizzaType = pizza.getPizzaType();
117 case CARDINALE: sum = sum + 6;
119 case MARGHERITA: sum = sum + 5;
121 case SALAMI: sum = sum + 5.5;
124 throw new RuntimeException("Unhandled Pizzatype: " + pizzaType);
130 public void listenForPreparedDeliveryPizzas() {
131 getDefaultBuilder("listenForPreparedDeliveryPizzas").setCref(preparedDeliveryPizzasContainer).setLookaround(true).setSpaceAction(new SpaceAction() {
134 public void onEntriesWritten(List<? extends Serializable> entries)
137 List<Pizza> pizzas = castEntries(entries);
139 for (Pizza pizza : pizzas) {
140 int orderId = pizza.getOrderId();
141 Order order = new Order();
142 order.setId(orderId);
144 TransactionReference tx = getDefaultTransaction();
147 DeliveryGroupData entity = new DeliveryGroupData();
148 entity.setDeliveryStatus(null);
149 entity.setOrder(order);
151 final DeliveryGroupData groupData = takeMatchingEntity(entity,
152 deliveryOrderTakenContainer, tx, MzsConstants.RequestTimeout.DEFAULT,
153 "Another driver just checks if the delivery order is complete");
154 int numberOfPizzas = groupData.getOrder().getNumberOfPizzas();
156 Pizza pizzaTemplate = new Pizza();
157 pizzaTemplate.setOrderId(orderId);
159 List<Pizza> pizzasOfOrder = takeMatchingEntities(
160 pizzaTemplate, preparedDeliveryPizzasContainer, tx,
161 MzsConstants.RequestTimeout.DEFAULT,
162 "Cannot take the pizzas from the preparedDeliveryPizzasContainer");
164 if (pizzasOfOrder.size() == numberOfPizzas) {
165 final List<DeliveryGroupData> groupsWithCompleteOrder = Arrays.asList(groupData);
166 sendItemsToContainer(groupsWithCompleteOrder,
167 deliverDeliveryOrderContainer, MzsConstants.RequestTimeout.DEFAULT,
170 capi.commitTransaction(tx);
172 log.info("Not yet all pizzas prepared! Order with id "
173 + orderId + " has " + numberOfPizzas
174 + " pizzas, but only " + pizzasOfOrder.size()
175 + " pizzas are ready by now!");
176 capi.rollbackTransaction(tx);
178 } catch (NullPointerException e) {
180 } catch (Exception e) {
181 capi.rollbackTransaction(tx);
185 }).createSpaceListenerImpl();