]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupXVSM.java
GUI fix for Tables OutOfBoundsException
[pub/jan/sbc.git] / src / main / java / at / ac / tuwien / sbc / valesriegler / xvsm / GroupXVSM.java
1 package at.ac.tuwien.sbc.valesriegler.xvsm;
2
3 import at.ac.tuwien.sbc.valesriegler.common.Util;
4 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
5 import at.ac.tuwien.sbc.valesriegler.types.GroupState;
6 import at.ac.tuwien.sbc.valesriegler.types.Order;
7 import at.ac.tuwien.sbc.valesriegler.xvsm.spacehelpers.SpaceAction;
8 import org.mozartspaces.core.MzsConstants.RequestTimeout;
9 import org.mozartspaces.core.TransactionReference;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12
13 import java.io.Serializable;
14 import java.util.Arrays;
15 import java.util.List;
16
17 public class GroupXVSM extends AbstractXVSMConnector {
18         private static final Logger log = LoggerFactory.getLogger(GroupXVSM.class);
19         
20         private int groupId;
21
22         public GroupXVSM(int groupId, int port) {
23                 super(port);
24                 this.groupId = groupId;
25                 
26                 paymentRequestContainer = useContainerOfSpaceWithPort(Util.PAYMENT_REQUEST, port) ;
27                 orderDeliveredContainer = useContainerOfSpaceWithPort(Util.ORDER_COMPLETE, port) ;
28         pizzeriaGroupContainer = useContainerOfSpaceWithPort(Util.PIZZERIA_GROUP, port);
29         }
30
31         public void waitForMyOrder() {
32         getDefaultBuilder("").setCref(orderDeliveredContainer).setSpaceAction(new SpaceAction() {
33             @Override
34             public void onEntriesWritten(List<? extends Serializable> entries) throws Exception {
35                 final List<GroupData> groups = castEntries(entries);
36         
37                 final Order order = groups.get(0).getOrder();
38         
39                                 if(order.getGroupId() == groupId) {
40                                         eatAndThenPay();
41                                 }
42             }}).createSpaceListenerImpl();
43         }
44         
45         private void eatAndThenPay() {
46         try {
47             int timeToEat = Util.getRandom(3, 5);
48             log.info("I eat {} seconds now...", timeToEat);
49
50             try {
51                 Thread.sleep(timeToEat * 1000);
52             } catch (InterruptedException e) {
53                 e.printStackTrace();
54             }
55
56             GroupData groupData = new GroupData();
57             groupData.setId(groupId);
58             groupData.setState(GroupState.EATING);
59             final TransactionReference tx = getDefaultTransaction();
60
61             final GroupData group = takeMatchingEntity(groupData, pizzeriaGroupContainer, tx, RequestTimeout.DEFAULT, "Group does not exist!");
62
63             group.setState(GroupState.PAY);
64             final List<GroupData> groupList = Arrays.asList(group);
65             sendItemsToContainer(groupList, paymentRequestContainer, RequestTimeout.DEFAULT, tx);
66             sendItemsToContainer(groupList, pizzeriaGroupContainer, RequestTimeout.DEFAULT, tx);
67             capi.commitTransaction(tx);
68             log.info("I sent my payment request to the space! GroupId: {}", groupId);
69
70             notificationMgr.shutdown();
71
72         } catch (Exception e) {
73             log.error("hab mich verschluckt!");
74             e.printStackTrace();
75         }
76     }
77
78 }