Last sync 2016.04.01
[deliverable/titan.core.git] / titan_executor_api / TITAN_Executor_API_test / src / org / eclipse / titan / executorapi / test / JniExecutorSync.java
1 /******************************************************************************
2 * Copyright (c) 2000-2016 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Balasko, Jeno
10 * Lovassy, Arpad
11 *
12 ******************************************************************************/
13 package org.eclipse.titan.executorapi.test;
14
15 import java.util.Map;
16
17 import org.eclipse.titan.executor.jni.McStateEnum;
18 import org.eclipse.titan.executor.jni.Timeval;
19 import org.eclipse.titan.executor.jni.VerdictTypeEnum;
20 import org.eclipse.titan.executorapi.HostController;
21 import org.eclipse.titan.executorapi.IJniExecutorObserver;
22 import org.eclipse.titan.executorapi.JniExecutor;
23 import org.eclipse.titan.executorapi.exception.JniExecutorIllegalArgumentException;
24 import org.eclipse.titan.executorapi.exception.JniExecutorJniLoadException;
25 import org.eclipse.titan.executorapi.exception.JniExecutorStartSessionException;
26 import org.eclipse.titan.executorapi.exception.JniExecutorWrongStateException;
27 import org.eclipse.titan.executorapi.util.Log;
28
29 /**
30 * Wrapper class for synchronous usage of JniExecutor. SINGLETON
31 */
32 public final class JniExecutorSync implements IJniExecutorObserver {
33
34 /** Default timeout of synchronous functions of the asynchronous requests in ms */
35 private static final int DEFAULT_TIMEOUT = 0;
36
37 /**
38 * lock for synchronous access of mRequestOngoing
39 */
40 private final Object mLockObject = new Object();
41
42 /**
43 * Flag for signaling the asynchronous requests, used for the synchronous versions of the functions
44 */
45 private volatile boolean mRequestOngoing = false;
46
47 /**
48 * true, if setObserver() was called
49 */
50 private boolean mObserverSet = false;
51
52 /**
53 * Timeout of synchronous functions of the asynchronous requests in ms
54 */
55 private long mTimeout = DEFAULT_TIMEOUT;
56
57 /**
58 * Private constructor, because it is a singleton.
59 */
60 private JniExecutorSync() {
61 }
62
63 /**
64 * Lazy holder for the singleton (Bill Pugh solution)
65 * Until we need an instance, the holder class will not be initialized until required and you can still use other static members of the singleton class.
66 * @see "http://en.wikipedia.org/wiki/Singleton_pattern#Initialization_On_Demand_Holder_Idiom"
67 */
68 private static class SingletonHolder {
69 private static final JniExecutorSync mInstance = new JniExecutorSync();
70 }
71
72 /**
73 * @return the singleton instance
74 */
75 public static JniExecutorSync getInstance() {
76 return SingletonHolder.mInstance;
77 }
78
79 @Override
80 public void statusChanged( final McStateEnum aNewState ) {
81 Log.fi( aNewState );
82 // in case of final states
83 // synchronous function is signaled that function can be ended
84 if ( !isIntermediateState( aNewState ) ) {
85 signalEndSync();
86 }
87 Log.fo();
88 }
89
90 @Override
91 public void error( final int aSeverity, final String aMsg ) {
92 Log.fi( aSeverity, aMsg );
93 signalEndSync();
94 Log.fo();
95 }
96
97 @Override
98 public void notify(final Timeval aTime, final String aSource, final int aSeverity, final String aMsg) {
99 }
100
101 @Override
102 public void verdict(String aTestcase, VerdictTypeEnum aVerdictType) {
103 }
104
105 @Override
106 public void verdictStats(Map<VerdictTypeEnum, Integer> aVerdictStats) {
107 }
108
109 public void init() throws JniExecutorWrongStateException, JniExecutorJniLoadException {
110 JniExecutor.getInstance().init();
111 mRequestOngoing = false;
112 mObserverSet = false;
113 }
114
115 public void addHostController( final HostController aHc ) throws JniExecutorIllegalArgumentException, JniExecutorWrongStateException {
116 JniExecutor.getInstance().addHostController( aHc );
117 }
118
119 public void setConfigFileName( final String aConfigFileName ) throws JniExecutorWrongStateException, JniExecutorIllegalArgumentException {
120 JniExecutor.getInstance().setConfigFileName( aConfigFileName );
121 }
122
123 public void startSession() throws JniExecutorWrongStateException, JniExecutorStartSessionException {
124 JniExecutor.getInstance().startSession();
125 }
126
127 public int getExecuteCfgLen() throws JniExecutorWrongStateException {
128 return JniExecutor.getInstance().getExecuteCfgLen();
129 }
130
131 /**
132 * Sets new timeout
133 * @param aTimeout the new timeout value in ms,
134 * or 0, if no timeout
135 */
136 public void setTimeout( final long aTimeout ) {
137 mTimeout = aTimeout;
138 }
139
140 /**
141 * Sets observer if it's not set yet.
142 * It must be called before the 1st asynchronous function call.
143 * @throws JniExecutorWrongStateException
144 */
145 private void setObserver() throws JniExecutorWrongStateException {
146 if ( !mObserverSet ) {
147 JniExecutor.getInstance().setObserver( this );
148 mObserverSet = true;
149 }
150 }
151
152 // ------------ SYNCHRONOUS VERSION OF ASYNCHRONOUS FUNCTIONS --------------------
153
154
155 public void startHostControllersSync() throws JniExecutorWrongStateException {
156 // observer is set before the 1st async request
157 setObserver();
158 startSync();
159 JniExecutor.getInstance().startHostControllers();
160 endSync();
161 }
162
163 public void configureSync() throws JniExecutorWrongStateException {
164 // observer is set before the 1st async request
165 setObserver();
166 startSync();
167 JniExecutor.getInstance().configure();
168 endSync();
169 }
170
171 public void createMTCSync() throws JniExecutorWrongStateException {
172 startSync();
173 JniExecutor.getInstance().createMTC();
174 endSync();
175 }
176
177 public void executeControlSync( final String aModule ) throws JniExecutorWrongStateException, JniExecutorIllegalArgumentException {
178 startSync();
179 JniExecutor.getInstance().executeControl( aModule );
180 endSync();
181 }
182
183 public void executeTestcaseSync( final String aModule, final String aTestcase ) throws JniExecutorWrongStateException, JniExecutorIllegalArgumentException {
184 startSync();
185 JniExecutor.getInstance().executeTestcase( aModule, aTestcase );
186 endSync();
187 }
188
189 public void executeCfgSync( final int aIndex ) throws JniExecutorWrongStateException, JniExecutorIllegalArgumentException {
190 startSync();
191 JniExecutor.getInstance().executeCfg( aIndex );
192 endSync();
193 }
194
195 public void continueExecutionSync() throws JniExecutorWrongStateException {
196 startSync();
197 JniExecutor.getInstance().continueExecution();
198 endSync();
199 }
200
201 public void exitMTCSync() throws JniExecutorWrongStateException {
202 startSync();
203 JniExecutor.getInstance().exitMTC();
204 endSync();
205 }
206
207 public void shutdownSessionSync() {
208 startSync();
209 JniExecutor.getInstance().shutdownSession();
210 endSync();
211 }
212
213 private void startSync() {
214 Log.fi();
215 synchronized (mLockObject) {
216 Log.i("startSync()");
217 // Make sure, that ...Sync() functions do NOT call each other
218 if ( mRequestOngoing ) {
219 //This should not happen, another request is ongoing
220 Log.i(" startSync() mRequestOngoing == true --- This should not happen, another request is ongoing");
221 }
222 mRequestOngoing = true;
223 }
224 Log.fo();
225 }
226
227 private void endSync() {
228 Log.fi();
229 synchronized (mLockObject) {
230 if ( !mRequestOngoing ) {
231 // The request is already finished, faster than the ...Sync() function
232 Log.i(" endSync() mRequestOngoing == false --- The request is already finished");
233 }
234 else {
235 try {
236 if ( mTimeout == 0 ) {
237 // no timeout
238 mLockObject.wait();
239 } else {
240 mLockObject.wait( mTimeout );
241 }
242 } catch (InterruptedException e) {
243 Log.f(e.toString());
244 }
245 }
246 Log.i("endSync()");
247 }
248 Log.fo();
249 }
250
251 private void signalEndSync() {
252 Log.fi();
253 synchronized (mLockObject) {
254 Log.i("signalEndSync()");
255 if ( !mRequestOngoing ) {
256 // There is no synchronous function called
257 Log.i(" signalEndSync() There is no synchronous function called");
258 }
259 mLockObject.notifyAll();
260 mRequestOngoing = false;
261 }
262 Log.fo();
263 }
264
265 /**
266 * Checks if state is intermediate state. Intermediate state is a state, when asynchronous request is ongoing.
267 * When it is finished, it will switched to some final state automatically.
268 * @param aState the state to check
269 * @return if state is intermediate
270 */
271 private static boolean isIntermediateState( final McStateEnum aState ) {
272 return
273 aState == McStateEnum.MC_LISTENING || // it is stable, but cannot be a result state of any async request
274 aState == McStateEnum.MC_CONFIGURING ||
275 aState == McStateEnum.MC_CREATING_MTC ||
276 aState == McStateEnum.MC_TERMINATING_MTC ||
277 aState == McStateEnum.MC_EXECUTING_CONTROL ||
278 aState == McStateEnum.MC_EXECUTING_TESTCASE ||
279 aState == McStateEnum.MC_TERMINATING_TESTCASE ||
280 aState == McStateEnum.MC_SHUTDOWN;
281 }
282 }
This page took 0.037487 seconds and 5 git commands to generate.