]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/xvsm/CookXVSM.java
Space cook listens for pizza requests
[pub/jan/sbc.git] / src / main / java / at / ac / tuwien / sbc / valesriegler / xvsm / CookXVSM.java
1 package at.ac.tuwien.sbc.valesriegler.xvsm;
2
3 import java.io.Serializable;
4 import java.util.List;
5
6 import org.mozartspaces.notifications.Notification;
7 import org.mozartspaces.notifications.NotificationListener;
8 import org.mozartspaces.notifications.Operation;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
12 import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder;
13
14 public class CookXVSM extends AbstractXVSMConnector {
15         private static final Logger log = LoggerFactory.getLogger(CookXVSM.class);
16
17         private int cookId;
18
19         public CookXVSM(int id) {
20                 super();
21                 
22                 this.cookId = id;
23                 useGroupsContainer();
24                 useOrdersContainer();
25                 useDeliverPizzasContainer();
26                 usePreparePizzasContainer();
27         }
28
29         public void listenForPizzas() {
30                 NotificationListener pizzasListener = new NotificationListener() {
31             @Override
32             public void entryOperationFinished(final Notification notification, final Operation operation, final List<? extends Serializable> entries) {
33                
34                                 log.info("New Pizzas to prepare!");
35                                 
36                                 List<PizzaOrder> pizzas = castEntries(entries);
37                                 for (PizzaOrder pizzaOrder : pizzas) {
38                                         log.info(pizzaOrder.toString());
39                                 }
40 //                              
41 //                              List<GroupData> groups = castEntries(entries);
42 //                              Collections.shuffle(groups);
43 //                              
44 //                              for (GroupData group : groups) {
45 //                                      try {
46 //                                              TransactionReference tx = capi.createTransaction(1500, URI.create(Util.SERVER_ADDR));
47 //                                              
48 //                                              try {
49 //                                                      // Acquire a lock for the group in the AssignTableContainer
50 //                                                      String groupNotFound = String.format("Group with id %d was already assigned a table by another waiter!", group.getId());
51 //                                                      
52 //                                                      GroupData lockedGroup = takeEntityByTemplateFromContainer(new GroupData(group.getId()), assignTableContainer, tx, 1000, groupNotFound);
53 //                                                      // Acquire a lock for one free table in the TablesContainer
54 //                                                      Table lockedFreeTable = takeEntityByTemplateFromContainer(new Table(null), freeTablesContainer, tx, RequestTimeout.DEFAULT, String.format("No free table for group with id %d could be found", group.getId()));
55 //                                                      
56 //                                                      assignGroupToTable(lockedGroup, lockedFreeTable, tx);
57 //                                                      
58 //                                              } catch (IllegalArgumentException e) {
59 //                                                      log.info("IllegalArgumentException");
60 //                                                      e.printStackTrace();
61 //                                              } catch (EntityNotFoundByTemplate e) {
62 //                                                      log.info(e.getMessage());
63 //                                              }  catch (Exception e) {
64 //                                                      log.error("AN INNER EXCEPTION");
65 //                                                      e.printStackTrace();
66 //                                              }
67 //                                      } catch (TransactionException e) {
68 //                                              log.info("An unimportant TransactionException has occurred");
69 //                                      } catch (Exception e) {
70 //                                              log.error("OUTER EXCEPTION");
71 //                                      }
72 //                                      
73 //                              }
74             }
75         };
76         try {
77                         notificationMgr.createNotification(preparePizzasContainer, pizzasListener, Operation.WRITE);
78                         log.info("Created pizzasContainer notification for a cook");
79         } catch (Exception e) {
80                         handleSpaceErrorAndTerminate(e);
81                 }
82         }
83
84 }