All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.joynr.generator.cpp.joynrmessaging.InterfaceJoynrMessagingConnectorCppTemplate.xtend Maven / Gradle / Ivy

There is a newer version: 0.9.3
Show newest version
package io.joynr.generator.cpp.joynrmessaging
/*
 * !!!
 *
 * Copyright (C) 2011 - 2015 BMW Car IT GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import com.google.inject.Inject
import io.joynr.generator.cpp.util.CppStdTypeUtil
import io.joynr.generator.cpp.util.DatatypeSystemTransformation
import io.joynr.generator.cpp.util.JoynrCppGeneratorExtensions
import io.joynr.generator.cpp.util.QtTypeUtil
import io.joynr.generator.cpp.util.TemplateBase
import io.joynr.generator.util.InterfaceTemplate
import org.franca.core.franca.FInterface
import org.franca.core.franca.FMethod
import org.franca.core.franca.FType

class InterfaceJoynrMessagingConnectorCppTemplate implements InterfaceTemplate{

	@Inject
	private extension TemplateBase

	@Inject
	private extension QtTypeUtil qtTypeUtil

	@Inject
	private CppStdTypeUtil cppStdTypeUtil
	
	@Inject
	private extension JoynrCppGeneratorExtensions

	def produceParameterSetters(FMethod method)
'''
joynr::Request internalRequestObject;
internalRequestObject.setMethodName(QString("«method.joynrName»"));
«FOR param : getInputParameters(method)»
	«val paramRef = qtTypeUtil.fromStdTypeToQTType(param, param.joynrName, true)»
	«IF isEnum(param.type) && isArray(param)»
		internalRequestObject.addParam(joynr::Util::convertListToVariantList(«paramRef»), "«getJoynrTypeName(param)»");
	«ELSEIF isEnum(param.type)»
		internalRequestObject.addParam(QVariant::fromValue(«paramRef»), "«getJoynrTypeName(param)»");
	«ELSEIF isArray(param)»
		QList «param.joynrName»QVarList = joynr::Util::convertListToVariantList(«paramRef»);
		internalRequestObject.addParam(QVariant::fromValue(«param.joynrName»QVarList), "«getJoynrTypeName(param)»");
	«ELSEIF isComplex(param.type)»
		internalRequestObject.addParam(QVariant::fromValue(«paramRef»), "«getJoynrTypeName(param)»");
	«ELSE»
		internalRequestObject.addParam(QVariant(«paramRef»), "«getJoynrTypeName(param)»");
	«ENDIF»
«ENDFOR»
'''

	override generate(FInterface serviceInterface)
'''
«val interfaceName = serviceInterface.joynrName»
«warning()»

#include "«getPackagePathWithJoynrPrefix(serviceInterface, "/")»/«interfaceName»JoynrMessagingConnector.h"
#include "joynr/ReplyCaller.h"
#include "joynr/JoynrMessageSender.h"
#include "joynr/joynrlogging.h"
#include "joynr/ISubscriptionManager.h"
#include "joynr/SubscriptionCallback.h"
#include "joynr/BroadcastSubscriptionRequest.h"
#include "joynr/Util.h"
#include "joynr/TypeUtil.h"
#include "joynr/SubscriptionStop.h"
#include "joynr/Future.h"
#include "joynr/RequestStatus.h"
#include "joynr/RequestStatusCode.h"

«FOR datatype: getAllComplexAndEnumTypes(serviceInterface)»
«IF datatype instanceof FType»
	«IF isComplex(datatype)»
		#include "«getIncludeOf(datatype)»"
	«ENDIF»
«ENDIF»
«ENDFOR»

«getNamespaceStarter(serviceInterface)»

«interfaceName»JoynrMessagingConnector::«interfaceName»JoynrMessagingConnector(
		joynr::IJoynrMessageSender* joynrMessageSender,
		joynr::ISubscriptionManager* subscriptionManager,
		const std::string &domain,
		const std::string proxyParticipantId,
		const std::string& providerParticipantId,
		const joynr::MessagingQos &qosSettings,
		joynr::IClientCache *cache,
		bool cached)
	: joynr::AbstractJoynrMessagingConnector(joynrMessageSender, subscriptionManager, domain, INTERFACE_NAME(), proxyParticipantId, providerParticipantId, qosSettings, cache, cached)
{
}

bool «interfaceName»JoynrMessagingConnector::usesClusterController() const{
	return joynr::AbstractJoynrMessagingConnector::usesClusterController();
}

«FOR attribute: getAttributes(serviceInterface)»
	«val returnTypeStd = cppStdTypeUtil.getTypeName(attribute)»
	«val returnTypeQT = qtTypeUtil.getTypeName(attribute)»
	«val attributeName = attribute.joynrName»
	«IF attribute.readable»
		joynr::RequestStatus «interfaceName»JoynrMessagingConnector::get«attributeName.toFirstUpper»(
				«returnTypeStd»& «attributeName»
		) {
			std::shared_ptr > future(new joynr::Future<«returnTypeStd»>());

			std::function onSuccess =
					[future] (const joynr::RequestStatus& status, const «returnTypeQT»& «attributeName») {
						if (status.getCode() == joynr::RequestStatusCode::OK) {
							future->onSuccess(«qtTypeUtil.fromQTTypeToStdType(attribute, attribute.joynrName)»);
						} else {
							future->onError(status);
						}
					};

			std::function onError =
					[future] (const joynr::RequestStatus& status) {
						future->onError(status);
					};

			QSharedPointer replyCaller(new joynr::ReplyCaller<«returnTypeQT»>(
					onSuccess,
					onError));
			attributeRequest<«returnTypeQT»>(QString("get«attributeName.toFirstUpper»"), replyCaller);
			joynr::RequestStatus status(future->waitForFinished());
			if (status.successful()) {
				future->getValues(«attributeName»);
			}
			return status;
		}

		std::shared_ptr> «interfaceName»JoynrMessagingConnector::get«attributeName.toFirstUpper»Async(
				std::function onSuccess,
				std::function onError
		) {
			std::shared_ptr > future(new joynr::Future<«returnTypeStd»>());

			std::function onSuccessWrapper =
					[future, onSuccess, onError] (const joynr::RequestStatus& status, const «returnTypeQT»& «attributeName») {
						if (status.getCode() == joynr::RequestStatusCode::OK) {
							future->onSuccess(«qtTypeUtil.fromQTTypeToStdType(attribute, attribute.joynrName)»);
							if (onSuccess){
								onSuccess(«qtTypeUtil.fromQTTypeToStdType(attribute, attribute.joynrName)»);
							}
						} else {
							future->onError(status);
							if (onError){
								onError(status);
							}
						}
					};

			std::function onErrorWrapper =
					[future, onError] (const joynr::RequestStatus& status) {
						future->onError(status);
						if (onError){
							onError(status);
						}
					};

			QSharedPointer replyCaller(new joynr::ReplyCaller<«returnTypeQT»>(
					onSuccessWrapper,
					onErrorWrapper));
			attributeRequest<«returnTypeQT»>(QString("get«attributeName.toFirstUpper»"), replyCaller);

			return future;
		}

	«ENDIF»
	«IF attribute.writable»
		std::shared_ptr> «interfaceName»JoynrMessagingConnector::set«attributeName.toFirstUpper»Async(
				«returnTypeStd» «attributeName»,
				std::function onSuccess,
				std::function onError
		) {
			joynr::Request internalRequestObject;
			internalRequestObject.setMethodName(QString("set«attributeName.toFirstUpper»"));
			«IF isArray(attribute)»
				QList «attributeName»QVarList = joynr::Util::convertListToVariantList(«qtTypeUtil.fromStdTypeToQTType(attribute, attributeName)»);
				internalRequestObject.addParam(QVariant::fromValue(«attributeName»QVarList), "«getJoynrTypeName(attribute)»");
			«ELSE»
				internalRequestObject.addParam(QVariant::fromValue(«qtTypeUtil.fromStdTypeToQTType(attribute, attributeName)»), "«getJoynrTypeName(attribute)»");
			«ENDIF»

			std::shared_ptr> future(new joynr::Future());

			std::function onSuccessWrapper =
					[future, onSuccess, onError] (const joynr::RequestStatus& status) {
						if (status.getCode() == joynr::RequestStatusCode::OK) {
							future->onSuccess();
							if (onSuccess) {
								onSuccess();
							}
						} else {
							future->onError(status);
							if (onError){
								onError(status);
							}
						}
					};

			std::function onErrorWrapper =
				[future, onError] (const joynr::RequestStatus& status) {
					future->onError(status);
					if (onError) {
						onError(status);
					}
				};

			QSharedPointer replyCaller(new joynr::ReplyCaller(
					onSuccessWrapper,
					onErrorWrapper));
			operationRequest(replyCaller, internalRequestObject);
			return future;
		}

		joynr::RequestStatus «interfaceName»JoynrMessagingConnector::set«attributeName.toFirstUpper»(
				const «returnTypeStd»& «attributeName»
		) {
			joynr::Request internalRequestObject;
			internalRequestObject.setMethodName(QString("set«attributeName.toFirstUpper»"));
			«IF isArray(attribute)»
				QList «attributeName»QVarList = joynr::Util::convertListToVariantList(«qtTypeUtil.fromStdTypeToQTType(attribute, attributeName)»);
				internalRequestObject.addParam(QVariant::fromValue(«attributeName»QVarList), "«getJoynrTypeName(attribute)»");
			«ELSE»
				internalRequestObject.addParam(QVariant::fromValue(«qtTypeUtil.fromStdTypeToQTType(attribute, attributeName)»), "«getJoynrTypeName(attribute)»");
			«ENDIF»

			QSharedPointer > future( new joynr::Future());

			std::function onSuccess =
					[future] (const joynr::RequestStatus& status) {
						if (status.getCode() == joynr::RequestStatusCode::OK) {
							future->onSuccess();
						} else {
							future->onError(status);
						}
					};

			QSharedPointer replyCaller(new joynr::ReplyCaller(
					onSuccess,
					std::bind(&joynr::Future::onError, future, std::placeholders::_1)));
			operationRequest(replyCaller, internalRequestObject);
			return future->waitForFinished();
		}

	«ENDIF»
	«IF attribute.notifiable»
		std::string «interfaceName»JoynrMessagingConnector::subscribeTo«attributeName.toFirstUpper»(
					std::shared_ptr > subscriptionListener,
					const joynr::SubscriptionQos& subscriptionQos
		) {
			joynr::SubscriptionRequest subscriptionRequest;
			return subscribeTo«attributeName.toFirstUpper»(subscriptionListener, subscriptionQos, subscriptionRequest);
		}

		std::string «interfaceName»JoynrMessagingConnector::subscribeTo«attributeName.toFirstUpper»(
					std::shared_ptr > subscriptionListener,
					const joynr::SubscriptionQos& subscriptionQos,
					std::string& subscriptionId
		) {

			joynr::SubscriptionRequest subscriptionRequest;
			subscriptionRequest.setSubscriptionId(QString::fromStdString(subscriptionId));
			return subscribeTo«attributeName.toFirstUpper»(subscriptionListener, subscriptionQos, subscriptionRequest);
		}

		std::string «interfaceName»JoynrMessagingConnector::subscribeTo«attributeName.toFirstUpper»(
					std::shared_ptr > subscriptionListener,
					const joynr::SubscriptionQos& subscriptionQos,
					SubscriptionRequest& subscriptionRequest
		) {
			LOG_DEBUG(logger, "Subscribing to «attributeName».");
			QString attributeName("«attributeName»");
			joynr::MessagingQos clonedMessagingQos(qosSettings);
			if (subscriptionQos.getExpiryDate() == joynr::SubscriptionQos::NO_EXPIRY_DATE()) {
				clonedMessagingQos.setTtl(joynr::SubscriptionQos::NO_EXPIRY_DATE_TTL());
			}
			else{
				clonedMessagingQos.setTtl(subscriptionQos.getExpiryDate() - QDateTime::currentMSecsSinceEpoch());
			}

			«val subscriptionListenerName = if (needsDatatypeConversion(attribute)) "subscriptionListenerWrapper" else "subscriptionListener"»
			«IF needsDatatypeConversion(attribute)»
				std::shared_ptr<«attribute.joynrName.toFirstUpper»AttributeSubscriptionListenerWrapper> subscriptionListenerWrapper(
					new «attribute.joynrName.toFirstUpper»AttributeSubscriptionListenerWrapper(subscriptionListener));
			«ENDIF»
			QSharedPointer> subscriptionCallback(new joynr::SubscriptionCallback<«returnTypeQT»>(«subscriptionListenerName»));
			subscriptionManager->registerSubscription(
						attributeName,
						subscriptionCallback,
						QSharedPointer(QtSubscriptionQos::createQt(subscriptionQos)),
						subscriptionRequest);
			LOG_DEBUG(logger, subscriptionRequest.toQString());
			joynrMessageSender->sendSubscriptionRequest(
						proxyParticipantId,
						providerParticipantId,
						clonedMessagingQos,
						subscriptionRequest
			);
			return subscriptionRequest.getSubscriptionId().toStdString();
		}

		void «interfaceName»JoynrMessagingConnector::unsubscribeFrom«attributeName.toFirstUpper»(
				std::string& subscriptionId
		) {
			joynr::SubscriptionStop subscriptionStop;
			subscriptionStop.setSubscriptionId(QString::fromStdString(subscriptionId));

			subscriptionManager->unregisterSubscription(QString::fromStdString(subscriptionId));
			joynrMessageSender->sendSubscriptionStop(
						proxyParticipantId,
						providerParticipantId,
						qosSettings,
						subscriptionStop
			);
		}

	«ENDIF»
«ENDFOR»

«FOR method: getMethods(serviceInterface)»
	«var outputTypedConstParamListQT = prependCommaIfNotEmpty(qtTypeUtil.getCommaSeperatedTypedConstOutputParameterList(method))»
	«var outputTypedConstParamListStd = cppStdTypeUtil.getCommaSeperatedTypedConstOutputParameterList(method)»
	«val outputTypedParamListStd = cppStdTypeUtil.getCommaSeperatedTypedOutputParameterList(method)»
	«val outputParametersStd = cppStdTypeUtil.getCommaSeparatedOutputParameterTypes(method)»
	«val outputParametersQT = qtTypeUtil.getCommaSeparatedOutputParameterTypes(method)»
	«var outputUntypedParamList = qtTypeUtil.getCommaSeperatedUntypedOutputParameterList(method, DatatypeSystemTransformation::FROM_QT_TO_STANDARD)»
	«val inputTypedParamListStd = cppStdTypeUtil.getCommaSeperatedTypedConstInputParameterList(method)»
	«val methodName = method.joynrName»
	joynr::RequestStatus «interfaceName»JoynrMessagingConnector::«methodName»(
		«outputTypedParamListStd»«IF method.outputParameters.size > 0 && method.inputParameters.size > 0», «ENDIF»«inputTypedParamListStd»
	) {
		«produceParameterSetters(method)»
		QSharedPointer > future(
				new joynr::Future<«outputParametersStd»>());

		std::function onSuccess =
				[future] (const joynr::RequestStatus& status«outputTypedConstParamListQT») {
					if (status.getCode() == joynr::RequestStatusCode::OK) {
						future->onSuccess(«outputUntypedParamList»);
					} else {
						future->onError(status);
					}
				};

		std::function onError =
			[future] (const joynr::RequestStatus& status) {
				future->onError(status);
			};

		QSharedPointer replyCaller(new joynr::ReplyCaller<«outputParametersQT»>(
				onSuccess,
				onError));
		operationRequest(replyCaller, internalRequestObject);
		«IF method.outputParameters.empty»
			return future->waitForFinished();
		«ELSE»
			joynr::RequestStatus status = future->waitForFinished();
			if (status.successful()) {
				future->getValues(«cppStdTypeUtil.getCommaSeperatedUntypedOutputParameterList(method)»);
			}
			return status;
		«ENDIF»
	}

	std::shared_ptr > «interfaceName»JoynrMessagingConnector::«methodName»Async(
			«cppStdTypeUtil.getCommaSeperatedTypedConstInputParameterList(method)»«IF !method.inputParameters.empty»,«ENDIF»
			std::function onSuccess,
			std::function onError
	)
	{
		«produceParameterSetters(method)»

		std::shared_ptr > future(
				new joynr::Future<«outputParametersStd»>());

		std::function onSuccessWrapper =
				[future, onSuccess, onError] (const joynr::RequestStatus& status«outputTypedConstParamListQT») {
					if (status.getCode() == joynr::RequestStatusCode::OK) {
						future->onSuccess(«outputUntypedParamList»);
						if (onSuccess) {
							onSuccess(«outputUntypedParamList»);
						}
					} else {
						future->onError(status);
						if (onError){
							onError(status);
						}
					}
				};

		std::function onErrorWrapper =
				[future, onError] (const joynr::RequestStatus& status) {
					future->onError(status);
					if (onError) {
						onError(status);
					}
				};

		QSharedPointer replyCaller(new joynr::ReplyCaller<«outputParametersQT»>(
				onSuccessWrapper,
				onErrorWrapper));
		operationRequest(replyCaller, internalRequestObject);
		return future;
	}

«ENDFOR»

«FOR broadcast: serviceInterface.broadcasts»
	«val returnTypes = cppStdTypeUtil.getCommaSeparatedOutputParameterTypes(broadcast)»
	«val returnTypesQt = qtTypeUtil.getCommaSeparatedOutputParameterTypes(broadcast)»
	«val broadcastName = broadcast.joynrName»
	«IF isSelective(broadcast)»
		std::string «interfaceName»JoynrMessagingConnector::subscribeTo«broadcastName.toFirstUpper»Broadcast(
					const «interfaceName.toFirstUpper»«broadcastName.toFirstUpper»BroadcastFilterParameters& filterParameters,
					std::shared_ptr > subscriptionListener,
					const joynr::OnChangeSubscriptionQos& subscriptionQos
	«ELSE»
		std::string «interfaceName»JoynrMessagingConnector::subscribeTo«broadcastName.toFirstUpper»Broadcast(
					std::shared_ptr > subscriptionListener,
					const joynr::OnChangeSubscriptionQos& subscriptionQos
	«ENDIF»
	) {
		joynr::BroadcastSubscriptionRequest subscriptionRequest;
		«IF isSelective(broadcast)»
			subscriptionRequest.setFilterParameters(QtBroadcastFilterParameters::createQt(filterParameters));
		«ENDIF»
		return subscribeTo«broadcastName.toFirstUpper»Broadcast(subscriptionListener, subscriptionQos, subscriptionRequest);
	}

	«IF isSelective(broadcast)»
		std::string «interfaceName»JoynrMessagingConnector::subscribeTo«broadcastName.toFirstUpper»Broadcast(
					const «interfaceName.toFirstUpper»«broadcastName.toFirstUpper»BroadcastFilterParameters& filterParameters,
					std::shared_ptr > subscriptionListener,
					const joynr::OnChangeSubscriptionQos& subscriptionQos,
					std::string& subscriptionId
	«ELSE»
		std::string «interfaceName»JoynrMessagingConnector::subscribeTo«broadcastName.toFirstUpper»Broadcast(
					std::shared_ptr > subscriptionListener,
					const joynr::OnChangeSubscriptionQos& subscriptionQos,
					std::string& subscriptionId
	«ENDIF»
	) {
		joynr::BroadcastSubscriptionRequest subscriptionRequest;
		«IF isSelective(broadcast)»
			subscriptionRequest.setFilterParameters(QtBroadcastFilterParameters::createQt(filterParameters));
		«ENDIF»
		subscriptionRequest.setSubscriptionId(QString::fromStdString(subscriptionId));
		return subscribeTo«broadcastName.toFirstUpper»Broadcast(subscriptionListener, subscriptionQos, subscriptionRequest);
	}

	std::string «interfaceName»JoynrMessagingConnector::subscribeTo«broadcastName.toFirstUpper»Broadcast(
				std::shared_ptr > subscriptionListener,
				const joynr::OnChangeSubscriptionQos& subscriptionQos,
				BroadcastSubscriptionRequest& subscriptionRequest
	) {
		LOG_DEBUG(logger, "Subscribing to «broadcastName» broadcast.");
		QString broadcastName("«broadcastName»");
		joynr::MessagingQos clonedMessagingQos(qosSettings);
		if (subscriptionQos.getExpiryDate() == joynr::SubscriptionQos::NO_EXPIRY_DATE()) {
			clonedMessagingQos.setTtl(joynr::SubscriptionQos::NO_EXPIRY_DATE_TTL());
		}
		else{
			clonedMessagingQos.setTtl(subscriptionQos.getExpiryDate() - QDateTime::currentMSecsSinceEpoch());
		}

		«val subscriptionListenerName = if (needsDatatypeConversion(broadcast)) "subscriptionListenerWrapper" else "subscriptionListener"»
		«IF needsDatatypeConversion(broadcast)»
			std::shared_ptr<«broadcast.joynrName.toFirstUpper»BroadcastSubscriptionListenerWrapper> subscriptionListenerWrapper(
				new «broadcast.joynrName.toFirstUpper»BroadcastSubscriptionListenerWrapper(subscriptionListener));
		«ENDIF»
		QSharedPointer> subscriptionCallback(
					new joynr::SubscriptionCallback<«returnTypesQt»>(«subscriptionListenerName»));
		subscriptionManager->registerSubscription(
					broadcastName,
					subscriptionCallback,
					QSharedPointer(QtSubscriptionQos::createQt(subscriptionQos)),
					subscriptionRequest);
		LOG_DEBUG(logger, subscriptionRequest.toQString());
		joynrMessageSender->sendBroadcastSubscriptionRequest(
					proxyParticipantId,
					providerParticipantId,
					clonedMessagingQos,
					subscriptionRequest
		);
		return subscriptionRequest.getSubscriptionId().toStdString();
	}

	void «interfaceName»JoynrMessagingConnector::unsubscribeFrom«broadcastName.toFirstUpper»Broadcast(
			std::string& subscriptionId
	) {
		joynr::SubscriptionStop subscriptionStop;
		subscriptionStop.setSubscriptionId(QString::fromStdString(subscriptionId));

		subscriptionManager->unregisterSubscription(QString::fromStdString(subscriptionId));
		joynrMessageSender->sendSubscriptionStop(
					proxyParticipantId,
					providerParticipantId,
					qosSettings,
					subscriptionStop
		);
	}

«ENDFOR»
«getNamespaceEnder(serviceInterface)»
'''
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy