void init()

Initializes the Link. There is no guarantee that the link will be ready when this method returns. If the configure method is not called prior to calling this method, it is called.

This method handles the following:

  • calling configure if it has not been called.
  • creating a provider if it has not been created.
  • loading the nodes.json file.
  • creating the actual link.
  • discovering brokers if that was enabled.

Source

void init() {
  if (!_configured) {
    if (!configure()) {
      return;
    }
  }

  if (_initialized) {
    return;
  }

  _initialized = true;

  if (profiles != null) {
    for (var key in profiles.keys.toList()) {
      var value = profiles[key];

      if (value is _TwoArgumentProfileFunction) {
        profiles[key] = (String path) {
          return value(path, provider);
        };
      }
    }
  }

  if (provider == null) {
    provider = new SimpleNodeProvider(null, profiles);
    (provider as SimpleNodeProvider).setPersistFunction(saveAsync);
  }

  loadNodesFile();

  void doRun() {
    link = createHttpLink();
    _ready = true;

    if (_connectOnReady) {
      connect();
    }
  }

  if (_discoverBroker) {
    var discovery = new BrokerDiscoveryClient();
    new Future(() async {
      await discovery.init();
      try {
        var broker = await chooseBroker(discovery.discover());
        logger.info("Discovered Broker at ${broker}");
        brokerUrl = broker;
        doRun();
      } catch (e, stack) {
        logger.severe("Failed to discover a broker.", e, stack);
        exit(1);
      }
    });
  } else {
    doRun();
  }
}