Example #1
0
        private MQTTBroker StartBrokerAutoFindPort()
        {
            var        port   = MQTTBroker.DefaultPort - 1;
            MQTTBroker broker = null;

            var brokerStarted = false;

            do
            {
                ++port;

                if (!IsPortAvailable(port))
                {
                    continue;
                }

                try
                {
                    broker = new MQTTBroker(port);
                    broker.Start();
                    brokerStarted = true;
                }
                catch (Exception)
                {
                    continue;
                }
            } while (!brokerStarted);

            return(broker);
        }
Example #2
0
        private MQTTBroker StartBrokerWithPort(int port)
        {
            var broker = new MQTTBroker(port);

            broker.Start();
            return(broker);
        }
Example #3
0
        public override void Start()
        {
            if (_isRunning)
            {
                throw new InvalidOperationException("MQTT Data Transport is already running");
            }

            if (_config.IsBroker)
            {
                _broker = StartBroker(_config.BrokerPort);
            }

            _discovery = _config.UseAutoDiscovery ? (IDiscovery) new AutoDiscovery(_config) : new StaticDiscovery(_config);
            _discovery.BrokerDiscovered += (networkID, address, port) =>
            {
                _brokerIP   = address;
                _brokerPort = port;
                InvokeBrokerDiscovered(networkID, address, port);
                _startWait.Set();

                if (!_isRunning)
                {
                    Log.Information($"MQTTDataTransport: MQTT Broker found.");
                    Log.Information($"MQTTDataTransport:     RoboCore Network: {networkID}");
                    Log.Information($"MQTTDataTransport:     Broker Address: {address}:{port}");
                }
            };
            Log.Information("MQTTDataTransport: Waiting to discover the MQTT Broker");
            _discovery.Start();

            while (!_startWait.WaitOne())
            {
            }

            Log.Information("MQTTDataTransport: Connecting to Broker...");
            CreateMQTTClient();

            while (!_connectWait.WaitOne())
            {
            }

            Log.Information("MQTTDataTransport: Connected to Broker");

            _isRunning = true;
        }