プロジェクト

全般

プロフィール

ModuleNameImpl.java

katami, 2018/03/02 09:12

 
1
// -*- Java -*-
2
/*!
3
 * @file  ModuleNameImpl.java
4
 * @brief ModuleDescription
5
 * @date  $Date$
6
 *
7
 * $Id$
8
 */
9

    
10
import java.lang.reflect.Array;
11
import java.lang.reflect.Constructor;
12
import java.lang.reflect.Field;
13
import java.lang.reflect.InvocationTargetException;
14
import java.lang.reflect.Method;
15

    
16
import RTC.TimedString;
17
import jp.go.aist.rtm.RTC.DataFlowComponentBase;
18
import jp.go.aist.rtm.RTC.Manager;
19
import jp.go.aist.rtm.RTC.port.InPort;
20
import jp.go.aist.rtm.RTC.port.OutPort;
21
import jp.go.aist.rtm.RTC.util.DataRef;
22
import jp.go.aist.rtm.RTC.port.CorbaPort;
23
import org.omg.PortableServer.POAPackage.ObjectNotActive;
24
import org.omg.PortableServer.POAPackage.ServantAlreadyActive;
25
import org.omg.PortableServer.POAPackage.WrongPolicy;
26
import RTC.ReturnCode_t;
27

    
28
/**
29
 * ModuleNameImpl
30
 * <p>
31
 * ModuleDescription
32
 *
33
 */
34
public class ModuleNameImpl extends DataFlowComponentBase {
35

    
36
  /**
37
   * constructor
38
   * @param manager Manager Object
39
   */
40
    public ModuleNameImpl(Manager manager) {  
41
        super(manager);
42
        // <rtc-template block="initializer">
43
        m_sen_val = new TimedString();
44
        initializeParam(m_sen_val);
45
        m_sen = new DataRef<TimedString>(m_sen_val);
46
        m_senIn = new InPort<TimedString>("sen", m_sen);
47
        m_mot_val = new TimedString();
48
        m_mot = new DataRef<TimedString>(m_mot_val);
49
        m_motOut = new OutPort<TimedString>("mot", m_mot);
50
        m_sv_namePort = new CorbaPort("sv_name");
51
        // </rtc-template>
52

    
53
    }
54

    
55
    /**
56
     *
57
     * The initialize action (on CREATED-&gt;ALIVE transition)
58
     * former rtc_init_entry() 
59
     *
60
     * @return RTC::ReturnCode_t
61
     * 
62
     * 
63
     */
64
    @Override
65
    protected ReturnCode_t onInitialize() {
66
        // Registration: InPort/OutPort/Service
67
        // <rtc-template block="registration">
68
        // Set InPort buffers
69
        addInPort("sen", m_senIn);
70
        
71
        // Set OutPort buffer
72
        addOutPort("mot", m_motOut);
73
        
74
        // Set service provider to Ports
75
        try {
76
            m_sv_namePort.registerProvider("if_name", "MyService", m_if_name);
77
        } catch (ServantAlreadyActive e) {
78
            e.printStackTrace();
79
        } catch (WrongPolicy e) {
80
            e.printStackTrace();
81
        } catch (ObjectNotActive e) {
82
            e.printStackTrace();
83
        }
84
        
85
        // Set service consumers to Ports
86
        
87
        // Set CORBA Service Ports
88
        addPort(m_sv_namePort);
89
        // </rtc-template>
90
        return super.onInitialize();
91
    }
92

    
93
    /**
94
     *
95
     * The finalize action (on ALIVE-&gt;END transition)
96
     * former rtc_exiting_entry()
97
     *
98
     * @return RTC::ReturnCode_t
99
     * 
100
     * 
101
     */
102
//    @Override
103
//    protected ReturnCode_t onFinalize() {
104
//        return super.onFinalize();
105
//    }
106

    
107
    /**
108
     *
109
     * The startup action when ExecutionContext startup
110
     * former rtc_starting_entry()
111
     *
112
     * @param ec_id target ExecutionContext Id
113
     *
114
     * @return RTC::ReturnCode_t
115
     * 
116
     * 
117
     */
118
//    @Override
119
//    protected ReturnCode_t onStartup(int ec_id) {
120
//        return super.onStartup(ec_id);
121
//    }
122

    
123
    /**
124
     *
125
     * The shutdown action when ExecutionContext stop
126
     * former rtc_stopping_entry()
127
     *
128
     * @param ec_id target ExecutionContext Id
129
     *
130
     * @return RTC::ReturnCode_t
131
     * 
132
     * 
133
     */
134
//    @Override
135
//    protected ReturnCode_t onShutdown(int ec_id) {
136
//        return super.onShutdown(ec_id);
137
//    }
138

    
139
    /**
140
     *
141
     * The activated action (Active state entry action)
142
     * former rtc_active_entry()
143
     *
144
     * @param ec_id target ExecutionContext Id
145
     *
146
     * @return RTC::ReturnCode_t
147
     * 
148
     * 
149
     */
150
//    @Override
151
//    protected ReturnCode_t onActivated(int ec_id) {
152
//        return super.onActivated(ec_id);
153
//    }
154

    
155
    /**
156
     *
157
     * The deactivated action (Active state exit action)
158
     * former rtc_active_exit()
159
     *
160
     * @param ec_id target ExecutionContext Id
161
     *
162
     * @return RTC::ReturnCode_t
163
     * 
164
     * 
165
     */
166
//    @Override
167
//    protected ReturnCode_t onDeactivated(int ec_id) {
168
//        return super.onDeactivated(ec_id);
169
//    }
170

    
171
    /**
172
     *
173
     * The execution action that is invoked periodically
174
     * former rtc_active_do()
175
     *
176
     * @param ec_id target ExecutionContext Id
177
     *
178
     * @return RTC::ReturnCode_t
179
     * 
180
     * 
181
     */
182
//    @Override
183
//    protected ReturnCode_t onExecute(int ec_id) {
184
//        return super.onExecute(ec_id);
185
//    }
186

    
187
    /**
188
     *
189
     * The aborting action when main logic error occurred.
190
     * former rtc_aborting_entry()
191
     *
192
     * @param ec_id target ExecutionContext Id
193
     *
194
     * @return RTC::ReturnCode_t
195
     * 
196
     * 
197
     */
198
//  @Override
199
//  public ReturnCode_t onAborting(int ec_id) {
200
//      return super.onAborting(ec_id);
201
//  }
202

    
203
    /**
204
     *
205
     * The error action in ERROR state
206
     * former rtc_error_do()
207
     *
208
     * @param ec_id target ExecutionContext Id
209
     *
210
     * @return RTC::ReturnCode_t
211
     * 
212
     * 
213
     */
214
//    @Override
215
//    public ReturnCode_t onError(int ec_id) {
216
//        return super.onError(ec_id);
217
//    }
218

    
219
    /**
220
     *
221
     * The reset action that is invoked resetting
222
     * This is same but different the former rtc_init_entry()
223
     *
224
     * @param ec_id target ExecutionContext Id
225
     *
226
     * @return RTC::ReturnCode_t
227
     * 
228
     * 
229
     */
230
//    @Override
231
//    protected ReturnCode_t onReset(int ec_id) {
232
//        return super.onReset(ec_id);
233
//    }
234

    
235
    /**
236
     *
237
     * The state update action that is invoked after onExecute() action
238
     * no corresponding operation exists in OpenRTm-aist-0.2.0
239
     *
240
     * @param ec_id target ExecutionContext Id
241
     *
242
     * @return RTC::ReturnCode_t
243
     * 
244
     * 
245
     */
246
//    @Override
247
//    protected ReturnCode_t onStateUpdate(int ec_id) {
248
//        return super.onStateUpdate(ec_id);
249
//    }
250

    
251
    /**
252
     *
253
     * The action that is invoked when execution context's rate is changed
254
     * no corresponding operation exists in OpenRTm-aist-0.2.0
255
     *
256
     * @param ec_id target ExecutionContext Id
257
     *
258
     * @return RTC::ReturnCode_t
259
     * 
260
     * 
261
     */
262
//    @Override
263
//    protected ReturnCode_t onRateChanged(int ec_id) {
264
//        return super.onRateChanged(ec_id);
265
//    }
266

    
267
    /**
268
     */
269
    // DataInPort declaration
270
    // <rtc-template block="inport_declare">
271
    protected TimedString m_sen_val;
272
    protected DataRef<TimedString> m_sen;
273
    /*!
274
     */
275
    protected InPort<TimedString> m_senIn;
276

    
277
    
278
    // </rtc-template>
279

    
280
    // DataOutPort declaration
281
    // <rtc-template block="outport_declare">
282
    protected TimedString m_mot_val;
283
    protected DataRef<TimedString> m_mot;
284
    /*!
285
     */
286
    protected OutPort<TimedString> m_motOut;
287

    
288
    
289
    // </rtc-template>
290

    
291
    // CORBA Port declaration
292
    // <rtc-template block="corbaport_declare">
293
    /*!
294
     */
295
    protected CorbaPort m_sv_namePort;
296
    
297
    // </rtc-template>
298

    
299
    // Service declaration
300
    // <rtc-template block="service_declare">
301
    /*!
302
     */
303
    protected MyServiceSVC_impl m_if_name = new MyServiceSVC_impl();
304
    
305
    // </rtc-template>
306

    
307
    // Consumer declaration
308
    // <rtc-template block="consumer_declare">
309
    
310
    // </rtc-template>
311

    
312

    
313
    private void initializeParam(Object target) {
314
        Class<?> targetClass = target.getClass();
315
        ClassLoader loader = target.getClass().getClassLoader();
316
        //
317
        Field[] fields = targetClass.getFields();
318
        for(Field field : fields) {
319
            if(field.getType().isPrimitive()) continue;
320
            
321
            try {
322
                if(field.getType().isArray()) {
323
                    Object arrayValue = null;
324
                    Class<?> clazz = null;
325
                    if(field.getType().getComponentType().isPrimitive()) {
326
                        clazz = field.getType().getComponentType();
327
                    } else {
328
                            clazz = loader.loadClass(field.getType().getComponentType().getName());
329
                    }
330
                    arrayValue = Array.newInstance(clazz, 0);
331
                    field.set(target, arrayValue);
332
                    
333
                } else {
334
                    Constructor<?>[] constList = field.getType().getConstructors();
335
                    if(constList.length==0) {
336
                        Method[] methodList = field.getType().getMethods();
337
                        for(Method method : methodList) {
338
                            if(method.getName().equals("from_int")==false) continue;
339
                            Object objFld = method.invoke(target, new Object[]{ new Integer(0) });
340
                            field.set(target, objFld);
341
                            break;
342
                        }
343
                        
344
                    } else {
345
                        Class<?> classFld = Class.forName(field.getType().getName(), true, loader);
346
                        Object objFld = classFld.newInstance();
347
                        initializeParam(objFld);
348
                        field.set(target, objFld);
349
                    }
350
                }
351
            } catch (ClassNotFoundException e) {
352
                e.printStackTrace();
353
            } catch (InstantiationException e) {
354
                e.printStackTrace();
355
            } catch (IllegalAccessException e) {
356
                e.printStackTrace();
357
            } catch (IllegalArgumentException e) {
358
                e.printStackTrace();
359
            } catch (InvocationTargetException e) {
360
                e.printStackTrace();
361
            }
362
        }
363
    }
364
}