<commit_before>//     telegram-mt-node
//     Copyright 2014 <NAME> '<EMAIL>'
//     Released under the MIT License
//     https://github.com/enricostara/telegram-mt-node

//     RpcChannel class
//
// This class provides a remote procedure call channel to `Telegram` through the given TCP|HTTP connection.
// This channel in not encrypted and therefore use PlainMessage objects to wrap the communication.
// According with the MTProto specification, only few methods can use unencrypted message, see:
// https://core.telegram.org/mtproto/description#unencrypted-messages

// Import dependencies
require('requirish')._(module);
var message = require('lib/message');
var logger = require('get-log')('net.RpcChannel');

// The constructor require a [Http|Tcp]Connection, the connection must be already open.
function RpcChannel(connection) {
    if (!connection || !connection.isConnected()) {
        var msg = 'The connection is mandatory and it must be already open!';
        logger.error(msg);
        throw new TypeError(msg);
    }
    this._connection = connection;
    this._open = true;
}

// Execute remotely the given method (Type function) using the channel, call back with the response
RpcChannel.prototype.callMethod = function (method, callback) {
    if (!this._open) {
        callback(new Error('Channel is closed'));
        return;
    }
    var reqMsg = new message.PlainMessage({message: method});
    this._call(reqMsg, method.typeName, callback);
};

// Execute the call (protected).
RpcChannel.prototype._call = function (reqMsg, context, callback) {
    var conn = this._connection;
    var start = new Date().getTime();
    var self = this;
    conn.write(reqMsg.serialize(), function (ex) {
        if (ex) {
            logger.error('Unable to write: %s ', ex);
            if (callback) {
                callback(ex);
            }
            return;
        }
        conn.read(function (ex2, response) {
            if (ex2) {
                logger.error('Unable to read: %s ', ex2);
                if (callback) {
                    callback(ex2);
                }
                return;
            }
            try {
                if (logger.isDebugEnabled()) {
                    logger.debug('response: %s ', response.toString('hex'));
                }
                var resObj = self._deserializeResponse(response, context);
                var duration = new Date().getTime() - start;
                if (logger.isDebugEnabled()) {
                    logger.debug('%s executed in %sms', context.methodName, duration);
                }
                if (callback) {
                    callback(null, resObj, duration);
                }
            } catch (ex3) {
                logger.error('Unable to deserialize response %s from %s due to %s ', response.toString('hex'), context.methodName, ex3);
                if (callback) {
                    callback(ex3);
                }
            }
        });
    });
};

RpcChannel.prototype._deserializeResponse = function (response) {
    return new message.PlainMessage({buffer: response}).deserialize().body;
};

// Check if the channel is open.
RpcChannel.prototype.isOpen = function () {
    return this._open;
};

// Close the channel
RpcChannel.prototype.close = function () {
    this._open = false;
};

// Export the class
module.exports = exports = RpcChannel;<commit_msg>Remove the 'context' parameter in the callMethod chain<commit_after>//     telegram-mt-node
//     Copyright 2014 <NAME> '<EMAIL>'
//     Released under the MIT License
//     https://github.com/enricostara/telegram-mt-node

//     RpcChannel class
//
// This class provides a remote procedure call channel to `Telegram` through the given TCP|HTTP connection.
// This channel in not encrypted and therefore use PlainMessage objects to wrap the communication.
// According with the MTProto specification, only few methods can use unencrypted message, see:
// https://core.telegram.org/mtproto/description#unencrypted-messages

// Import dependencies
require('requirish')._(module);
var message = require('lib/message');
var logger = require('get-log')('net.RpcChannel');

// The constructor require a [Http|Tcp]Connection, the connection must be already open.
function RpcChannel(connection) {
    if (!connection || !connection.isConnected()) {
        var msg = 'The connection is mandatory and it must be already open!';
        logger.error(msg);
        throw new TypeError(msg);
    }
    this._connection = connection;
    this._open = true;
}

// Execute remotely the given method (Type function) using the channel, call back with the response
RpcChannel.prototype.callMethod = function (method, callback) {
    if (!this._open) {
        callback(new Error('Channel is closed'));
        return;
    }
    var reqMsg = new message.PlainMessage({message: method});
    this._call(reqMsg, callback);
};

// Execute the call (protected).
RpcChannel.prototype._call = function (reqMsg, callback) {
    var conn = this._connection;
    var start = new Date().getTime();
    var self = this;
    conn.write(reqMsg.serialize(), function (ex) {
        if (ex) {
            logger.error('Unable to write: %s ', ex);
            if (callback) {
                callback(ex);
            }
            return;
        }
        conn.read(function (ex2, response) {
            if (ex2) {
                logger.error('Unable to read: %s ', ex2);
                if (callback) {
                    callback(ex2);
                }
                return;
            }
            try {
                if (logger.isDebugEnabled()) {
                    logger.debug('response: %s ', response.toString('hex'));
                }
                var resObj = self._deserializeResponse(response);
                var duration = new Date().getTime() - start;
                if (logger.isDebugEnabled()) {
                    logger.debug('%s executed in %sms',
                        (reqMsg.body ? reqMsg.body.typeName : ''), duration);
                }
                if (callback) {
                    callback(null, resObj, duration);
                }
            } catch (ex3) {
                logger.error('Unable to deserialize response %s from %s due to %s ',
                    response.toString('hex'), (reqMsg.body ? reqMsg.body.typeName : ''), ex3.stack);
                if (callback) {
                    callback(ex3);
                }
            }
        });
    });
};

RpcChannel.prototype._deserializeResponse = function (response) {
    return new message.PlainMessage({buffer: response}).deserialize().body;
};

// Check if the channel is open.
RpcChannel.prototype.isOpen = function () {
    return this._open;
};

// Close the channel
RpcChannel.prototype.close = function () {
    this._open = false;
};

// Export the class
module.exports = exports = RpcChannel;
{'fraction_non_alphanumeric': 0.11890243902439024, 'fraction_numerical': 0.003193960511033682, 'mean_word_length': 2.1242630385487526, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 4, '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': '7520395', 'n_tokens_mistral': 1848, 'n_tokens_neox': 1790, 'n_words': 523}