]> rtime.felk.cvut.cz Git - orte/eurobot.git/blob - orte/java/src/org/ocera/orte/Subscription.java
485f9e3301f8aa2234a0ffd00834fcec6f6bbe97
[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   /* TODO - dodelat middle C zdrojak !! */
154   /**
155    * Read data
156    * @param Time how long to wait.
157    * @param Number of retries if specified number of publications not reached.
158    * @param Desired number of publications.
159    * @return ORTE_OK if succesful, ORTE_BAD_HANDLE if bad subscription handle, 
160    *         ORTE_TIMEOUT if number of retries has been exhausted.   
161    */
162   public boolean pull(long subsHandle)
163   { 
164     int b = jORTESubscriptionPull(subsHandle);
165     if (b == ORTEConstant.ORTE_BAD_HANDLE) 
166     {
167       System.out.println(":j!: method pull failed  [bad subsHandle]!");
168       return false;
169     }   
170     return true; 
171   }
172   
173   /* TODO - dodelat!! */
174   public void patternCreate()
175   {}
176   /* TODO - dodelat!! */
177   public void patternRemove()
178   {}
179   /* TODO - dodelat!! */
180   public void patternDelete()
181   {}
182   
183   
184   
185 /* ****************************************************************** *
186  *                                                                    *
187  *                         native methods                             *
188  *                                                                    *
189  * ****************************************************************** */
190
191   private native
192   long jORTESubscriptionCreate(long appDomainHandle,
193                                int subsmode,  /*! union */
194                                int substype,  /*! union */
195                                String topic,
196                                String typeName,
197                                ByteBuffer buffer,
198                                int byte_order,
199                                MessageData message,
200                                                           NtpTime deadline,
201                                NtpTime minSeparation,
202                                SubscriptionCallback cb,
203                              // recvCallBackParam
204                                long multicastIPAddress);
205
206   private native
207   boolean jORTESubscriptionDestroy(long subsHandle);
208
209   private native
210   SubsProp jORTESubscriptionPropertiesGet(long subsHandle);
211   
212   private native
213   boolean jORTESubscriptionPropertiesSet(long subsHandle, SubsProp subProps);
214
215   private native
216   Status jORTESubscriptionGetStatus(long subsHandle);
217
218   private native
219   int jORTESubscriptionWaitForPublications(long subsHandle, 
220                                                NtpTime wait,
221                                                                    long retries,
222                                                                                    long noPublications);  
223   private native
224   int jORTESubscriptionPull(long subsHandle);  
225  
226   
227   
228 }
229