UNPKG

6.21 kBJavaScriptView Raw
1var WebSocket = require('ws');
2var agent = require("./agent-setting");
3var AutoMessageMessageHandler = require("./autoSensorMessage-handler");
4var util = require('./util');
5var retrying=false;
6var backlog = [];
7var thisInstance= undefined;
8
9
10function autoSensorConnHandler(){
11 thisInstance=this;
12 this.client=null;
13 this.timeOut=null;
14 this.autoMsgHandler=null;
15 this.discardedFPLength =0;
16 this.protocol;
17 /*
18 * Adding NDCHost & port in constructor, so if control connection is getting close during running test
19 * then agent will switch over and will try to connect with backup NDC, but Data and Auto connection will connect with
20 * previous NDC, untill control connection with new server has been made
21 * */
22 this.ndcHost=0 ;this.ndcPort=7892;
23}
24
25var closeConnListener = function(err) {
26 var self=thisInstance;
27 util.logger.warn(agent.currentTestRun + " | AutoSensor connection, Received socket close event with Host : " + self.ndcHost + " ,Port=" + self.ndcPort + ", Error : " + err);
28 if(this.client) {
29 this.client.removeAllListeners();
30 }
31 self.connectToServer();
32}
33
34var connectConnListener = function() {
35 try {
36 var self=thisInstance;
37 self.timeOut=undefined;
38 util.logger.info(agent.currentTestRun+" | AS Connection established with NDCollector : Socket[addr="+self.ndcHost+",port="+self.ndcPort + ",localport=" +self.getLocalPort() );
39 self.autoMsgHandler = new AutoMessageMessageHandler(self);
40 if( backlog.length ) {
41 for(var i= 0, len= backlog.length; len>i; ++i)
42 self.client.send(backlog[i]);
43
44 backlog.length= 0;
45 }
46 }catch(err){util.logger.warn(agent.currentTestRun+" | Error occured in connectConnListener function : "+err);}
47}
48
49autoSensorConnHandler.prototype.getLocalPort = function(){
50 try {
51 if (!this.client)return;
52 return this.client._socket.address() ? this.client._socket.address().port : undefined;
53 } catch(err){util.logger.warn(err);}
54}
55
56autoSensorConnHandler.prototype.createAutoSensorConn = function(server,currProto){
57
58 try {
59 var self = this ;
60 self.ndcHost = server.ndcHost; //Setting new Host port, at time of new Object [connection] creation
61 self.ndcPort = currProto.port;
62 this.protocol = currProto.protocol
63 this._connect();
64 }
65 catch(e){
66 util.logger.warn(agent.currentTestRun+" | Error occured in createAutoSensorConn function : "+e);
67 }
68};
69
70autoSensorConnHandler.prototype.connectToServer = function()
71{
72 try {
73 if (!agent.isTestRunning)
74 return;
75
76 var self = this;
77
78 if (!self.client)
79 return;
80 if (self.timeOut)
81 return;
82 this.timeOut = setTimeout(function () {
83 try {
84 self.timeOut = undefined;
85 self._connect();
86 util.logger.info(agent.currentTestRun + " | Timer for retrying Auto Sensor connectoion expired. trying to connect with Host : " + self.ndcHost + " ,Port=" + self.ndcPort);
87 } catch (e) {
88 util.logger.warn(agent.currentTestRun + " | " + e);
89 }
90 }, 60000);
91 }
92 catch(e){
93 util.logger.warn(e);
94 }
95};
96
97autoSensorConnHandler.prototype.closeConnection =function() {
98 try {
99 /*
100 * 1. We are removing close listener befor closing connection because is close listener we are retrying for connection .
101 * 2. After closing connection , removing all liteners ,because in close's handshaking, server will send error, so agent should handle
102 * exceptions*/
103 util.logger.info(agent.currentTestRun + " | Closing the Auto_sensor connection .");
104 if (this.client != null) {
105 this.client.removeListener('close',closeConnListener)
106 this.client.removeListener('open',connectConnListener);
107 this.client.close();
108 //this.client.removeAllListeners();
109
110 delete this.client;
111 this.client = undefined;
112 }
113 this.ndcHost = 0;
114 this.ndcPort = 7892;
115 clearTimeout(this.timeOut) //If connection is closed forcefully , and timeout is running, then it will make another connection , because timeout=null will not stop the setTimeout loop
116 this.timeOut = null;
117 this.discardedFPLength = 0;
118 delete this.autoMsgHandler
119 }
120 catch(e){
121 util.logger.warn(e);
122 }
123}
124
125autoSensorConnHandler.prototype._connect = function()
126{
127 var self=this;
128 if(!agent.isTestRunning) {
129 return;
130 }
131 try {
132 var url = this.protocol+'://'+self.ndcHost+':'+self.ndcPort+'/' ;
133 var options={}
134 if(this.protocol.toLowerCase() == 'wss')
135 options.rejectUnauthorized = false
136 this.client = new WebSocket(url,options);
137
138 this.client.on('error', function(err) {});
139 this.client.on('close', closeConnListener);
140 this.client.on('open',connectConnListener);
141 }
142 catch(err) {
143 util.logger.warn(agent.currentTestRun+" | Error occured in _connect function : "+err);
144 }
145};
146
147autoSensorConnHandler.prototype.write=function(data){
148 try{
149 if(!this.client || !data || !data.length)return
150 if(this.client.bufferedAmount >= agent.ndASBufferSize) {
151 if(this.discardedFPLength % 1000 === 0) {
152 util.logger.warn(agent.currentTestRun + " | Discarding AutoSensor data, Buffer size : ", this.client.bufferSize, " is greater then ndASBufferSize");
153 this.discardedFPLength = 0
154 }
155 ++this.discardedFPLength;
156 return false
157 }
158 //if(this.client.writable) {
159 if(this.client.readyState === WebSocket.OPEN){
160 this.client.send(data)
161 }
162 else {
163 if(backlog.length <= 500 )
164 backlog.push(data)
165 }
166 }
167 catch(e){
168 util.logger.warn(e);
169 }
170};
171
172module.exports = autoSensorConnHandler;