示例#1
0
        private Rexp parseEvalResponse(RpacketResponse packet)
        {
            Int32 rxo = 0;

            Byte[] data = packet.Content;

            if (rserveVersion > 100)
            {
                /* since 0101 eval responds correctly by using DT_SEXP type/len header which is 4 bytes long */
                rxo = 4;

                /* we should check parameter type (should be DT_SEXP) and fail if it's not */
                if (data[0] != (byte)DataTypes.SEXP && data[0] != ((byte)DataTypes.SEXP | (byte)DataTypes.LARGE))
                {
                    throw new RconnectionException("Error while processing eval output: SEXP (type " +
                                                   DataTypes.SEXP.ToString() + ") expected but found result type " +
                                                   data[0].ToString() + ".");
                }

                if (data[0] == ((byte)DataTypes.SEXP | (byte)DataTypes.LARGE))
                {
                    rxo = 8; // large data need skip of 8 bytes
                }
                /* warning: we are not checking or using the length - we assume that only the one SEXP is returned. This is true for the current CMD_eval implementation, but may not be in the future. */
            }
            Rexp rx = new Rexp();

            if (data.Length > rxo)
            {
                Rexp.parseREXP(out rx, ref data, rxo);
            }
            return(rx);
        }
示例#2
0
        /// <summary>
        /// Method write string value to the remote file
        /// </summary>
        /// <param name="input">value to send</param>
        public void WriteFile(Stream input)
        {
            while (input.Position != input.Length)
            {
                Byte[] data;

                if ((input.Length - input.Position) > packetSize)
                {
                    data = new Byte[packetSize];
                }
                else
                {
                    data = new Byte[input.Length - input.Position];
                }

                input.Read(data, 0, data.Length);
                Rmessage msg    = new Rmessage(data);
                Rpacket  packet = new Rpacket(RCommands.writeFile, msg);

                sendedBytesCount += s.Send(packet.BinaryPacket, packet.PacketLength, SocketFlags.None);
                RpacketResponse response = new RpacketResponse(this.GetResponse());

                if (response.IsError)
                {
                    throw new RconnectionException(response.ErrorCode);
                }
            }
        }
示例#3
0
        /// <summary>
        /// Close file on the Rserve
        /// </summary>
        public void CloseFile()
        {
            Rpacket packet = new Rpacket(RCommands.closeFile);

            sendedBytesCount += s.Send(packet.BinaryPacket, packet.PacketLength, SocketFlags.None);
            RpacketResponse response = new RpacketResponse(this.GetResponse());

            if (response.IsError)
            {
                throw new RconnectionException(response.ErrorCode);
            }
        }
示例#4
0
        /// <summary>
        /// Assign a content of a REXP to a symbol in R. The symbol is created if
        /// it doesn't exist already.
        /// </summary>
        /// <param name="symbol">
        /// Symbol name.It is the responsibility of the user to make sure that the symbol
        /// name is valid in R. In fact R will always create the symbol, but it may not
        /// be accessible (examples: "bar\nfoo" or "bar$foo").
        /// </param>
        /// <param name="input">Contents</param>
        public void Assign(String symbol, Rexp input)
        {
            Rmessage Rsymbol = new Rmessage(symbol);
            Rpacket  packet  = new Rpacket(RCommands.assignSEXP, Rsymbol, input);

            sendedBytesCount += s.Send(packet.BinaryPacket, packet.PacketLength, SocketFlags.None);
            RpacketResponse response = new RpacketResponse(GetResponse());

            if (response.IsError)
            {
                throw new RconnectionException(response.ErrorCode);
            }
        }
示例#5
0
        /// <summary>
        /// Evaluates the given command without retriving the result
        /// </summary>
        /// <param name="Cmd">command</param>
        public void VoidEval(String Cmd)
        {
            Rmessage msg    = new Rmessage(Cmd);
            Rpacket  packet = new Rpacket(RCommands.voidEval, msg);

            s.Send(packet.BinaryPacket, packet.PacketLength, SocketFlags.None);
            RpacketResponse response = new RpacketResponse(this.GetResponse());

            if (response.IsError)
            {
                throw new RconnectionException(response.ErrorCode);
            }
        }
示例#6
0
        /// <summary>
        /// Method write string value to the remote file
        /// </summary>
        /// <param name="input">value to send</param>
        public void WriteFile(String input)
        {
            Rmessage msg    = new Rmessage(ASCIIEncoding.ASCII.GetBytes(input));
            Rpacket  packet = new Rpacket(RCommands.writeFile, msg);

            sendedBytesCount += s.Send(packet.BinaryPacket, packet.PacketLength, SocketFlags.None);
            RpacketResponse response = new RpacketResponse(this.GetResponse());

            if (response.IsError)
            {
                throw new RconnectionException(response.ErrorCode);
            }
        }
示例#7
0
        /// <summary>
        /// Remove a file on the Rserve
        /// </summary>
        /// <param name="FileName">
        /// File name should not contain any path delimiters, since
        /// Rserve may restrict the access to local working directory.
        /// </param>
        public void RemoveFile(String FileName)
        {
            Rmessage msg    = new Rmessage(FileName);
            Rpacket  packet = new Rpacket(RCommands.removeFile, msg);

            s.Send(packet.BinaryPacket, packet.PacketLength, SocketFlags.None);
            RpacketResponse response = new RpacketResponse(this.GetResponse());

            if (response.IsError)
            {
                throw new RconnectionException(response.ErrorCode);
            }
        }
示例#8
0
        /// <summary>Evaluates the given command and retrieves the result</summary>
        /// <param name="Cmd">command</param>
        /// <returns>R-xpression</returns>
        public Rexp Eval(String Cmd)
        {
            Rmessage message = new Rmessage(Cmd);                                   //Make message(command with header)
            Rpacket  packet  = new Rpacket(RCommands.eval, message);

            sendedBytesCount += s.Send(packet.BinaryPacket, packet.PacketLength, SocketFlags.None);
            RpacketResponse response = new RpacketResponse(GetResponse());

            if (response.IsOk && (response.ContentLength > 0))
            {
                return(parseEvalResponse(response));
            }
            else
            {
                throw new RconnectionException("Eval failed");
            }
        }
示例#9
0
        /// <summary>
        /// Reads specified number of bytes (or less) from the remote file.
        /// </summary>
        /// <param name="size">maximal number of bytes to read</param>
        /// <param name="data">buffer to store the read bytes</param>
        public void ReadFile(Int32 size, out Byte[] data)
        {
            Rmessage msg    = new Rmessage(size);
            Rpacket  packet = new Rpacket(RCommands.readFile, msg);

            sendedBytesCount += s.Send(packet.BinaryPacket, packet.PacketLength, SocketFlags.None);
            RpacketResponse response = new RpacketResponse(this.GetResponse());

            if (response.IsOk)
            {
                data = response.Content;
            }
            else
            {
                throw new RconnectionException(response.ErrorCode);
            }
        }
示例#10
0
        /// <summary>
        /// Reads specified number of bytes (or less) from the remote file.
        /// </summary>
        /// <param name="size">maximal number of bytes to read</param>
        /// <param name="Value">String value to store the read and ASCII decoded bytes</param>
        public void ReadFile(Int32 size, out String Value)
        {
            Rmessage msg    = new Rmessage(size);
            Rpacket  packet = new Rpacket(RCommands.readFile, msg);

            sendedBytesCount += s.Send(packet.BinaryPacket, packet.PacketLength, SocketFlags.None);
            RpacketResponse response = new RpacketResponse(this.GetResponse());

            if (response.IsOk)
            {
                Value = ASCIIEncoding.ASCII.GetString(response.Content, 0, response.Content.Length * 2);
            }
            else
            {
                throw new RconnectionException(response.ErrorCode);
            }
        }
示例#11
0
        ///** login using supplied user/pwd. Note that login must be the first
        ///command if used
        ///@param user username
        ///@param pwd password
        ///@return returns <code>true</code> on success */
        public void login(String user, String pwd)
        {
            if (authReq)
            {
                //if(!s.Connected);not connected
                if (ARtype == authTypes.crypt)
                {
                    String   command = user + "\n" + UnixCrypt.crypt(pwd, key);
                    Rmessage msg     = new Rmessage(command);
                    Rpacket  packet  = new Rpacket(RCommands.login, msg);

                    s.Send(packet.BinaryPacket, packet.PacketLength, SocketFlags.None);
                    RpacketResponse response = new RpacketResponse(this.GetResponse());

                    if (response.IsError)
                    {
                        throw new RconnectionException(response.ErrorCode);
                        //try {s.close();} catch (Exception e) {};
                        //is=null; os=null; s=null; connected=false;
                    }
                }
                else
                {
                    String   command = user + "\n" + pwd;
                    Rmessage msg     = new Rmessage(command);
                    Rpacket  packet  = new Rpacket(RCommands.login, msg);

                    s.Send(packet.BinaryPacket, packet.PacketLength, SocketFlags.None);
                    RpacketResponse response = new RpacketResponse(this.GetResponse());

                    if (response.IsError)
                    {
                        throw new RconnectionException(response.ErrorCode);
                        //try {s.close();} catch (Exception e) {};
                        //is=null; os=null; s=null; connected=false;
                    }
                }
            }
        }
示例#12
0
        /// <summary>
        /// Assign a content of a REXP to a symbol in R. The symbol is created if 
        /// it doesn't exist already.
        /// </summary>
        /// <param name="symbol">
        /// Symbol name.It is the responsibility of the user to make sure that the symbol 
        /// name is valid in R. In fact R will always create the symbol, but it may not 
        /// be accessible (examples: "bar\nfoo" or "bar$foo").
        /// </param>
        /// <param name="input">Contents</param>
        public void Assign(String symbol, Rexp input)
        {
            Rmessage Rsymbol = new Rmessage(symbol);
            Rpacket packet = new Rpacket(RCommands.assignSEXP, Rsymbol, input);

            sendedBytesCount += s.Send(packet.BinaryPacket, packet.PacketLength, SocketFlags.None);
            RpacketResponse response = new RpacketResponse(GetResponse());

            if (response.IsError)
            {
                throw new RconnectionException(response.ErrorCode);
            }
        }
示例#13
0
        /// <summary>
        /// Close file on the Rserve         
        /// </summary>
        public void CloseFile()
        {
            Rpacket packet = new Rpacket(RCommands.closeFile);

            sendedBytesCount += s.Send(packet.BinaryPacket, packet.PacketLength, SocketFlags.None);
            RpacketResponse response = new RpacketResponse(this.GetResponse());

            if (response.IsError)
            {
                throw new RconnectionException(response.ErrorCode);
            }
        }
示例#14
0
        ///** login using supplied user/pwd. Note that login must be the first
        ///command if used
        ///@param user username
        ///@param pwd password
        ///@return returns <code>true</code> on success */
        public void login(String user, String pwd)
        {
            if(authReq)
            {
                //if(!s.Connected);not connected
                if (ARtype == authTypes.crypt)
                {

                    String command = user + "\n" + UnixCrypt.crypt(pwd, key);
                    Rmessage msg = new Rmessage(command);
                    Rpacket packet = new Rpacket(RCommands.login, msg);

                    s.Send(packet.BinaryPacket, packet.PacketLength, SocketFlags.None);
                    RpacketResponse response = new RpacketResponse(this.GetResponse());

                    if (response.IsError)
                    {
                        throw new RconnectionException(response.ErrorCode);
                        //try {s.close();} catch (Exception e) {};
                        //is=null; os=null; s=null; connected=false;

                    }
                }
                else
                {
                    String command = user + "\n" + pwd;
                    Rmessage msg = new Rmessage(command);
                    Rpacket packet = new Rpacket(RCommands.login, msg);

                    s.Send(packet.BinaryPacket, packet.PacketLength, SocketFlags.None);
                    RpacketResponse response = new RpacketResponse(this.GetResponse());

                    if (response.IsError)
                    {
                        throw new RconnectionException(response.ErrorCode);
                        //try {s.close();} catch (Exception e) {};
                        //is=null; os=null; s=null; connected=false;

                    }
                }
            }
        }
示例#15
0
        /// <summary>Evaluates the given command and retrieves the result</summary>
        /// <param name="Cmd">command</param>
        /// <returns>R-xpression</returns>        
        public Rexp Eval(String Cmd)
        {
            Rmessage message = new Rmessage(Cmd);                                   //Make message(command with header)
            Rpacket packet = new Rpacket(RCommands.eval, message);

            sendedBytesCount += s.Send(packet.BinaryPacket, packet.PacketLength, SocketFlags.None);
            RpacketResponse response = new RpacketResponse(GetResponse());

            if (response.IsOk && (response.ContentLength > 0))
            {
                return (parseEvalResponse(response));
            }
            else throw new RconnectionException("Eval failed");
        }
示例#16
0
        /// <summary>
        /// Method write string value to the remote file
        /// </summary>
        /// <param name="input">value to send</param>
        public void WriteFile(Stream input)
        {
            while (input.Position != input.Length)
            {
                Byte[] data;

                if ((input.Length - input.Position) > packetSize) data = new Byte[packetSize];
                else data = new Byte[input.Length - input.Position];

                input.Read(data, 0, data.Length);
                Rmessage msg = new Rmessage(data);
                Rpacket packet = new Rpacket(RCommands.writeFile, msg);

                sendedBytesCount += s.Send(packet.BinaryPacket, packet.PacketLength, SocketFlags.None);
                RpacketResponse response = new RpacketResponse(this.GetResponse());

                if (response.IsError)
                {
                    throw new RconnectionException(response.ErrorCode);
                }
            }
        }
示例#17
0
        /// <summary>
        /// Evaluates the given command without retriving the result
        /// </summary>
        /// <param name="Cmd">command</param>
        public void VoidEval(String Cmd)
        {
            Rmessage msg = new Rmessage(Cmd);
            Rpacket packet = new Rpacket(RCommands.voidEval, msg);

            s.Send(packet.BinaryPacket, packet.PacketLength, SocketFlags.None);
            RpacketResponse response = new RpacketResponse(this.GetResponse());

            if (response.IsError)
            {
                throw new RconnectionException(response.ErrorCode);
            }
        }
示例#18
0
        /// <summary>
        /// Method write string value to the remote file
        /// </summary>
        /// <param name="input">value to send</param>
        public void WriteFile(String input)
        {
            Rmessage msg = new Rmessage(ASCIIEncoding.ASCII.GetBytes(input));
            Rpacket packet = new Rpacket(RCommands.writeFile, msg);

            sendedBytesCount += s.Send(packet.BinaryPacket, packet.PacketLength, SocketFlags.None);
            RpacketResponse response = new RpacketResponse(this.GetResponse());

            if (response.IsError)
            {
                throw new RconnectionException(response.ErrorCode);
            }
        }
示例#19
0
        /// <summary>
        /// Remove a file on the Rserve
        /// </summary>
        /// <param name="FileName">
        /// File name should not contain any path delimiters, since 
        /// Rserve may restrict the access to local working directory.
        /// </param>
        public void RemoveFile(String FileName)
        {
            Rmessage msg = new Rmessage(FileName);
            Rpacket packet = new Rpacket(RCommands.removeFile, msg);

            s.Send(packet.BinaryPacket, packet.PacketLength, SocketFlags.None);
            RpacketResponse response = new RpacketResponse(this.GetResponse());

            if (response.IsError)
            {
                throw new RconnectionException(response.ErrorCode);
            }
        }
示例#20
0
        /// <summary>
        /// Reads specified number of bytes (or less) from the remote file.
        /// </summary>
        /// <param name="size">maximal number of bytes to read</param>
        /// <param name="data">buffer to store the read bytes</param>
        public void ReadFile(Int32 size, out Byte[] data)
        {
            Rmessage msg = new Rmessage(size);
            Rpacket packet = new Rpacket(RCommands.readFile, msg);

            sendedBytesCount += s.Send(packet.BinaryPacket, packet.PacketLength, SocketFlags.None);
            RpacketResponse response = new RpacketResponse(this.GetResponse());

            if (response.IsOk)
            {
                data = response.Content;
            }
            else
            {
                throw new RconnectionException(response.ErrorCode);
            }
        }
示例#21
0
        /// <summary>
        /// Reads specified number of bytes (or less) from the remote file.
        /// </summary>
        /// <param name="size">maximal number of bytes to read</param>
        /// <param name="Value">String value to store the read and ASCII decoded bytes</param>
        public void ReadFile(Int32 size, out String Value)
        {
            Rmessage msg = new Rmessage(size);
            Rpacket packet = new Rpacket(RCommands.readFile, msg);

            sendedBytesCount += s.Send(packet.BinaryPacket, packet.PacketLength, SocketFlags.None);
            RpacketResponse response = new RpacketResponse(this.GetResponse());

            if (response.IsOk)
            {
                Value = ASCIIEncoding.ASCII.GetString(response.Content, 0, response.Content.Length * 2);
            }
            else
            {
                throw new RconnectionException(response.ErrorCode);
            }
        }
示例#22
0
        private Rexp parseEvalResponse(RpacketResponse packet)
        {
            Int32 rxo = 0;
            Byte[] data = packet.Content;

            if (rserveVersion > 100)
            {
                /* since 0101 eval responds correctly by using DT_SEXP type/len header which is 4 bytes long */
                rxo = 4;

                /* we should check parameter type (should be DT_SEXP) and fail if it's not */
                if (data[0] != (byte)DataTypes.SEXP && data[0] != ((byte)DataTypes.SEXP | (byte)DataTypes.LARGE))
                {
                    throw new RconnectionException("Error while processing eval output: SEXP (type " +
                        DataTypes.SEXP.ToString() + ") expected but found result type " +
                        data[0].ToString() + ".");
                }

                if (data[0] == ((byte)DataTypes.SEXP | (byte)DataTypes.LARGE))
                {
                    rxo = 8; // large data need skip of 8 bytes
                }
                /* warning: we are not checking or using the length - we assume that only the one SEXP is returned. This is true for the current CMD_eval implementation, but may not be in the future. */
            }
            Rexp rx = new Rexp();
            if (data.Length > rxo)
            {
                Rexp.parseREXP(out rx, ref data, rxo);
            }
            return rx;
        }