/// <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); } }
/// <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); } }
public Rpacket(RCommands RCommand, Rmessage Param, Rexp Value) { /* Create array to message */ Byte[] binValue = Value.getBinaryRepresentation(); Int32 ValueLength = binValue.Length; Int32 ParamLength = Param.Message.Length; message = new Byte[ParamLength + ValueLength]; header = new Rheader(RCommand, (ParamLength + ValueLength)); Array.Copy(Param.Message, 0, message, 0, ParamLength); Array.Copy(binValue, 0, message, ParamLength, ValueLength); }
/// <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); } }
/// <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"); } }
/// <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); } }
///** 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; } } } }
public Rpacket(RCommands RCommand, Rmessage Param1, Rmessage Param2) { }
public Rpacket(RCommands RCommand, Rmessage Param) { message = Param.Message; header = new Rheader(RCommand, Param.Message.Length); }
/// <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); } } }
/// <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); } }
/// <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); } }
///** 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; } } } }
/// <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"); }