/
all-agents.js
215 lines (181 loc) · 4.95 KB
/
all-agents.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
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
"use strict";
var util = require("util");
var EventEmitter2 = require("../event-emitter");
var EventYoshi = require("eventyoshi");
var Agent = require("./agent");
var Target = require("./target");
var makeURLFromComponents = require("./url-builder");
/**
* @class AllAgents
* @constructor
* @inherits EventEmitter2
* @param {Hub} hub Hub associated with this object.
*/
function AllAgents(hub) {
this.hub = hub;
this.agents = {};
this.boundTargets = {};
EventEmitter2.call(this, {
verbose: true
});
}
util.inherits(AllAgents, EventEmitter2);
AllAgents.prototype.bindTarget = function (target) {
this.debug("bindTarget", target.id);
target.pipeLog(this);
target.on("complete", this.unbindTarget.bind(this, target));
this.boundTargets[target.id] = target;
};
AllAgents.prototype.unbindTarget = function (target) {
delete this.boundTargets[target.id];
};
/**
* Get all Agents marked as available.
*
* @method getAvailableAgents
* @return {Agent[]} Agents marked available.
*/
AllAgents.prototype.getAvailableAgents = function () {
return this.getAgents().filter(function (agent) {
return agent.available();
});
};
/**
* Create new Target objects for the given Agents
* grouped by their User-Agent string.
*
* Side-effect: This will make all given agents unavailable
* to prepare for binding to a Batch.
*
* @method createTargetsForAgents
* @return {Target[]} Targets.
*/
AllAgents.prototype.createTargetsForAgents = function (agents) {
var self = this,
agentsByUA = {},
targets = [];
agents.forEach(function (agent) {
if (!(agent.ua in agentsByUA)) {
agentsByUA[agent.ua] = [];
}
agentsByUA[agent.ua].push(agent);
});
Object.keys(agentsByUA).forEach(function (ua) {
var target,
agentsForUA = agentsByUA[ua];
target = new Target(self, agentsForUA);
self.bindTarget(target);
targets.push(target);
});
return targets;
};
/**
* Get all Agents.
*
* @method getAgents
* @return {Agent[]} Agents.
*/
AllAgents.prototype.getAgents = function () {
var out = [],
self = this;
Object.keys(self.agents).forEach(function (id) {
out.push(self.agents[id]);
});
return out;
};
/**
* Get an Agent by ID.
*
* @method getAgent
* @param {Number} id Agent ID.
* @return {Agent} The matching agent.
*/
AllAgents.prototype.getAgent = function (id) {
return this.agents[id];
};
AllAgents.prototype.removeAgent = function (id) {
var agent = this.agents[id];
if (agent) {
agent.unload();
delete this.agents[id];
}
return !!agent;
};
/**
* Append this Agent to an active Target,
* if available. This allows a new Agent to
* join a Batch that is already in progress.
*
* @method findTargetForNewAgent
* @private
* @param {Agent} agent Agent to add.
*/
AllAgents.prototype.findTargetForNewAgent = function (agent) {
var self = this,
ua = agent.ua,
boundTargets = this.boundTargets,
target;
Object.keys(boundTargets).forEach(function (targetId) {
target = boundTargets[targetId];
if (target.ua === ua) {
target.appendAgents([agent]);
}
});
};
/**
* Connect the given socket and UA string
* to the Agent instance identified by the
* given ID.
*
* @method connectAgent
* @param {Object} message Registration data.
* @param {String} message.agentId Agent ID.
* @param {String} [message.ua] User-Agent.
* @param {String} [message.originId] Origin ID.
* @param {SimpleEvents} socket Socket.
*/
AllAgents.prototype.connectAgent = function (message, socket) {
var self = this,
id = message.agentId,
ua = message.ua,
firstConnect = false,
agent = self.agents[id],
mountpoint = self.hub.mountpoint,
connectSuccess;
if (!id) {
throw new Error("ID required.");
} else if (!socket) {
throw new Error("Socket required.");
}
if (agent) {
connectSuccess = agent.connect(socket);
if (!connectSuccess) {
socket.emit("regenerateId");
}
} else if (ua) {
firstConnect = true;
agent = self.agents[id] = new Agent(self, {
id: id,
ua: ua
});
agent.once("disconnect", function () {
delete self.agents[agent.id];
self.emit("agentDisconnect", agent.getName());
});
agent.pipeLog(self);
agent.connect(socket);
self.findTargetForNewAgent(agent);
} else {
// First-time connection from an injected
// page instead of the capture page.
socket.emit("navigate", makeURLFromComponents(mountpoint, id));
return;
}
if (firstConnect) {
// XXX Serialize the agent to JSON.
self.emit("agentConnect", agent.getName());
self.emit("newAgent", agent);
}
self.emit("agentSeen", agent.getName());
};
module.exports = AllAgents;