]> rtime.felk.cvut.cz Git - orte.git/blob - orte/java/src/org/ocera/orte/Publication.java
JORTE: don't use finalizers in Java classes
[orte.git] / orte / java / src / org / ocera / orte / Publication.java
1 /* Publication.java */
2
3 /**
4  * Class Publication provides methods for creating and working
5  * with the ORTE publisher.
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
28 import org.ocera.orte.types.*;
29
30
31 public class Publication {
32
33   private long handle;
34   private org.ocera.orte.types.MessageData msgData;
35
36   private boolean b;
37   private Domain appDomain;
38 /*
39   private int callbackEnvHandle = 0;
40 */
41   /**
42    * getHandle - returns handler of the publication
43    *
44    * @return handler of the publication
45    */
46    public long getHandle()
47    {
48      return this.handle;
49    }
50
51
52   /**
53    * constructor
54    *
55    */
56
57   /* TODO   
58    * nemelo by se v jORTEPublicationCreate misto cele instance predavat 
59    * jen instance.data ????? resp instance.getData???? 
60    * 
61    * 
62    */
63    public Publication(Domain d,
64                       PublProp publProp,
65                                           MessageData instance)
66   {
67    this.appDomain = d;
68         this.handle = jORTEPublicationCreate(d.handle,
69                                              publProp.getTopic(),
70                                              publProp.getTypeName(),
71                                                                                  instance.getBuffer(),
72                                          publProp.getPersistence(),
73                                                                              publProp.getStrength());
74                                       // int sendCallBack,
75                                           // sendCallBackParam,
76                                       // NtpTime cbDelay
77   }
78
79
80  /**
81   * destroy - Removes a publication.
82   * @return False if bad publication handle, True if  succesful.
83   */
84   public
85   boolean destroy()
86   {
87         /* TODO vyradit vypis na nasledujici radce */
88         System.out.println(":j: publication destroy called..");
89         // destroy publication
90         if(!jORTEPublicationDestroy(this.handle)) 
91         {
92             System.out.println(":j!: publication destroy fault!");
93             return false;               
94         }
95         // destroy application domain    
96     if(!appDomain.destroy()) 
97     {
98             System.out.println(":j!: publication destroy fault!");
99             return false;                       
100     }
101     System.out.println(":j: publication destroy successfull..");
102     return true;
103   }
104
105
106  /**
107   * send - Send the publication.
108   * @return void
109   */
110   public void send(MessageData instance)
111   {
112     //this.msgData.write(); // volat v Publisher.java
113     instance.write(); 
114
115     b = jORTEPublicationSend(this.handle);
116     if(!b) System.out.println(":j!: Sending Publication Fault!");
117     return;
118   }
119
120   
121   /**
122    * Get Publication properties.
123    * @return Publication properties.
124    */
125   public PublProp getProperties()
126   {
127         PublProp sp = new PublProp();
128     sp = jORTEPublicationPropertiesGet(this.handle);    
129         return sp;
130   }
131   
132   /**
133    * Set Publication properties.
134    * @return True - setting new properties Ok, False - setting new properties fault.  
135    */
136   public boolean setProperties(PublProp sp)
137   {
138         boolean  b;
139
140         // modify PublProp object - sp    
141         b = jORTEPublicationPropertiesSet(this.handle, sp);     
142         if(b == false) 
143         {
144         System.out.println(":j!: set PublProp failed! (bad Subs. handle)"); 
145                 return false; 
146         } 
147         return true;
148   }
149   
150   
151   /**
152    * Get Publication status.
153    * @return Status of Publication.  
154    */
155   public Status getStatus()
156   {
157     Status ps = new Status();
158         ps = jORTEPublicationGetStatus(this.handle);
159         return ps;
160   }
161
162   
163   /**
164    * Waits for given number of subscriptions. 
165    * @param Time how long to wait.
166    * @param Number of retries if specified number of subscriptions not reached.
167    * @param Desired number of subscriptions.
168    * @return ORTE_OK if succesful, ORTE_BAD_HANDLE if bad publication handle, 
169    *         ORTE_TIMEOUT if number of retries has been exhausted.   
170    */
171   public int waitForSubscriptions(NtpTime wait,
172                                           long retries,
173                                                           long noSubscription)   
174   { 
175     int i;
176         i = jORTEPublicationWaitForSubscriptions(this.handle,
177                                                          wait,
178                                                          retries,
179                                                                                          noSubscription);
180     return i; 
181   }
182   
183   
184  /* ****************************************************************** *
185   *                                                                    *
186   *                         native methods                             *
187   *                                                                    *
188   * ****************************************************************** */
189
190   private native
191   long jORTEPublicationCreate(long appDomainHandle,
192                              String topic,
193                              String typeName,
194                                                          ByteBuffer buffer,
195                                                          NtpTime persistence,
196                              int strength);
197                           // int sendCallBack, -->NULL
198                           // sendCallBackParam,--> NULL
199                           // NtpTime sendCallBackDelay
200
201   private native
202   boolean jORTEPublicationDestroy(long publHandle);
203
204   private native
205   boolean jORTEPublicationSend(long publHandle);
206                                        
207
208   private native
209   PublProp jORTEPublicationPropertiesGet(long publHandle);
210   
211   private native
212   boolean jORTEPublicationPropertiesSet(long publHandle,
213                                                 PublProp publProps);
214
215   private native
216   Status jORTEPublicationGetStatus(long publHandle);
217
218   private native
219   int jORTEPublicationWaitForSubscriptions(long publHandle,
220                                                NtpTime wait,
221                                                                            long retries,
222                                                                                    long noSubscription);
223
224 }