Search in sources :

Example 1 with APIModule

use of com.arm.mbed.cloud.sdk.testserver.internal.model.APIModule in project mbed-cloud-sdk-java by ARMmbed.

the class Engine method stopInstanceDaemons.

private void stopInstanceDaemons(String instanceId) throws ServerCacheException, MissingInstanceException {
    APIModule module = cache.fetchModuleFromInstance(instanceId);
    if (module != null) {
        if (module.hasDaemonControlMethods()) {
            ModuleInstance instance = retrieveInstance(instanceId);
            APICaller caller = createAnApiCaller();
            logger.logInfo("Stopping SDK module instance [" + instanceId + "] daemon threads");
            List<APIMethod> stoppingMethods = module.getStopDaemonMethods();
            if (stoppingMethods != null) {
                stoppingMethods.forEach(m -> {
                    try {
                        caller.callAPIOnModuleInstance(instance, m.getName(), null);
                    } catch (UnknownAPIException | APICallException e) {
                        e.printStackTrace();
                    }
                });
            }
            logger.logInfo("Shutting down SDK module instance [" + instanceId + "] daemon executor service");
            List<APIMethod> shuttingdownMethods = module.getShutdownDaemonMethods();
            if (shuttingdownMethods != null) {
                shuttingdownMethods.forEach(m -> {
                    try {
                        caller.callAPIOnModuleInstance(instance, m.getName(), null);
                    } catch (UnknownAPIException | APICallException e) {
                        e.printStackTrace();
                    }
                });
            }
        }
    }
}
Also used : UnknownAPIException(com.arm.mbed.cloud.sdk.testserver.internal.model.UnknownAPIException) APIModule(com.arm.mbed.cloud.sdk.testserver.internal.model.APIModule) APIMethod(com.arm.mbed.cloud.sdk.testserver.internal.model.APIMethod) APICaller(com.arm.mbed.cloud.sdk.testutils.APICaller) APICallException(com.arm.mbed.cloud.sdk.testutils.APICallException) ModuleInstance(com.arm.mbed.cloud.sdk.testserver.internal.model.ModuleInstance)

Example 2 with APIModule

use of com.arm.mbed.cloud.sdk.testserver.internal.model.APIModule in project mbed-cloud-sdk-java by ARMmbed.

the class APICaller method callAPIOnModuleInstance.

@SuppressWarnings("null")
public APIMethodResult callAPIOnModuleInstance(ModuleInstance moduleInstance, String method, Map<String, Object> parameters) throws UnknownAPIException, APICallException {
    if (moduleInstance == null) {
        throwMissingModule(null);
    }
    APIModule moduleDescription = moduleInstance.getModuleDescription();
    if (moduleDescription == null) {
        throwMissingModule(moduleDescription);
    }
    if (method == null) {
        throwUnknownAPI(moduleDescription.getSimpleName(), method);
    }
    final List<APIMethod> methodObjs = moduleDescription.getMethod(method);
    if (methodObjs == null) {
        throwUnknownAPI(moduleDescription.getSimpleName(), method);
    }
    APICallException lastException = null;
    APIMethodResult result = null;
    Object instance = moduleInstance.getInstance();
    // then last exception is raised or the last result is returned if not null.
    for (final APIMethod methodObj : methodObjs) {
        try {
            result = null;
            lastException = null;
            API api = new API(moduleDescription, methodObj);
            result = api.call(instance, parameters);
            // other methods
            if (!result.wasExceptionRaised()) {
                return result;
            }
        } catch (APICallException exception) {
            lastException = exception;
        }
    }
    // If the call was successful but an exception was raised during it then the failure is returned.
    if (result != null) {
        return result;
    }
    // If the call was not successful and hence, an exception was raised, then it is thrown.
    throw lastException;
}
Also used : APIModule(com.arm.mbed.cloud.sdk.testserver.internal.model.APIModule) APIMethod(com.arm.mbed.cloud.sdk.testserver.internal.model.APIMethod) APIMethodResult(com.arm.mbed.cloud.sdk.testserver.internal.model.APIMethodResult)

Aggregations

APIMethod (com.arm.mbed.cloud.sdk.testserver.internal.model.APIMethod)2 APIModule (com.arm.mbed.cloud.sdk.testserver.internal.model.APIModule)2 APIMethodResult (com.arm.mbed.cloud.sdk.testserver.internal.model.APIMethodResult)1 ModuleInstance (com.arm.mbed.cloud.sdk.testserver.internal.model.ModuleInstance)1 UnknownAPIException (com.arm.mbed.cloud.sdk.testserver.internal.model.UnknownAPIException)1 APICallException (com.arm.mbed.cloud.sdk.testutils.APICallException)1 APICaller (com.arm.mbed.cloud.sdk.testutils.APICaller)1