File size: 4,280 Bytes
e4bf523
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
'use strict'
/**

 * Copyright (c) 2017~2022, OBCon Inc.

 * All rights reserved.

 */

/**

 * @file

 * @copyright 2017~2022, OBCon Inc.

 * @author gye hyun james kim [pnuskgh@gmail.com]

 */

// let cluster = require('cluster');
let EventEmitter = require('events');

class ModbusServerClient extends EventEmitter {
    constructor(server, client) {
        super();

        this._server = server;                              //--- ModbusServer
        this._client = client;                              //--- ModbusServerClient

        this._socket = this._client._socket;
        this._socket.on('connect', this.onConnect.bind(this));
        this._socket.on('data', this.onData.bind(this));
        this._socket.on('drain', this.onDrain.bind(this));
        this._socket.on('end', this.onEnd.bind(this));
        this._socket.on('error', this.onError.bind(this));
        this._socket.on('lookup', this.onLookup.bind(this));
        this._socket.on('close', this.onClose.bind(this));
        this._socket.on('ready', this.onReady.bind(this));
        this._socket.on('timeout', this.onTimeout.bind(this));
    }

    //----------------------------------------------------------------------------------------------
    //--- Server Socket 처리 함수
    //----------------------------------------------------------------------------------------------
    // on(event: 'connect', listener: () => void): this;
    onConnect() {
        console.log(`modules/ModbusServerClient.js (${this._client._guid}) :: onConnect`);
    }

    //-- on(event: 'data', listener: (data: Buffer) => void): this;
    onData(data) {
        console.log(`modules/ModbusServerClient.js (${this._client._guid}) :: onData(data), data : ${this._bufferToHex(data)}`);
        console.log(' ');
    }
   
    // on(event: 'drain', listener: () => void): this;
    onDrain() {
        console.log(`modules/ModbusServerClient.js (${this._client._guid}) :: onDrain`);
    }
        
    // on(event: 'end', listener: () => void): this;
    onEnd() {
        console.log(`modules/ModbusServerClient.js (${this._client._guid}) :: onEnd`);
    }
        
    //--- on(event: 'error', listener: (err: Error) => void): this;
    onError(err) {
        console.log(`modules/ModbusServerClient.js (${this._client._guid}) :: onError(err), err : ${JSON.stringify(err)}`);
        //--- Client에서 비정상 접속 종료시 : {"errno":-4077,"code":"ECONNRESET","syscall":"read"}  
    }

    //--- 도메인으로 IP을 찾는 경우에 호출됨
    // on(event: 'lookup', listener: (err: Error, address: string, family: string | number, host: string) => void): this;
    onLookup(err, address, family, host) {
        console.log(`modules/ModbusServerClient.js (${this._client._guid}) :: onLookup, err : ${JSON.stringify(err)}, address : ${address}, family : ${family}, host : ${host}`);   
    }
        
    //--- on(event: 'close', listener: (hadError: boolean) => void): this;
    onClose(hadError) {
        console.log(`modules/ModbusServerClient.js (${this._client._guid}) :: onClose(hadError), hadError : ${hadError}`);

        //--- To-Do : 현재의 ModbusServerClient를 완전 종료 한다.
        this._server._deleteClient(this._client._guid);
        // process.exit(1);
    }
        
    // on(event: 'ready', listener: () => void): this;
    onReady() {
        console.log(`modules/ModbusServerClient.js (${this._client._guid}) :: onReady`);
    }

    // on(event: 'timeout', listener: () => void): this;
    //--- Timeout 이벤트가 발생하면 안됨
    onTimeout() {
        console.log(`modules/ModbusServerClient.js (${this._client._guid}) :: onTimeout`);
    }

    //----------------------------------------------------------------------------------------------
    //--- 일반 함수
    //----------------------------------------------------------------------------------------------
    _bufferToHex(buf) {
        let hex = buf.toString('hex');
        let data = [];
        for (let idx = 0; idx < hex.length; idx = idx + 2) {
            data.push(hex.substring(idx, idx + 2));
        }
        return data.join(' ');
    }
}

module.exports = ModbusServerClient;