joynr.messaging.routing.MessageQueue.js Maven / Gradle / Ivy
/*
* #%L
* %%
* 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.
* #L%
*/
/**
* The MessageQueue
is a joynr internal data structure. The Message Queue caches incoming messages, which cannot be shipped
* to the correct participant. Once a participant with the matching participantId is registered, the incoming message is forwarded to him
*/
define(
"joynr/messaging/routing/MessageQueue",
[
"joynr/system/LoggerFactory",
"joynr/system/DiagnosticTags",
"joynr/util/LongTimer",
"joynr/util/UtilInternal",
"joynr/messaging/JoynrMessage"
],
function(LoggerFactory, DiagnosticTags, LongTimer, Util, JoynrMessage) {
var defaultSettings;
/**
* MessageQueue caches incoming messages, and cached messages can be retrieved in case the destination participant becomes visible
*
* @constructor
* @name MessageQueue
*
* @param {Object}
* settings the settings object holding dependencies
* @param {Number}
* settings.maxQueueSizeInKBytes the maximum buffer size that shall be allocated by the message queue
*
* @classdesc The MessageQueue
is a joynr internal data structure. The Message Queue caches incoming messages, which cannot be shipped
* to the correct participant. Once a participant with the matching participantId is registered, the incoming message is forwarded to him
*/
function MessageQueue(settings) {
var log = LoggerFactory.getLogger("joynr/messaging/routing/MessageQueue");
var messageQueues = {};
var self = this;
Util.extend(self, defaultSettings, settings);
self.currentQueueSize = 0;
/**
* resets the queue
*
* @name MessageQueue#reset()
* @function
*
*/
self.reset = function reset() {
self.currentQueueSize = 0;
messageQueues = {};
};
function removeExpiredMessage(message) {
var index, queue;
queue = messageQueues[message.to];
if (queue === undefined) {
return;
}
index = queue.indexOf(message);
if (index !== -1) {
queue.splice(index, 1);
}
}
function removeMessageWhenTtlExpired(message) {
var ttl;
ttl = message.expiryDate - Date.now();
// some browsers do not support negative timeout times.
ttl = ttl > 0 ? ttl : 0;
LongTimer.setTimeout(function() {
removeExpiredMessage(message);
}, ttl);
}
/**
* @name MessageQueue#putMessage
* @function
*
* @param {JoynrMessage}
* joynrMessage
*/
self.putMessage =
function putMessage(message) {
// drop message if maximum queue size has been reached
if (message.payload !== undefined) {
var messageSize = Util.getLengthInBytes(message.payload);
if ((self.currentQueueSize + messageSize) <= (self.maxQueueSizeInKBytes * 1024)) {
self.currentQueueSize = self.currentQueueSize + messageSize;
if (messageQueues[message.to] === undefined) {
messageQueues[message.to] = [];
}
messageQueues[message.to].push(message);
removeMessageWhenTtlExpired(message);
} else {
log
.error(
"message cannot be added to message queue, as the queue buffer size has been exceeded",
DiagnosticTags.forJoynrMessage(message));
}
}
};
/**
* gets the queue messages for the participant
*
* @name MessageQueue#getAndRemoveMessages
* @function
*
* @param {String}
* participantId
*/
self.getAndRemoveMessages = function getAndRemoveMessages(participantId) {
var result = [];
if (messageQueues[participantId] !== undefined) {
result = messageQueues[participantId];
delete messageQueues[participantId];
}
return result;
};
}
MessageQueue.CHECK_TTL_ON_QUEUED_MESSAGES_INTERVAL_MS = 5000;
MessageQueue.DEFAULT_MAX_QUEUE_SIZE_IN_KBYTES = 10000;
defaultSettings = {
maxQueueSizeInKBytes : MessageQueue.DEFAULT_MAX_QUEUE_SIZE_IN_KBYTES
};
return MessageQueue;
});
© 2015 - 2025 Weber Informatics LLC | Privacy Policy