]> rtime.felk.cvut.cz Git - orte/eurobot.git/blob - orte/java/src/org/ocera/orte/Subscription.java
JORTE: add wrapper for ORTESubscriptionPull() and revert ORTEDomainStart() removal
[orte/eurobot.git] / orte / java / src / org / ocera / orte / Subscription.java
1 /* Subscription.java */
2
3 /**
4  * Class Subscription provides methods for creating and working
5  * with the ORTE subscribtion.
6  *
7  * @author Lukas Pokorny (lukas_pokorny@centrum.cz)
8  * @author CTU FEE Prague - Department of Control Engineering (dce.felk.cvut.cz)
9  * @author Project ORTE - OCERA Real Time Ethernet (www.ocera.org)
10  * @author dedication to Kj
11  * @version 0.1
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  */
24
25 package org.ocera.orte;
26 import java.nio.ByteBuffer;
27 import java.nio.ByteOrder;
28
29 import  org.ocera.orte.types.*;
30
31
32 public class Subscription {
33
34    private long handle;
35    private long callbackContextHandle = 0;
36
37   /**
38    * Get Subscription's handle.
39    * @return Handle of the subscription.
40    */
41    public long getHandle()
42    {
43      return this.handle;
44    }
45
46
47   /**
48    * constructor 
49    */
50   /* PROC protected??? - aby sel volat jen z tridy Domain */
51   protected Subscription(Domain appDomain,
52                          SubsProp subsProp,                         
53                                                  MessageData message,
54                                                  SubscriptionCallback subsCallback)
55   {
56         this.handle = jORTESubscriptionCreate(appDomain.handle,
57                                               subsProp.getMode(),
58                                               subsProp.getType(),
59                                               subsProp.getTopic(),
60                                               subsProp.getTypeName(),
61                                               message.getBuffer(),
62                                               message.getBuffer().order().equals(ByteOrder.BIG_ENDIAN) ? 0 : 1,
63                                               message,
64                                                                                   subsProp.getDeadline(),
65                                                               subsProp.getMinSeparation(),
66                                                               subsCallback,
67                                        // callBackParam 
68                                                               subsProp.getMulticastIPAddr());
69   }
70
71  /**
72   * Removes a Subscription.
73   * @return False if bad Publication handle, True if  succesful
74   */
75   public
76   boolean destroy()
77   {
78         /* TODO vyradit vypis na nasledujici radce */
79         System.out.println(":j: subscription destroy called..");
80         // destroy subscription
81         if(!jORTESubscriptionDestroy(this.handle)) 
82         {
83             System.out.println(":j!: subscription destroy fault!");
84             return false;               
85         }
86     System.out.println(":j: subscription destroy successfull..");
87     return true;
88     
89   }
90  
91   /**
92    * Get Subscription properties.
93    * @return Subscription properties.
94    */
95   public SubsProp getProperties()
96   {
97         SubsProp sp = new SubsProp();
98     sp = jORTESubscriptionPropertiesGet(this.handle);   
99         return sp;
100   }
101   
102   /**
103    * Set Subscription properties.
104    * @return True - setting new properties Ok, False - setting new properties fault.  
105    */
106   public boolean setProperties(SubsProp sp)
107   {
108         boolean  b;
109
110         // modify SubsProp object - sp    
111         b = jORTESubscriptionPropertiesSet(this.handle, sp);    
112         if(b == false) 
113         {
114         System.out.println(":j!: set SubsProp failed! (bad Subs. handle)"); 
115                 return false; 
116         } 
117         return true;
118   }
119   
120   
121   /**
122    * Get Subscription status.
123    * @return Status of Subscription.  
124    */
125   public Status getStatus()
126   {
127     Status ss = new Status();
128         ss = jORTESubscriptionGetStatus(this.handle);
129         return ss;
130   }
131
132  
133   /**
134    * Waits for given number of publications.
135    * @param Time how long to wait.
136    * @param Number of retries if specified number of publications not reached.
137    * @param Desired number of publications.
138    * @return ORTE_OK if succesful, ORTE_BAD_HANDLE if bad subscription handle, 
139    *         ORTE_TIMEOUT if number of retries has been exhausted.   
140    */
141   public int waitForPublications(NtpTime wait,
142                                          long retries,
143                                                          long noPublication)   
144   { 
145     int i;
146         i = jORTESubscriptionWaitForPublications(this.handle,
147                                                          wait,
148                                                          retries,
149                                                                                          noPublication);
150     return i; 
151   }
152   
153   /**
154    * Read data
155    * @param Time how long to wait.
156    * @param Number of retries if specified number of publications not reached.
157    * @param Desired number of publications.
158    * @return ORTE_OK if succesful, ORTE_BAD_HANDLE if bad subscription handle, 
159    *         ORTE_TIMEOUT if number of retries has been exhausted.   
160    */
161   public boolean pull(long subsHandle)
162   { 
163     int b = jORTESubscriptionPull(subsHandle);
164     if (b == ORTEConstant.ORTE_BAD_HANDLE) 
165     {
166       System.out.println(":j!: method pull failed  [bad subsHandle]!");
167       return false;
168     }   
169     return true; 
170   }
171   
172   /* TODO - dodelat!! */
173   public void patternCreate()
174   {}
175   /* TODO - dodelat!! */
176   public void patternRemove()
177   {}
178   /* TODO - dodelat!! */
179   public void patternDelete()
180   {}
181   
182   
183   
184 /* ****************************************************************** *
185  *                                                                    *
186  *                         native methods                             *
187  *                                                                    *
188  * ****************************************************************** */
189
190   private native
191   long jORTESubscriptionCreate(long appDomainHandle,
192                                int subsmode,  /*! union */
193                                int substype,  /*! union */
194                                String topic,
195                                String typeName,
196                                ByteBuffer buffer,
197                                int byte_order,
198                                MessageData message,
199                                                           NtpTime deadline,
200                                NtpTime minSeparation,
201                                SubscriptionCallback cb,
202                              // recvCallBackParam
203                                long multicastIPAddress);
204
205   private native
206   boolean jORTESubscriptionDestroy(long subsHandle);
207
208   private native
209   SubsProp jORTESubscriptionPropertiesGet(long subsHandle);
210   
211   private native
212   boolean jORTESubscriptionPropertiesSet(long subsHandle, SubsProp subProps);
213
214   private native
215   Status jORTESubscriptionGetStatus(long subsHandle);
216
217   private native
218   int jORTESubscriptionWaitForPublications(long subsHandle, 
219                                                NtpTime wait,
220                                                                    long retries,
221                                                                                    long noPublications);  
222   private native
223   int jORTESubscriptionPull(long subsHandle);  
224  
225   
226   
227 }
228