<commit_before>'use strict';

const Layer = require('./../../Stack/Layers/Layer');

const ConnectionManager = require('./ConnectionManager');
const MessageRouter = require('./MessageRouter');

class Connection extends Layer {
  constructor(cipLayer, options) {
    super(cipLayer);

    this.mergeOptionsWithDefaults(options);

    this._connectionState = 0;
    this._disconnectState = 0;

    this._sequenceCount = 0;
    this._callbacks = {};

    this._sequenceToContext = new Map();

    this.connect();
  }

  mergeOptionsWithDefaults(options) {
    if (!options) options = {};
    this.VendorID = options.VendorID || 0x1339;
    this.OriginatorSerialNumber = options.OriginatorSerialNumber || 42;
    this.ConnectionTimeoutMultiplier = options.ConnectionTimeoutMultiplier || 0x01;
    this.OtoTRPI = options.OtoTRPI || 0x00201234;
    this.OtoTNetworkConnectionParameters = options.OtoTNetworkConnectionParameters || 0x43F4;
    this.TtoORPI = options.TtoORPI || 0x00204001;
    this.TtoONetworkConnectionParameters = options.TtoONetworkConnectionParameters || 0x43F4;
    this.TransportClassTrigger = options.TransportClassTrigger || 0xA3 // 0xA3: Direction = Server, Production Trigger = Application Object, Trasport Class = 3
    this.ProcessorSlot = options.ProcessorSlot || 0;
  }

  connect(callback) {
    if (this._connectionState > 0) return;

    let self = this;
    self._connectionState = 1;

    this._connectCallback = callback;

    self.send(ConnectionManager.ForwardOpen(self), null, false);
  }

  disconnect(callback) {
    if (this._disconnecting === 1) return;
    if (this._connectionState === 0) {
      if (callback != null) {
        callback();
      }
      return;
    }

    this._disconnectState = 1;
    this._disconnectCallback = callback;
    this.send(ConnectionManager.ForwardClose(this), null, false);
  }<commit_msg>Use latest callback, improved connectionState handling<commit_after>'use strict';

const Layer = require('./../../Stack/Layers/Layer');

const ConnectionManager = require('./ConnectionManager');
const MessageRouter = require('./MessageRouter');

class Connection extends Layer {
  constructor(cipLayer, options) {
    super(cipLayer);

    this.mergeOptionsWithDefaults(options);

    this._connectionState = 0;
    this._disconnectState = 0;

    this._sequenceCount = 0;
    this._callbacks = {};

    this._sequenceToContext = new Map();

    this.connect();
  }

  mergeOptionsWithDefaults(options) {
    if (!options) options = {};
    this.VendorID = options.VendorID || 0x1339;
    this.OriginatorSerialNumber = options.OriginatorSerialNumber || 42;
    this.ConnectionTimeoutMultiplier = options.ConnectionTimeoutMultiplier || 0x01;
    this.OtoTRPI = options.OtoTRPI || 0x00201234;
    this.OtoTNetworkConnectionParameters = options.OtoTNetworkConnectionParameters || 0x43F4;
    this.TtoORPI = options.TtoORPI || 0x00204001;
    this.TtoONetworkConnectionParameters = options.TtoONetworkConnectionParameters || 0x43F4;
    this.TransportClassTrigger = options.TransportClassTrigger || 0xA3 // 0xA3: Direction = Server, Production Trigger = Application Object, Trasport Class = 3
    this.ProcessorSlot = options.ProcessorSlot || 0;
  }

  connect(callback) {
    this._connectCallback = callback;
    if (this._connectionState === 1) return;
    if (this._connectionState === 2 && callback != null) {
      if (callback != null) {
        callback();
      }
      return;
    }

    this._connectionState = 1;
    this.send(ConnectionManager.ForwardOpen(this), null, false);
  }

  disconnect(callback) {
    this._disconnectCallback = callback;
    if (this._disconnecting === 1) return;
    if (this._connectionState === 0) {
      if (callback != null) {
        callback();
      }
      return;
    }

    this._disconnectState = 1;
    this.send(ConnectionManager.ForwardClose(this), null, false);
  }
{'fraction_non_alphanumeric': 0.11729827275070895, 'fraction_numerical': 0.026037638566640885, 'mean_word_length': 3.7374847374847375, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '494231', 'n_tokens_mistral': 1209, 'n_tokens_neox': 1112, 'n_words': 224}