1 | var WebSocket = require('ws');
|
2 | var agent = require("./agent-setting");
|
3 | var DataMessageHandler = require("./datamessage-handler");
|
4 | var util = require('./util');
|
5 |
|
6 | var backlog = [];
|
7 | var thisInstance= undefined;
|
8 |
|
9 | function dataConnHandler(){
|
10 | thisInstance= this;
|
11 | this.client=null;
|
12 | this.timeout=null;
|
13 | this.dataMsgHandler=null;
|
14 | this.discardedFPLength = 0;
|
15 | this.istempDataConn = false;
|
16 | this.client;
|
17 | this.protocol;
|
18 | |
19 |
|
20 |
|
21 |
|
22 |
|
23 | this.ndcHost=0 ;this.ndcPort=7892;
|
24 | }
|
25 |
|
26 | var closeConnListener =function(err) {
|
27 | var self =thisInstance
|
28 | util.logger.warn(agent.currentTestRun+" | Data connection, Received socket close event from Host : "+self.ndcHost+" ,Port="+self.ndcPort +", Error : "+err);
|
29 | if(self.istempDataConn) {
|
30 | self.closeConnection();
|
31 | }
|
32 | else{
|
33 | if(this.client) {
|
34 | self.client.removeAllListeners();
|
35 | }
|
36 | self.connectToServer();
|
37 | }
|
38 | }
|
39 |
|
40 | var connectConnListener = function() {
|
41 | try {
|
42 | var self =thisInstance
|
43 | self.timeout = undefined;
|
44 | if(!self.istempDataConn){
|
45 | util.logger.info(agent.currentTestRun+" | Data Connection established with NDCollector : Socket[addr="+self.ndcHost+",port="+self.ndcPort + ",localport=" +self.getLocalPort()+"]" );
|
46 | self.dataMsgHandler = new DataMessageHandler(self);
|
47 | if( backlog.length ) {
|
48 | for(var i= 0, len= backlog.length; len>i; ++i)
|
49 | self.client.send(backlog[i]);
|
50 |
|
51 | backlog.length= 0;
|
52 | }
|
53 | }
|
54 | }
|
55 | catch(e){
|
56 | util.logger.warn(e);
|
57 | }
|
58 | }
|
59 |
|
60 |
|
61 | dataConnHandler.prototype.getLocalPort = function(){
|
62 | try {
|
63 | if(!this.client)return ;
|
64 | return this.client._socket.address() ? this.client._socket.address().port : undefined;
|
65 | }catch(err){util.logger.warn(err);}
|
66 | }
|
67 |
|
68 | dataConnHandler.prototype.createDataConn = function(server,istempDataConn,currProto){
|
69 | try {
|
70 | this.ndcHost = server.ndcHost;
|
71 | this.ndcPort = currProto.port;
|
72 | this.istempDataConn = istempDataConn;
|
73 | this.protocol = currProto.protocol
|
74 | var self = this;
|
75 | this._connect();
|
76 | }catch(err){util.logger.warn(err);}
|
77 | };
|
78 |
|
79 |
|
80 | dataConnHandler.prototype.connectToServer=function() {
|
81 | try {
|
82 | if (!agent.isTestRunning) {
|
83 | util.logger.warn(agent.currentTestRun + " | Test run is not running .")
|
84 | return;
|
85 | }
|
86 | var self = this;
|
87 | if (self.timeout)
|
88 | return;
|
89 |
|
90 | self.timeout = setTimeout(function () {
|
91 | try {
|
92 | self.timeout = undefined;
|
93 | self._connect();
|
94 | util.logger.warn(agent.currentTestRun + " | Timer for retrying Data connectoion expired. trying to connect with Host : " + self.ndcHost + " ,Port=" + self.ndcPort);
|
95 | } catch (e) {
|
96 | util.logger.warn(e);
|
97 | }
|
98 | }, 60000);
|
99 | }
|
100 | catch(e){
|
101 | util.logger.warn(e);
|
102 | }
|
103 | }
|
104 |
|
105 |
|
106 | dataConnHandler.prototype.closeConnection =function() {
|
107 | try {
|
108 | |
109 |
|
110 |
|
111 |
|
112 | util.logger.info(agent.currentTestRun + " | Closing the New Data connection .");
|
113 | if (this.client) {
|
114 | this.client.removeListener('close',closeConnListener)
|
115 | this.client.removeListener('open',connectConnListener);
|
116 | this.client.close();
|
117 |
|
118 |
|
119 | delete this.client;
|
120 | this.client = undefined;
|
121 | }
|
122 | this.ndcHost = 0;
|
123 | this.ndcPort = 7892;
|
124 | clearTimeout(this.timeout)
|
125 | this.timeout = null;
|
126 | this.istempDataConn = false;
|
127 | this.discardedFPLength = 0;
|
128 | delete this.dataMsgHandler;
|
129 | }
|
130 | catch(e){
|
131 | util.logger.warn(e);
|
132 | }
|
133 | }
|
134 |
|
135 | dataConnHandler.prototype._connect = function() {
|
136 | var self = this;
|
137 | if(!agent.isTestRunning && !self.istempDataConn) {
|
138 | util.logger.warn(agent.currentTestRun+" | Test is not running ,error in making data connection")
|
139 | return;
|
140 | }
|
141 | try {
|
142 | var url = this.protocol+'://'+self.ndcHost+':'+self.ndcPort+'/' ;
|
143 | var options={}
|
144 | if(this.protocol.toLowerCase() == 'wss')
|
145 | options.rejectUnauthorized = false
|
146 | this.client = new WebSocket(url,options);
|
147 |
|
148 | this.client.on('error', function(err) {});
|
149 | this.client.on('close',closeConnListener);
|
150 | this.client.on('open',connectConnListener);
|
151 | }
|
152 | catch(err) {
|
153 | util.logger.warn(agent.currentTestRun+" | Error in making data connection");
|
154 | }
|
155 | };
|
156 |
|
157 | dataConnHandler.prototype.write=function(data){
|
158 | try {
|
159 | if(!this.client ||!data || !data.length)return
|
160 | if(this.client.readyState === WebSocket.OPEN) {
|
161 | if(!this.istempDataConn) {
|
162 | if (this.client.bufferedAmount >= agent.ndDataBufferSize) {
|
163 | if (this.discardedFPLength % 1000 === 0) {
|
164 | util.logger.warn(agent.currentTestRun + " | Discarding Data conn data, Buffer size : ", this.client.bufferSize, " is greater then ndDataBufferSize");
|
165 | this.discardedFPLength = 0
|
166 | }
|
167 | ++this.discardedFPLength;
|
168 | return false
|
169 | }
|
170 | }
|
171 | this.client.send(data);
|
172 | }
|
173 | else{
|
174 | if (backlog.length <= 500)
|
175 | backlog.push(data);
|
176 |
|
177 | util.logger.warn(agent.currentTestRun+"| client not connected ...........")
|
178 | return
|
179 | }
|
180 | }
|
181 | catch(e){
|
182 | util.logger.warn(agent.currentTestRun+" | Error in sending data on socket in dataConnHandler ",e);
|
183 | }
|
184 | };
|
185 |
|
186 | module.exports = dataConnHandler; |
\ | No newline at end of file |