Code coverage report for lib/services/server/coapUtils.js

Statements: 94.44% (34 / 36)      Branches: 75% (9 / 12)      Functions: 100% (8 / 8)      Lines: 94.44% (34 / 36)     

All files » lib/services/server/ » coapUtils.js
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                                              1   1       1 35   35 15 15     35     1 20   20 9     20 20     20                   1 35     35 35 15   20       35 3 3 3   32                       1 17 17 14 3 3             1 1
/*
 * Copyright 2014 Telefonica Investigación y Desarrollo, S.A.U
 *
 * This file is part of iotagent-lwm2m-lib
 *
 * iotagent-lwm2m-lib is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * iotagent-lwm2m-lib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with iotagent-lwm2m-lib.
 * If not, seehttp://www.gnu.org/licenses/.
 *
 * For those usages not covered by the GNU Affero General Public License
 * please contact with::[contacto@tid.es]
 */
 
'use strict';
 
var coap = require('coap'),
    Readable = require('stream').Readable,
    errors = require('../../errors');
 
function isObserveAction(res) {
    var observeFlag = false;
 
    for (var i = 0; i < res.options.length; i++) {
        Eif (res.options[i].name === 'Observe') {
            observeFlag = true;
        }
    }
    return observeFlag;
}
 
function readResponse(res, callback) {
    var data = '';
 
    res.on('data', function (chunk) {
        data += chunk;
    });
 
    res.on('end', function(chunk) {
        Iif (chunk) {
            data += chunk;
        }
        callback(null, res);
    });
}
 
/**
 * Send the COAP Request passed as a parameter. If the request contains a parameter "payload", the parameter is sent
 * as the payload of the request; otherwise, the request is sent without any payload.
 *
 * @param {Object} request          Object containing all the request information (in the Node COAP format).
 */
function sendRequest(request, callback) {
    var req = coap.request(request),
        rs = new Readable();
 
    req.on('response', function(res) {
        if (isObserveAction(res)) {
            callback(null, res);
        } else {
            readResponse(res, callback);
        }
    });
 
    if (request.payload) {
        rs.push(request.payload);
        rs.push(null);
        rs.pipe(req);
    } else {
        req.end();
    }
}
 
/**
 * Generates a generic response processing callback for all the resource based operations.
 *
 * @param {String} objectType           ID of the type of object.
 * @param {String} objectId             ID of the instance where the operation was performed.
 * @param code                          Return code if the callback is successful.
 * @returns {processResponse}           The generated handler.
 */
function generateProcessResponse(objectType, objectId, resourceId, code) {
    return function processResponse(res, callback) {
        if (res.code === code) {
            callback(null, res.payload.toString('utf8'));
        } else Eif (res.code === '4.04') {
            callback(new errors.ResourceNotFound(objectId, objectType, resourceId));
        } else {
            callback(new errors.ClientError(res.code));
        }
    };
}
 
exports.generateProcessResponse = generateProcessResponse;
exports.sendRequest = sendRequest;