]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/GroupXVSM.java
Some space refactoring. Doing away with unnecessary containers. Auto-Reloading of...
[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                 paymentRequestContainer = useContainer(Util.PAYMENT_REQUEST) ;
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                                 if(order.getGroupId() == groupId) {
51                                         eatAndThenPay();
52                                 }
53             }
54         };
55           try {
56                 notificationMgr.createNotification(orderCompleteContainer, deliveredOrders, Operation.WRITE);
57         } catch (Exception e) {
58            handleSpaceErrorAndTerminate(e);
59         }
60         }
61         
62         private void eatAndThenPay() {
63                 int timeToEat = getRandom(3, 5);
64                 log.info("I eat {} seconds now...", timeToEat);
65                 
66                 GroupData groupData = new GroupData();
67                 groupData.setId(groupId);
68                 
69                 try {
70                         Thread.sleep(timeToEat * 1000);
71                 } catch (InterruptedException e) {
72                         e.printStackTrace();
73                 }
74                 
75                 sendItemsToContainer(Arrays.asList(groupData), paymentRequestContainer, RequestTimeout.DEFAULT, null);
76                 log.info("I sent my payment request to the space! GroupId: {}", groupId);
77         }
78         
79         private int getRandom(int min, int max){
80             return random.nextInt(max - min + 1) + min;
81         }
82
83 }