UNPKG

7.18 kBJavaScriptView Raw
1/**
2 * Created by bala on 10/7/15.
3 */
4var net = require('net');
5var MessageHandler = require('./controlmessage-handler');
6var agentSetting = require("./agent-setting");
7var serverConfig = require("./NDCServerConfig");
8var util = require('./util');
9var NDConnectionManager = require('./NDConnectionManager')
10
11var socket_timeout;
12var client ;
13var controlMessageHandler;
14var retryOnClose=0 // Counter that will store retry count for particular Server
15var ndcHost=0;
16var ndcPort=0;
17var protocol;
18
19function clientConn() {}
20
21var closeConnListener = function(err) {
22 try {
23 agentSetting.isTestRunning = false;
24 agentSetting.isToInstrument = false;
25 clearInterval(agentSetting.reconnectTimer) //Clearing reconnect timer interval
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 /*if(serverConfig.currentActiveServer.connectWithoutWait){
30 serverConfig.currentActiveServer.connectWithoutWait = false;
31 clientConn._connect();
32 }
33 else*/
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
40var connectConnListener = function() {
41 try {
42 retryOnClose = 0 //After connecting , reseting value of reconnect for particular server to =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();//Sending intial message i.e control connection
52 serverConfig.updateLastConnInfo();
53 }
54 catch(err){util.logger.warn(agentSetting.currentTestRun+" | error" , err);}
55}
56
57function 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
75clientConn.connectToServer = function(){
76 try {
77 controlMessageHandler = undefined
78 retryOnClose=0 , client= undefined
79
80 client = new net.Socket();
81
82 client.setTimeout(60000); // setting 1 min. inactivity on the socket.
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
101function connectToServer()
102{
103 if(client.writable)
104 return;
105
106 if(socket_timeout)
107 return;
108
109 agentSetting.readSettingFile()
110 if(!serverConfig.currentActiveServer) // First time current server refrence is null , so pointing it to 0th index of server list
111 serverConfig.currentActiveServer = serverConfig.serverList[0] ? serverConfig.serverList[0] : serverConfig.defaultServer;
112
113 //Invoking retry timer, to connect again with NDC
114 socket_timeout = setTimeout(function () {
115 try {
116 socket_timeout = 0;
117 if (serverConfig.serverList.length > 1) {
118 if (retryOnClose >= serverConfig.retryCount) { //Switich current server, if retrying is greater then retry count
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
132clientConn._connect = function()
133{
134 try{
135 ++retryOnClose
136 if(!serverConfig.currentActiveServer) { // First time current server refrence is null , so pointing it to 0th index of server list
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
156clientConn.destroy = function(){
157 try {
158 if (client) {
159 agentSetting.isTestRunning = false;
160 agentSetting.isToInstrument = false;
161 clearInterval(agentSetting.reconnectTimer) //Clearing reconnect timer interval
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
174clientConn.getLocalHost = function(){
175 if(!client)return
176 return client.localAddress
177}
178
179clientConn.getControlSocket = function(){
180 return client;
181}
182
183clientConn.handleDataEvent = function(cb){
184 client.on('data',cb)
185}
186
187clientConn.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
201module.exports = clientConn;