UNPKG

6.51 kBJavaScriptView Raw
1
2var net = require('net');
3var isToInstrument = false;
4
5var agent = require("./agent-setting");
6var DataMessageHandler = require("./datamessage-handler");
7var util = require('./util');
8
9var retrying=false;
10var backlog = [];
11var thisInstance= undefined;
12
13function dataConnHandler(istempConn){ //tempConn : Boolean Flag representing that Connection is Temporary Connection
14 thisInstance= this;
15 this.client=null;
16 this.timeout=null;
17 this.dataMsgHandler=null;
18 this.discardedFPLength = 0;
19 this.istempDataConn = false;
20 this.client = istempConn ? (new net.Socket({highWaterMark:1024*1024*15})) : new net.Socket();
21 /* *
22 * Adding NDCHost & port in constructor, so if control connection is getting close during running test
23 * then agent will switch over and will try to connect with backup NDC, but Data and Auto connection will connect with
24 * previous NDC, untill control connection with new server has been made
25 * */
26 this.ndcHost=0 ;this.ndcPort=7892;
27}
28
29var closeConnListener =function(err) {
30 var self =thisInstance
31 util.logger.warn(agent.currentTestRun+" | Data connection, Received socket close event from Host : "+self.ndcHost+" ,Port="+self.ndcPort +", Error : "+err);
32 if(self.istempDataConn) {
33 self.closeConnection();
34 }
35 else{
36 self.connectToServer();
37 }
38}
39
40var connectConnListener = function() {
41 try {
42 var self =thisInstance
43 //clearTimeout(self.timeout);
44 self.timeout = undefined;
45 if(!self.istempDataConn && self.client){ // if Temporary Connection is True , Then Dont send Data Connection Message
46 util.logger.info(agent.currentTestRun+" | Data Connection established with NDCollector : Socket[addr="+self.ndcHost+",port="+self.ndcPort + ",localport=" +this.localPort );
47 self.dataMsgHandler = new DataMessageHandler(self);
48 if( backlog.length )
49 {
50 for(var i= 0, len= backlog.length; len>i; ++i)
51 self.client.write(backlog[i]);
52
53 backlog.length= 0;
54 }
55 }
56 }
57 catch(e){
58 util.logger.warn(e);
59 }
60}
61
62dataConnHandler.prototype.createDataConn = function(server,istempDataConn,currProto,cbForHeapDump){
63 try {
64 this.ndcHost = server.ndcHost; //Setting new Host port, at time of new Object [connection] creation
65 this.ndcPort = currProto.port;
66 this.istempDataConn = istempDataConn; //Flag for New Data Connection.
67 var self = this;
68 // this.client = new net.Socket();
69 this.client.on('error', function(err) {
70 // util.logger.warn(agent.currentTestRun+" | Data connection], Received error event with retrying : ",err);
71 });
72/*
73 this.client.on('end', function(err) {
74 //util.logger.warn(agent.currentTestRun+" | Data connection], Received end event with retrying : "+retrying+", on socket- "+err);
75 });*/
76
77 this.client.on('close',closeConnListener);
78
79 this.client.on('connect',connectConnListener);
80 if(cbForHeapDump)this.client.on('connect',cbForHeapDump);
81
82 /*this.client.on("data", function (data) {
83 // console.log("data from ndc : " + data.toString());
84 });*/
85
86 this._connect();
87
88 }catch(err){util.logger.warn(err);}
89};
90
91
92dataConnHandler.prototype.connectToServer=function()
93{
94 try {
95 if (!agent.isTestRunning) {
96 util.logger.warn(agent.currentTestRun + " | Test run is not running .")
97 return;
98 }
99 var self = this;
100
101 if (!this.client)
102 return;
103
104 if (self.client && self.client.writable)
105 return;
106
107 if (self.timeout)
108 return;
109
110 self.timeout = setTimeout(function () {
111 try {
112 self.timeout = undefined;
113 self._connect();
114 util.logger.warn(agent.currentTestRun + " | Timer for retrying Data connectoion expired. trying to connect with Host : " + self.ndcHost + " ,Port=" + self.ndcPort);
115 } catch (e) {
116 util.logger.warn(e);
117 }
118 }, 60000);
119 }
120 catch(e){
121 util.logger.warn(e);
122 }
123}
124
125
126dataConnHandler.prototype.closeConnection =function()
127{
128 try {
129 util.logger.info(agent.currentTestRun + " | Closing the New Data connection .");
130 if (this.client) {
131 this.client.removeListener('close', closeConnListener);
132 this.client.removeListener('connect', connectConnListener);
133 this.client.destroy();
134 delete this.client;
135 this.client = undefined;
136 }
137 this.ndcHost = 0;
138 this.ndcPort = 7892;
139 this.timeout = null;
140 this.istempDataConn = false;
141 this.discardedFPLength = 0;
142 delete this.dataMsgHandler;
143 }
144 catch(e){
145 util.logger.warn(e);
146 }
147}
148
149dataConnHandler.prototype._connect = function()
150{
151 var self = this;
152 if(!agent.isTestRunning && !self.istempDataConn) {
153 util.logger.warn(agent.currentTestRun+" | Test is not running ,error in making data connection")
154 return;
155 }
156 if(!this.client)
157 return;
158
159 if(this.client.writable)
160 return
161 try {
162 this.client.connect(self.ndcPort, self.ndcHost);
163 }
164 catch(err) {
165 util.logger.warn(err);
166 }
167};
168
169dataConnHandler.prototype.write=function(data){
170 try {
171 if(!this.client ||!data || !data.length)return
172 if(this.client.writable) {
173 if(!this.istempDataConn) {
174 if (this.client.bufferSize >= agent.ndDataBufferSize) {
175 if (this.discardedFPLength % 1000 === 0) {
176 util.logger.warn(agent.currentTestRun + " | Discarding Data conn data, Buffer size : ", this.client.bufferSize, " is greater then ndDataBufferSize");
177 this.discardedFPLength = 0
178 }
179 ++this.discardedFPLength;
180 return false
181 }
182 }
183 }
184 if (this.client == undefined) {
185 if (backlog.length <= 500)
186 backlog.push(data);
187
188 return
189 util.logger.warn(agent.currentTestRun+"| client undefined ...........")
190 }
191
192 this.client.write(data);
193 }
194 catch(e){
195 util.logger.warn(e);
196 }
197
198};
199
200dataConnHandler.prototype.getSocket=function(){
201 return this.client;
202}
203
204module.exports = dataConnHandler;