UNPKG

1.54 kBJavaScriptView Raw
1var eve = require('../../index');
2
3/**
4 * DelayedAgent prototype.
5 * This agent uses the eve.system.timer. This timer can be configured to run
6 * in real time, discrete time, or hyper time.
7 * @param {String} id
8 * @constructor
9 * @extend eve.Agent
10 */
11function PlanningAgent(id) {
12 // execute super constructor
13 eve.Agent.call(this, id);
14
15 // connect to all transports configured by the system
16 this.connect(eve.system.transports.getAll());
17}
18
19// extend the eve.Agent prototype
20PlanningAgent.prototype = Object.create(eve.Agent.prototype);
21PlanningAgent.prototype.constructor = PlanningAgent;
22
23/**
24 * Send a greeting to an agent
25 * @param {String} to
26 * @param {number} delay Delay in milliseconds
27 */
28PlanningAgent.prototype.sayDelayedHello = function(to, delay) {
29 var time = eve.system.timer.getTime();
30 console.log(time.toISOString(), this.id, 'planned saying hello with delay of', delay, 'ms');
31
32 var me = this;
33 eve.system.timer.setTimeout(function () {
34 me.send(to, 'Hello ' + to + '!').catch(function (err) {
35 console.log(err)
36 })
37 }, delay)
38};
39
40/**
41 * Handle incoming greetings. This overloads the default receive,
42 * so we can't use HelloAgent.on(pattern, listener) anymore
43 * @param {String} from Id of the sender
44 * @param {*} message Received message, a JSON object (often a string)
45 */
46PlanningAgent.prototype.receive = function(from, message) {
47 var time = eve.system.timer.getTime();
48 console.log(time.toISOString(), from, 'said:', JSON.stringify(message));
49};
50
51module.exports = PlanningAgent;