|
16 | 16 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
17 | 17 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
18 | 18 |
|
19 |
| -const xt = require('./itoolkit'); |
20 |
| - |
21 |
| -const timeoutMsg = 'Timeout!'; |
22 |
| -const retryInterval = 100; // wait 0.1 second to retry to get result in sync mode. |
23 |
| -const retryTimes = Math.round(xt.timeout / retryInterval); |
| 19 | +const ProgramCall = require('./ProgramCall'); |
| 20 | +const { xmlToJson } = require('./utils'); |
24 | 21 |
|
25 | 22 | const IPToNum = (ip) => {
|
26 | 23 | const ipArray = ip.split('.');
|
27 |
| - let num = Number(ipArray[0]) * 256 * 256 * 256 + Number(ipArray[1]) |
28 |
| - * 256 * 256 + Number(ipArray[2]) * 256 + Number(ipArray[3]); |
29 |
| - num >>>= 0; // TODO: estlint no-bitwise |
| 24 | + let num = 0; |
| 25 | + |
| 26 | + /* eslint-disable no-bitwise */ |
| 27 | + num += parseInt(ipArray[0], 10) << 24; |
| 28 | + num += parseInt(ipArray[1], 10) << 16; |
| 29 | + num += parseInt(ipArray[2], 10) << 8; |
| 30 | + num += parseInt(ipArray[3], 10); |
| 31 | + num >>>= 0; // zero-fill right-shift, returns non-negative |
| 32 | + /* eslint-enable no-bitwise */ |
| 33 | + |
30 | 34 | return num;
|
31 | 35 | };
|
32 | 36 |
|
33 |
| -class iNetwork { |
| 37 | +module.exports = class iNetwork { |
34 | 38 | constructor(conn) {
|
35 | 39 | this.conn = conn; // Pass in the connection object.
|
36 | 40 | this.errno = [
|
@@ -77,19 +81,17 @@ class iNetwork {
|
77 | 81 | ['', '256A'], // [31]Domain search list
|
78 | 82 | ];
|
79 | 83 |
|
80 |
| - const pgm = new xt.iPgm('QTOCNETSTS', { lib: 'QSYS', func: 'QtocRtvTCPA' }); // eslint-disable-line new-cap |
| 84 | + const pgm = new ProgramCall('QTOCNETSTS', { lib: 'QSYS', func: 'QtocRtvTCPA' }); |
81 | 85 | pgm.addParam(outBuf, { io: 'out', len: 'rec1' });
|
82 | 86 | pgm.addParam(0, '10i0', { setlen: 'rec1' });
|
83 | 87 | pgm.addParam('TCPA0300', '8A');
|
84 | 88 | pgm.addParam(this.errno, { io: 'both', len: 'rec2' });
|
85 | 89 | this.conn.add(pgm.toXML());
|
86 | 90 |
|
87 |
| - const async = cb && typeof cb === 'function'; // If there is a callback function param, then it is in asynchronized mode. |
88 | 91 | let rtValue; // The returned value.
|
89 |
| - let stop = 0; // A flag indicating whether the process is finished. |
90 |
| - let retry = 0; // How many times we have retried. |
| 92 | + |
91 | 93 | const toJson = (str) => { // Convert the XML output into JSON
|
92 |
| - const output = xt.xmlToJson(str); |
| 94 | + const output = xmlToJson(str); |
93 | 95 | if (Object.prototype.hasOwnProperty.call(output[0], 'success') && output[0].success === true) {
|
94 | 96 | rtValue = {
|
95 | 97 | 'TCP/IPv4_stack_status': output[0].data[2].value,
|
@@ -124,31 +126,10 @@ class iNetwork {
|
124 | 126 | Domain_search_list: output[0].data[31].value,
|
125 | 127 | };
|
126 | 128 | } else { rtValue = str; }
|
127 |
| - if (async) { // If it is in asynchronized mode. |
128 |
| - cb(rtValue); // Run the call back function against the returned value. |
129 |
| - } |
130 |
| - stop = 1; |
131 |
| - }; |
132 |
| - |
133 |
| - const waitForResult = () => { |
134 |
| - retry += 1; |
135 |
| - if (stop === 0) { |
136 |
| - setTimeout(waitForResult, retryInterval); |
137 |
| - return null; |
138 |
| - } |
139 |
| - if (retry >= retryTimes) { |
140 |
| - return timeoutMsg; |
141 |
| - } |
142 |
| - |
143 |
| - return rtValue; |
| 129 | + cb(rtValue); // Run the call back function against the returned value. |
144 | 130 | };
|
145 | 131 |
|
146 |
| - this.conn.run(toJson, !async); // Post the input XML and get the response. |
147 |
| - if (!async) { // If it is in synchronized mode. |
148 |
| - return waitForResult(); // Run the user defined call back function against the returned value. |
149 |
| - } |
150 |
| - |
151 |
| - return null; // TODO: eslint consistent-return |
| 132 | + this.conn.run(toJson); // Post the input XML and get the response. |
152 | 133 | }
|
153 | 134 |
|
154 | 135 | getNetInterfaceData(ipaddr, cb) {
|
@@ -220,20 +201,18 @@ class iNetwork {
|
220 | 201 | [1, '10i0'],
|
221 | 202 | [IPToNum(address), '10i0'],
|
222 | 203 | ];
|
223 |
| - const pgm = new xt.iPgm('QTOCNETSTS', { lib: 'QSYS', func: 'QtocRtvNetIfcDta' }); // eslint-disable-line new-cap |
| 204 | + const pgm = new ProgramCall('QTOCNETSTS', { lib: 'QSYS', func: 'QtocRtvNetIfcDta' }); |
224 | 205 | pgm.addParam(outBuf, { io: 'out', len: 'rec1' });
|
225 | 206 | pgm.addParam(0, '10i0', { setlen: 'rec1' });
|
226 | 207 | pgm.addParam('NIFD0100', '8A');
|
227 | 208 | pgm.addParam(request);
|
228 | 209 | pgm.addParam(this.errno, { io: 'both', len: 'rec2' });
|
229 | 210 | this.conn.add(pgm.toXML());
|
230 | 211 |
|
231 |
| - const async = cb && typeof cb === 'function'; // If there is a callback function param, then it is in asynchronized mode. |
232 | 212 | let rtValue; // The returned value.
|
233 |
| - let stop = 0; // A flag indicating whether the process is finished. |
234 |
| - let retry = 0; // How many times we have retried. |
| 213 | + |
235 | 214 | const toJson = (str) => { // Convert the XML output into JSON
|
236 |
| - const output = xt.xmlToJson(str); |
| 215 | + const output = xmlToJson(str); |
237 | 216 | if (Object.prototype.hasOwnProperty.call(output[0], 'success') && output[0].success === true) {
|
238 | 217 | rtValue = {
|
239 | 218 | Internet_address: output[0].data[2].value,
|
@@ -284,32 +263,10 @@ class iNetwork {
|
284 | 263 | Preferred_interface_Internet_address_binary: output[0].data[58].value,
|
285 | 264 | };
|
286 | 265 | } else { rtValue = str; }
|
287 |
| - if (async) { // If it is in asynchronized mode. |
288 |
| - cb(rtValue); // Run the call back function against the returned value. |
289 |
| - } |
290 |
| - stop = 1; |
291 |
| - }; |
292 |
| - |
293 |
| - const waitForResult = () => { |
294 |
| - retry += 1; |
295 |
| - if (stop === 0) { |
296 |
| - setTimeout(waitForResult, retryInterval); |
297 |
| - return null; |
298 |
| - } |
299 |
| - if (retry >= retryTimes) { |
300 |
| - return timeoutMsg; |
301 |
| - } |
302 | 266 |
|
303 |
| - return rtValue; |
| 267 | + cb(rtValue); // Run the call back function against the returned value. |
304 | 268 | };
|
305 | 269 |
|
306 |
| - this.conn.run(toJson, !async); // Post the input XML and get the response. |
307 |
| - if (!async) { // If it is in synchronized mode. |
308 |
| - return waitForResult(); // Run the user defined call back function against the returned value. |
309 |
| - } |
310 |
| - |
311 |
| - return null; // TODO: eslint consistent-return |
| 270 | + this.conn.run(toJson); // Post the input XML and get the response. |
312 | 271 | }
|
313 |
| -} |
314 |
| - |
315 |
| -exports.iNetwork = iNetwork; |
| 272 | +}; |
0 commit comments