]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupXVSM.java
Space waiters pay and overall remaining space workflow refactoring
[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 java.io.Serializable;
4 import java.util.Arrays;
5 import java.util.List;
6 import java.util.Random;
7
8 import javax.swing.SwingUtilities;
9
10 import org.mozartspaces.capi3.AnyCoordinator;
11 import org.mozartspaces.capi3.LindaCoordinator;
12 import org.mozartspaces.core.MzsConstants.RequestTimeout;
13 import org.mozartspaces.notifications.Notification;
14 import org.mozartspaces.notifications.NotificationListener;
15 import org.mozartspaces.notifications.Operation;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 import at.ac.tuwien.sbc.valesriegler.common.Util;
20 import at.ac.tuwien.sbc.valesriegler.types.GroupData;
21 import at.ac.tuwien.sbc.valesriegler.types.Order;
22 import at.ac.tuwien.sbc.valesriegler.types.Table;
23
24 public class GroupXVSM extends AbstractXVSMConnector {
25         private static final Logger log = LoggerFactory.getLogger(GroupXVSM.class);
26         
27         private int groupId;
28
29         private Random random = new Random();
30
31         public GroupXVSM(int groupId) {
32                 super();
33                 this.groupId = groupId;
34                 
35                 paymentContainer = useContainer(Util.PAYMENT) ;
36                 orderCompleteContainer = useContainer(Util.ORDER_COMPLETE) ;
37                 isEatingContainer = useContainer(Util.IS_EATING) ;
38                 freeTablesContainer = useContainer(Util.FREE_TABLES) ;
39         }
40
41         public void waitForMyOrder() {
42                 log.info("Thred started for group {}", groupId);
43                 NotificationListener deliveredOrders = new NotificationListener() {
44             @Override
45             public void entryOperationFinished(final Notification notification, final Operation operation, final List<? extends Serializable> entries) {
46                 final List<GroupData> groups = castEntries(entries);
47         
48                 final Order order = groups.get(0).getOrder();
49                            
50                 SwingUtilities.invokeLater(new Runnable() {
51                                         @Override
52                                         public void run() {
53                                                 
54                                                 if(order.getGroupId() == groupId) {
55                                                         eatAndThenPay();
56                                                 }
57                                         }
58                                 });
59                 
60             }
61         };
62           try {
63                 notificationMgr.createNotification(orderCompleteContainer, deliveredOrders, Operation.WRITE);
64         } catch (Exception e) {
65            handleSpaceErrorAndTerminate(e);
66         }
67         }
68         
69         private void eatAndThenPay() {
70                 int timeToEat = getRandom(3, 5);
71                 log.info("I eat {} seconds now...", timeToEat);
72                 
73                 GroupData groupData = new GroupData();
74                 groupData.setId(groupId);
75                 
76                 try {
77                         Thread.sleep(timeToEat * 1000);
78                 } catch (InterruptedException e) {
79                         e.printStackTrace();
80                 }
81                 
82                 sendItemsToContainer(Arrays.asList(groupData), paymentContainer, RequestTimeout.DEFAULT, null);
83                 log.info("I sent my payment request to the space! GroupId: {}", groupId);
84         }
85         
86         private int getRandom(int min, int max){
87             return random.nextInt(max - min + 1) + min;
88         }
89
90 }