

/*=============================================================================================
    author:ZhaoJun
    version:1.2
    email:2732810281@qq.com
=============================================================================================*/


//严格模式
/*-------------------------------------------------------------------------------------------*/	
'use strict';
/*-------------------------------------------------------------------------------------------*/
const {spawn} = require('child_process')

const packages=$Sys.model('/socket/package')
 
// 时间token初始操作 
/*-------------------------------------------------------------------------------------------*/	
    class stream
    {
        static cmd(req,path='/$cmd')
        {
            if(!req)
            {
                console.warn('[cmd流]req不合法',req)
                return
            }

            let tag='cmd'
            if(process.getegid)tag='/bin/bash'

            let free = spawn(tag,[]); 
            

            free.stdout.on
            (
                'data', (data) => 
                { 
                    data=$Sys.model('/tool/com/gbk','decode',[data])
                    let param={path,param:data}
                    if(req.writable)req.json(param) 
                }
            );
            
            
            // 捕获标准错误输出并将其打印到控制台 
            free.stderr.on
            (
                'data', (data) =>
                { 
                    data=$Sys.model('/tool/com/gbk','decode',[data])
                    if(req.writable)req.push(data)  
                }
            )
            
            
            // 注册子进程关闭事件 
            free.on
            (
                'close', function (code, signal) 
                { 
                    if(req.writable)req.close()
                    console.warn('[cmd-源]关闭',signal)
                }
            )
                
            packages.reset.call(req)

            req.on
            (
                'data',
                (data)=>
                {
                    
                    let list=packages.decodeWs.call(req,data)
                    if(!list)return
					

                    for (let index = 0; index < list.length; index++) 
                    {
                        data=list[index]
                        console.white('[交换流-目标]收到数据\r\n',data)
                    
                        data=$Sys.model('/tool/com/gbk','encode',[data])
                        free.stdin.write(data)
                    }


                }
            )
            req.on
            (
                'error',
                (error)=>
                {
                    console.error('[交换流-目标]发生错误',error)
                }
            )
            req.on
            (
                'close',
                (error)=>
                {
                    console.warn('[交换流-目标]关闭',error)
                    free.kill()
                }
            )

        }
        
        static swap({src,dst})
        {
            src.on
            (
                'data',
                (data)=>
                {
                    if(dst.writable)dst.write(data)
                }
            )
            src.on
            (
                'error',
                (error)=>
                {
                    console.error('[交换流-源]发生错误',error)
                }
            )
            src.on
            (
                'close',
                ()=>
                {
                    console.warn('[交换流-源]关闭',error)
                    if(dst.writable)dst.close()
                }
            )

            dst.on
            (
                'data',
                (data)=>
                {
                    src.write(data)
                }
            )
            dst.on
            (
                'error',
                (error)=>
                {
                    console.error('[交换流-目标]发生错误',error)
                }
            )
            dst.on
            (
                'close',
                ()=>
                {
                    console.warn('[交换流-目标]关闭',error)
                    if(src.writable)src.close()
                }
            )

            return true
        }
    }
/*-------------------------------------------------------------------------------------------*/    
module.exports=stream;