protected override async Task <Task> StartProcessAsync(CancellationToken stopCancellationToken)
        {
            Log.Debug("Trying to accure instance semaphore");
            if (!_instanceSemaphore.WaitOne(0))
            {
                throw new BrokerIsAlreadyRunningException(_workingDir);
            }
            try
            {
                stopCancellationToken.ThrowIfCancellationRequested();
                Log.Info("Starting broker in directory {0}", _workingDir);
                BrokerProcessor brokerProcess;
                using (stopCancellationToken.Register(OnStop))
                {
                    await _transportServer.StartAsync().ConfigureAwait(false);

                    brokerProcess = new BrokerProcessor(
                        _transportServer,
                        _registryProvider,
                        DefaultProtocolSerializationProvider,
                        _appLifecycleManager);
                    Log.Info("Broker started in directory {0}", _workingDir);
                }
                return(TaskRunner.RunInBackground(
                           async() =>
                {
                    try
                    {
                        using (stopCancellationToken.Register(OnStop))
                        {
                            await brokerProcess.Completion.ConfigureAwait(false);
                        }
                    }
                    finally
                    {
                        _instanceSemaphore.Release();
                    }
                },
                           stopCancellationToken));
            }
            catch
            {
                _instanceSemaphore.Release();
                throw;
            }
        }
Exemple #2
0
 public BrokerRunner(string metadataDir = null, IRegistryProvider registryProvider = null)
 {
     _workingDir       = Directory.GetCurrentDirectory();
     metadataDir       = metadataDir ?? Path.Combine(_workingDir, "metadata");
     _transportServers = new ITransportServer[]
     {
         new TransportServer(new PipeTransmissionServer(_workingDir), DefaultTransportSerializationProvider),
         new TransportServer(new WebSocketTransmissionServer(_workingDir), DefaultTransportSerializationProvider)
     };
     _connectionListener = new ServerConnectionListener(_transportServers);
     registryProvider    = registryProvider ?? JsonRegistryProvider.Initialize(Path.Combine(metadataDir, "interop.json"));
     _connectionTracker  = new AppLifecycleManager(metadataDir);
     _brokerProcessor    = new BrokerProcessor(
         _connectionListener.In,
         registryProvider,
         DefaultProtocolSerializationProvider,
         _connectionTracker);
     OnStop(_connectionListener.Stop);
 }