UNPKG

5.37 kBJavaScriptView Raw
1/**
2 * Created by Siddhant on 18-09-2015.
3 */
4
5var net = require('net');
6var agent = require("./agent-setting");
7var AutoMessageMessageHandler = require("./autoSensorMessage-handler");
8var util = require('./util');
9var backlog = [];
10var thisInstance= undefined;
11
12function autoSensorConnHandler(){
13 thisInstance=this;
14 this.client=null;
15 this.timeOut=null;
16 this.autoMsgHandler=null;
17 this.discardedFPLength =0;
18 /*
19 * Adding NDCHost & port in constructor, so if control connection is getting close during running test
20 * then agent will switch over and will try to connect with backup NDC, but Data and Auto connection will connect with
21 * previous NDC, untill control connection with new server has been made
22 * */
23 this.ndcHost=0 ;this.ndcPort=7892;
24}
25
26var closeConnListener = function(err) {
27 var self=thisInstance;
28 util.logger.warn(agent.currentTestRun + " | AutoSensor connection, Received socket close event with Host : " + self.ndcHost + " ,Port=" + self.ndcPort + ", Error : " + err);
29 self.connectToServer();
30}
31
32var connectConnListener = function() {
33 try {
34 //clearTimeout(self.timeOut);
35 var self=thisInstance;
36 self.timeOut=undefined;
37 util.logger.info(agent.currentTestRun+" | AS Connection established with NDCollector : Socket[addr="+self.ndcHost+",port="+self.ndcPort + ",localport=" +this.localPort );
38 self.autoMsgHandler = new AutoMessageMessageHandler(self);
39 if( backlog.length ) {
40 for(var i= 0, len= backlog.length; len>i; ++i)
41 self.client.write(backlog[i]);
42
43 backlog.length= 0;
44 }
45 }catch(err){util.logger.warn(agent.currentTestRun+" | "+err);}
46}
47
48autoSensorConnHandler.prototype.createAutoSensorConn = function(server,currProto){
49
50 try {
51 var self = this ;
52 self.ndcHost = server.ndcHost; //Setting new Host port, at time of new Object [connection] creation
53 self.ndcPort = currProto.port;
54
55 this.client = new net.Socket();
56
57 this.client.on('error', function(err) {
58 console.log('Error in au connection',err)
59 console.log(err.stack)
60 });
61
62 this.client.on('end', function(err) {});
63
64 this.client.on('close', closeConnListener);
65
66 this.client.on('connect',connectConnListener);
67
68 this._connect();
69 }
70 catch(e){
71 util.logger.warn(agent.currentTestRun+" | "+e);
72 }
73};
74
75autoSensorConnHandler.prototype.connectToServer = function()
76{
77 try {
78 if (!agent.isTestRunning)
79 return;
80
81 var self = this;
82
83 if (!self.client)
84 return;
85 if (self.client && self.client.writable)
86 return;
87
88 if (self.timeOut)
89 return;
90
91 this.timeOut = setTimeout(function () {
92 try {
93 self.timeOut = undefined;
94 self._connect();
95 util.logger.info(agent.currentTestRun + " | Timer for retrying Auto Sensor connectoion expired. trying to connect with Host : " + self.ndcHost + " ,Port=" + self.ndcPort);
96 } catch (e) {
97 util.logger.warn(agent.currentTestRun + " | " + e);
98 }
99 }, 60000);
100 }
101 catch(e){
102 util.logger.warn(e);
103 }
104};
105
106autoSensorConnHandler.prototype.closeConnection =function() {
107 try {
108 util.logger.info(agent.currentTestRun + " | Closing the Auto_sensor connection .");
109 if (this.client != null) {
110 this.client.removeAllListeners();
111
112 this.client.destroy();
113 delete this.client;
114 this.client = undefined;
115 }
116 this.ndcHost = 0;
117 this.ndcPort = 7892;
118 this.timeOut = null;
119 this.discardedFPLength = 0;
120 delete this.autoMsgHandler
121 }
122 catch(e){
123 util.logger.warn(e);
124 }
125}
126
127autoSensorConnHandler.prototype._connect = function()
128{
129 var self=this;
130 if(!agent.isTestRunning)
131 return;
132
133 if(!this.client)
134 return;
135 if(this.client.writable)
136 return
137 try {
138 this.client.connect(self.ndcPort, self.ndcHost);
139 }
140 catch(err) {
141 util.logger.warn(agent.currentTestRun+" | "+err);
142 }
143};
144
145autoSensorConnHandler.prototype.write=function(data){
146 try{
147
148 if(!this.client || !data || !data.length)return
149 if(this.client.bufferSize >= agent.ndASBufferSize) {
150 if(this.discardedFPLength % 1000 === 0) {
151 util.logger.warn(agent.currentTestRun + " | Discarding AutoSensor data, Buffer size : ", this.client.bufferSize, " is greater then ndASBufferSize");
152 this.discardedFPLength = 0
153 }
154 ++this.discardedFPLength;
155 return false
156 }
157 if(this.client.writable) {
158 this.client.write(data)
159 }
160 else
161 {
162 if(backlog.length <= 500 )
163 backlog.push(data)
164 }
165
166
167 /* if(this.client == null)
168 return ;
169
170 if(agent.autoSensorConnHandler)
171 this.client.write(data)
172 */
173 }
174 catch(e){
175 util.logger.warn(e);
176 }
177
178};
179
180module.exports = autoSensorConnHandler;
\No newline at end of file