1 | var eve = require('../../index');
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | function CalcAgent(id) {
|
10 |
|
11 | eve.Agent.call(this, id);
|
12 |
|
13 |
|
14 | this.extend('request');
|
15 |
|
16 |
|
17 | this.connect(eve.system.transports.getAll());
|
18 | }
|
19 |
|
20 |
|
21 | CalcAgent.prototype = Object.create(eve.Agent.prototype);
|
22 | CalcAgent.prototype.constructor = CalcAgent;
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | CalcAgent.prototype.receive = function(from, message) {
|
32 | if (typeof message === 'object' && 'fn' in message && 'a' in message && 'b' in message) {
|
33 | switch(message.fn) {
|
34 | case 'add': return message.a + message.b;
|
35 | case 'subtract': return message.a - message.b;
|
36 | case 'multiply': return message.a * message.b;
|
37 | case 'divide': return message.a / message.b;
|
38 | default:
|
39 | throw new Error('Unknown function "' + message.fn + '"');
|
40 | }
|
41 | }
|
42 | else {
|
43 | throw new Error('Object expected with properties fn, a, and b');
|
44 | }
|
45 | };
|
46 |
|
47 | module.exports = CalcAgent;
|