示例#1
0
        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;
        }
示例#2
0
 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;
 }
示例#3
0
 private static void ServiceRequest(Request request)
 {
     int weight, difference;
     Segments compare;
     DeviceInfo current = request.Next();
     while (current != null)
     {
         compare = request.Handler.CreateSegments(current.UserAgent);
         difference = request.Target.Count - compare.Count;
         for (int i = 0; i < difference; i++)
             compare.Add(new Segment(String.Empty));
         for (int i = 0; i < request.Target.Count; i++)
         {
             weight = request.Handler.GetSegmentWeight(i, request.Target.Count);
             // If the two segments are exactly equal score as zero.
             if (request.Target[i].Value == compare[i].Value)
                 compare[i].Score = 0;
                 // Otherwise assign the EditDistance value multiplied by the
                 // segment weight or if one is not available the index of the segment.
             else
             {
                 compare[i].Score = Algorithms.EditDistance(
                                        request.Target[i].Value,
                                        compare[i].Value,
                                        int.MaxValue)*weight;
             }
         }
         int totalScore = compare.TotalScore;
         if (totalScore <= request.Results.LowestScore)
         {
             lock (request.Results)
             {
                 if (totalScore == request.Results.LowestScore)
                 {
                     request.Results.Add(current);
                 }
                 else if (totalScore < request.Results.LowestScore)
                 {
                     request.Results.LowestScore = totalScore;
                     request.Results.Clear();
                     request.Results.Add(current);
                 }
             }
         }
         current = request.Next();
         request.Complete();
     }
 }