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.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.core.ContainerReference;
12 import org.mozartspaces.core.MzsConstants;
13 import org.mozartspaces.core.TransactionReference;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
17 import java.io.Serializable;
19 import java.util.Arrays;
20 import java.util.List;
22 public class DriverXVSM extends AbstractXVSMConnector {
23 private static final Logger log = LoggerFactory.getLogger(DriverXVSM.class);
27 public DriverXVSM(int id, int port) {
32 preparedDeliveryPizzasContainer = useContainer(Util.DELIVER_DELIVERY_PIZZAS);
33 deliveryOrderTakenContainer = useContainer(Util.DELIVERY_ORDER_TAKEN);
34 deliverDeliveryOrderContainer = useContainer(Util.DELIVER_DELIVERY_ORDER);
35 pizzeriaDeliveryContainer = useContainer(Util.PIZZERIA_DELIVERY);
40 public void listenForPreparedDeliveryOrders() {
41 getDefaultBuilder().setCref(deliverDeliveryOrderContainer).setLookaround(true).setSpaceAction(new SpaceAction() {
44 public void onEntriesWritten(List<? extends Serializable> entries) throws Exception {
45 final List<DeliveryGroupData> deliveries = castEntries(entries);
47 for (DeliveryGroupData delivery : deliveries) {
48 DeliveryGroupData template = new DeliveryGroupData(delivery.getId());
49 template.setDeliveryStatus(DeliveryStatus.ORDERED);
51 final TransactionReference tx = capi.createTransaction(
53 URI.create(String.format(Util.SERVER_ADDR, port)));
54 final String error = "Another driver wants to deliver this order!";
57 // Get lock for delivering this delivery order
58 final DeliveryGroupData deliveryGroup = takeMatchingEntity(template, deliverDeliveryOrderContainer, tx, MzsConstants.RequestTimeout.DEFAULT, error);
59 final DeliveryGroupData group = takeMatchingEntity(template, pizzeriaDeliveryContainer, tx, MzsConstants.RequestTimeout.INFINITE, "Cannot get the delivery order!");
60 group.setDriverId(driverId);
61 group.setDeliveryStatus(DeliveryStatus.IN_PROGRESS);
63 final List<DeliveryGroupData> groups = Arrays.asList(group);
64 // sendItemsToContainer(groups,
65 // pizzeriaDeliveryContainer, MzsConstants.RequestTimeout.ZERO, null);
67 if (!Util.runSimulation) {
71 final String address = String.format(Util.SERVER_ADDR, Util.DELIVERY_CUSTOMERS_PORT);
72 ContainerReference cref = null;
73 boolean success = false;
75 cref = capi.lookupContainer(group.getAddress(), URI.create(address), MzsConstants.RequestTimeout.ZERO, null);
76 log.debug("Looked up container {} successfully!", address);
79 } catch (Exception ex) {
80 log.info("Could not find container {}", address);
83 group.setDeliveryStatus(DeliveryStatus.DELIVERED);
84 if (!Util.runSimulation) {
85 sendItemsToContainer(Arrays.asList(new Bill(1000, group.getId())), cref, MzsConstants.RequestTimeout.DEFAULT, null);
88 group.setDeliveryStatus(DeliveryStatus.DELIVERY_FAILED);
90 sendItemsToContainer(groups, pizzeriaDeliveryContainer, MzsConstants.RequestTimeout.DEFAULT, tx);
91 capi.commitTransaction(tx);
94 } catch (Exception e) {
95 log.info("DRiverXVSM exception");
96 log.info(e.getMessage());
102 }).createSpaceListenerImpl();
105 public void listenForPreparedDeliveryPizzas() {
106 getDefaultBuilder().setCref(preparedDeliveryPizzasContainer).setLookaround(true).setSpaceAction(new SpaceAction() {
109 public void onEntriesWritten(List<? extends Serializable> entries)
112 List<Pizza> pizzas = castEntries(entries);
114 for (Pizza pizza : pizzas) {
115 int orderId = pizza.getOrderId();
116 Order order = new Order();
117 order.setId(orderId);
119 TransactionReference tx = getDefaultTransaction();
122 DeliveryGroupData entity = new DeliveryGroupData();
123 entity.setDeliveryStatus(null);
124 entity.setOrder(order);
126 final DeliveryGroupData groupData = takeMatchingEntity(entity,
127 deliveryOrderTakenContainer, tx, MzsConstants.RequestTimeout.DEFAULT,
128 "Another driver just checks if the delivery order is complete");
129 int numberOfPizzas = groupData.getOrder().getNumberOfPizzas();
131 Pizza pizzaTemplate = new Pizza();
132 pizzaTemplate.setOrderId(orderId);
134 List<Pizza> pizzasOfOrder = takeMatchingEntities(
135 pizzaTemplate, preparedDeliveryPizzasContainer, tx,
136 MzsConstants.RequestTimeout.DEFAULT,
137 "Cannot take the pizzas from the preparedDeliveryPizzasContainer");
139 if (pizzasOfOrder.size() == numberOfPizzas) {
140 final List<DeliveryGroupData> groupsWithCompleteOrder = Arrays.asList(groupData);
141 sendItemsToContainer(groupsWithCompleteOrder,
142 deliverDeliveryOrderContainer, MzsConstants.RequestTimeout.DEFAULT,
145 capi.commitTransaction(tx);
147 log.info("Not yet all pizzas prepared! Order with id "
148 + orderId + " has " + numberOfPizzas
149 + " pizzas, but only " + pizzasOfOrder.size()
150 + " pizzas are ready by now!");
151 capi.rollbackTransaction(tx);
153 } catch (NullPointerException e) {
155 } catch (Exception e) {
156 capi.rollbackTransaction(tx);
160 }).createSpaceListenerImpl();