-
Notifications
You must be signed in to change notification settings - Fork 530
Expand file tree
/
Copy pathNetSimClientNode.js
More file actions
129 lines (116 loc) · 3.77 KB
/
Copy pathNetSimClientNode.js
File metadata and controls
129 lines (116 loc) · 3.77 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
/**
* @overview Simulated client node.
*/
var _ = require('lodash');
var i18n = require('@cdo/netsim/locale');
var NodeType = require('./NetSimConstants').NodeType;
var NetSimEntity = require('./NetSimEntity');
var NetSimNode = require('./NetSimNode');
var NetSimWire = require('./NetSimWire');
require('../utils'); // Provides Function.prototype.inherits
/**
* Client model of simulated node
*
* Represents the client's view of a node that is controlled by a user client,
* either by our own client or somebody else's. Is a NetSimEntity, meaning
* it wraps a row in the node table and provides functionality around it.
*
* You may be looking for NetSimLocalClientNode if you're trying to manipulate
* your local client node.
*
* @param {!NetSimShard} shard
* @param {Object} [clientRow] - Lobby row for this router.
* @constructor
* @augments NetSimNode
*/
var NetSimClientNode = (module.exports = function (shard, clientRow) {
NetSimNode.call(this, shard, clientRow);
});
NetSimClientNode.inherits(NetSimNode);
/** @inheritdoc */
NetSimClientNode.prototype.getNodeType = function () {
return NodeType.CLIENT;
};
/** @inheritdoc */
NetSimClientNode.prototype.getStatus = function () {
var outgoingWire = this.getOutgoingWire();
if (!outgoingWire) {
return i18n.notConnected();
}
// Get remote node for display name / hostname
var cachedNodeRows = this.shard_.nodeTable.readAll();
var remoteNodeRow = _.find(cachedNodeRows, function (nodeRow) {
return nodeRow.id === outgoingWire.remoteNodeID;
});
var remoteNodeName = i18n.unknownNode();
if (remoteNodeRow) {
remoteNodeName = remoteNodeRow.name;
}
// Check for connection state
var mutualConnection;
if (remoteNodeRow && remoteNodeRow.type === NodeType.ROUTER) {
mutualConnection = true;
} else {
var cachedWireRows = this.shard_.wireTable.readAll();
mutualConnection = cachedWireRows.some(function (wireRow) {
return (
wireRow.localNodeID === outgoingWire.remoteNodeID &&
wireRow.remoteNodeID === outgoingWire.localNodeID
);
});
}
if (mutualConnection) {
return i18n.connectedToNodeName({nodeName: remoteNodeName});
}
return i18n.connectingToNodeName({nodeName: remoteNodeName});
};
/** @inheritdoc */
NetSimClientNode.prototype.isFull = function () {
var outgoingWire = this.getOutgoingWire();
if (!outgoingWire) {
return false;
}
var cachedWireRows = this.shard_.wireTable.readAll();
return cachedWireRows.some(function (wireRow) {
return (
wireRow.localNodeID === outgoingWire.remoteNodeID &&
wireRow.remoteNodeID === outgoingWire.localNodeID
);
});
};
/**
* Determine what address has been assigned to this client on its outgoing
* wire.
* @returns {string|undefined}
*/
NetSimClientNode.prototype.getAddress = function () {
var wire = this.getOutgoingWire();
if (!wire) {
return undefined;
}
return wire.localAddress;
};
/**
* Based on cached wire data, retrieve this node's outgoing wire.
* @returns {NetSimWire|null} null if wire does not exist.
*/
NetSimClientNode.prototype.getOutgoingWire = function () {
var cachedWireRows = this.shard_.wireTable.readAll();
var outgoingWireRow = _.find(cachedWireRows, wireRow => {
return wireRow.localNodeID === this.entityID;
});
if (outgoingWireRow) {
return new NetSimWire(this.shard_, outgoingWireRow);
}
return null;
};
/**
* Static async retrieval method. See NetSimEntity.get().
* @param {!number} nodeID - The row ID for the entity you'd like to find.
* @param {!NetSimShard} shard
* @param {!NodeStyleCallback} onComplete - Method that will be given the
* found entity, or null if entity search failed.
*/
NetSimClientNode.get = function (nodeID, shard, onComplete) {
NetSimEntity.get(NetSimClientNode, nodeID, shard, onComplete);
};