<commit_before>var dgram = require('dgram');
var erbium = require('node-erbium');
var URI = require('URIjs');
require('buffertools');

//http://tools.ietf.org/html/draft-ietf-core-coap-18#section-6.4 (TBD)

var global_mid = 0;

function request(app, code, type, coap_url, opts) {
    var uri = URI(coap_url);
    var port = 5683;
    var listeners = app.listeners;
    var mid;

    if (opts === undefined)
        opts = {};
    if (opts.mid === undefined)
        mid = global_mid++;
    else
        mid = opts.mid;

    if (uri.scheme() !== 'coap') {
        throw new Error("Only scheme coap:// supported");
    }
    if (uri.port() !== "")
        port = uri.port();

    var outpkt = new erbium.Erbium(type, code, mid);
    outpkt.setHeaderUriHost(uri.hostname());
    outpkt.setHeaderUriPath(uri.path());
    outpkt.setHeaderUriQuery(uri.query());

    if (opts.token !== undefined)
        outpkt.setHeaderToken(new Buffer(opts.token));
    if (opts.payload !== undefined)
        outpkt.setPayload(new Buffer(opts.payload));

    var raw = outpkt.serialize();

    if (opts.beforeSend !== undefined) {
        opts.beforeSend(new Buffer(raw));   // copy so they can't break it
    }
    app.bearer.send(raw, 0, raw.length, port, uri.hostname(), function() {
        listeners.push({
            handler: function(rsppkt, raw) {
                if (opts.beforeReceive !== undefined) {
                    opts.beforeReceive(new Buffer(raw));    // copy
                }
                // if needs acking, ack it
                if (rsppkt.getHeaderType() == erbium.COAP_TYPE_CON) {
                    var ackpkt = new erbium.Erbium(erbium.COAP_TYPE_ACK, 0/*empty*/, rsppkt.getHeaderMID());
                    var ackraw = ackpkt.serialize();
                    if (opts.beforeSend !== undefined) {
                        opts.beforeSend(new Buffer(ackraw));    // copy of
                    }
                    app.bearer.send(ackraw, 0, ackraw.length, port, uri.hostname(), function() {
                        // console.log("ACK sent");
                    });
                }

                // if is non-empty ack
                if (outpkt.getHeaderType() == erbium.COAP_TYPE_CON) {
                    if ((rsppkt.getHeaderType() == erbium.COAP_TYPE_ACK || rsppkt.getHeaderType() == erbium.COAP_TYPE_CON) && rsppkt.getHeaderStatusCode() !== 0) {
                        if (erbium.codeClass(rsppkt.getHeaderStatusCode()) == 2) {   // 2xx status
                            if (opts.success !== undefined)
                                opts.success(rsppkt, rsppkt.getPayload());
                        }
                        else {
                            if (opts.error !== undefined)
                                opts.error(rsppkt, rsppkt.getPayload());
                        }
                    }
                }
                if (outpkt.getHeaderType() == erbium.COAP_TYPE_NON) {
                    if (rsppkt.getHeaderType() == erbium.COAP_TYPE_NON && rsppkt.getHeaderStatusCode() !== 0) {
                        if (erbium.codeClass(rsppkt.getHeaderStatusCode()) == 2) {   // 2xx status
                            if (opts.success !== undefined)
                                opts.success(rsppkt, rsppkt.getPayload());
                        }
                        else {
                            if (opts.error !== undefined)
                                opts.error(rsppkt, rsppkt.getPayload());
                        }
                    }
                }

            },
            id: outpkt.getHeaderMID(),
            tok: outpkt.getHeaderToken()
        });
    });
}
<commit_msg>Reminder to fix listeners<commit_after>var dgram = require('dgram');
var erbium = require('node-erbium');
var URI = require('URIjs');
require('buffertools');

//http://tools.ietf.org/html/draft-ietf-core-coap-18#section-6.4 (TBD)

var global_mid = 0;

function request(app, code, type, coap_url, opts) {
    var uri = URI(coap_url);
    var port = 5683;
    var listeners = app.listeners;
    var mid;

    if (opts === undefined)
        opts = {};
    if (opts.mid === undefined)
        mid = global_mid++;
    else
        mid = opts.mid;

    if (uri.scheme() !== 'coap') {
        throw new Error("Only scheme coap:// supported");
    }
    if (uri.port() !== "")
        port = uri.port();

    var outpkt = new erbium.Erbium(type, code, mid);
    outpkt.setHeaderUriHost(uri.hostname());
    outpkt.setHeaderUriPath(uri.path());
    outpkt.setHeaderUriQuery(uri.query());

    if (opts.token !== undefined)
        outpkt.setHeaderToken(new Buffer(opts.token));
    if (opts.payload !== undefined)
        outpkt.setPayload(new Buffer(opts.payload));

    var raw = outpkt.serialize();

    if (opts.beforeSend !== undefined) {
        opts.beforeSend(new Buffer(raw));   // copy so they can't break it
    }
    app.bearer.send(raw, 0, raw.length, port, uri.hostname(), function() {
        listeners.push({
            handler: function(rsppkt, raw) {
                if (opts.beforeReceive !== undefined) {
                    opts.beforeReceive(new Buffer(raw));    // copy
                }
                // if needs acking, ack it
                if (rsppkt.getHeaderType() == erbium.COAP_TYPE_CON) {
                    var ackpkt = new erbium.Erbium(erbium.COAP_TYPE_ACK, 0/*empty*/, rsppkt.getHeaderMID());
                    var ackraw = ackpkt.serialize();
                    if (opts.beforeSend !== undefined) {
                        opts.beforeSend(new Buffer(ackraw));    // copy of
                    }
                    app.bearer.send(ackraw, 0, ackraw.length, port, uri.hostname(), function() {
                        // console.log("ACK sent");
                    });
                }

                // if is non-empty ack
                if (outpkt.getHeaderType() == erbium.COAP_TYPE_CON) {
                    if ((rsppkt.getHeaderType() == erbium.COAP_TYPE_ACK || rsppkt.getHeaderType() == erbium.COAP_TYPE_CON) && rsppkt.getHeaderStatusCode() !== 0) {
                        if (erbium.codeClass(rsppkt.getHeaderStatusCode()) == 2) {   // 2xx status
                            if (opts.success !== undefined)
                                opts.success(rsppkt, rsppkt.getPayload());
                        }
                        else {
                            if (opts.error !== undefined)
                                opts.error(rsppkt, rsppkt.getPayload());
                        }
                    }
                }
                if (outpkt.getHeaderType() == erbium.COAP_TYPE_NON) {
                    if (rsppkt.getHeaderType() == erbium.COAP_TYPE_NON && rsppkt.getHeaderStatusCode() !== 0) {
                        if (erbium.codeClass(rsppkt.getHeaderStatusCode()) == 2) {   // 2xx status
                            if (opts.success !== undefined)
                                opts.success(rsppkt, rsppkt.getPayload());
                        }
                        else {
                            if (opts.error !== undefined)
                                opts.error(rsppkt, rsppkt.getPayload());
                        }
                    }
                }
                /* FIXME!!
                // remove from listeners
                for (var i=0;i<app.listeners.length;i++) {
                    if (inpkt.getHeaderMID() == app.listeners[i].id && 0 === app.listeners[i].tok.compare(inpkt.getHeaderToken())) {
                        app.listeners[i].handler(inpkt, msg);
                        app.listeners.splice(i, 1);
                        break;
                    }
                }
                */
            },
            id: outpkt.getHeaderMID(),
            tok: outpkt.getHeaderToken()
        });
    });
}

{'fraction_non_alphanumeric': 0.13270932170791128, 'fraction_numerical': 0.005000641107834338, 'mean_word_length': 1.4306637581801185, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '564890', 'n_tokens_mistral': 2308, 'n_tokens_neox': 2224, 'n_words': 412}