2 * Copyright 2014 IBM Corp.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 var util = require("util");
18 var EventEmitter = require("events").EventEmitter;
19 var clone = require("clone");
20 var when = require("when");
22 var flows = require("./flows");
23 var comms = require("../comms");
32 this.wires = n.wires||[];
35 util.inherits(Node,EventEmitter);
37 Node.prototype._on = Node.prototype.on;
39 Node.prototype.on = function(event,callback) {
41 if (event == "close") {
42 if (callback.length == 1) {
43 this.close = function() {
44 return when.promise(function(resolve) {
45 callback.call(node,function() {
51 this.close = callback;
54 this._on(event,callback);
58 Node.prototype.close = function() {
61 Node.prototype.send = function(msg) {
62 // instanceof doesn't work for some reason here
65 } else if (!util.isArray(msg)) {
68 for (var i=0;i<this.wires.length;i++) {
69 var wires = this.wires[i];
73 if (!util.isArray(msg[i])) {
76 //if (wires.length == 1) {
77 // // Single recipient, don't need to clone the message
78 // var node = flows.get(wires[0]);
80 // for (var k in msgs) {
86 // Multiple recipients, must send message copies
87 for (var j=0;j<wires.length;j++) {
88 var node = flows.get(wires[j]);
90 for (var k=0;k<msgs.length;k++) {
92 // Temporary fix for #97
93 // TODO: remove this http-node-specific fix somehow
117 Node.prototype.receive = function(msg) {
118 this.emit("input",msg);
121 function log_helper(self, level, msg) {
122 var o = {level:level, id:self.id, type:self.type, msg:msg};
129 Node.prototype.log = function(msg) {
130 log_helper(this, 'log', msg);
133 Node.prototype.warn = function(msg) {
134 log_helper(this, 'warn', msg);
137 Node.prototype.error = function(msg) {
138 log_helper(this, 'error', msg);
142 * status: { fill:"red|green", shape:"dot|ring", text:"blah" }
144 Node.prototype.status = function(status) {
145 comms.publish("status/"+this.id,status,true);
147 module.exports = Node;