使用节点js的代码体系结构

时间:2016-08-08 19:12:11

标签: node.js data-structures architecture

我的代码架构出现问题,正在寻求建议。我通过udp向服务器发送多个读取请求并打印出读取响应。下面是请求和响应的外观示例。我将响应作为从0008开始的一个大十六进制字符串得到回复。我需要一种方法让代码知道发送了多少个地址以及请求了什么数据大小,并在打印出数据时将其考虑在内。由于数据大小可以改变,我不能只使用一个确定的值来拆分字符串。我不是在寻找实际的代码,而只是关于如何解决这个问题的一些想法。

Request
    00 06 - Opcode
    00 00 - Block #
    02 - Count
    34 97 00 20 - Address 1
    04 00 - Data Size 1 (bytes)
    30 97 00 20 - Address 2 
    01 00 - Data Size 2 (bytes)


Response- 00080001e60300009
    00 08 - Opcode
    00 01 - Block # 
    e6 03 00 00 - Data 1
    09 - Data 2

  What I am printing right now- e603000009
  How I want it printed - Address 1 = e6030000
                          Address 2 = 09  ...

                          Address 3 = 00 00
                          etc. 
  (it would know what it is a new data by the data size that was requested and the # of addresses that were requested)

我发送读取请求并将其发送到html的部分代码

app.post('/output3', function(req, res){

  res.sendFile(__dirname + '/upload3.html');
 //Define the host and port values of UDP

  var HOST= '192.168.0.136';
  var PORT= 69;    

    var io = require('socket.io')(http);

    //Mulitple parameters
    //setInterval will send message constantly.
    var client= dgram.createSocket('udp4');
    var counter = 0;

      //array for addresses
      var address=[];
      //array for size
      var size=[];
      //iterate through all addresses and convert to little endian
      for (var i=0; i<req.body.address.length; i++) {
        var n= req.body.address[i];
        var s = n.toString(16).match(/.{1,2}/g);
        address[i]= s.reverse().join("").toString(16); // ==> "0x985c0020" (= 2556166176)
      }

      //iterate through all size and make hex strings and little endian
      for (var i=0; i<req.body.size.length; i++) {
        function pad(number, length) {
          var my_string = '' + number;
          while (my_string.length < length) {
            my_string = '0' + my_string;
          }
          return my_string;
        }

        var n2= pad(req.body.size[i], 4);
        var s2 = n2.toString(16).match(/.{1,2}/g);
        size[i]= s2.reverse().join("").toString(16);
      }

      //empty string to add address and size together
      var x='';
      for (var i=0; i<req.body.address.length; i++) {
        x += address[i]+size[i];
      }

        console.log(req.body.size);
      var mrq= read(x);

      //Open listener to recieve intial message and print in to webpage
      io.on('connection', function(socket){
        var mrq= read(x);
          io.emit('mrq', mrq);
        });
      function read() {
        // Memory Read Request is a 16-bit word with a value of 6
        var message = '0006'
        // Block number is a 16-bit word that we will always set to 0
        message += '0000'
        // Count is variable, and calculated by the size of the parameter list
        message += '02'

        for (var i=0; i < arguments.length; i++) {
          message += arguments[i];
        }
        return message;
      }


      var message = new Buffer(mrq, 'hex');
      counter++;

      var loop= setInterval(function () {
      //Sends packets to UDP and setInterval sends packets again for specific time
      client.send(message, 0, message.length, PORT, HOST, function (err, bytes) {
        if (err) throw err;
      });
      if (counter === 1000000000000000) {
        clearInterval(loop);
      }
    }, 1/50);



    //Open listener to recieve intial message and print in to webpage
    io.on('connection', function(socket){
      client.on('message', function( message, rinfo ){
        //hex string
        var temp = message.readUIntBE(0, 2);
        //console.log(message.toString(16));
        io.emit('temp', temp);
      });
    });

1 个答案:

答案 0 :(得分:0)

显示您当前的代码有助于我们更好地回答这个问题。

但一般来说,方法是使用写入流并将字符串推出块,而不是整个块。