]> rtime.felk.cvut.cz Git - orte.git/blob - orte/Robot_Demo/src/org/ocera/orte/demo/Subscribers/MotionSpeedSubscribe.java
JORTE: fix bug in subscriber callback
[orte.git] / orte / Robot_Demo / src / org / ocera / orte / demo / Subscribers / MotionSpeedSubscribe.java
1 package org.ocera.orte.demo.Subscribers;
2
3 import java.util.concurrent.locks.ReentrantReadWriteLock;
4 import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
5 import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
6
7 import org.ocera.orte.DomainApp;
8 import org.ocera.orte.Subscription;
9 import org.ocera.orte.SubscriptionCallback;
10 import org.ocera.orte.demo.DataTypes.SpeedMotionType;
11 import org.ocera.orte.demo.GUI.HokuyoView;
12 import org.ocera.orte.types.MessageData;
13 import org.ocera.orte.types.NtpTime;
14 import org.ocera.orte.types.RecvInfo;
15 import org.ocera.orte.types.SubsProp;
16 import org.ocera.orte.types.ORTEConstant;
17
18 public class MotionSpeedSubscribe extends SubscriptionCallback{
19
20         private Subscription sub;
21         private HokuyoView view;
22         private SpeedMotionType speedmsg;
23         private DomainApp domainApp;
24         private SubsProp subProps;
25         
26         private boolean isCancelled = true;
27         private final ReentrantReadWriteLock controlRrwl = new ReentrantReadWriteLock(true);
28         private final ReadLock rcLock = controlRrwl.readLock();
29         private final WriteLock wcLock = controlRrwl.writeLock();
30         
31         public MotionSpeedSubscribe(DomainApp domainApp, HokuyoView view) {
32                 this.view = view;
33                 this.domainApp = domainApp;
34                 
35             NtpTime deadline      = new NtpTime(0,1288490189);
36             NtpTime minSeparation = new NtpTime(0);
37             
38             speedmsg = new SpeedMotionType(domainApp, "motion_speed");
39                 
40             subProps = new SubsProp(speedmsg.getTopic(),
41                                                         "motion_speed",                                 
42                                                         minSeparation,  
43                                                         deadline,
44                                                         ORTEConstant.IMMEDIATE,
45                                                         ORTEConstant.BEST_EFFORTS,
46                                                         0);
47         }
48         
49         public void start() {
50                 wcLock.lock();
51                 try {
52                         isCancelled = false;
53                         sub = domainApp.createSubscription(subProps, speedmsg, this);
54                 } finally {
55                         wcLock.unlock();
56                 }
57         }
58         
59         public void cancel() {
60                 wcLock.lock();
61                 try {
62                         isCancelled = true;
63                         sub.destroy();
64                 } finally {
65                         wcLock.unlock();
66                 }
67         }
68         
69         public boolean isCancelled() {
70                 rcLock.lock();
71                 try {
72                         return isCancelled;
73                 } finally {
74                         rcLock.unlock();
75                 }
76         }
77         
78     public void callback(RecvInfo info, MessageData msg) {
79       if (info.getRecvStatus() == ORTEConstant.NEW_DATA)
80         view.setDataMotion(((SpeedMotionType)msg).speed);
81     }
82 }