<commit_before>var exec = cordova.require('cordova/exec');

// Events: 'message', 'error'
function Socket(type) {
    this._multicastSocket = type === 'multicast-udp4';
    this._socketId = ++Socket.socketCount;
    this._eventHandlers = { };
    Socket.sockets[this._socketId] = this;

    exec(null, null, 'Multicast', 'create', [ this._socketId, this._multicastSocket ]);
}

Socket.socketCount = 0;
Socket.sockets = { };

Socket.prototype.on = function (event, callback) {
    this._eventHandlers[event] = callback;
};

Socket.prototype.bind = function (port, callback) {
    callback = callback || function () { };
    exec(callback.bind(null, null), callback.bind(errorHandler), 'Multicast', 'bind', [ this._socketId, port ]);
};

Socket.prototype.close = function () {
    exec(null, null, 'Multicast', 'close', [ this._socketId ]);
    delete Socket.sockets[this._socketId];
    this._socketId = 0;
};

// sends utf-8
Socket.prototype.send = function (buffer, destAddress, destPort, callback) {
    callback = callback || function () { };
    exec(callback.bind(null, null), // success
         callback.bind(errorHandler), // failure
         'Multicast',
         'send',
         [ this._socketId, buffer, destAddress, destPort ]);
};

Socket.prototype.address = function () {
};

Socket.prototype.joinGroup = function (address, callback) {
    console.log('joinGroup');
    callback = callback || function () { };
    if (!this._multicastSocket) throw new Error('Invalid operation');
    exec(callback.bind(null, null), callback.bind(errorHandler), 'Multicast', 'joinGroup', [ this._socketId, address ]);
};

Socket.prototype.leaveGroup = function (address, callback) {
    callback = callback || function () { };
    if (!this._multicastSocket) throw new Error('Invalid operation');
    exec(callback.bind(null, null), errorHandler, 'Multicast', 'leaveGroup', [ this._socketId, address ]);
};

function createSocket(type) {
    console.log('createSocket');
    if (type !== 'udp4' && type !== 'multicast-udp4') {
        throw new Error('Illegal Argument, only udp4 and multicast-udp4 supported');
    }
    return new Socket(type);
}

function onMessage(id, msg, remoteAddress, remotePort) {
    console.log('onMessage');
    var socket = Socket.sockets[id];
    if (socket && 'message' in socket._eventHandlers) {
        socket._eventHandlers['message'].call(null, msg, { address: remoteAddress, port: remotePort });
    }
}

function errorHandler() {
    debugger
}

module.exports = {
    createSocket: createSocket,
    _onMessage: onMessage
}<commit_msg>remove debug error handler code<commit_after>var exec = cordova.require('cordova/exec');

// Events: 'message', 'error'
function Socket(type) {
    this._multicastSocket = type === 'multicast-udp4';
    this._socketId = ++Socket.socketCount;
    this._eventHandlers = { };
    Socket.sockets[this._socketId] = this;

    exec(null, null, 'Multicast', 'create', [ this._socketId, this._multicastSocket ]);
}

Socket.socketCount = 0;
Socket.sockets = { };

Socket.prototype.on = function (event, callback) {
    this._eventHandlers[event] = callback;
};

Socket.prototype.bind = function (port, callback) {
    callback = callback || function () { };
    exec(callback.bind(null, null), callback.bind(null), 'Multicast', 'bind', [ this._socketId, port ]);
};

Socket.prototype.close = function () {
    exec(null, null, 'Multicast', 'close', [ this._socketId ]);
    delete Socket.sockets[this._socketId];
    this._socketId = 0;
};

// sends utf-8
Socket.prototype.send = function (buffer, destAddress, destPort, callback) {
    callback = callback || function () { };
    exec(callback.bind(null, null), // success
         callback.bind(errorHandler), // failure
         'Multicast',
         'send',
         [ this._socketId, buffer, destAddress, destPort ]);
};

Socket.prototype.address = function () {
};

Socket.prototype.joinGroup = function (address, callback) {
    console.log('joinGroup');
    callback = callback || function () { };
    if (!this._multicastSocket) throw new Error('Invalid operation');
    exec(callback.bind(null, null), callback.bind(errorHandler), 'Multicast', 'joinGroup', [ this._socketId, address ]);
};

Socket.prototype.leaveGroup = function (address, callback) {
    callback = callback || function () { };
    if (!this._multicastSocket) throw new Error('Invalid operation');
    exec(callback.bind(null, null), callback.bind(null), 'Multicast', 'leaveGroup', [ this._socketId, address ]);
};

function createSocket(type) {
    console.log('createSocket');
    if (type !== 'udp4' && type !== 'multicast-udp4') {
        throw new Error('Illegal Argument, only udp4 and multicast-udp4 supported');
    }
    return new Socket(type);
}

function onMessage(id, msg, remoteAddress, remotePort) {
    console.log('onMessage');
    var socket = Socket.sockets[id];
    if (socket && 'message' in socket._eventHandlers) {
        socket._eventHandlers['message'].call(null, msg, { address: remoteAddress, port: remotePort });
    }
}

module.exports = {
    createSocket: createSocket,
    _onMessage: onMessage
}
{'fraction_non_alphanumeric': 0.15758640890451084, 'fraction_numerical': 0.003124389767623511, 'mean_word_length': 3.9728155339805826, 'pattern_counts': {'":': 0, '<': 3, '<?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': '6464259', 'n_tokens_mistral': 1566, 'n_tokens_neox': 1554, 'n_words': 339}