Brunet.Services.Coordinate.VivaldiTargetSelector.CloseHandler C# (CSharp) Method

CloseHandler() protected method

protected CloseHandler ( object o, EventArgs args ) : void
o object
args System.EventArgs
return void
    protected void CloseHandler(object o, EventArgs args) {
      RequestState request = null;
      lock(_sync) {
        //retrieve the request state assiciated with the closed channel
        request = (RequestState) _channel_to_state[o];
        if (request == null) {
          //cannot happen
          Console.Error.WriteLine("VTS unable to retrieve request for a closed channel");
          return;
        }
#if VTS_DEBUG
        Console.Error.WriteLine("VTS local: {0}, start: {1} channel closed.", _node.Address, request.Start);
#endif
        _channel_to_state.Remove(o);
        _num_requests--;
      }

      /** 
       * The request object has been removed the shared data structure.
       *  Since no one alse has access to it, it can be accessed outside the lock.
       */

      SortedList sorted_result = new SortedList();
#if VTS_DEBUG
      SortedList sorted_stat = new SortedList();
#endif
      NCService.VivaldiState local_vs = _nc_service.State;
      foreach(Address target in request.ResultTable.Keys) {
        object[] curr_result = (object[]) request.ResultTable[target];
        NCService.VivaldiState vs = (NCService.VivaldiState) curr_result[0];
        double d = local_vs.Position.GetEucledianDistance(vs.Position);
        sorted_result[d] = target;
#if VTS_DEBUG
        Console.Error.WriteLine("VTS local: {0}, start: {1}, dest: {2}, distance: {3}", 
                                _node.Address, request.Start, target, d);
        string host = (string) curr_result[1];
        sorted_stat[d] = new object[] {d, host};
#endif
      }

#if VTS_DEBUG
      lock(_sync) {
        _query_list.Add(sorted_stat);
      }
#endif
      request.Callback(request.Start, sorted_result, request.Current);
    }