-
Notifications
You must be signed in to change notification settings - Fork 55
/
protocol.js
86 lines (73 loc) · 2.6 KB
/
protocol.js
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
import ProtocolMessage from '../types/protocolmessage';
import Utils from '../util/utils';
import EventEmitter from '../util/eventemitter';
import Logger from '../util/logger';
import MessageQueue from './messagequeue';
import ErrorInfo from '../types/errorinfo';
var Protocol = (function() {
var actions = ProtocolMessage.Action;
function Protocol(transport) {
EventEmitter.call(this);
this.transport = transport;
this.messageQueue = new MessageQueue();
var self = this;
transport.on('ack', function(serial, count) { self.onAck(serial, count); });
transport.on('nack', function(serial, count, err) { self.onNack(serial, count, err); });
}
Utils.inherits(Protocol, EventEmitter);
Protocol.prototype.onAck = function(serial, count) {
Logger.logAction(Logger.LOG_MICRO, 'Protocol.onAck()', 'serial = ' + serial + '; count = ' + count);
this.messageQueue.completeMessages(serial, count);
};
Protocol.prototype.onNack = function(serial, count, err) {
Logger.logAction(Logger.LOG_ERROR, 'Protocol.onNack()', 'serial = ' + serial + '; count = ' + count + '; err = ' + Utils.inspectError(err));
if(!err) {
err = new ErrorInfo('Unable to send message; channel not responding', 50001, 500);
}
this.messageQueue.completeMessages(serial, count, err);
};
Protocol.prototype.onceIdle = function(listener) {
var messageQueue = this.messageQueue;
if(messageQueue.count() === 0) {
listener();
return;
}
messageQueue.once('idle', listener);
};
Protocol.prototype.send = function(pendingMessage) {
if(pendingMessage.ackRequired) {
this.messageQueue.push(pendingMessage);
}
if (Logger.shouldLog(Logger.LOG_MICRO)) {
Logger.logAction(Logger.LOG_MICRO, 'Protocol.send()', 'sending msg; ' + ProtocolMessage.stringify(pendingMessage.message));
}
pendingMessage.sendAttempted = true;
this.transport.send(pendingMessage.message);
};
Protocol.prototype.getTransport = function() {
return this.transport;
};
Protocol.prototype.getPendingMessages = function() {
return this.messageQueue.copyAll();
};
Protocol.prototype.clearPendingMessages = function() {
return this.messageQueue.clear();
};
Protocol.prototype.finish = function() {
var transport = this.transport;
this.onceIdle(function() {
transport.disconnect();
});
};
function PendingMessage(message, callback) {
this.message = message;
this.callback = callback;
this.merged = false;
var action = message.action;
this.sendAttempted = false;
this.ackRequired = (action == actions.MESSAGE || action == actions.PRESENCE);
}
Protocol.PendingMessage = PendingMessage;
return Protocol;
})();
export default Protocol;