Ejemplo n.º 1
0
        public RserveConnection(RserveParameter parameter)
        {
            Parameter = parameter;

            try
            {
                if (Parameter.Scheme == "FILE")
                {
                    RTermToken = new CancellationTokenSource();
                    Task.Factory.StartNew(FindRTermProcess, RTermToken.Token,
                                          TaskCreationOptions.LongRunning, TaskScheduler.Default);
                }

                switch (Parameter.Scheme)
                {
                case "RSERVE":
                case "FILE":
                    RserveToken = new CancellationTokenSource();
                    Task.Factory.StartNew(ReConnectToRserve, RserveToken.Token,
                                          TaskCreationOptions.LongRunning, TaskScheduler.Default);

                    break;

                default:
                    throw new Exception($"The uri scheme \"{Parameter.Scheme}\" is unknown.");
                }
            }
            catch (Exception ex)
            {
                throw new Exception($"R connection could not be established: {ex.Message}");
            }
        }
Ejemplo n.º 2
0
        public RserveConnection GetConnection(RserveParameter rserveParams)
        {
            Interlocked.Increment(ref poolcounter);
            int poolnodeindex = poolcounter % (rserveParams.PoolNodes.Count);

            PoolNode node = rserveParams.PoolNodes.ElementAt(poolnodeindex);

            var hash = $"{node.rserveParameter.GetHashCode().ToString()}-{node.getNextConnectionNumber().ToString()}";

            logger.Info($"Connecting to node {poolnodeindex} with hash: {hash}");
            RserveConnection conn = null;



            if (rserveConns.ContainsKey(hash))
            {
                rserveConns.TryGetValue(hash, out conn);
                var dateTime = DateTime.MinValue;
                if (lastUsed.TryGetValue(hash, out dateTime))
                {
                    lastUsed.TryUpdate(hash, DateTime.Now, dateTime);
                }
            }
            else
            {
                conn = new RserveConnection(node.rserveParameter);
                Thread.Sleep(500);
                rserveConns.TryAdd(hash, conn);
                lastUsed.TryAdd(hash, DateTime.Now);
            }

            return(conn);
        }
Ejemplo n.º 3
0
        public RServeEvaluator(RserveParameter para)
        {
            connPool   = new RserveConnectionPool();
            rservePara = para;
            RserveConnection rserveConnInitial = connPool.GetConnection(rservePara);

            logger.Trace($"Rserve connection initiated: {rserveConnInitial!=null}");
        }
Ejemplo n.º 4
0
        public RServeEvaluator(RserveParameter para, bool enableScript, string functionDefinitionsFile)
        {
            allowScript = enableScript;
            if (!String.IsNullOrEmpty(functionDefinitionsFile))
            {
                definedFunctions = new DefinedFunctions(functionDefinitionsFile);
            }
            CreateCapabilities();
            connPool   = new RserveConnectionPool();
            rservePara = para;

            RserveConnection rserveConnInitial = connPool.GetConnection(rservePara);

            logger.Trace($"Rserve connection initiated: {rserveConnInitial != null}");
        }
Ejemplo n.º 5
0
        public RserveConnection GetConnection(RserveParameter rserveParams)
        {
            var hash = rserveParams.GetHashCode().ToString();
            RserveConnection conn = null;

            if (rserveConns.ContainsKey(hash))
            {
                rserveConns.TryGetValue(hash, out conn);
                var dateTime = DateTime.MinValue;
                if (lastUsed.TryGetValue(hash, out dateTime))
                {
                    lastUsed.TryUpdate(hash, DateTime.Now, dateTime);
                }
            }
            else
            {
                conn = new RserveConnection(rserveParams);
                Thread.Sleep(500);
                rserveConns.TryAdd(hash, conn);
                lastUsed.TryAdd(hash, DateTime.Now);
            }

            return(conn);
        }
Ejemplo n.º 6
0
 public PoolNode(string address, int port, RserveParameter masterparams, int connections)
 {
     rserveParameter = new RserveParameter(new Uri($"rserve://{address}:{port}"), port, masterparams.InitScript, masterparams.RProcessCommandLineArgs, masterparams.User, masterparams.Password);
     poolcounter     = 0;
     Connections     = connections;
 }
Ejemplo n.º 7
0
        static void Main(string[] args)
        {
            try
            {
                var  grpcHost                = Convert.ToString(Properties.Settings.Default.grpcHost ?? "localhost");
                int  grpcPort                = Convert.ToInt32(Properties.Settings.Default.grpcPort ?? "50051");
                var  rserveHost              = Convert.ToString(Properties.Settings.Default.rserveHost ?? "127.0.0.1");
                int  rservePort              = Convert.ToInt32(Properties.Settings.Default.rservePort ?? "6311");
                var  rserveUser              = Convert.ToString(Properties.Settings.Default.rserveUser ?? "");
                var  rservePassword          = Convert.ToString(Properties.Settings.Default.rservePassword ?? "");
                var  rProcessPath            = Convert.ToString(Properties.Settings.Default.rProcessPathToStart ?? "");
                var  rProcessCommandLineArgs = Convert.ToString(Properties.Settings.Default.rProcessCommandLineArgs ?? "");
                var  rserveInitScript        = Convert.ToString(Properties.Settings.Default.rserveInitScript ?? "");
                bool allowScript             = Convert.ToBoolean(Properties.Settings.Default.allowScript);
                var  functionDefinitionsFile = Convert.ToString(Properties.Settings.Default.functionDefinitionsFile ?? "");

                var sslCredentials            = ServerCredentials.Insecure;
                var certificateFolderFullPath = Convert.ToString(Properties.Settings.Default.certificateFolderFullPath ?? "");

                if (certificateFolderFullPath.Length > 3)
                {
                    var rootCertPath   = Path.Combine(certificateFolderFullPath, @"root_cert.pem");
                    var serverCertPath = Path.Combine(certificateFolderFullPath, @"sse_server_cert.pem");
                    var serverKeyPath  = Path.Combine(certificateFolderFullPath, @"sse_server_key.pem");
                    if (File.Exists(rootCertPath) &&
                        File.Exists(serverCertPath) &&
                        File.Exists(serverKeyPath))
                    {
                        var rootCert      = File.ReadAllText(rootCertPath);
                        var serverCert    = File.ReadAllText(serverCertPath);
                        var serverKey     = File.ReadAllText(serverKeyPath);
                        var serverKeyPair = new KeyCertificatePair(serverCert, serverKey);
                        sslCredentials = new SslServerCredentials(new List <KeyCertificatePair>()
                        {
                            serverKeyPair
                        }, rootCert, true);
                        logger.Info($"Path to certificates ({certificateFolderFullPath}) and certificate files found. Opening secure channel with mutual authentication.");
                    }
                    else
                    {
                        logger.Warn($"Path to certificates ({certificateFolderFullPath}) not found or files missing. Opening insecure channel instead.");
                    }
                }
                else
                {
                    logger.Info("No certificates defined. Opening insecure channel.");
                }

                var uri = new Uri($"rserve://{rserveHost}:{rservePort}");
                if (!String.IsNullOrEmpty(rProcessPath))
                {
                    uri = new Uri(rProcessPath);
                }
                var parameter = new RserveParameter(uri, rservePort, rserveInitScript, rProcessCommandLineArgs, rserveUser, rservePassword);

                using (var rServeEvaluator = new RServeEvaluator(parameter, allowScript, functionDefinitionsFile))
                {
                    var server = new Server
                    {
                        Services = { Connector.BindService(rServeEvaluator) },
                        Ports    = { new ServerPort(grpcHost, grpcPort, sslCredentials) }
                    };

                    server.Start();
                    Console.WriteLine("Press any key to stop SSEtoRserve...");
                    logger.Info($"gRPC listening on port {grpcPort}");
                    Console.ReadKey();
                    logger.Info("Shutting down SSEtoRserve... Bye!");
                    server?.ShutdownAsync().Wait();
                    rServeEvaluator?.Dispose();
                }
            }
            catch (Exception ex)
            {
                logger.Error($"Error in main entry point of SSEtoRserve: {ex}");
            }
        }
Ejemplo n.º 8
0
        static void Main(string[] args)
        {
            try
            {
                var builder = new ConfigurationBuilder()
                              .SetBasePath(Directory.GetCurrentDirectory())
                              .AddXmlFile("config.xml");

                Configuration = builder.Build();

                //Convert.ToString(Configuration["grpcHost"] ?? "localhost");
                var grpcHost        = ParameterValue("grpcHost", "localhost");
                int grpcPort        = Convert.ToInt32(ParameterValue("grpcPort", "50051"));
                var rserveUser      = Convert.ToString(ParameterValue("rserveUser", ""));
                var rservePassword  = Convert.ToString(ParameterValue("rservePassword", ""));
                var rservePoolNodes = Convert.ToString(ParameterValue("rservePoolNodes", ""));

                string rProcessPath;

                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    rProcessPath = Convert.ToString(Configuration["rProcessPathToStart-Win"] ?? "");
                }
                else
                {
                    rProcessPath = Convert.ToString(Configuration["rProcessPathToStart-Linux"] ?? "");
                }

                var  rProcessCommandLineArgs = Convert.ToString(Configuration["rProcessCommandLineArgs"] ?? "");
                var  rserveInitScript        = Convert.ToString(Configuration["rserveInitScript"] ?? "");
                bool allowScript             = Convert.ToBoolean(ParameterValue("allowScript", "True"));
                var  functionDefinitionsFile = Convert.ToString(Configuration["functionDefinitionsFile"] ?? "");



                var sslCredentials            = ServerCredentials.Insecure;
                var certificateFolderFullPath = Convert.ToString(ParameterValue("certificateFolderFullPath", ""));

                if (certificateFolderFullPath.Length > 3)
                {
                    var rootCertPath   = Path.Combine(certificateFolderFullPath, @"root_cert.pem");
                    var serverCertPath = Path.Combine(certificateFolderFullPath, @"sse_server_cert.pem");
                    var serverKeyPath  = Path.Combine(certificateFolderFullPath, @"sse_server_key.pem");
                    if (File.Exists(rootCertPath) &&
                        File.Exists(serverCertPath) &&
                        File.Exists(serverKeyPath))
                    {
                        var rootCert      = File.ReadAllText(rootCertPath);
                        var serverCert    = File.ReadAllText(serverCertPath);
                        var serverKey     = File.ReadAllText(serverKeyPath);
                        var serverKeyPair = new KeyCertificatePair(serverCert, serverKey);
                        sslCredentials = new SslServerCredentials(new List <KeyCertificatePair>()
                        {
                            serverKeyPair
                        }, rootCert, true);
                        logger.Info($"Path to certificates ({certificateFolderFullPath}) and certificate files found. Opening secure channel with mutual authentication.");
                    }
                    else
                    {
                        logger.Warn($"Path to certificates ({certificateFolderFullPath}) not found or files missing. Opening insecure channel instead.");
                    }
                }
                else
                {
                    logger.Info("No certificates defined. Opening insecure channel.");
                }


                var parameter = new RserveParameter(rserveInitScript, rProcessCommandLineArgs, rserveUser, rservePassword, rservePoolNodes);

                //GrpcEnvironment.SetThreadPoolSize(100);
                //GrpcEnvironment.SetCompletionQueueCount(100);

                using (var rServeEvaluator = new RServeEvaluator(parameter, allowScript, functionDefinitionsFile))
                {
                    var server = new Server
                    {
                        Services = { Connector.BindService(rServeEvaluator) },
                        Ports    = { new ServerPort(grpcHost, grpcPort, sslCredentials) }
                    };
                    server.RequestCallTokensPerCompletionQueue = 32768;

                    server.Start();
                    //Console.WriteLine("Press any key to stop SSEtoRserve...");
                    logger.Info($"gRPC listening to host {grpcHost}");
                    logger.Info($"gRPC listening on port {grpcPort}");
                    //Console.ReadKey();
                    try {
                        while (true)
                        {
                            Thread.Sleep(10000);
                        }
                    } finally {
                        logger.Info("Shutting down SSEtoRserve... Bye!");
                        server?.ShutdownAsync().Wait();
                        rServeEvaluator?.Dispose();
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error($"Error in main entry point of SSEtoRserve: {ex}");
                Console.WriteLine(ex);
                Console.ReadKey();
            }
        }