-
Notifications
You must be signed in to change notification settings - Fork 530
Expand file tree
/
Copy pathNetSimLogEntry.js
More file actions
240 lines (216 loc) · 6.45 KB
/
Copy pathNetSimLogEntry.js
File metadata and controls
240 lines (216 loc) · 6.45 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/**
* @overview Simulation entity for router log entries.
*/
var _ = require('lodash');
var moment = require('moment');
var i18n = require('@cdo/netsim/locale');
var utils = require('../utils'); // Provides Function.prototype.inherits
var DataConverters = require('./DataConverters');
var BITS_PER_BYTE = require('./NetSimConstants').BITS_PER_BYTE;
var NetSimEntity = require('./NetSimEntity');
var NetSimLogger = require('./NetSimLogger');
var NetSimNodeFactory = require('./NetSimNodeFactory');
var Packet = require('./Packet');
var formatBinary = DataConverters.formatBinary;
var base64ToBinary = DataConverters.base64ToBinary;
var binaryToBase64 = DataConverters.binaryToBase64;
var logger = NetSimLogger.getSingleton();
/**
* @typedef {Object} LogEntryRow
* @property {number} nodeID
* @property {Base64Payload} base64Binary - base64-encoded binary
* message content, all of which can be exposed to the
* student. May contain headers of its own.
* @property {NetSimLogEntry.LogStatus} status
* @property {number} timestamp
* @property {string} sentBy
*/
/**
* Entry in shared log for a node on the network.
*
* Once created, should not be modified until/unless a cleanup process
* removes it.
*
* @param {!NetSimShard} shard - The shard where this log entry lives.
* @param {LogEntryRow} [row] - A row out of the log table on the
* shard. If provided, will initialize this log with the given
* data. If not, this log will initialize to default values.
* @param {Packet.HeaderType[]} [packetSpec] - Packet layout spec used to
* interpret the contents of the logged packet
* @constructor
* @augments NetSimEntity
*/
var NetSimLogEntry = (module.exports = function (shard, row, packetSpec) {
row = row !== undefined ? row : {};
NetSimEntity.call(this, shard, row);
/**
* Node ID of the node that owns this log entry (e.g. a router node)
* @type {number}
*/
this.nodeID = row.nodeID;
/**
* Binary content of the log entry. Defaults to empty string.
* @type {string}
*/
this.binary = '';
if (row.base64Binary) {
try {
this.binary = base64ToBinary(
row.base64Binary.string,
row.base64Binary.len
);
} catch (e) {
logger.error(e.message);
}
}
/**
* Status value for log entry; for router log, usually SUCCESS for completion
* of routing or DROPPED if routing failed.
* @type {NetSimLogEntry.LogStatus}
*/
this.status = utils.valueOr(row.status, NetSimLogEntry.LogStatus.SUCCESS);
/**
* @type {Packet}
* @private
*/
this.packet_ = new Packet(utils.valueOr(packetSpec, []), this.binary);
/**
* Unix timestamp (local) of log creation time.
* @type {number}
*/
this.timestamp = row.timestamp !== undefined ? row.timestamp : Date.now();
/**
* Display name of the sender (for the teacher view)
* @type {string}
*/
this.sentBy = utils.valueOr(row.sentBy, '');
});
NetSimLogEntry.inherits(NetSimEntity);
/**
* @enum {string}
* @const
*/
NetSimLogEntry.LogStatus = {
SUCCESS: 'success',
DROPPED: 'dropped',
};
/**
* Helper that gets the log table for the configured instance.
* @returns {NetSimTable}
*/
NetSimLogEntry.prototype.getTable = function () {
return this.shard_.logTable;
};
/**
* Build own row for the log table
* @returns {LogEntryRow}
*/
NetSimLogEntry.prototype.buildRow = function () {
return {
nodeID: this.nodeID,
base64Binary: binaryToBase64(this.binary),
status: this.status,
timestamp: this.timestamp,
sentBy: this.sentBy,
};
};
/**
* Static async creation method. Creates a new message on the given shard,
* and then calls the callback with a success boolean.
* @param {!NetSimShard} shard
* @param {!number} nodeID - associated node's row ID
* @param {!string} binary - log contents
* @param {NetSimLogEntry.LogStatus} status
* @param {!string} sentBy - display name of sender
* @param {!NodeStyleCallback} onComplete (success)
*/
NetSimLogEntry.create = function (
shard,
nodeID,
binary,
status,
sentBy,
onComplete
) {
var entity = new NetSimLogEntry(shard);
entity.nodeID = nodeID;
entity.binary = binary;
entity.status = status;
entity.timestamp = Date.now();
entity.sentBy = sentBy;
entity.getTable().create(entity.buildRow(), function (err, result) {
if (err) {
onComplete(err, null);
return;
}
onComplete(err, new NetSimLogEntry(shard, result));
});
};
/**
* Get requested packet header field as a string. Returns empty string
* if the requested field is not in the current packet format.
* @param {Packet.HeaderType} field
* @returns {string}
*/
NetSimLogEntry.prototype.getHeaderField = function (field) {
try {
if (Packet.isAddressField(field)) {
return this.packet_.getHeaderAsAddressString(field);
} else {
return this.packet_.getHeaderAsInt(field).toString();
}
} catch (e) {
return '';
}
};
/** Get packet message as binary. */
NetSimLogEntry.prototype.getMessageBinary = function () {
return formatBinary(this.packet_.getBodyAsBinary(), BITS_PER_BYTE);
};
/** Get packet message as ASCII */
NetSimLogEntry.prototype.getMessageAscii = function () {
return this.packet_.getBodyAsAscii(BITS_PER_BYTE);
};
/**
* @returns {string} Localized packet status, "success" or "dropped"
*/
NetSimLogEntry.prototype.getLocalizedStatus = function () {
if (this.status === NetSimLogEntry.LogStatus.SUCCESS) {
return i18n.logStatus_success();
} else if (this.status === NetSimLogEntry.LogStatus.DROPPED) {
return i18n.logStatus_dropped();
}
return '';
};
/**
* @returns {string} Localized "X of Y" packet count info for this entry.
*/
NetSimLogEntry.prototype.getLocalizedPacketInfo = function () {
return i18n.xOfYPackets({
x: this.getHeaderField(Packet.HeaderType.PACKET_INDEX),
y: this.getHeaderField(Packet.HeaderType.PACKET_COUNT),
});
};
/**
* @returns {string} 12-hour time with milliseconds
*/
NetSimLogEntry.prototype.getTimeString = function () {
return moment(this.timestamp).format('h:mm:ss.SSS A');
};
/**
* Get a controller for the node that generated this log entry
* @returns {NetSimClientNode|NetSimRouterNode|null}
*/
NetSimLogEntry.prototype.getOriginNode = function () {
var nodeRows = this.shard_.nodeTable.readAll();
var originNodeRow = _.find(
nodeRows,
function (row) {
return row.id === this.nodeID;
}.bind(this)
);
if (!originNodeRow) {
return null;
}
return NetSimNodeFactory.nodeFromRow(this.shard_, originNodeRow);
};