1 | var WebSocket = require('ws');
|
2 | var agent = require("./agent-setting");
|
3 | var AutoMessageMessageHandler = require("./autoSensorMessage-handler");
|
4 | var util = require('./util');
|
5 | var retrying=false;
|
6 | var backlog = [];
|
7 | var thisInstance= undefined;
|
8 |
|
9 |
|
10 | function 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 |
|
19 |
|
20 |
|
21 |
|
22 | this.ndcHost=0 ;this.ndcPort=7892;
|
23 | }
|
24 |
|
25 | var 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 |
|
34 | var 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 |
|
49 | autoSensorConnHandler.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 |
|
56 | autoSensorConnHandler.prototype.createAutoSensorConn = function(server,currProto){
|
57 |
|
58 | try {
|
59 | var self = this ;
|
60 | self.ndcHost = server.ndcHost;
|
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 |
|
70 | autoSensorConnHandler.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 |
|
97 | autoSensorConnHandler.prototype.closeConnection =function() {
|
98 | try {
|
99 | |
100 |
|
101 |
|
102 |
|
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 |
|
109 |
|
110 | delete this.client;
|
111 | this.client = undefined;
|
112 | }
|
113 | this.ndcHost = 0;
|
114 | this.ndcPort = 7892;
|
115 | clearTimeout(this.timeOut)
|
116 | this.timeOut = null;
|
117 | this.discardedFPLength = 0;
|
118 | delete this.autoMsgHandler
|
119 | }
|
120 | catch(e){
|
121 | util.logger.warn(e);
|
122 | }
|
123 | }
|
124 |
|
125 | autoSensorConnHandler.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 |
|
147 | autoSensorConnHandler.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 |
|
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 |
|
172 | module.exports = autoSensorConnHandler;
|