
joynr.proxy.ProxyEvent.js Maven / Gradle / Ivy
/*
* #%L
* %%
* Copyright (C) 2011 - 2017 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.
* #L%
*/
const Promise = require("../../global/Promise");
const BroadcastFilterParameters = require("./BroadcastFilterParameters");
const SubscriptionUtil = require("../dispatching/subscription/util/SubscriptionUtil");
/**
* Checks if the given datatypes and values match the given broadcast parameters
*
* @name ProxyEvent#getNamedArguments
* @function
* @private
*
* @param {Array}
* unnamedBroadcastValues an array containing the unnamedBroadcastValues, e.g. [1234, "asdf"]
* @param {?}
* unnamedBroadcastValues.array the broadcast value
* @param {Array}
* broadcastParameter an array of supported parametes
* @returns undefined if unnamedBroadcastValues does not match broadcastSignature
*/
function getNamedParameters(unnamedBroadcastValues, broadcastParameter) {
const namedParameters = {};
// check if number of given parameters matches number
// of parameters in broadcast signature (keys.length)
if (unnamedBroadcastValues.length !== broadcastParameter.length) {
return undefined;
}
// cycle over all parameters
for (let i = 0; i < unnamedBroadcastValues.length; ++i) {
const parameter = broadcastParameter[i];
namedParameters[parameter.name] = unnamedBroadcastValues[i];
}
return namedParameters;
}
/**
* Constructor of ProxyEvent object that is used in the generation of proxy objects
*
* @constructor
* @name ProxyEvent
* @param {Object}
* parent is the proxy object that contains this attribute
* @param {Object}
* settings the settings for this broadcast proxy
* @param {String}
* settings.fromParticipantId of the proxy itself
* @param {String}
* settings.toParticipantId of the provider being addressed
* @param {DiscoveryQos}
* settings.discoveryQos the Quality of Service parameters for arbitration
* @param {MessagingQos}
* settings.messagingQos the Quality of Service parameters for messaging
* @param {Object}
* settings.dependencies the dependencies object for this function call
* @param {SubscriptionManager}
* settings.dependencies.subscriptionManager
* @param {Boolean}
* settings.selective true if the broadcast is selective
* @param {String}
* settings.broadcastName the name of the broadcast as modelled in Franca
* @param {String[]}
* settings.broadcastParameter the parameter meta information of the broadcast being subscribed to
* @param {Object}
* settings.filterParameters the filter parameters of the broadcast
* @returns {ProxyEvent}
*/
function ProxyEvent(parent, settings) {
if (!(this instanceof ProxyEvent)) {
// in case someone calls constructor without new keyword (e.g. var c =
// Constructor({..}))
return new ProxyEvent(parent, settings);
}
this._settings = settings;
this._parent = parent;
}
/**
* @name ProxyEvent#subscribe
* @function
* @param {Object}
* subscribeParameters the settings object for this function call
* @param {SubscriptionQos}
* subscribeParameters.subscriptionQos the subscription quality of service object
* @param {String}
* [subscribeParameters.subscriptionId] optional subscriptionId. Used to refresh or
* reinstate an existing subscription.
* @param {String[]}
* [subscribeParameters.partitions] optional parameter for multicast subscriptions.
* This parameter becomes relevant for non selective broadcasts and specifies the interested partitions
* of publications. It is interpreted hierarchically.
* @param {Function}
* subscribeParameters.onReceive this function is called when an event as been
* received. method signature: "void onReceive({?}value)"
* @param {Function}
* subscribeParameters.onError this function is called when an error occurs with
* a subscribed event. method signature: "void onError({Error} error)"
* @param {Function}
* subscribeParameters.onSubscribed the callback to inform once the subscription request has
* been delivered successfully
* @returns {Object} returns a promise that is resolved with the subscriptionId, which is to
* be used to unsubscribe from this subscription later.
* @throws {Error} if subscribeParameters.partitions contains invalid characters
*/
ProxyEvent.prototype.subscribe = function subscribe(subscribeParameters) {
SubscriptionUtil.validatePartitions(subscribeParameters.partitions);
if (subscribeParameters.filterParameters !== undefined && subscribeParameters.filterParameters !== null) {
const checkResult = SubscriptionUtil.checkFilterParameters(
this._settings.filterParameters,
subscribeParameters.filterParameters.filterParameters,
this._settings.broadcastName
);
if (checkResult.caughtErrors.length !== 0) {
const errorMessage = JSON.stringify(checkResult.caughtErrors);
return Promise.reject(
new Error(
'SubscriptionRequest could not be processed, as the filterParameters "' +
JSON.stringify(subscribeParameters.filterParameters) +
'" are wrong: ' +
errorMessage
)
);
}
}
const that = this;
return this._settings.dependencies.subscriptionManager.registerBroadcastSubscription({
proxyId: this._parent.proxyParticipantId,
providerDiscoveryEntry: this._parent.providerDiscoveryEntry,
broadcastName: this._settings.broadcastName,
broadcastParameter: this._settings.broadcastParameter,
subscriptionQos: subscribeParameters.subscriptionQos,
subscriptionId: subscribeParameters.subscriptionId,
onReceive(response) {
subscribeParameters.onReceive(getNamedParameters(response, that._settings.broadcastParameter));
},
selective: this._settings.selective,
partitions: subscribeParameters.partitions || [],
onError: subscribeParameters.onError,
onSubscribed: subscribeParameters.onSubscribed,
filterParameters: subscribeParameters.filterParameters
});
};
ProxyEvent.prototype.createFilterParameters = function createFilterParameters() {
return new BroadcastFilterParameters(this._settings.filterParameters);
};
/**
* @name ProxyEvent#unsubscribe
* @function
* @param {Object}
* unsubscribeParameters the settings object for this function call
* @param {Object}
* unsubscribeParameters.subscriptionId the subscription token retrieved from the
* subscribe function
* @returns {Object} returns a promise that is resolved when unsubscribe has been executed.
* @see ProxyEvent#subscribe
*/
ProxyEvent.prototype.unsubscribe = function unsubscribe(unsubscribeParameters) {
return this._settings.dependencies.subscriptionManager.unregisterSubscription({
messagingQos: this._settings.messagingQos,
subscriptionId: unsubscribeParameters.subscriptionId
});
};
module.exports = ProxyEvent;
© 2015 - 2025 Weber Informatics LLC | Privacy Policy