1 |
|
2 |
|
3 |
|
4 | var net = require('net');
|
5 | var MessageHandler = require('./controlmessage-handler');
|
6 | var agentSetting = require("./agent-setting");
|
7 | var serverConfig = require("./NDCServerConfig");
|
8 | var util = require('./util');
|
9 | var NDConnectionManager = require('./NDConnectionManager')
|
10 |
|
11 | var socket_timeout;
|
12 | var client ;
|
13 | var controlMessageHandler;
|
14 | var retryOnClose=0
|
15 | var ndcHost=0;
|
16 | var ndcPort=0;
|
17 | var protocol;
|
18 |
|
19 | function clientConn() {}
|
20 |
|
21 | var closeConnListener = function(err) {
|
22 | try {
|
23 | agentSetting.isTestRunning = false;
|
24 | agentSetting.isToInstrument = false;
|
25 | clearInterval(agentSetting.reconnectTimer)
|
26 | agentSetting.reconnectTimer = undefined;
|
27 | util.logger.info(agentSetting.currentTestRun+" | Control connection, Received socket close event from Host : "
|
28 | ,ndcHost," ,Port : ",ndcPort," ,error : ", err);
|
29 | |
30 |
|
31 |
|
32 |
|
33 |
|
34 | connectToServer();
|
35 | }
|
36 | catch(err){util.logger.warn(agentSetting.currentTestRun+" | Error in retrying for control connection with Host : "
|
37 | ,ndcHost," ,Port : ",ndcPort," ,error : ", err);}
|
38 | }
|
39 |
|
40 | var connectConnListener = function() {
|
41 | try {
|
42 | retryOnClose = 0
|
43 | clearTimeout(socket_timeout);
|
44 |
|
45 | if(!controlMessageHandler) {
|
46 | controlMessageHandler = new MessageHandler(clientConn);
|
47 | controlMessageHandler.handleMessages();
|
48 | }
|
49 | util.logger.info(agentSetting.currentTestRun+" | Connection established with NDCollector : Socket[addr="+ndcHost+" ,Port : "+ndcPort + " ,Protocol : "+protocol+" ,localport :" +this.localPort );
|
50 | controlMessageHandler.setClientSocket(clientConn)
|
51 | controlMessageHandler.sendIntialMessages();
|
52 | serverConfig.updateLastConnInfo();
|
53 | }
|
54 | catch(err){util.logger.warn(agentSetting.currentTestRun+" | error" , err);}
|
55 | }
|
56 |
|
57 | function idleTimeOutCheck(){
|
58 | try{
|
59 | var self = this;
|
60 | self.setTimeout(60000);
|
61 | var idleTimeout = new Date().getTime();
|
62 | if(agentSetting.reconnectTimer == undefined){
|
63 | if(idleTimeout - agentSetting.lastHeartBeatReceived > agentSetting.ndcHeartBeatThreshold){
|
64 | util.logger.warn(agentSetting.currentTestRun + ' | Idle Socket Condition ,Hence Breaking the Connection')
|
65 | agentSetting.lastHeartBeatReceived = new Date().getTime();
|
66 | clientConn.destroy();
|
67 | NDConnectionManager.checkProtocolAndMakeConnection();
|
68 | }
|
69 | }
|
70 | }catch(e){
|
71 | util.logger.error(agentSetting.currentTestRun + ' | Error in IdleTimeOutCheck :',e)
|
72 | }
|
73 | }
|
74 |
|
75 | clientConn.connectToServer = function(){
|
76 | try {
|
77 | controlMessageHandler = undefined
|
78 | retryOnClose=0 , client= undefined
|
79 |
|
80 | client = new net.Socket();
|
81 |
|
82 | client.setTimeout(60000);
|
83 |
|
84 | client.on('error', function(err) {});
|
85 |
|
86 | client.on('end', function(err) {});
|
87 |
|
88 | client.on('close',closeConnListener)
|
89 |
|
90 | client.on('connect',connectConnListener)
|
91 |
|
92 | client.on('timeout' ,idleTimeOutCheck)
|
93 |
|
94 | clientConn._connect();
|
95 |
|
96 | }
|
97 | catch(err){util.logger.warn("error" , err);}
|
98 | };
|
99 |
|
100 |
|
101 | function connectToServer()
|
102 | {
|
103 | if(client.writable)
|
104 | return;
|
105 |
|
106 | if(socket_timeout)
|
107 | return;
|
108 |
|
109 | agentSetting.readSettingFile()
|
110 | if(!serverConfig.currentActiveServer)
|
111 | serverConfig.currentActiveServer = serverConfig.serverList[0] ? serverConfig.serverList[0] : serverConfig.defaultServer;
|
112 |
|
113 |
|
114 | socket_timeout = setTimeout(function () {
|
115 | try {
|
116 | socket_timeout = 0;
|
117 | if (serverConfig.serverList.length > 1) {
|
118 | if (retryOnClose >= serverConfig.retryCount) {
|
119 | retryOnClose = 0;
|
120 | serverConfig.switchNDCMode();
|
121 | }
|
122 | }
|
123 | util.logger.info(agentSetting.currentTestRun+" | Timer for retrying control connectoion expired. trying to connect with Host : "+ndcHost+" ,Port : "+ndcPort,
|
124 | "Type : ",serverConfig.currentActiveServer.type);
|
125 | clientConn._connect();
|
126 | }
|
127 | catch(err){util.logger.warn(agentSetting.currentTestRun+" | Error in retrying" + err);}
|
128 | },serverConfig.sleepInterval);
|
129 | }
|
130 |
|
131 |
|
132 | clientConn._connect = function()
|
133 | {
|
134 | try{
|
135 | ++retryOnClose
|
136 | if(!serverConfig.currentActiveServer) {
|
137 | serverConfig.currentActiveServer = serverConfig.serverList[0] ? serverConfig.serverList[0] : serverConfig.defaultServer;
|
138 | }
|
139 |
|
140 | var currProto = serverConfig.getCurrentActiveProtocol()
|
141 | ndcHost = serverConfig.currentActiveServer.ndcHost;
|
142 | ndcPort = currProto.port;
|
143 | protocol = currProto.protocol;
|
144 |
|
145 | if((serverConfig.currentActiveServer.ndcHost == undefined) || (currProto.port == undefined))
|
146 | connectToServer();
|
147 | else{
|
148 | client.connect(currProto.port,serverConfig.currentActiveServer.ndcHost)
|
149 | }
|
150 | }
|
151 | catch(err){
|
152 | util.logger.warn(agentSetting.currentTestRun+" | Error in making connection" + err);
|
153 | }
|
154 | };
|
155 |
|
156 | clientConn.destroy = function(){
|
157 | try {
|
158 | if (client) {
|
159 | agentSetting.isTestRunning = false;
|
160 | agentSetting.isToInstrument = false;
|
161 | clearInterval(agentSetting.reconnectTimer)
|
162 | agentSetting.reconnectTimer = undefined;
|
163 | client.removeAllListeners();
|
164 | client.destroy();
|
165 | client.end();
|
166 | client= undefined
|
167 | }
|
168 | }
|
169 | catch(err){
|
170 | util.logger.warn(agentSetting.currentTestRun+" | Error in Destroying the connection" + err);
|
171 | }
|
172 | }
|
173 |
|
174 | clientConn.getLocalHost = function(){
|
175 | if(!client)return
|
176 | return client.localAddress
|
177 | }
|
178 |
|
179 | clientConn.getControlSocket = function(){
|
180 | return client;
|
181 | }
|
182 |
|
183 | clientConn.handleDataEvent = function(cb){
|
184 | client.on('data',cb)
|
185 | }
|
186 |
|
187 | clientConn.write = function(data){
|
188 | try {
|
189 | if (!client || !data) {
|
190 | util.logger.error('Cant able to write, Not connected with NDC')
|
191 | return;
|
192 | }
|
193 |
|
194 | client.write(data)
|
195 |
|
196 | } catch(err){
|
197 | util.logger.warn(agentSetting.currentTestRun+" | Error in writing over connection" + err);
|
198 | }
|
199 | }
|
200 |
|
201 | module.exports = clientConn;
|