1. 程式人生 > >node.js檔案傳輸框架

node.js檔案傳輸框架

client.js:

var net = require('net');
var fs = require('fs');
var strm = require('stream');
 
var listenPort = 9999;
var serverIP = "127.0.0.1";
var clientPort = 8888;
var handler = [];
 
var args = process.argv.splice(2);
var arg_mode = args[0] ? args[0] : "put_bin_file";
var arg_filename = args[1] ? args[1] : "test.txt";
 
console.log('transfer file:' + args[0] + ' ' + args[1]);
 
var client = net.createConnection(listenPort, serverIP, handleClientConnection);
 
var handleClientConnection = function(){
    console.log("connected to server");
};
 
var handleClientError = function(error){
    console.log("server errors:: " + error);
};
 
var handleClientData = function(data){
    console.log("data received : " + data.toString());
};
 
putFile = function(toPutFileName, writeAbleStream){
    var rs = fs.createReadStream(toPutFileName);
    console.log("put file name is " + toPutFileName + '\n');
    rs.on('data',function(chunk) {
        console.log(chunk.length);
        writeAbleStream.write(chunk);
        rs.pause();
      });
    rs.on('end', () => {
        console.log("GoodBye\n");
        rs.close();
        client.destroy();
      });
    writeAbleStream.on('data', function(chunk) {
      if(chunk == 'go') {
         rs.resume();
      }
    });
};

var handleGO = function(data){
    console.log("tmp received");
    str = data.toString().split(' ');
    if(str[0] == 'go')
        putFile(str[1], this);
    this.removeListener('data', handleGO);
};
 
handler["PUTFILE"] = function(fileName, socket){
    console.log("client put file begin:" + fileName);
    process.stdin.pause();
    socket.write('putfile ' + fileName);
    socket.on('data', handleGO);
 };
 
 
var handleProcessData = function(data){
    var str = data.toString().split(' ');
    if(handler[str[0].toUpperCase()]){
        handler[str[0].toUpperCase()](str[1], client);
    }
 
};
 
 
client.on('error', handleClientError);
client.on('data', handleGO);
client.write("putfile " + arg_filename);


server.js

var net = require('net');
var fs = require('fs');
var strm = require('stream');
 
//var path = './test';
var handler = [];
 
getFile = function(toGetFileName, readAbleStream){
    var ws = fs.createWriteStream(toGetFileName);
    console.log("get file name is " + toGetFileName + '\n');
    readAbleStream.on('data', function(chunk) {
        console.log(chunk.length);
        ws.write(chunk);
    });
    ws.on('drain',  () => {
        readAbleStream.write('go');
    });
    readAbleStream.on('end', () => {
        console.log("GoodBye\n");
        ws.close();
      });
};
 
handler["PUTFILE"] = function(fileName, socket){
    socket.write("go " + fileName);
    getFile(fileName + 'tmp', socket);
 
};
 
var handleConnection = function(client){
    process.stdout.write("received a new connecton\n");
    client.on('data', handleData);
    client.on('error', handleError);
    client.on('close', handleClose);
};
 
var handleData = function(data){
    var str = data.toString().split(' ');
 //   process.stdout.write(data);
    if(handler[str[0].toUpperCase()]){
        handler[str[0].toUpperCase()](str[1], this);
    }
    else{
 
    }
    
};
 
var handleError = function(err){
    process.stdout.write(err);
};
 
var handleClose = function(){
    process.stdout.write("client close\n");
};
 
var server = net.createServer(handleConnection);
server.listen(9999);

框架2:

client.js

var net = require('net');
var fs = require('fs');
var strm = require('stream');
 
var listenPort = 9999;
var serverIP = "127.0.0.1";
var clientPort = 8888;
var handler = [];
 
var args = process.argv.splice(2);
console.log("arguments: %s", args[0]);
var arg_mode = args[0] ? args[0] : "put_txt_file";
var arg_filename = args[1] ? args[1] : "test.txt";
 
console.log('transfer file:' + args[0] + ' ' + args[1]);
 
var client = net.createConnection(listenPort, serverIP, handleClientConnection);
 
var handleClientConnection = function(){
    console.log("connected to server");
};
 
var handleClientError = function(error){
    console.log("server errors:: " + error);
};
 
var handleClientData = function(data){
    console.log("data received : " + data.toString());
};
 
putFile = function(toPutFileName, writeAbleStream){
    var statsSave;
    var rs = fs.createReadStream(toPutFileName);
    statsSave = fs.statSync(toPutFileName);
 
    console.log("put file name is " + toPutFileName + '\n');
    rs.on('readable', rsOnReadable);
    function rsOnReadable() {
        var len;
        len = statsSave.size;
        console.log("len: " + len);
        writeAbleStream.write('' + len);
        rs.removeListener("readable", rsOnReadable);
        rs.pause();
    }
    rs.on('data',function(chunk) {
        console.log(chunk.length);
        writeAbleStream.write(chunk);
        rs.pause();
      });
    rs.on('end', () => {
        console.log("GoodBye\n");
        rs.close();
        client.destroy();
      });
    writeAbleStream.on('data', function(chunk) {
        if(chunk == 'go') {
             rs.resume();
        }
    });
};
 
getFile = function(readAbleStream){
    var toGetFileName = arg_filename.split(':')[2];
    var ws = fs.createWriteStream(toGetFileName);
    var xferedLen = 0;
    var bStart = 1;
    var totalLen = 0;
    var block_16384 = 0;
    console.log('getFile: ' + toGetFileName);
    readAbleStream.write('go');
 
    readAbleStream.on('data', function (chunk) {
        console.log('read: ' + xferedLen + ' ' + chunk.length);
        if(bStart == 1) {
            totalLen = + chunk.toString();
            readAbleStream.write('go');
            bStart = 0;
            console.log('totalLen: ' + totalLen);
        } else {
            block_16384 += chunk.length;
            if(block_16384 >= 16384) {
                console.log('send go');
                readAbleStream.write('go');
                block_16384 -= 16384;
            }
            ws.write(chunk);
            xferedLen += chunk.length;
            if(xferedLen >= totalLen) {
                this.destroy();
            }
        }
    });
}
 
const op_put_bin_file = 0;
const op_put_txt_file = 1;
const op_write_reg    = 2;
const op_read_reg     = 3;
const op_get_data     = 4;
var oprate = op_read_reg;
switch(arg_mode.toUpperCase()){
    case "PUT_BIN_FILE":
        oprate = op_put_bin_file;
        break;
    case "PUT_TXT_FILE":
        oprate = op_put_txt_file;
        break;
    case "WRITE_REG":
        oprate = op_write_reg;
        break;
    case "READ_REG":
        opreate = op_read_reg;
        break;
    case "GET_DATA":
        oprate = op_get_data;
        break;
}
 
var handleGO = function(data){
    console.log("tmp received, op:%d", oprate);
    switch(oprate) {
    case op_put_bin_file:
    case op_put_txt_file:
        str = data.toString().split(' ');
        if(str[0] == 'go')
            putFile(str[1], this);
        break;
    case op_write_reg:
        console.log("op_write_reg: " + data);
        client.destroy();
        break;
    case op_read_reg:
        console.log("op_read_reg: " + data);
        client.destroy();
        break;
    case op_get_data:
        str = data.toString().split(' ');
        if(str[0] == 'go') {
          getFile(this);
        }
        break;
    }
    this.removeListener('data', handleGO);
};

client.on('error', handleClientError);
client.on('data', handleGO);
 
console.log("arg_mode: %d", oprate);
 
client.write(arg_mode + " " + arg_filename);

server.js

var net = require('net');
var fs = require('fs');
var strm = require('stream');
var readline = require('readline');

 //0: write register, 1: only test socket xfer
var test_txt_mode = 1;
if(test_txt_mode == 0) {
    addon = require('./build/Release/addon');
    console.log('This should be eight:', addon.add(3, 5));
}

if(test_txt_mode == 0) {
    var usbStat = addon.open().split();
    if(usbStat[0].match('Success')) {
        console.log('open CyUSB:', usbStat);
        console.log('init CyUSB:', addon.initxfer());
    } else {
        console.log('open CyUSB:', usbStat);
        process.exit();
    }
}
 
var handler = [];
var one_file_start = 0;
 
getBinFile = function(toGetFileName, base, readAbleStream){
    var ws = fs.createWriteStream(toGetFileName);
    var AXImode = base >= 0x05d00000 || base < 0x0a800000 ? 1 : 0;
    var fileLen = 0;
    var fileBuffer = Buffer.from("");
    var xferedLen = 0;
    console.log("get file name is " + toGetFileName + '\n');
    readAbleStream.on('data', function(chunk) {
        if(one_file_start == 0) {
            xferedLen = 0;
            console.log(chunk.length);
            one_file_start = 1;
            fileLen = + chunk.toString();
            console.log(fileLen);
            fileBuffer = Buffer.alloc(fileLen);
            readAbleStream.write('go');
        } else { 
            // TODO: save data in buffer here:
            fileBuffer.fill(chunk, xferedLen);
            xferedLen += chunk.length;
            console.log("len" + xferedLen);
 
            //console.log(chunk.length);
            ws.write(chunk);
        }
    });
    ws.on('drain',  () => {
        readAbleStream.write('go');
    });
    readAbleStream.on('end', () => {
        console.log("GoodBye\n");
        ws.close();
        if(AXImode == 0) {
            var addr = base;
            for(var i=0; i<xferedLen / 4; i++) {
                var val = fileBuffer.readInt32BE();
                if(test_txt_mode == 0) {
                    addon.writereg(addr, val);
                } else {
                    console.log('write bin addr: ' + addr + ',' + val);
                    addr += 4;
                }
            }
        } else {
            console.log("donnot support AXI write currently.\n");
        }
      });
};
 
getTxtFile = function(toGetFileName, readAbleStream){
 
    var fileLen = 0;
    var fileBuffer = Buffer.from("");
    var xferedLen = 0;
    const rl = readline.createInterface({
        input: readAbleStream,
      });
    var line_cnt = 0;
    var ws = fs.createWriteStream(toGetFileName);
    console.log("get file name is " + toGetFileName + '\n');
    readAbleStream.on('data', function(chunk) {
        if(one_file_start == 0) {
            xferedLen = 0;
            console.log(chunk.length);
            console.log(chunk);
            one_file_start = 1;
            fileLen = + chunk.toString();
            console.log(fileLen);
            fileBuffer = Buffer.alloc(fileLen);
            readAbleStream.write('go');
        } else { 
            // TODO: save data in buffer here:
            fileBuffer.fill(chunk, xferedLen);
            xferedLen += chunk.length;
            console.log("len" + xferedLen);
 
            //console.log(chunk.length);
            ws.write(chunk);
        }
    });
    rl.on("line", function(line){
        //console.log('line:' + line);
        line_cnt ++;
        //ws.write(line);
        // TODO: when file xfer over, do registers write here
        var hexStr = line.split(' ');
        //console.log(hexStr.length);
        if(hexStr.length > 1) {
            if(test_txt_mode == 0) {
                var addr = addon.hexstr2Hex(hexStr[1]);
                var val = addon.hexstr2Hex(hexStr[2]);
 
                addon.writereg(addr, val);
            } else {
                var addr = + strArg[1];
                var val = + strArg[2];
                console.log('write txt addr: ' + addr + ',' + val);
            }
        }
    });
 
    ws.on('drain',  () => {
        readAbleStream.write('go');
    });
    readAbleStream.on('end', () => {
        console.log("line count: " + line_cnt);
        ws.close();
    });
};
 
putBinFile = function(strAddrLen, writeAbleStream){
    var strArg = strAddrLen.split(':');
    var base;
    var len;
    if(test_txt_mode == 0) {
        base = addon.hexstr2Hex(strArg[0]);
        len = addon.hexstr2Hex(strArg[1]);
    } else {
        base = + strArg[0];
        len = + strArg[1];
    }
    var StreamBuf = Buffer.alloc(16384);
    var lenBuf = Buffer.alloc(4);
    var startFlag = 1;
    var left = len;
    var idx = 0;
    console.log('xfer_data: ' + base + ' ' + len);
    writeAbleStream.on('data', function(chunk){
        console.log("idx: " + idx);
        if(startFlag == 1) {
            if(chunk == 'go') {
                lenBuf.fill(len);
                writeAbleStream.write('' + len);
                startFlag = 0;
            }
        } else {
            if(chunk == 'go') {
                if(strArg.length > 1) {
                    if(left >= 16384) {
                        var addr = base + idx * 16384;
                        if(test_txt_mode == 0) {
                            addon.xferdata_o(addr, 16384, StreamBuf);
                        } else {
                            StreamBuf.fill(0);
                            console.log('get_data: ' + addr + ',' + left);
                        }
                        writeAbleStream.write(StreamBuf);
                        left -= 16384;
                    } else {
                        //the last one
                        var addr = base + (idx - 1) * 16384;
                        if(test_txt_mode == 0) {
                            addon.xferdata_o(addr, 16384, StreamBuf);
                        } else {
                            console.log('get_data: ' + addr + ',' + left);
                        }
                        writeAbleStream.write(StreamBuf, left);
                    }
                    idx ++;
                }
            }
        }
    });
 
    writeAbleStream.on('end', ()=>{
        console.log("xfer end: " + len);
    });
}
 
handler["PUT_BIN_FILE"] = function(strBaseFileName, socket){
    var hexStr = strAddrVal.split(':');
    if(hexStr.length > 1) {
        var base;
        if(test_txt_mode == 0) {
            base = addon.hexstr2Hex(hexStr[0]);
        } else {
            base = + hexStr[0];
        }
        var fileName = hexStr[1];
        socket.write("go " + fileName);
        getBinFile('test.bin', base, socket);
    }
};
 
handler["PUT_TXT_FILE"] = function(fileName, socket){
    socket.write("go " + fileName);
    getTxtFile('test.txt', socket);
};
 
handler["WRITE_REG"] = function(strAddrVal, socket){
    var hexStr = strAddrVal.split(':');
    console.log(hexStr);
    if(hexStr.length > 1) {

        if(test_txt_mode == 0) {
            var addr = addon.hexstr2Hex(hexStr[0]);
            var val = addon.hexstr2Hex(hexStr[1]);
            ret = addon.writereg(addr, val);
        } else {
            var addr = + hexStr[0];
            var val = + hexStr[1];
            ret = 'OK sim.';
            console.log('write_reg: ' + addr + ',' + val);
        }
        console.log("write_reg: " + ret);
        socket.write("write_reg: " + ret);
    }
};
 
handler["READ_REG"] = function(strAddr, socket){
    var addr;
    var val;
    if(test_txt_mode == 0) {
        addr = addon.hexstr2Hex(strAddr);
        val = addon.readreg(addr);
    } else {
        addr = + strAddr;
        val = 12345678;
    }
    console.log("read_reg: " + val);
    socket.write("read_reg: " + val);
}
 
handler["GET_DATA"] = function(strAddrLen, socket){
    console.log("get_data: " + strAddrLen);
    socket.write("go");
    putBinFile(strAddrLen, socket);
}
 
var handleConnection = function(client){
    process.stdout.write("received a new connecton\n");
    client.on('data', handleData);
    client.on('error', handleError);
    client.on('close', handleClose);
};

var handleData = function(data){
    var str = data.toString().split(' ');
    if(handler[str[0].toUpperCase()]){
        handler[str[0].toUpperCase()](str[1], this);
    }
    else{
 
    }

};
 
var handleError = function(err){
    process.stdout.write(err);
};
 
var handleClose = function(){
    one_file_start = 0;
    process.stdout.write("client close\n");
};
 
var server = net.createServer(handleConnection);
server.listen(9999);

 

Reference:  

https://download.csdn.net/download/xljiulong/4509819