1 |
|
2 | var net = require('net');
|
3 | var isToInstrument = false;
|
4 |
|
5 | var agent = require("./agent-setting");
|
6 | var DataMessageHandler = require("./datamessage-handler");
|
7 | var util = require('./util');
|
8 |
|
9 | var retrying=false;
|
10 | var backlog = [];
|
11 | var thisInstance= undefined;
|
12 |
|
13 | function dataConnHandler(istempConn){
|
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 |
|
23 |
|
24 |
|
25 |
|
26 | this.ndcHost=0 ;this.ndcPort=7892;
|
27 | }
|
28 |
|
29 | var 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 |
|
40 | var connectConnListener = function() {
|
41 | try {
|
42 | var self =thisInstance
|
43 |
|
44 | self.timeout = undefined;
|
45 | if(!self.istempDataConn && self.client){
|
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 |
|
62 | dataConnHandler.prototype.createDataConn = function(server,istempDataConn,currProto,cbForHeapDump){
|
63 | try {
|
64 | this.ndcHost = server.ndcHost;
|
65 | this.ndcPort = currProto.port;
|
66 | this.istempDataConn = istempDataConn;
|
67 | var self = this;
|
68 |
|
69 | this.client.on('error', function(err) {
|
70 |
|
71 | });
|
72 |
|
73 |
|
74 |
|
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 | |
83 |
|
84 |
|
85 |
|
86 | this._connect();
|
87 |
|
88 | }catch(err){util.logger.warn(err);}
|
89 | };
|
90 |
|
91 |
|
92 | dataConnHandler.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 |
|
126 | dataConnHandler.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 |
|
149 | dataConnHandler.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 |
|
169 | dataConnHandler.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 |
|
200 | dataConnHandler.prototype.getSocket=function(){
|
201 | return this.client;
|
202 | }
|
203 |
|
204 | module.exports = dataConnHandler;
|