]> rtime.felk.cvut.cz Git - orte.git/blob - orte/java/src/org/ocera/orte/DomainApp.java
JORTE: don't use finalizers in Java classes
[orte.git] / orte / java / src / org / ocera / orte / DomainApp.java
1 /* DomainApp.java */
2
3 /**
4  * Class DomainApp provides methods for work with the ORTE application domain.
5  *
6  * @author Lukas Pokorny (lukas_pokorny@centrum.cz)
7  * @author CTU FEE Prague - Department of Control Engineering (dce.felk.cvut.cz)
8  * @author Project ORTE - OCERA Real Time Ethernet (www.ocera.org)
9  * @author dedication to Kj
10  * @version 0.1
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  */
23
24 package org.ocera.orte;
25 import  org.ocera.orte.types.*;
26
27
28 public class DomainApp extends Domain
29 {
30
31   /**
32    * Default constructor. Creates an Application Domain with default parameters.
33    * (domain = ORTE_DEFAULT_DOMAIN, suspended thread = false)
34    */
35    public DomainApp()
36    {
37       super();
38           // this.handle =jORTEDomainDefaultAppCreate(ORTEConstant.ORTE_DEFAULT_DOMAIN,true); - uspane vlakno po startu
39       this.handle = jORTEDomainDefaultAppCreate(ORTEConstant.ORTE_DEFAULT_DOMAIN,false);
40       if(this.handle == 0) System.out.println(":j!: zero domain handle! ..");   
41         else System.out.println(":j: application domain created..");
42    }
43
44    
45    /**
46     * User defined constructor. For creating an Application Domain with your parameters.
47     */
48    /** TODO dodelat objekty 'props', 'events' */
49    public DomainApp(int domain, DomainProp ps, DomainEvents ev, boolean suspend)
50    {
51      super();    // set Default Domain Properties
52      if(ps == null) {
53          this.props = DomainProp.defaultPropsCreate();
54      }
55      else {
56          this.props = ps;
57      }
58      // init Domain Events
59      if(ev == null) {
60          this.events.init();
61      }
62      else {
63          this.events = ev;
64      }
65      this.handle = jORTEDomainAppCreate(domain,ps.handle,ev.getHandle(),ev,suspend);
66      /* TODO osetrit neuspesne vytvoreni domeny */ 
67      if(this.handle == 0) System.out.println(":j!: zero domain handle! ..");
68       else System.out.println(":j: application domain created..");
69    }
70
71    /**
72     * Destroy the Application Domain.  
73     */
74    public boolean destroy()
75    {
76      if(!destroyAllRegTypes()) System.out.println(":j!: destroyAllRegTypes fault!");
77      if(!jORTEDomainAppDestroy(this.handle) || !this.props.destroy()) {
78        System.out.println(":j!: ORTEDomainADestroy fault!");
79        return false;
80      }
81      return true;
82        
83    }
84
85      
86    /**
87     * Register new data type.
88     * @param Name of the new type.
89     * @param Max length of the input buffer. 
90     * @return True - registration Ok, False - registration fault. 
91     */
92    public
93    boolean regNewDataType(String name,
94                                                   long maxlength)
95    {
96      int b = jORTETypeRegisterAdd(this.handle, 
97                                       name,
98                                   maxlength);
99          if (b == ORTEConstant.ORTE_BAD_HANDLE)
100      {
101        System.out.println(":!j: regNewDataType() failed! [bad domain handle]");
102        return false;
103      }
104      if (b == ORTEConstant.ORTE_OK)
105      {
106        System.out.println(":j: type: '" + name + "' successfully registered..");
107        return true;
108          }
109      return false;
110    }
111
112
113    /**
114     * Destroys all previous registered data types.
115     * @return True - registration Ok, False - destroying fault. 
116     */
117    public
118    boolean destroyAllRegTypes()
119    {
120      if (jORTETypeRegisterDestroyAll(this.handle)) return true;
121      return false;
122    }
123
124
125    /**
126     * Creates a new Subscription.
127     *
128     * @param Subscription properties.
129     * @param Length of output buffer.
130     * @param Callback instance.
131     * @return Handle to the new created Subscription.
132     */
133    public
134    Subscription createSubscription(SubsProp subsProp,
135                                     MessageData instance,
136                                         SubscriptionCallback subsCallback)
137    {
138        Subscription s = new Subscription(this,
139                                          subsProp,
140                                          instance,
141                                          subsCallback);
142        return s;
143    }
144
145
146    /**
147     * Creates a new Publication.
148     *
149     * @param Instance of MessageData (which data will be send).
150     * @param Time parameter, which defines how long are publicated data valid.
151     * @param The importance of this Publication (its rating).
152     * @return Handle to the new created Publication.
153     */
154    public
155    Publication createPublication(PublProp publProp,
156                                       MessageData instance)
157    {
158      //System.out.println(":j: vytvarim publikaci pro DataType: " + instance.getType());
159      Publication p = new Publication(this,
160                                      publProp,
161                                      instance);
162      return p;
163    }
164
165
166 /* ****************************************************************** *
167  *                                                                    *
168  *                         native methods                             *
169  *                                                                    *
170  * ****************************************************************** */
171
172 /**
173  * jORTEDomainAppCreate - creates an default application object within given
174  * domain, returns handle to created object (from C environment),
175  * NULL in case of any error.
176  *
177  *
178  * @param domain given domain
179  * @return addres value (from C environment) of the created domain
180  */
181  private static native long jORTEDomainDefaultAppCreate(int domain,boolean suspend);
182
183
184 /**
185  * jORTEDomainAppCreate - creates an application object within given
186  * domain, returns handle to created object (from C environment),
187  * NULL in case of any error.
188  *
189  *
190  * @param domain given domain
191  * @param handler for C pointer to ORTEDomainProps
192  * @param handler for C pointer to ORTEDomainEvents
193  * @param suspended specifies whether threads of this application should be started as well (False) or stay suspended (True)
194  * @return addres value (from C environment) of the created domain
195  */
196
197  private static native
198  long jORTEDomainAppCreate(int  domain,
199                            long propHandle,
200                            long eventsHandle,
201                                                   DomainEvents ev,
202                                                   boolean suspend);
203
204
205 /**
206  * jORTEDomainADestroy - destroys all application objects in specified
207  * domain, returns False if some error occures otherwise returns True.
208  *
209  *
210  * @param jp_domhandle handler to domain
211  * @return boolean value, False when some error occures, otherwise True
212  */
213  private static native
214  boolean jORTEDomainAppDestroy(long dhandle);
215
216  
217  /**
218   * jORTETypeRegisterAdd - register new data type.
219   *
220   *
221   * @param dhandle handler to domain
222   * @param
223   * @param
224   * @param
225   * @return
226   */
227   private static native
228   int jORTETypeRegisterAdd(long dhandle,
229                             String typeName,
230                             long maxlenght);
231
232
233  /**
234   * jORTETypeRegisterDestroyAll - destroys all previous registered data types.
235   *
236   *
237   * @param dhandle handler to domain
238   * @return boolean value, False when some error occures, otherwise True
239   */
240   private static native
241   boolean jORTETypeRegisterDestroyAll(long dhandle);
242
243 }