<commit_before>var events = require('events');
var util = require('util');
var Logger = require('./logger');
var Queue = require('./queue');

/**
 * Creates a new bi-directional message channel between two endpoints utilizing message queues.
 * @param bus
 * @param name
 * @param local
 * @param remote
 * @constructor
 */
function Channel(bus, name, local, remote) {
  events.EventEmitter.call(this);
  this.bus = bus;
  this.name = name;
  this.local = local || 'local';
  this.remote = remote || 'remote';

  this.handlers = {
    '1': this._onRemoteConnect,
    '2': this._onMessage,
    '3': this._onRemoteDisconnect,
    '4': this._onEnd
  }
}

util.inherits(Channel, events.EventEmitter);

/**
 * Connect to the channel, using the 'local' role to consume messages and 'remote' role to send messages.
 *   connect - emitted when this endpoint is connected to the channel
 *   remote:connect - emitted when the remote endpoint connects to the channel
 *   disconnect - emitted when this endpoint is disconnected from the channel
 *   remote:disconnect - emitted when the remote endpoint is disconnected from the channel
 *   message - emitted when a message is received from the peer
 *   end - emitted when the peer disconnects from the channel
 */
Channel.prototype.connect = function() {
  if (this.isBound()) {
    this.emit('error', 'cannot connect: channel is already bound');
    return;
  }

  this.qConsumer = new Queue(this.bus, 'channel:' + this.remote + ':' + this.name);
  this.qProducer = new Queue(this.bus, 'channel:' + this.local + ':' + this.name);
  this._attachQueues();
};

/**
 * Connect to the channel as a "listener", using the 'local' role to send messages and 'remote' role to consume messages.
 * This is just a syntactic-sugar for a connect with a reverse semantic of the local/remote roles.
 * Events:
 *   connect - emitted when this endpoint is connected to the channel
 *   remote:connect - emitted when the remote endpoint connects to the channel
 *   disconnect - emitted when this endpoint is disconnected from the channel
 *   remote:disconnect - emitted when the remote endpoint is disconnected from the channel
 *   message - emitted when a message is received from the peer
 *   end - emitted when the peer disconnects from the channel
 */
Channel.prototype.listen = function() {
  var remote = this.remote;
  this.remote = this.local;
  this.local = remote;
  this.connect();
};


/**
 * Send a message to the other endpoint
 * @param message the message to send<commit_msg>fix channel queues setup roles<commit_after>var events = require('events');
var util = require('util');
var Logger = require('./logger');
var Queue = require('./queue');

/**
 * Creates a new bi-directional message channel between two endpoints utilizing message queues.
 * @param bus
 * @param name
 * @param local
 * @param remote
 * @constructor
 */
function Channel(bus, name, local, remote) {
  events.EventEmitter.call(this);
  this.bus = bus;
  this.name = name;
  this.local = local || 'local';
  this.remote = remote || 'remote';

  this.handlers = {
    '1': this._onRemoteConnect,
    '2': this._onMessage,
    '3': this._onRemoteDisconnect,
    '4': this._onEnd
  }
}

util.inherits(Channel, events.EventEmitter);

/**
 * Connect to the channel, using the 'local' role to consume messages and 'remote' role to send messages.
 *   connect - emitted when this endpoint is connected to the channel
 *   remote:connect - emitted when the remote endpoint connects to the channel
 *   disconnect - emitted when this endpoint is disconnected from the channel
 *   remote:disconnect - emitted when the remote endpoint is disconnected from the channel
 *   message - emitted when a message is received from the peer
 *   end - emitted when the peer disconnects from the channel
 */
Channel.prototype.connect = function() {
  if (this.isBound()) {
    this.emit('error', 'cannot connect: channel is already bound');
    return;
  }

  this.qConsumer = new Queue(this.bus, 'channel:' + this.local + ':' + this.name);
  this.qProducer = new Queue(this.bus, 'channel:' + this.remote + ':' + this.name);
  this._attachQueues();
};

/**
 * Connect to the channel as a "listener", using the 'local' role to send messages and 'remote' role to consume messages.
 * This is just a syntactic-sugar for a connect with a reverse semantic of the local/remote roles.
 * Events:
 *   connect - emitted when this endpoint is connected to the channel
 *   remote:connect - emitted when the remote endpoint connects to the channel
 *   disconnect - emitted when this endpoint is disconnected from the channel
 *   remote:disconnect - emitted when the remote endpoint is disconnected from the channel
 *   message - emitted when a message is received from the peer
 *   end - emitted when the peer disconnects from the channel
 */
Channel.prototype.listen = function() {
  var remote = this.remote;
  this.remote = this.local;
  this.local = remote;
  this.connect();
};


/**
 * Send a message to the other endpoint
 * @param message the message to send
{'fraction_non_alphanumeric': 0.10440972908839233, 'fraction_numerical': 0.0015819655922483687, 'mean_word_length': 3.67900092506938, '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': '2918034', 'n_tokens_mistral': 1408, 'n_tokens_neox': 1359, 'n_words': 569}