]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/group/DeliveryGroup.java
[JMS] added IF benchmark to Thread.sleep
[pub/jan/sbc.git] / src / main / java / at / ac / tuwien / sbc / valesriegler / group / DeliveryGroup.java
1 package at.ac.tuwien.sbc.valesriegler.group;
2
3 import org.slf4j.Logger;
4 import org.slf4j.LoggerFactory;
5
6 import at.ac.tuwien.sbc.valesriegler.common.HasId;
7 import at.ac.tuwien.sbc.valesriegler.common.Util;
8 import at.ac.tuwien.sbc.valesriegler.group.actions.DeliveryOrderRequest;
9 import at.ac.tuwien.sbc.valesriegler.group.jms.JMSGroupConnector;
10 import at.ac.tuwien.sbc.valesriegler.types.DeliveryGroupData;
11 import at.ac.tuwien.sbc.valesriegler.types.DeliveryStatus;
12
13 public class DeliveryGroup implements Runnable, HasId {
14         private static final Logger log = LoggerFactory.getLogger(DeliveryGroup.class);
15         private static int idNext = 0;
16
17         private DeliveryGroupData deliveryGroupData;
18
19         public DeliveryGroup() {
20                 deliveryGroupData = new DeliveryGroupData(++idNext);
21         }
22
23         public DeliveryGroup(int id) {
24                 deliveryGroupData = new DeliveryGroupData(id);
25         }
26
27         public DeliveryGroupData getDeliveryGroupData() {
28                 return deliveryGroupData;
29         }
30
31         @Override
32         public int getId() {
33                 return deliveryGroupData.getId();
34         }
35
36         public void setDeliveryGroupData(DeliveryGroupData deliveryGroupData) {
37                 this.deliveryGroupData = deliveryGroupData;
38         }
39
40         @Override
41         public void run() {
42                 log.debug("Thread started for:" + this);
43                 try {
44                         if (!Util.runSimulation) {
45                                 Thread.sleep((long) (Math.random() * 10000));
46                         }
47                         switch (deliveryGroupData.getDeliveryStatus()) {
48                                 case START : // should not happen.
49                                         break;
50                                 case ORDER_PENDING :
51                                         getDeliveryGroupData().getOrder().genId();
52                                         JMSGroupConnector conn = JMSGroupConnector.getConnector(deliveryGroupData.getPizzeriaId());
53                                         conn.send(new DeliveryOrderRequest(deliveryGroupData));
54                                         break;
55                                 case IN_PROGRESS : // do nothing
56                                         break;
57                                 case DELIVERED : // do nothing
58                                         break;
59                                 case PAID : // do nothing
60                                         break;
61                                 case IS_DELIVERED : // do nothing
62                                         break;
63                                 case DELIVERY_FAILED : // do nothing
64                                         break;
65                                 default : // do nothing
66                                         break;
67                         }
68
69                 } catch (InterruptedException e) {
70                         e.printStackTrace();
71                 }
72         }
73
74         // lets order at the pizzaria.
75         public void orderSomeFood() {
76                 log.debug("orderSomeFood(): " + this);
77                 deliveryGroupData.setDeliveryStatus(DeliveryStatus.ORDER_PENDING);
78                 GroupAgent.getInstance().getGroupModel().fireTableDataChanged();
79                 new Thread(this).start();
80         }
81 }