]> rtime.felk.cvut.cz Git - orte.git/blob - orte/java/src/org/ocera/orte/DomainApp.java
2a0afea36e1c68e4d29ca90640716f07232fd3bc
[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    /**
73     * Destructor. Before it destroy Application Domain, destroy all registered 
74     * data types too.
75     */
76    protected void finalize()
77    {
78      if(!destroyAllRegTypes()) System.out.println(":j!: destroyAllRegTypes fault!");
79      if(!destroy()) System.out.println(":j!: ORTEDomainADestroy fault!");
80    }
81
82     
83    /**
84     * Destroy the Application Domain.  
85     */
86    public boolean destroy()
87    {
88      if (jORTEDomainAppDestroy(this.handle)) return true;
89      return false;
90    }
91
92      
93    /**
94     * Register new data type.
95     * @param Name of the new type.
96     * @param Max length of the input buffer. 
97     * @return True - registration Ok, False - registration fault. 
98     */
99    public
100    boolean regNewDataType(String name,
101                                                   long maxlength)
102    {
103      int b = jORTETypeRegisterAdd(this.handle, 
104                                       name,
105                                   maxlength);
106          if (b == ORTEConstant.ORTE_BAD_HANDLE)
107      {
108        System.out.println(":!j: regNewDataType() failed! [bad domain handle]");
109        return false;
110      }
111      if (b == ORTEConstant.ORTE_OK)
112      {
113        System.out.println(":j: type: '" + name + "' successfully registered..");
114        return true;
115          }
116      return false;
117    }
118
119
120    /**
121     * Destroys all previous registered data types.
122     * @return True - registration Ok, False - destroying fault. 
123     */
124    public
125    boolean destroyAllRegTypes()
126    {
127      if (jORTETypeRegisterDestroyAll(this.handle)) return true;
128      return false;
129    }
130
131
132    /**
133     * Creates a new Subscription.
134     *
135     * @param Subscription properties.
136     * @param Length of output buffer.
137     * @param Callback instance.
138     * @return Handle to the new created Subscription.
139     */
140    public
141    Subscription createSubscription(SubsProp subsProp,
142                                     MessageData instance,
143                                         SubscriptionCallback subsCallback)
144    {
145        Subscription s = new Subscription(this,
146                                          subsProp,
147                                          instance,
148                                          subsCallback);
149        return s;
150    }
151
152
153    /**
154     * Creates a new Publication.
155     *
156     * @param Instance of MessageData (which data will be send).
157     * @param Time parameter, which defines how long are publicated data valid.
158     * @param The importance of this Publication (its rating).
159     * @return Handle to the new created Publication.
160     */
161    public
162    Publication createPublication(PublProp publProp,
163                                       MessageData instance)
164    {
165      //System.out.println(":j: vytvarim publikaci pro DataType: " + instance.getType());
166      Publication p = new Publication(this,
167                                      publProp,
168                                      instance);
169      return p;
170    }
171
172
173 /* ****************************************************************** *
174  *                                                                    *
175  *                         native methods                             *
176  *                                                                    *
177  * ****************************************************************** */
178
179 /**
180  * jORTEDomainAppCreate - creates an default application object within given
181  * domain, returns handle to created object (from C environment),
182  * NULL in case of any error.
183  *
184  *
185  * @param domain given domain
186  * @return addres value (from C environment) of the created domain
187  */
188  private static native long jORTEDomainDefaultAppCreate(int domain,boolean suspend);
189
190
191 /**
192  * jORTEDomainAppCreate - creates an application object within given
193  * domain, returns handle to created object (from C environment),
194  * NULL in case of any error.
195  *
196  *
197  * @param domain given domain
198  * @param handler for C pointer to ORTEDomainProps
199  * @param handler for C pointer to ORTEDomainEvents
200  * @param suspended specifies whether threads of this application should be started as well (False) or stay suspended (True)
201  * @return addres value (from C environment) of the created domain
202  */
203
204  private static native
205  long jORTEDomainAppCreate(int  domain,
206                            long propHandle,
207                            long eventsHandle,
208                                                   DomainEvents ev,
209                                                   boolean suspend);
210
211
212 /**
213  * jORTEDomainADestroy - destroys all application objects in specified
214  * domain, returns False if some error occures otherwise returns True.
215  *
216  *
217  * @param jp_domhandle handler to domain
218  * @return boolean value, False when some error occures, otherwise True
219  */
220  private static native
221  boolean jORTEDomainAppDestroy(long dhandle);
222
223  
224  /**
225   * jORTETypeRegisterAdd - register new data type.
226   *
227   *
228   * @param dhandle handler to domain
229   * @param
230   * @param
231   * @param
232   * @return
233   */
234   private static native
235   int jORTETypeRegisterAdd(long dhandle,
236                             String typeName,
237                             long maxlenght);
238
239
240  /**
241   * jORTETypeRegisterDestroyAll - destroys all previous registered data types.
242   *
243   *
244   * @param dhandle handler to domain
245   * @return boolean value, False when some error occures, otherwise True
246   */
247   private static native
248   boolean jORTETypeRegisterDestroyAll(long dhandle);
249
250 }