-
Notifications
You must be signed in to change notification settings - Fork 403
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #682 from nklincoln/client-mods-mqtt
Enable alternative messaging between master and worker
- Loading branch information
Showing
67 changed files
with
1,708 additions
and
628 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
packages/caliper-core/lib/common/messaging/messenger-interface.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const Logger = require('../utils/caliper-utils').getLogger('messenger-base'); | ||
|
||
|
||
/** | ||
* Interface of messenger. Messenger implementations must follow a naming convention that is <type>-observer.js so | ||
* that they may be dynamically loaded in the WorkerOrchestrator and WorkerAdaptor | ||
*/ | ||
class MessengerInterface { | ||
|
||
/** | ||
* Set configuration details | ||
* @param {object} configuration configuration details for the messenger | ||
*/ | ||
constructor(configuration) { | ||
this.configuration = configuration; | ||
} | ||
|
||
/** | ||
* Initialize the Messenger | ||
* @async | ||
*/ | ||
async initialize() { | ||
this._throwNotImplementedError('initialize'); | ||
} | ||
|
||
/** | ||
* Configure the Messenger for use | ||
* @async | ||
*/ | ||
async configure() { | ||
this._throwNotImplementedError('configure'); | ||
} | ||
|
||
/** | ||
* Send a message using the messenger | ||
*/ | ||
send() { | ||
this._throwNotImplementedError('send'); | ||
} | ||
|
||
/** | ||
* Get the UUID for the messenger | ||
*/ | ||
getUUID() { | ||
this._throwNotImplementedError('getUUID'); | ||
} | ||
|
||
/** | ||
* Logs and throws a "not implemented" error for the given function. | ||
* @param {string} functionName The name of the function. | ||
* @private | ||
*/ | ||
_throwNotImplementedError(functionName) { | ||
let msg = `The function "${functionName}" is not implemented for this messenger`; | ||
Logger.error(msg); | ||
throw new Error(msg); | ||
} | ||
|
||
} | ||
|
||
module.exports = MessengerInterface; |
100 changes: 100 additions & 0 deletions
100
packages/caliper-core/lib/common/messaging/messenger.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* 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. | ||
*/ | ||
'use strict'; | ||
|
||
const CaliperUtils = require('..//utils/caliper-utils'); | ||
const Logger = CaliperUtils.getLogger('messenger.js'); | ||
|
||
const builtInMessengers = new Map([ | ||
['mqtt-master', './mqtt-master.js'], | ||
['mqtt-worker', './mqtt-worker.js'], | ||
['process-master', './process-master.js'], | ||
['process-worker', './process-worker.js'] | ||
]); | ||
|
||
const Messenger = class { | ||
|
||
/** | ||
* Instantiates the proxy messenger and creates the configured messenger behind it. | ||
* @param {object} configuration The messenger configuration object. | ||
*/ | ||
constructor(configuration) { | ||
this.configuration = configuration; | ||
|
||
Logger.info(`Creating messenger of type "${configuration.type}" ${configuration.sut ? ` for SUT ${configuration.sut}` : ''}`); | ||
|
||
// resolve the type to a module path | ||
let modulePath = builtInMessengers.has(configuration.type) | ||
? builtInMessengers.get(configuration.type) : CaliperUtils.resolvePath(configuration.type); // TODO: what if it's an external module name? | ||
|
||
let factoryFunction = require(modulePath).createMessenger; | ||
if (!factoryFunction) { | ||
throw new Error(`${configuration.type} does not export the mandatory factory function 'createMessenger'`); | ||
} | ||
|
||
this.messenger = factoryFunction(configuration); | ||
} | ||
|
||
/** | ||
* Initialize the Messenger | ||
* @async | ||
*/ | ||
async initialize() { | ||
await this.messenger.initialize(); | ||
} | ||
|
||
/** | ||
* Configure the Messenger for use | ||
* @param {object} configurationObject configuration object | ||
* @async | ||
*/ | ||
async configure(configurationObject) { | ||
await this.messenger.configure(configurationObject); | ||
} | ||
|
||
/** | ||
* Get the UUID for the messenger | ||
* @returns {string} the UUID of the messenger | ||
*/ | ||
getUUID() { | ||
return this.messenger.getUUID(); | ||
} | ||
|
||
/** | ||
* Method used to publish message to worker clients | ||
* @param {string[]} to string array of workers that the update is intended for | ||
* @param {*} type the string type of the update | ||
* @param {*} data data pertinent to the update type | ||
*/ | ||
send(to, type, data) { | ||
// Create Date object for timestamp | ||
const date = new Date(); | ||
|
||
// Augment data object with type | ||
data.type = type; | ||
|
||
// Create complete message | ||
const message = { | ||
to, | ||
from: this.messenger.getUUID(), | ||
timestamp: date.toISOString(), | ||
data | ||
}; | ||
|
||
this.messenger.send(message); | ||
} | ||
|
||
}; | ||
|
||
module.exports = Messenger; |
Oops, something went wrong.