-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy patherrorclient.js
More file actions
95 lines (92 loc) · 3.38 KB
/
errorclient.js
File metadata and controls
95 lines (92 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var zmq = require('zmq');
var _ = require('underscore');
var protobufMessage = require('machinetalk-protobuf').message;
var Container = protobufMessage.Container;
var ContainerType = protobufMessage.ContainerType;
function ErrorClient(address) {
this.address = address;
this.socket = zmq.socket('sub');
this.socket.on('message', this._handleMessage.bind(this));
this.socket.on('connect', this.emit.bind(this, 'connect'));
this.socket.on('connect_delay', this.emit.bind(this, 'connect_delay'));
this.socket.on('connect_retry', this.emit.bind(this, 'connect_retry'));
this.socket.on('listen', this.emit.bind(this, 'listen'));
this.socket.on('bind_error', this.emit.bind(this, 'bind_error'));
this.socket.on('accept', this.emit.bind(this, 'accept'));
this.socket.on('accept_error', this.emit.bind(this, 'accept_error'));
this.socket.on('close', this.emit.bind(this, 'close'));
this.socket.on('close_error', this.emit.bind(this, 'close_error'));
this.socket.on('disconnect', this.emit.bind(this, 'disconnect'));
this.socket.on('monitor_error', this.emit.bind(this, 'monitor_error'));
}
util.inherits(ErrorClient, EventEmitter);
ErrorClient.prototype.connect = function() {
this.socket.monitor(1000, 0);
this.socket.connect(this.address);
this.socket.subscribe('error');
this.socket.subscribe('text');
this.socket.subscribe('display');
};
ErrorClient.prototype._handleMessage = function(topic, message) {
topic = topic && topic.toString();
message = Container.decode(message);
var msg = {
type: message.type,
topic: topic,
note: message.note
};
this.emit('message', msg);
switch(topic) {
case 'error': return this._handleError(msg);
case 'text': return this._handleText(msg);
case 'display': return this._handleDisplay(msg);
default: throw new Error('Invalid message topic: ' + topic);
}
};
ErrorClient.prototype._handleError = function(message) {
switch(message.type) {
case ContainerType.MT_EMC_OPERATOR_ERROR:
this.emit('message:error:operator', message);
break;
case ContainerType.MT_EMC_NML_ERROR:
this.emit('message:error:nml', message);
break;
case ContainerType.MT_PING:
return; // Ignore ping messages
default:
throw new Error('Invalid message type for topic ' + message.topic + ': ' + message.type);
}
this.emit('message:error', message);
};
ErrorClient.prototype._handleText = function(message) {
switch(message.type) {
case ContainerType.MT_EMC_NML_TEXT:
this.emit('message:text', message);
break;
case ContainerType.MT_PING:
return; // Ignore ping messages
default:
throw new Error('Invalid message type for topic ' + message.topic + ': ' + message.type);
}
};
ErrorClient.prototype._handleDisplay = function(message) {
switch(message.type) {
case ContainerType.MT_EMC_OPERATOR_DISPLAY:
this.emit('message:display:operator', message);
break;
case ContainerType.MT_EMC_NML_DISPLAY:
this.emit('message:display:nml', message);
break;
case ContainerType.MT_PING:
return; // Ignore ping messages
default:
throw new Error('Invalid message type for topic ' + message.topic + ': ' + message.type);
}
this.emit('message:display', message);
};
ErrorClient.prototype.close = function close() {
this.socket.close();
};
module.exports = ErrorClient;