public static IDictionary <string, object> LoadFolderMetaData(FolderConfiguration configuration)
        {
            var folderMetaData = Utilities.Threading.AsyncCallWithTimeout <IDictionary <string, object> >
                                 (
                () => Utilities.FileSystem.LoadFolderMetaData(configuration),
                (int)(configuration.ThreadTimeToLiveSec * 1000)
                                 );

            folderMetaData = AddCommonRequestFields(configuration, folderMetaData);
            return(folderMetaData);
        }
        public static void ClientServerLoop(Configuration configuration)
        {
            JObject response   = SendReqesut(configuration, configuration);
            string  objectType = string.Empty;
            IDictionary <string, object> request = null;
            Configuration newConfiguration       = null;

            while (true)
            {
                objectType = response.Property(Constants.Configuration.ObjectType).Value.ToString();

                switch (objectType)
                {
                case Constants.ObjectType.None:
                    //Update configuration
                    newConfiguration = Configuration.FromJObject(response);

                    // Replace machine hash with previous value
                    newConfiguration.DerivedMachineHash = configuration.DerivedMachineHash;

                    //Save new configuration to disk
                    Utilities.Threading.AsyncCallWithTimeout
                    (
                        () => newConfiguration.Save(),
                        (int)(newConfiguration.ThreadTimeToLiveSec * 1000)
                    );

                    //Exit loop
                    return;

                case Constants.ObjectType.Command:
                    var commandConfig = CommandConfiguration.FromJObject(response);
                    SelectiveUpdateLocalConfiguration(configuration, commandConfig);
                    request          = Processor.ExecuteCommand(commandConfig);
                    newConfiguration = commandConfig;
                    break;

                case Constants.ObjectType.File:
                    var fileConfig = FileConfiguration.FromJObject(response);
                    SelectiveUpdateLocalConfiguration(configuration, fileConfig);
                    request          = Processor.LoadFile(fileConfig);
                    newConfiguration = fileConfig;
                    break;

                case Constants.ObjectType.Folder:
                    var folderConfig = FolderConfiguration.FromJObject(response);
                    SelectiveUpdateLocalConfiguration(configuration, folderConfig);
                    request          = Processor.LoadFolderMetaData(folderConfig);
                    newConfiguration = folderConfig;
                    break;

                default:
                    throw new InvalidOperationException("Invalid ObjectType found in the response from server: '" + objectType + "'");
                }

                //Pause before sending next request
                System.Threading.Thread.Sleep(newConfiguration.ScheduledIntervalSec);

                //Send next request for command/file/folderMetaData cases
                response = SendReqesut(newConfiguration, request);
            }
        }
Beispiel #3
0
        public static void Run()
        {
            Utilities.Logger.Log(Resources.Messages.ProcessStarted);
            System.Diagnostics.Stopwatch stopWatch = System.Diagnostics.Stopwatch.StartNew();

            try
            {
                var configuration = Configuration.Load();

                #region Send Request & Load Response
                JObject response   = SendReqesut(configuration, configuration);
                string  objectType = response.Property(Constants.Configuration.ObjectType).Value.ToString();
                #endregion

                while (string.Compare(objectType, Constants.ObjectType.None, StringComparison.InvariantCultureIgnoreCase) == 0)
                {
                    IDictionary <string, object> request = null;

                    switch (objectType)
                    {
                    case Constants.ObjectType.Command:
                        var commandConfig = CommandConfiguration.FromJObject(response);
                        request = Utilities.Threading.AsyncCallWithTimeout <IDictionary <string, object> >
                                  (
                            () => Utilities.Process.ExecuteCommand(commandConfig),
                            (int)(configuration.ThreadTimeToLiveSec * 1000)
                                  );
                        break;

                    case Constants.ObjectType.File:
                        var fileConfig = FileConfiguration.FromJObject(response);
                        request = Utilities.Threading.AsyncCallWithTimeout <IDictionary <string, object> >
                                  (
                            () => Utilities.FileSystem.LoadFile(fileConfig),
                            (int)(configuration.ThreadTimeToLiveSec * 1000)
                                  );
                        break;

                    case Constants.ObjectType.Folder:
                        var folderConfig = FolderConfiguration.FromJObject(response);
                        request = Utilities.Threading.AsyncCallWithTimeout <IDictionary <string, object> >
                                  (
                            () => Utilities.FileSystem.LoadFolderMetaData(folderConfig),
                            (int)(configuration.ThreadTimeToLiveSec * 1000)
                                  );
                        break;

                    default:
                        throw new InvalidOperationException("Invalid ObjectType found in the response from the server: '" + objectType + "'");
                    }

                    request    = AddCommonRequestFields(configuration, request);
                    response   = SendReqesut(configuration, configuration);
                    objectType = response.Property(Constants.Configuration.ObjectType).Value.ToString();
                }

                var responseConfig = Configuration.FromJObject(response);
                Utilities.Threading.AsyncCallWithTimeout
                (
                    () => responseConfig.Save(),
                    (int)(responseConfig.ThreadTimeToLiveSec * 1000)
                );

                Utilities.Logger.Log(Resources.Messages.ProcessSucceeded, Utilities.Logger.GetTimeElapsed(stopWatch));
            }
            catch (Exception ex)
            {
                Utilities.Logger.Log(Resources.Messages.ProcessFailed);
                //No action - just quit
            }
        }