/// <summary>
        /// Implementation of the refresh method on RBroker interface
        /// /// </summary>
        /// <param name="config">Pooled Broker Configuration object</param>
        /// <remarks></remarks>
        public new void refresh(RBrokerConfig config)
        {
            if (!status().isIdle)
            {
                throw new Exception("RBroker is not idle, refresh not permitted.");
            }

            if (!(config is PooledBrokerConfig))
            {
                throw new Exception("PooledTaskBroker refresh requires PooledBrokerConfig.");
            }

            PooledBrokerConfig pooledConfig = (PooledBrokerConfig)config;

            try
            {
                /*
                 * Temporarily disable RBroker to permit
                 * configuration refresh.
                 */
                Interlocked.Exchange(ref m_refreshingConfig, 1);

                ProjectExecutionOptions options = ROptionsTranslator.migrate(pooledConfig.poolCreationOptions);

                foreach (Object resourceToken in m_resourceTokenPool)
                {
                    RProject rProject = (RProject)resourceToken;

                    /*
                     * Recycle project to remove all existing
                     * workspace objects and directory files.
                     */
                    rProject.recycle();

                    /*
                     * Execute code to cause workspace and directory
                     * preloads and adoptions to take place.
                     */
                    rProject.executeCode("# Refresh project on PooledTaskBroker.", options);
                }
            }
            catch (Exception rex)
            {
                throw new Exception("RBroker refresh failed with unexpected error=" + rex.ToString());
            }
            finally
            {
                /*
                 * Re-enabled RBroker following
                 * configuration refresh.
                 */
                Interlocked.Exchange(ref m_refreshingConfig, 0);
            }
        }
        /// <summary>
        /// Constructor for specifying a Pooled Instance of RBroker
        /// </summary>
        /// <param name="brokerConfig">Pooled Broker Configuration object</param>
        /// <remarks></remarks>
        public PooledTaskBroker(PooledBrokerConfig brokerConfig)
            : base((RBrokerConfig)brokerConfig)
        {
            m_rClient = RClientFactory.createClient(brokerConfig.deployrEndpoint, brokerConfig.maxConcurrentTaskLimit);

            m_rUser = m_rClient.login(brokerConfig.userCredentials);

            if (brokerConfig.poolCreationOptions != null)
            {
                if (brokerConfig.poolCreationOptions.releaseGridResources == true)
                {
                    m_rUser.releaseProjects();
                }
            }

            ProjectCreationOptions options = ROptionsTranslator.translate(brokerConfig.poolCreationOptions);

            List <RProject> deployrProjectPool = m_rUser.createProjectPool(brokerConfig.maxConcurrentTaskLimit, options);

            /*
             * Prep the base RBrokerEngine.
             */

            initEngine(deployrProjectPool.Count());

            /*
             * Initialize the resourceTokenPool with RProject.
             */
            foreach (RProject rProject in deployrProjectPool)
            {
                m_resourceTokenPool.TryAdd(rProject);
            }

            try
            {
                Task.Factory.StartNew(() => HTTPKeepAliveManager(m_rUser));
            }
            catch (Exception rex)
            {
                shutdown();
                throw new Exception("Broker failed to start HTTP keep-alive manager, cause: " + rex.ToString());
            }
        }
        /// <summary>
        /// Constructor for specifying a Pooled Instance of RBroker
        /// </summary>
        /// <param name="brokerConfig">Pooled Broker Configuration object</param>
        /// <remarks></remarks>
        public PooledTaskBroker(PooledBrokerConfig brokerConfig)
            : base((RBrokerConfig) brokerConfig)
        {
            m_rClient = RClientFactory.createClient(brokerConfig.deployrEndpoint, brokerConfig.maxConcurrentTaskLimit);

            m_rUser = m_rClient.login(brokerConfig.userCredentials);

            if (brokerConfig.poolCreationOptions != null)
            {
                if (brokerConfig.poolCreationOptions.releaseGridResources == true)
                {
                    m_rUser.releaseProjects();
                }
            }

            ProjectCreationOptions options = ROptionsTranslator.translate(brokerConfig.poolCreationOptions);

            List<RProject> deployrProjectPool = m_rUser.createProjectPool(brokerConfig.maxConcurrentTaskLimit, options);

            /*
             * Prep the base RBrokerEngine.
             */

            initEngine(deployrProjectPool.Count());

            /*
             * Initialize the resourceTokenPool with RProject.
             */
            foreach(RProject rProject in deployrProjectPool)
            {
                m_resourceTokenPool.TryAdd(rProject);
            }

            try
            {
                Task.Factory.StartNew(() => HTTPKeepAliveManager(m_rUser));
            }
            catch(Exception rex)
            {
                shutdown();
                throw new Exception("Broker failed to start HTTP keep-alive manager, cause: " + rex.ToString());
            }
        }
 /// <summary>
 /// Utility function for creating a Pooled Instance of RBroker
 /// </summary>
 /// <param name="brokerConfig">Pooled Broker Configuration object</param>
 /// <returns>PooledTaskBroker instance</returns>
 /// <remarks></remarks>
 public static RBroker pooledTaskBroker(PooledBrokerConfig brokerConfig)
 {
     return new PooledTaskBroker(brokerConfig);
 }
        /// <summary>
        /// Implementation of RBroker Interface 'shutdown' method
        /// </summary>
        /// <remarks></remarks>
        public void shutdown()
        {
            Interlocked.Exchange(ref m_taskBrokerIsActive, 0);

            if (m_resourceTokenPool.Count > 0)
            {
                Boolean releaseGridResources = false;

                if (m_brokerConfig is PooledBrokerConfig)
                {
                    PooledBrokerConfig pbcfg = (PooledBrokerConfig)m_brokerConfig;
                    releaseGridResources = pbcfg.poolCreationOptions.releaseGridResources;
                }

                if (releaseGridResources)
                {
                    /*
                     * If PooledTaskBroker resource tokens
                     * and rUser available, perform a server-wide
                     * flush of projects on the grid.
                     */
                    m_rUser.releaseProjects();
                }
                else
                {
                    /*
                     * If PooledTaskBroker resource tokens
                     * and rUser not available, perform
                     * project-by-project flush on the grid.
                     */

                    foreach (Object resourceToken in m_resourceTokenPool)
                    {
                        try
                        {
                            if (resourceToken is RProject)
                            {
                                RProject projectToken = (RProject)resourceToken;
                                projectToken.close();
                            }
                        }
                        catch (Exception cex)
                        {
                            throw new Exception("RBroker: project close failed, cause: " + cex.ToString());
                        }
                    }
                }
            }

            if (m_rClient != null)
            {
                try
                {
                    if (m_rUser != null)
                    {
                        m_rClient.logout(m_rUser);
                    }
                }
                catch (Exception rex)
                {
                    throw new Exception("RBroker: RClient logout failed, cause: " + rex.ToString());
                }
            }
        }
        public static void Execute()
        {
            try
            {
                /*
                 * 1. Prepare PooledBrokerConfig instance.
                 *
                 * This example creates an authenticated PooledTaskBroker
                 * to act as our scoring engine.
                 *
                 * The following steps initialize the pool of
                 * R sessions dedicated to our scoring engine.
                 *
                 * In this example, we pre-initialize the workspace
                 * for each R session in the pool to contain the
                 * R model used when scoring customer requests.
                 *
                 * The R model, insurModel.rData, is loaded from
                 * the DeployR repository into each workspace at
                 * startup time. Preloading the R model at startup
                 * reduces runtime overhead and improves overall
                 * response times in the application.
                 */
                RAuthentication rAuth = new RBasicAuthentication(Program.AUTH_USER_NAME, Program.AUTH_PASSWORD);

                PoolCreationOptions poolOptions = new PoolCreationOptions();

                PoolPreloadOptions preloadOptions = new PoolPreloadOptions();
                preloadOptions.filename = Program.TUTORIAL_INSURANCE_MODEL;
                preloadOptions.directory = Program.TUTORIAL_REPO_DIRECTORY;
                preloadOptions.author = Program.TUTORIAL_REPO_OWNER;

                poolOptions.preloadWorkspace = preloadOptions;

                /*
                 * 2. Create RBroker instance.
                 *
                 * The application designer, in conjunction with the
                 * DeployR administrator who provisions and sets
                 * limits on DeployR grid resources, need to decide how
                 * many R sessions will be reserved for the PooledRBroker
                 * used by our scoring engine application.
                 */

                int TARGET_POOL_SIZE = 10;

                PooledBrokerConfig brokerConfig = new PooledBrokerConfig(Program.DEPLOYR_ENDPOINT,
                                                                           rAuth,
                                                                           TARGET_POOL_SIZE,
                                                                           poolOptions);

                Console.WriteLine("About to create pooledTaskBroker.");

                RBroker rBroker = RBrokerFactory.pooledTaskBroker(brokerConfig);

                Console.WriteLine("R session pool size, requested: " +
                        TARGET_POOL_SIZE + " , actual: " +
                        rBroker.maxConcurrency() + "\n");

                /*
                 * 3. Create an RTaskAppSimulator. It will drive
                 * sample customer data scoring requests through the
                 * RBroker.
                 */

                ScoringEngineSimulation simulation = new ScoringEngineSimulation(rBroker);

                /*
                 * 4. Launch RTaskAppSimulator simulation.
                 */

                rBroker.simulateApp(simulation);

                /*
                 * 5. Block until all tasks are complete, and shutdown has been called
                 */
                rBroker.waitUntilShutdown();

            }
            catch(Exception tex)
            {
                Console.WriteLine("constructor: ex=" + tex.ToString());
            }
        }
 /// <summary>
 /// Utility function for creating a Pooled Instance of RBroker
 /// </summary>
 /// <param name="brokerConfig">Pooled Broker Configuration object</param>
 /// <returns>PooledTaskBroker instance</returns>
 /// <remarks></remarks>
 public static RBroker pooledTaskBroker(PooledBrokerConfig brokerConfig)
 {
     return(new PooledTaskBroker(brokerConfig));
 }