History

TVA.ServiceProcess - Added new Trasfer command to ServiceHelper for uploading and downloading files from remote console. - Added new Files command to ServiceHelper for copying and moving files on the server.

Pinal Patel committed 10 years ago

Commit c8be7121 parent 4e21d082 files · +244 −15Browse files

Unified · Split

2 files
  1. Source/Libraries/GSF.ServiceProcess/ClientHelper.cs +59 −3
  2. Source/Libraries/GSF.ServiceProcess/ServiceHelper.cs +185 −12
Source/Libraries/GSF.ServiceProcess/ClientHelper.cs+59−3
@@ -49,14 +49,17 @@ · 48 unmodified lines
49//49//
50//******************************************************************************************************50//******************************************************************************************************
51 51
52using GSF.Communication;
53using GSF.Configuration;
54using GSF.Console;
55using GSF.IO;
52using System;56using System;
53using System.ComponentModel;57using System.ComponentModel;
54using System.Configuration;58using System.Configuration;
55using System.Drawing;59using System.Drawing;
60using System.IO;
56using System.Text;61using System.Text;
57using System.Threading;62using System.Threading;
58using GSF.Communication;
59using GSF.Configuration;
60 63
61namespace GSF.ServiceProcess64namespace GSF.ServiceProcess
62{65{
@@ -550,7 +553,31 @@ namespace GSF.ServiceProcess · 487 unmodified lines
550 /// <param name="request"><see cref="ClientRequest"/> object to be sent.</param>553 /// <param name="request"><see cref="ClientRequest"/> object to be sent.</param>
551 public void SendRequest(ClientRequest request)554 public void SendRequest(ClientRequest request)
552 {555 {
553 m_remotingClient.SendAsync(request);556 try
557 {
558 // Intercept TRANSFER requests for uploads.
559 if (request.Command == "TRANSFER" && request.Arguments.Exists("upload"))
560 {
561 string source = FilePath.GetAbsolutePath(request.Arguments["orderedarg1"]);
562 if (File.Exists(source))
563 {
564 // Attach the file content.
565 request.Attachments.Add(File.ReadAllBytes(source));
566 }
567 else
568 {
569 // File does not exist.
570 throw new FileNotFoundException(string.Format("File '{0}' does not exist", source));
571 }
572 }
573
574 // Pass the request along.
575 m_remotingClient.SendAsync(request);
576 }
577 catch (Exception ex)
578 {
579 UpdateStatus(UpdateType.Alarm, "Error processing request - {0}", ex.Message);
580 }
554 }581 }
555 582
556 /// <summary>583 /// <summary>
@@ -834,6 +861,35 @@ namespace GSF.ServiceProcess · 277 unmodified lines
834 break;861 break;
835 }862 }
836 break;863 break;
864 case "TRANSFER:SUCCESS":
865 if (response.Attachments.Count == 2)
866 {
867 // Extract data from response.
868 byte[] content = (byte[])response.Attachments[0];
869 Arguments arguments = (Arguments)response.Attachments[1];
870 string target = arguments["orderedarg2"];
871 bool overwrite = arguments.Exists("overwrite");
872
873 target = FilePath.GetAbsolutePath(target);
874 if (!File.Exists(target) || overwrite)
875 {
876 // Save the received file.
877 UpdateStatus(UpdateType.Information, "Saving file '{0}'...\r\n\r\n", target);
878 File.WriteAllBytes(target, content);
879 UpdateStatus(UpdateType.Information, "File '{0}' saved successfully.\r\n\r\n", target);
880 }
881 else
882 {
883 // File exists and cannot be overwritten.
884 UpdateStatus(UpdateType.Alarm, "File '{0}' already exists.\r\n\r\n", target);
885 }
886 }
887 else
888 {
889 // Response is malformed.
890 UpdateStatus(UpdateType.Alarm, "{0} response is malformed.\r\n\r\n", response.Type);
891 }
892 break;
837 default:893 default:
838 OnReceivedServiceResponse(response);894 OnReceivedServiceResponse(response);
839 break;895 break;
Source/Libraries/GSF.ServiceProcess/ServiceHelper.cs+185−12
@@ -89,12 +89,25 @@ · 88 unmodified lines
89//89//
90//******************************************************************************************************90//******************************************************************************************************
91 91
92using GSF.Annotations;
93using GSF.Collections;
94using GSF.Communication;
95using GSF.Configuration;
96using GSF.Diagnostics;
97using GSF.ErrorManagement;
98using GSF.IO;
99using GSF.Reflection;
100using GSF.Scheduling;
101using GSF.Security;
102using GSF.Security.Cryptography;
103using GSF.Units;
92using System;104using System;
93using System.Collections.Generic;105using System.Collections.Generic;
94using System.ComponentModel;106using System.ComponentModel;
95using System.Configuration;107using System.Configuration;
96using System.Diagnostics;108using System.Diagnostics;
97using System.Drawing;109using System.Drawing;
110using System.IO;
98using System.Linq;111using System.Linq;
99using System.Reflection;112using System.Reflection;
100using System.Security;113using System.Security;
@@ -102,18 +115,6 @@ using System.Security.Principal; · 1 unmodified line
102using System.ServiceProcess;115using System.ServiceProcess;
103using System.Text;116using System.Text;
104using System.Threading;117using System.Threading;
105using GSF.Annotations;
106using GSF.Collections;
107using GSF.Communication;
108using GSF.Configuration;
109using GSF.Diagnostics;
110using GSF.ErrorManagement;
111using GSF.IO;
112using GSF.Reflection;
113using GSF.Scheduling;
114using GSF.Security;
115using GSF.Security.Cryptography;
116using GSF.Units;
117 118
118namespace GSF.ServiceProcess119namespace GSF.ServiceProcess
119{120{
@@ -1223,6 +1224,8 @@ namespace GSF.ServiceProcess · 1103 unmodified lines
1223 m_clientRequestHandlers.Add(new ClientRequestHandler("Version", "Displays current service version", ShowVersion, new[] { "ver" }));1224 m_clientRequestHandlers.Add(new ClientRequestHandler("Version", "Displays current service version", ShowVersion, new[] { "ver" }));
1224 m_clientRequestHandlers.Add(new ClientRequestHandler("Time", "Displays current system time", ShowTime));1225 m_clientRequestHandlers.Add(new ClientRequestHandler("Time", "Displays current system time", ShowTime));
1225 m_clientRequestHandlers.Add(new ClientRequestHandler("User", "Displays current user information", ShowUser, new[] { "whoami" }));1226 m_clientRequestHandlers.Add(new ClientRequestHandler("User", "Displays current user information", ShowUser, new[] { "whoami" }));
1227 m_clientRequestHandlers.Add(new ClientRequestHandler("Files", "Manages files on the server", ManageFiles));
1228 m_clientRequestHandlers.Add(new ClientRequestHandler("Transfer", "Transfers files to and from the server", TransferFile));
1226 1229
1227 // Enable telnet support if requested.1230 // Enable telnet support if requested.
1228 if (m_supportTelnetSessions)1231 if (m_supportTelnetSessions)
@@ -3915,6 +3918,176 @@ namespace GSF.ServiceProcess · 2686 unmodified lines
3915 }3918 }
3916 }3919 }
3917 3920
3921 private void ManageFiles(ClientRequestInfo requestInfo)
3922 {
3923 if (requestInfo.Request.Arguments.ContainsHelpRequest || requestInfo.Request.Arguments.OrderedArgCount < 2)
3924 {
3925 StringBuilder helpMessage = new StringBuilder();
3926
3927 helpMessage.Append("Manages files on the server.");
3928 helpMessage.AppendLine();
3929 helpMessage.AppendLine();
3930 helpMessage.Append(" Usage:");
3931 helpMessage.AppendLine();
3932 helpMessage.Append(" Files \"Source Path\" \"Target Path\" -options");
3933 helpMessage.AppendLine();
3934 helpMessage.AppendLine();
3935 helpMessage.Append(" Options:");
3936 helpMessage.AppendLine();
3937 helpMessage.Append(" -?".PadRight(20));
3938 helpMessage.Append("Displays this help message");
3939 helpMessage.AppendLine();
3940 helpMessage.Append(" -copy".PadRight(20));
3941 helpMessage.Append("Copies files from one location to another");
3942 helpMessage.AppendLine();
3943 helpMessage.Append(" -move".PadRight(20));
3944 helpMessage.Append("Moves files from one location to another");
3945 helpMessage.AppendLine();
3946 helpMessage.Append(" -overwrite".PadRight(20));
3947 helpMessage.Append("Overwrites files if they exist in the target location");
3948 helpMessage.AppendLine();
3949 helpMessage.AppendLine();
3950
3951 UpdateStatus(requestInfo.Sender.ClientID, UpdateType.Information, helpMessage.ToString());
3952 }
3953 else
3954 {
3955 string source = FilePath.GetAbsolutePath(requestInfo.Request.Arguments["orderedarg1"]);
3956 string target = FilePath.GetDirectoryName(FilePath.GetAbsolutePath(requestInfo.Request.Arguments["orderedarg2"]));
3957 string directory = FilePath.GetDirectoryName(source);
3958 string filePattern = FilePath.GetFileName(source);
3959 bool copy = requestInfo.Request.Arguments.Exists("copy");
3960 bool move = requestInfo.Request.Arguments.Exists("move");
3961 bool overwrite = requestInfo.Request.Arguments.Exists("overwrite");
3962
3963 string targetFile;
3964 foreach (string sourceFile in Directory.GetFiles(directory, filePattern))
3965 {
3966 targetFile = Path.Combine(target, FilePath.GetFileName(sourceFile));
3967 if (!File.Exists(targetFile) || overwrite)
3968 {
3969 // File can be copied or moved.
3970 if (copy)
3971 {
3972 // Copy the file.
3973 UpdateStatus(requestInfo.Sender.ClientID, UpdateType.Information, "Copying file '{0}'...\r\n\r\n", target);
3974 File.Copy(sourceFile, targetFile, true);
3975 UpdateStatus(requestInfo.Sender.ClientID, UpdateType.Information, "File '{0}' copied successfully.\r\n\r\n", target);
3976 }
3977 else if (move)
3978 {
3979 // Move the file.
3980 UpdateStatus(requestInfo.Sender.ClientID, UpdateType.Information, "Moving file '{0}'...\r\n\r\n", target);
3981 File.Delete(targetFile);
3982 File.Move(sourceFile, targetFile);
3983 UpdateStatus(requestInfo.Sender.ClientID, UpdateType.Information, "File '{0}' moved successfully.\r\n\r\n", target);
3984 }
3985 else
3986 {
3987 // Invalid command option specified.
3988 UpdateStatus(UpdateType.Alarm, "Command option is not valid.\r\n\r\n");
3989 }
3990 }
3991 else
3992 {
3993 // File already exists.
3994 UpdateStatus(UpdateType.Alarm, "File '{0}' already exists.\r\n\r\n", targetFile);
3995 }
3996 }
3997 }
3998 }
3999
4000 private void TransferFile(ClientRequestInfo requestInfo)
4001 {
4002 if (requestInfo.Request.Arguments.ContainsHelpRequest || requestInfo.Request.Arguments.OrderedArgCount < 2)
4003 {
4004 StringBuilder helpMessage = new StringBuilder();
4005
4006 helpMessage.Append("Transfers files to and from the server.");
4007 helpMessage.AppendLine();
4008 helpMessage.AppendLine();
4009 helpMessage.Append(" Usage:");
4010 helpMessage.AppendLine();
4011 helpMessage.Append(" Transfer \"Source File Path\" \"Target File Path\" -options");
4012 helpMessage.AppendLine();
4013 helpMessage.AppendLine();
4014 helpMessage.Append(" Options:");
4015 helpMessage.AppendLine();
4016 helpMessage.Append(" -?".PadRight(20));
4017 helpMessage.Append("Displays this help message");
4018 helpMessage.AppendLine();
4019 helpMessage.Append(" -upload".PadRight(20));
4020 helpMessage.Append("Uploads file from local machine to server");
4021 helpMessage.AppendLine();
4022 helpMessage.Append(" -download".PadRight(20));
4023 helpMessage.Append("Downloads file from server to local machine");
4024 helpMessage.AppendLine();
4025 helpMessage.Append(" -overwrite".PadRight(20));
4026 helpMessage.Append("Overwrites file if one exists in the target location");
4027 helpMessage.AppendLine();
4028 helpMessage.AppendLine();
4029
4030 UpdateStatus(requestInfo.Sender.ClientID, UpdateType.Information, helpMessage.ToString());
4031 }
4032 else
4033 {
4034 string source = requestInfo.Request.Arguments["orderedarg1"];
4035 string target = requestInfo.Request.Arguments["orderedarg2"];
4036 bool upload = requestInfo.Request.Arguments.Exists("upload");
4037 bool download = requestInfo.Request.Arguments.Exists("download");
4038 bool overwrite = requestInfo.Request.Arguments.Exists("overwrite");
4039
4040 if (upload)
4041 {
4042 // Request is for uploading a file.
4043 if (requestInfo.Request.Attachments.Count == 1)
4044 {
4045 // File content is present.
4046 target = FilePath.GetAbsolutePath(target);
4047 if (!File.Exists(target) || overwrite)
4048 {
4049 // Save the received file.
4050 UpdateStatus(requestInfo.Sender.ClientID, UpdateType.Information, "Saving file '{0}'...\r\n\r\n", target);
4051 File.WriteAllBytes(target, (byte[])requestInfo.Request.Attachments[0]);
4052 UpdateStatus(requestInfo.Sender.ClientID, UpdateType.Information, "File '{0}' saved successfully.\r\n\r\n", target);
4053 }
4054 else
4055 {
4056 // File exists and cannot be overwritten.
4057 UpdateStatus(UpdateType.Alarm, "File '{0}' already exists.\r\n\r\n", target);
4058 }
4059 }
4060 else
4061 {
4062 // File content is not present.
4063 UpdateStatus(UpdateType.Alarm, "Content for file '{0}' is missing.\r\n\r\n", target);
4064 }
4065 }
4066 else if (download)
4067 {
4068 // Request is for uploading a file.
4069 source = FilePath.GetAbsolutePath(source);
4070 if (File.Exists(source))
4071 {
4072 // Send file to client.
4073 UpdateStatus(requestInfo.Sender.ClientID, UpdateType.Information, "Sending file '{0}'...\r\n\r\n", source);
4074 SendActionableResponse(requestInfo, true, File.ReadAllBytes(source));
4075 UpdateStatus(requestInfo.Sender.ClientID, UpdateType.Information, "File '{0}' sent successfully.\r\n\r\n", source);
4076 }
4077 else
4078 {
4079 // File does not exist.
4080 UpdateStatus(UpdateType.Alarm, "File '{0}' does not exist.\r\n\r\n", source);
4081 }
4082 }
4083 else
4084 {
4085 // Invalid command option specified.
4086 UpdateStatus(UpdateType.Alarm, "Command option is not valid.\r\n\r\n");
4087 }
4088 }
4089 }
4090
3918 private void RemoteTelnetSession(ClientRequestInfo requestinfo)4091 private void RemoteTelnetSession(ClientRequestInfo requestinfo)
3919 {4092 {
3920 if ((object)m_remoteCommandProcess == null && requestinfo.Request.Arguments.ContainsHelpRequest)4093 if ((object)m_remoteCommandProcess == null && requestinfo.Request.Arguments.ContainsHelpRequest)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t