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