private static Results MatchMultiProcessor(string userAgent, SegmentHandler handler) { // Provide an object to signal when the request has completed. AutoResetEvent waiter = new AutoResetEvent(false); // Create the request. Request request = new Request(userAgent, handler, waiter); if (request.Count > 0) { // For each thread add this to the queue. for (int i = 0; i < Environment.ProcessorCount; i++) ThreadPool.QueueUserWorkItem(ServiceRequest, request); // Wait until a signal is received. Keeping coming back to // this thread so that a request to close the request // can be processed. while (waiter.WaitOne(1, false) == false) { // Do nothing } ; } // Return the results. return request.Results; }
private static Results MatchSingleProcessor(string userAgent, SegmentHandler handler) { // Create a segment matcher request. Request request = new Request(userAgent, handler); // Process the request. ServiceRequest(request); // Return the results. return request.Results; }
private static void ServiceRequest(Request request) { int index; uint runningScore, score; BaseDeviceInfo current = request.Next(); while (current != null) { // Reset the counters. runningScore = 0; index = 0; while (index < request.Target.Count && runningScore <= request.Results.LowestScore) { // Get the next segment for the comparision. var compare = request.Handler.CreateSegments( current.UserAgent, index); // If the two results are not equal in length so do not consider // this useragent as a possible match. if (request.Target[index].Count != compare.Count) { runningScore = uint.MaxValue; break; } // Work out the score for each of the returned segments. for (int segmentIndex = 0; segmentIndex < request.Target[index].Count; segmentIndex++) { // If the two are equal then set to zero. if (request.Target[index][segmentIndex].Value == compare[segmentIndex].Value) score = 0; else score = (uint)Algorithms.EditDistance( request.Target[index][segmentIndex].Value, compare[segmentIndex].Value, int.MaxValue) * (uint)request.Target[index][segmentIndex].Weight; // Update the counters. compare[segmentIndex].Score = score; runningScore += score; } index++; } if (runningScore <= request.Results.LowestScore) { lock (request.Results) { if (runningScore == request.Results.LowestScore) { request.Results.Add(current); } else if (runningScore < request.Results.LowestScore) { request.Results.LowestScore = runningScore; request.Results.Clear(); request.Results.Add(current); } } } current = request.Next(); request.Complete(); } }