Exemplo n.º 1
0
 void provider_WorkCompleted(ThreadedWorker <HashInputArgs, HashOutputArgs> sender, HashOutputArgs val)
 {
     this.showDots     = false;
     this.Result       = val.Hash.ToString(ByteSplitter);
     this.lastProgress = -1d;
     this.Progress     = 100d;
 }
Exemplo n.º 2
0
        public Host(string directory, Action <string> OnPageRegister = null)
        {
            directoryPath = directory;

            if (OnPageRegister != null)
            {
                this.OnPageRegister = OnPageRegister;
            }

            ServerHandler.LogMessage("Reading Directory...");

            ThreadedWorker worker = new ThreadedWorker();

            foreach (var file in Directory.GetFiles(directory))
            {
                worker.EnqueueJob((Action <string>)ProcessFile, file);
            }

            worker.Join(null);

            ServerHandler.LogMessage("Starting FileSystemWatcher...");

            fileSystemWatcher = new FileSystemWatcher(directory);
            RegisterFileSystemWatcher();

            ServerHandler.LogMessage("FileSystemWatcher is now listening.");
        }
Exemplo n.º 3
0
 void provider_WorkFailed(ThreadedWorker <HashInputArgs, HashOutputArgs> sender, Exception val)
 {
     this.showDots     = false;
     this.lastProgress = -1d;
     this.Progress     = 0d;
     this.Result       = val.ToString();
 }
Exemplo n.º 4
0
 void provider_WorkAborted(ThreadedWorker <HashInputArgs, HashOutputArgs> sender)
 {
     this.showDots     = false;
     this.lastProgress = -1d;
     this.Progress     = 0d;
     this.Result       = "Aborted";
 }
Exemplo n.º 5
0
    static void Main(string[] args)
    {
        for (int i = 0; i < 10; i++)
        {
            ThreadedWorker tr = new ThreadedWorker(i);
        }

        Console.ReadLine();
    }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            for (int i = 0; i < 10; i++)
            {
                ThreadedWorker tw = new ThreadedWorker(i);
            }

            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("Main is running.");
            }
        }
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            for (int i = 0; i < 10; i++)
            {
                ThreadedWorker tw = new ThreadedWorker(i);
            }

            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("Main is running.");
            }
        }
Exemplo n.º 8
0
	/// <summary>
	/// Socket Initialization Place Any Other Code Before the Socket Initialization
	/// </summary>
	private void Start()
	{
		limits.x = -4;
		limits.y =  8;
		limits.z =  4;
		
		AudioManager.Instance.PlaySound(EAudioPlayType.BGM, audioClip);

		worker = new ThreadedWorker();
		readFromDB = new Thread(new ThreadStart(worker.Run));

		readFromDB.Start();
		//StartCoroutine(UpdatePotatoCounter());
	}
Exemplo n.º 9
0
    /// <summary>
    /// Socket Initialization Place Any Other Code Before the Socket Initialization
    /// </summary>
    private void Start()
    {
        limits.x = -4;
        limits.y = 8;
        limits.z = 4;

        AudioManager.Instance.PlaySound(EAudioPlayType.BGM, audioClip);

        worker     = new ThreadedWorker();
        readFromDB = new Thread(new ThreadStart(worker.Run));

        readFromDB.Start();
        //StartCoroutine(UpdatePotatoCounter());
    }
        /// <summary>
        /// Makes room when the cache is overflowing.
        /// </summary>
        /// <param name="requestedSpace">The requested amount of free space. (Not including CacheMakeRoom_AdditionalFreeSpace)</param>
        protected virtual void MakeRoom(ulong requestedSpace)
        {
            if (!StringResponses.Any())
            {
                return;
            }

            if (!MaximumStringResponseCacheSize.HasValue)
            {
                return;
            }

            Logger.LogInformation($"Cache space has been overflowed. Making Room... ({StringResponses.Count} Entries) ({CurrentStringResponseCacheSize} + {requestedSpace} ( + {(ulong)(CacheMakeRoom_AdditionalFreeSpacePercentage * MaximumStringResponseCacheSize)} additional ) of {MaximumStringResponseCacheSize.Value} -> {(100.0 * ((CurrentStringResponseCacheSize + requestedSpace) / (double)MaximumStringResponseCacheSize.Value)).ToString("0.00")}% filled)");

            System.Diagnostics.Stopwatch stopwatch = System.Diagnostics.Stopwatch.StartNew();

            KeyValuePair <string, ResponseCacheEntry <string> >[] responses;

            using (StringResponseLock.LockRead())
                responses = StringResponses.ToArray();

            // Remove outdated.
            foreach (var kvpair in responses)
            {
                if (kvpair.Value.RefreshTime != null && kvpair.Value.LastUpdatedDateTime + kvpair.Value.RefreshTime < DateTime.UtcNow)
                {
                    RemoveStringResponseEntry(kvpair.Key, kvpair.Value);
                }
            }

            DateTime upperTenthPercentileDate;
            ulong    upperTenthPercentileCount;

            KeyValuePair <string, ResponseCacheEntry <string> >[] DateSorted = null, CountSorted = null, SizeSorted = null;


            var worker0 = ThreadedWorker.CurrentWorker.Instance.EnqueueJob((Action)(() => DateSorted = StringResponses.OrderBy((kvpair) => kvpair.Value.LastRequestedDateTime).ToArray()));
            var worker1 = ThreadedWorker.CurrentWorker.Instance.EnqueueJob((Action)(() => CountSorted = StringResponses.OrderBy((kvpair) => kvpair.Value.Count).ToArray()));
            var worker2 = ThreadedWorker.CurrentWorker.Instance.EnqueueJob((Action)(() => SizeSorted = StringResponses.OrderBy((kvpair) => kvpair.Value.Response.Length).ToArray()));

            ThreadedWorker.JoinTasks(worker0, worker1, worker2);

            upperTenthPercentileDate  = DateSorted[(int)(DateSorted.Length * (1.0 - CacheMakeRoom_UpperPercentile_Date))].Value.LastRequestedDateTime;
            upperTenthPercentileCount = CountSorted[(int)(CountSorted.Length * (1.0 - CacheMakeRoom_UpperPercentile_Count))].Value.Count;

            // Remove by size descending if not in upper tenth percentile.
            int lastRemoveBySizeIndex = (int)(SizeSorted.Length * CacheMakeRoom_RemoveBySizePercentage);

            for (int i = SizeSorted.Length - 1; i >= lastRemoveBySizeIndex; i--)
            {
                if (CurrentStringResponseCacheSize + requestedSpace <= MaximumStringResponseCacheSize.Value) // First run without additional size
                {
                    break;
                }

                var response = StringResponses[SizeSorted[i].Key];

                if (response.Count <= upperTenthPercentileCount && response.LastRequestedDateTime <= upperTenthPercentileDate)
                {
                    RemoveStringResponseEntry(SizeSorted[i].Key, response);
                }
            }

            int j = 0;

            // Remove by count and last access time if not in upper tenth percentile.
            while (CurrentStringResponseCacheSize + requestedSpace + (ulong)(MaximumStringResponseCacheSize * CacheMakeRoom_AdditionalFreeSpacePercentage) > MaximumStringResponseCacheSize && StringResponses.Any())
            {
                var response = StringResponses[DateSorted[j].Key];

                if (response && response.Count <= upperTenthPercentileCount)
                {
                    RemoveStringResponseEntry(DateSorted[j].Key, response);
                }

                response = StringResponses[CountSorted[j].Key];

                if (response && response.LastRequestedDateTime <= upperTenthPercentileDate)
                {
                    RemoveStringResponseEntry(CountSorted[j].Key, response);
                }

                j++;
            }

            // Remove by time left till refresh till percentage.
            var SortedTimeLeftTillRefresh = (from p in StringResponses where p.Value.RefreshTime.HasValue select new { kvpair = p, timeLeft = DateTime.UtcNow - p.Value.LastUpdatedDateTime + p.Value.RefreshTime }).OrderBy((k) => k.timeLeft).ToArray();

            for (int i = 0; i < SortedTimeLeftTillRefresh.Length * CacheMakeRoom_RemoveByTimePercentage; i++)
            {
                if (CurrentStringResponseCacheSize + requestedSpace + (ulong)(MaximumStringResponseCacheSize * CacheMakeRoom_AdditionalFreeSpacePercentage) <= MaximumStringResponseCacheSize)
                {
                    goto epilogue;
                }

                var response = StringResponses[SortedTimeLeftTillRefresh[j].kvpair.Key];

                RemoveStringResponseEntry(SortedTimeLeftTillRefresh[j].kvpair.Key, response);
            }

            // Remove by count and last accesstime regardless of percentiles.
            j = 0;

            while (CurrentStringResponseCacheSize + requestedSpace + (ulong)(MaximumStringResponseCacheSize * CacheMakeRoom_AdditionalFreeSpacePercentage) > MaximumStringResponseCacheSize && StringResponses.Any())
            {
                var response = StringResponses[DateSorted[j].Key];

                if (response)
                {
                    RemoveStringResponseEntry(DateSorted[j].Key, response);
                }

                if (!(CurrentStringResponseCacheSize + requestedSpace + (ulong)(MaximumStringResponseCacheSize * CacheMakeRoom_AdditionalFreeSpacePercentage) > MaximumStringResponseCacheSize && StringResponses.Any()))
                {
                    goto epilogue;
                }

                response = StringResponses[CountSorted[j].Key];

                if (response)
                {
                    RemoveStringResponseEntry(CountSorted[j].Key, response);
                }

                j++;
            }

epilogue:
            stopwatch.Stop();

            Logger.LogInformation($"Cache space has been cleared. ({stopwatch.ElapsedMilliseconds} ms) ({StringResponses.Count} Entries => {(100.0 * ((CurrentStringResponseCacheSize + requestedSpace) / (double)MaximumStringResponseCacheSize.Value)).ToString("0.00")}% filled)");
        }
Exemplo n.º 11
0
 void provider_WorkStarted(ThreadedWorker <HashInputArgs, HashOutputArgs> sender)
 {
     this.Result = "Working";
 }