UNPKG

5.02 kBJavaScriptView Raw
1/*
2 * Copyright 2015 Amadeus s.a.s.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16var crypto = require("crypto");
17var idNumber = 0;
18
19var createSlaveId = function () {
20 idNumber++;
21 return idNumber + "-" + new Buffer(crypto.pseudoRandomBytes(8)).toString("hex");
22};
23
24var SlaveController = module.exports = function (socket, data, testServer) {
25 this.socket = socket;
26 this.testServer = testServer;
27 this.slavesInfo = {};
28 this.onSocketMessage = this.onSocketMessage.bind(this);
29 this.onSocketDisconnect = this.onSocketDisconnect.bind(this);
30 socket.on('data', this.onSocketMessage);
31 socket.on('close', this.onSocketDisconnect);
32};
33
34SlaveController.prototype.onSocketMessage = function (message) {
35 try {
36 var data = JSON.parse(message);
37 var type = data.type;
38 var handler = this["onSocketMessage_" + type];
39 if (handler) {
40 handler.call(this, data);
41 }
42 } catch (e) {}
43};
44
45SlaveController.prototype.onSocketMessage_status = function () {
46 this.socket.write(JSON.stringify({
47 type: "status",
48 status: this.testServer.getStatus()
49 }));
50};
51
52SlaveController.prototype.onSocketMessage_slaveCreate = function () {
53 var slaveId = createSlaveId();
54 var slaveInfo = this.slavesInfo[slaveId] = {
55 slaveId: slaveId,
56 slave: null
57 };
58 slaveInfo.onSlaveConnect = this.onSlaveConnect.bind(this, slaveInfo);
59 slaveInfo.onSlaveDisconnect = this.onSlaveDisconnect.bind(this, slaveInfo);
60 slaveInfo.onSlaveIdle = this.onSlaveIdle.bind(this, slaveInfo);
61 slaveInfo.onSlaveBusy = this.onSlaveBusy.bind(this, slaveInfo);
62 this.testServer.on("slave-added-" + slaveId, slaveInfo.onSlaveConnect);
63 this.socket.write(JSON.stringify({
64 type: "slaveCreated",
65 slaveId: slaveId
66 }));
67};
68
69var removeSlave = function (slaveInfo) {
70 var slave = slaveInfo.slave;
71 if (slave) {
72 slaveInfo.slave = null;
73 slave.removeListener("disconnect", slaveInfo.onSlaveDisconnect);
74 slave.removeListener("idle", slaveInfo.onSlaveIdle);
75 slave.removeListener("busy", slaveInfo.onSlaveBusy);
76 slave.disconnect(); // makes sure the slave is disconnected
77 }
78};
79
80SlaveController.prototype.onSocketMessage_slaveDelete = function (data) {
81 this.deleteSlave(data.slaveId);
82};
83
84SlaveController.prototype.deleteSlave = function (slaveId) {
85 if (!this.slavesInfo.hasOwnProperty(slaveId)) {
86 return;
87 }
88 var slaveInfo = this.slavesInfo[slaveId];
89 delete this.slavesInfo[slaveId];
90 removeSlave(slaveInfo);
91 this.testServer.removeListener("slave-added-" + slaveId, slaveInfo.onSlaveConnect);
92 if (this.socket) {
93 this.socket.write(JSON.stringify({
94 type: "slaveDeleted",
95 slaveId: slaveId
96 }));
97 }
98};
99
100SlaveController.prototype.onSlaveConnect = function (slaveInfo, slave) {
101 removeSlave(slaveInfo);
102 slaveInfo.slave = slave;
103 slave.on("disconnect", slaveInfo.onSlaveDisconnect);
104 slave.on("busy", slaveInfo.onSlaveBusy);
105 slave.on("idle", slaveInfo.onSlaveIdle);
106 this.socket.write(JSON.stringify({
107 type: "slaveConnected",
108 slaveId: slaveInfo.slaveId,
109 address: slave.address,
110 port: slave.port,
111 displayName: slave.displayName,
112 userAgent: slave.userAgent,
113 campaignBrowsers: slave.matchingCampaignBrowsers.map(function (info) {
114 return {
115 campaign: info.campaign.id,
116 browser: info.browser.getJsonInfo()
117 };
118 })
119 }));
120};
121
122SlaveController.prototype.onSlaveDisconnect = function (slaveInfo) {
123 removeSlave(slaveInfo);
124 if (this.socket) {
125 this.socket.write(JSON.stringify({
126 type: "slaveDisconnected",
127 slaveId: slaveInfo.slaveId
128 }));
129 }
130};
131
132SlaveController.prototype.onSlaveIdle = function (slaveInfo) {
133 if (this.socket) {
134 this.socket.write(JSON.stringify({
135 type: "slaveIdle",
136 slaveId: slaveInfo.slaveId
137 }));
138 }
139};
140
141SlaveController.prototype.onSlaveBusy = function (slaveInfo) {
142 if (this.socket) {
143 this.socket.write(JSON.stringify({
144 type: "slaveBusy",
145 slaveId: slaveInfo.slaveId
146 }));
147 }
148};
149
150SlaveController.prototype.onSocketDisconnect = function () {
151 this.socket = null;
152 var slavesInfo = this.slavesInfo;
153 for (var slaveId in slavesInfo) {
154 this.deleteSlave(slaveId);
155 }
156 this.slavesInfo = null;
157};