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