Examples of AESemaphore


Examples of org.gudy.azureus2.core3.util.AESemaphore

      final DHTUDPPacketRequestPing  request =
          new DHTUDPPacketRequestPing( this, connection_id, local_contact, contact );
       
      stats.pingSent( request );

      final AESemaphore  sem = new AESemaphore( "DHTTransUDP:extping" );

      final InetSocketAddress[]  result = new InetSocketAddress[1];
     
      packet_handler.sendAndReceive(
        request,
        contact.getTransportAddress(),
        new DHTUDPPacketReceiver()
        {
          public void
          packetReceived(
            DHTUDPPacketReply  _packet,
            InetSocketAddress  from_address,
            long        elapsed_time )
          {
            try{
             
              if ( _packet instanceof DHTUDPPacketReplyPing ){
               
                // ping was OK so current address is OK
               
                result[0] = local_contact.getExternalAddress();
               
              }else if ( _packet instanceof DHTUDPPacketReplyError ){
               
                DHTUDPPacketReplyError  packet = (DHTUDPPacketReplyError)_packet;
               
                if ( packet.getErrorType() == DHTUDPPacketReplyError.ET_ORIGINATOR_ADDRESS_WRONG ){
                 
                  result[0] = packet.getOriginatingAddress();
                }
              }
            }finally{
             
              sem.release();
            }
          }
         
          public void
          error(
            DHTUDPPacketHandlerException  e )
          {
            try{
              stats.pingFailed();
             
            }finally{
           
              sem.release();
            }
          }
        },
        5000, PRUDPPacketHandler.PRIORITY_HIGH );
     
      sem.reserve( 5000 );
     
      return( result[0] );
     
    }catch( Throwable e ){
     
View Full Code Here

Examples of org.gudy.azureus2.core3.util.AESemaphore

      return( request_handler.statsRequest( contact ));
    }
   
    final DHTTransportFullStats[] res = { null };
   
    final AESemaphore  sem = new AESemaphore( "DHTTransportUDP:getFullStats");
   
    sendStatscontact,
          new DHTTransportReplyHandlerAdapter()
          {
            public void
            statsReply(
              DHTTransportContact   _contact,
              DHTTransportFullStats  _stats )
            {
              res[0= _stats;
             
              sem.release();
            }
           
            public void
            failed(
              DHTTransportContact   _contact,
              Throwable        _error )
            {
              sem.release();
            }
       
          });
   
    sem.reserve();

    return( res[0] );
  }
View Full Code Here

Examples of org.gudy.azureus2.core3.util.AESemaphore

  {
    byte[]  call_key = new byte[20];
   
    random.nextBytes( call_key );
   
    AESemaphore  call_sem = new AESemaphore( "DHTTransportUDP:calSem" );
   
    HashWrapper  wrapped_key = new HashWrapper( call_key );
   
    try{
      this_mon.enter();
     
      call_transfers.put( wrapped_key, call_sem );
   
    }finally{
     
      this_mon.exit();
    }
   
    writeTransfer( listener, target, handler_key, call_key, data, timeout );
   
    if ( call_sem.reserve( timeout )){
     
      try{
        this_mon.enter();
     
        Object  res = call_transfers.remove( wrapped_key );
View Full Code Here

Examples of org.gudy.azureus2.core3.util.AESemaphore

    final String f_term = term;
   
    try{
      final List<PluginResult>  results = new ArrayList<PluginResult>();
     
      final AESemaphore  sem = new AESemaphore( "waiter" );

      provider.search(
        search_parameters,
        new SearchObserver()
        {
          private boolean  complete = false;
         
          public void
          resultReceived(
            SearchInstance     search,
            SearchResult     result )
          {
            PluginResult p_result = new PluginResult( PluginEngine.this, result, f_term );
           
            synchronized( this ){
             
              if ( complete ){
               
                return;
              }
             
              results.add( p_result );
            }
           
            if ( listener != null ){
             
              listener.resultsReceived( PluginEngine.this, new Result[]{ p_result });
            }
           
            synchronized( this ){

              if ( absolute_max_matches >= 0 && results.size() >= absolute_max_matches ){
               
                complete = true;
               
                sem.release();
              }
            }
          }
         
          public void
          cancelled()
          {
            sem.release();
          }
         
          public void
          complete()
          {
            sem.release();
          }
         
          public Object
          getProperty(
            int property )
          {
            if ( property == PR_MAX_RESULTS_WANTED ){
             
              return( new Long( desired_max_matches ));
            }
           
            return( null );
          }
        });
     
      sem.reserve();
     
      if ( listener != null ){
       
        listener.resultsComplete( this );
      }
View Full Code Here

Examples of org.gudy.azureus2.core3.util.AESemaphore

         
          final int  num_threads = Math.min( BT_MAX_SLAVES, contacts.length );
         
          listener.log( "Searching " + contacts.length + " contacts for " + num_threads + " test targets" );
             
          final AESemaphore  sem = new AESemaphore( "NetStatusProbe" );
         
          for (int i=0;i<num_threads;i++){
           
            new AEThread2( "NetStatusProbe", true )
            {
              public void
              run()
              {
                try{
                  while( !bt_tester.isDestroyed()){
                   
                    DHTTransportContact  contact = null;
                   
                    synchronized( ok ){
                     
                      if ( ok[0] < num_threads && f_contacts.size() > 0 ){
                       
                        contact = (DHTTransportContact)f_contacts.remove(0);
                      }
                    }
                   
                    if ( contact == null ){
                     
                      break;
                    }
                   
                    try{
                      DistributedDatabaseContact ddb_contact = ddb.importContact( contact.getAddress());
                     
                      if ( tryTest( bt_tester, ddb_contact )){
                       
                        synchronized( ok ){
                         
                          ok[0]++;
                        }
                      }
                    }catch( Throwable e ){
                     
                      listener.logError( "Contact import for " + contact.getName() + " failed", e );
                    }
                  }
                }finally{
                 
                  sem.release();
                }
              }
            }.start();
          }
         
          for (int i=0;i<num_threads;i++){
           
            sem.reserve();
          }
         
          listener.log( "Searching complete, " + ok[0] + " targets found" );
        }
      }else{
View Full Code Here

Examples of org.gudy.azureus2.core3.util.AESemaphore

         
          Object  obj = call_transfers.get( key_wrapper );
         
          if ( obj instanceof AESemaphore ){
           
            AESemaphore  sem = (AESemaphore)obj;
           
            call_transfers.put( key_wrapper, value );
           
            sem.release();
           
            return( null );
          }
        }finally{
         
View Full Code Here

Examples of org.gudy.azureus2.core3.util.AESemaphore

        }
      }
     
      if ( xcode_required ){
       
        final AESemaphore xcode_sem = new AESemaphore( "xcode:proc" );
       
        final TranscodeProviderJob[] provider_job = { null };
 
        TranscodeProviderAdapter xcode_adapter =
          new TranscodeProviderAdapter()
          {
            private boolean  resolution_updated;
         
            private final int ETA_AVERAGE_SIZE  = 10;
           
            private int    last_eta;
            private int    eta_samples;
            private Average eta_average     = AverageFactory.MovingAverage(ETA_AVERAGE_SIZE);
            private int    last_percent;
           
            public void
            updateProgress(
              int      percent,
              int      eta_secs,
              int      new_width,
              int      new_height )
            {
              last_eta     = eta_secs;
              last_percent  = percent;
             
              TranscodeProviderJob  prov_job = provider_job[0];
             
              if ( prov_job == null ){
               
                return;
              }
             
              int  job_state = job.getState();
             
              if (   job_state == TranscodeJob.ST_CANCELLED ||
                  job_state == TranscodeJob.ST_REMOVED ){
                               
                prov_job.cancel();
               
              }else if ( paused || job_state == TranscodeJob.ST_PAUSED ){
                 
                prov_job.pause();
               
              }else{
               
                if ( job_state == TranscodeJob.ST_RUNNING ){
                 
                  prov_job.resume();
                }
               
                job.updateProgress( percent, eta_secs );
               
                prov_job.setMaxBytesPerSecond( max_bytes_per_sec );
               
                if ( !resolution_updated ){
                 
                  if ( new_width > 0 && new_height > 0 ){
                 
                    transcode_file.setResolution( new_width, new_height );
                   
                    resolution_updated = true;
                 
                }
              }
            }
           
            public void
            streamStats(
              long          connect_rate,
              long          write_speed )
            {
                // problem on OSX with some files thrashing the indirect piped input
                // and eventually failing - try and spot this behaviour and revert
                // to direct input if needed
             
              if (   Constants.isOSX &&
                  job.getEnableAutoRetry() &&
                  job.canUseDirectInput() &&
                  job.getAutoRetryCount() == 0 ){
               
                if ( connect_rate > 5 && last_percent < 100 ){
                 
                  long eta = (long)eta_average.update(last_eta );

                  eta_samples++;
                 
                  if ( eta_samples >= ETA_AVERAGE_SIZE ){
                   
                    long  total_time = (eta*100 )/(100-last_percent);
                   
                    long   total_write = total_time*write_speed;
                   
                    DiskManagerFileInfo file = job.getFile();
                   
                    long  length = file.getLength();

                    if ( length > 0 ){

                      double over_write = ((double)total_write)/length;
                   
                      if ( over_write > 5.0 ){
                         
                        failed( new TranscodeException( "Overwrite limit exceeded, abandoning transcode" ));
                         
                        provider_job[0].cancel();   
                      }
                    } 
                  }
                }else{
                 
                  eta_samples = 0;
                }
              }
            }
           
            public void
            failed(
              TranscodeException    e )
            {
              if ( error[0] == null ){
             
                error[0] = e;
              }
             
              xcode_sem.release();
            }
           
            public void
            complete()
            {
              xcode_sem.release();
            }
          };
       
        boolean  direct_input = job.useDirectInput();
         
        if ( job.isStream()){
         
          /*
          provider_job[0] =
            provider.transcode(
              adapter,
              job.getFile(),
              profile,
              new File( "C:\\temp\\arse").toURI().toURL());
          */
         
          pipe = new TranscodePipeStreamSource2(
                new TranscodePipeStreamSource2.streamListener()
                {
                  public void
                  gotStream(
                    InputStream is )
                  {
                    job.setStream( is );
                  }
                });
         
          provider_job[0] =
            provider.transcode(
              xcode_adapter,
              provider_analysis,
              direct_input,
              job.getFile(),
              profile,
              new URL( "tcp://127.0.0.1:" + pipe.getPort()));
 
        }else{
                   
          File output_file = transcode_file.getCacheFile();
         
          provider_job[0] =
            provider.transcode(
              xcode_adapter,
              provider_analysis,
              direct_input,
              job.getFile(),
              profile,
              output_file.toURI().toURL());
        }
       
        provider_job[0].setMaxBytesPerSecond( max_bytes_per_sec );
       
        TranscodeQueueListener listener =
          new TranscodeQueueListener()
          {
            public void
            jobAdded(
              TranscodeJob    job )
            {         
            }
           
            public void
            jobChanged(
              TranscodeJob    changed_job )
            {
              if ( changed_job == job ){
               
                int  state = job.getState();
               
                if ( state == TranscodeJob.ST_PAUSED ){
                 
                  provider_job[0].pause();
                 
                }else if ( state == TranscodeJob.ST_RUNNING ){
                   
                  provider_job[0].resume();
                 
                }else if (   state == TranscodeJob.ST_CANCELLED ||
                      state == TranscodeJob.ST_STOPPED ){
               
                  provider_job[0].cancel();
                }
              }
            }
           
            public void
            jobRemoved(
              TranscodeJob    removed_job )
            { 
              if ( removed_job == job ){
               
                provider_job[0].cancel();
              }
            }
          };
         
        try{
          addListener( listener );
       
          xcode_sem.reserve();
         
        }finally{
         
          removeListener( listener );
        }
View Full Code Here

Examples of org.gudy.azureus2.core3.util.AESemaphore

 
    final TranscodeException[] error = { null };
   
    TranscodeProfile profile = job.getProfile();
     
    final AESemaphore analysis_sem = new AESemaphore( "analysis:proc" );   

    final boolean  was_stopped = job.getState() == TranscodeJob.ST_STOPPED;
   
    TranscodeProviderAdapter analysis_adapter =
      new TranscodeProviderAdapter()
      {
        public void
        updateProgress(
          int    percent,
          int    eta_secs,
          int    width,
          int    height )
        {
        }
       
        public void
        streamStats(
          long          connect_rate,
          long          write_speed )
        {
        }
       
        public void
        failed(
          TranscodeException    e )
        {
          error[0] = e;
         
          analysis_sem.release();
        }
       
        public void
        complete()
        {
          analysis_sem.release();
        }
      };
     
    final TranscodeProviderAnalysis provider_analysis =  provider.analyse( analysis_adapter, job.getFile(), profile );
   
    TranscodeQueueListener analysis_q_listener =
      new TranscodeQueueListener()
      {
        public void
        jobAdded(
          TranscodeJob    job )
        {         
        }
       
        public void
        jobChanged(
          TranscodeJob    changed_job )
        {
          if ( changed_job == job ){
           
            int  state = job.getState();
           
            if (   state == TranscodeJob.ST_CANCELLED ){
             
              provider_analysis.cancel();
             
            }else if (   state == TranscodeJob.ST_STOPPED ){
           
                // only treat this as a failure if the job was initially
                // running and has been explicitly stopped
             
              if ( !was_stopped ){
             
                provider_analysis.cancel();
              }
            }
          }
        }
       
        public void
        jobRemoved(
          TranscodeJob    removed_job )
        { 
          if ( removed_job == job ){
           
            provider_analysis.cancel();
          }
        }
      };
     
    try{
      addListener( analysis_q_listener );
   
      analysis_sem.reserve();
     
    }finally{
     
      removeListener( analysis_q_listener );
    }
View Full Code Here

Examples of org.gudy.azureus2.core3.util.AESemaphore

        }
      }
     
      if ( to_do.size() > 0 ){
       
        final AESemaphore  sem = new AESemaphore( "PM:check" );
       
        for ( final Runnable r: to_do ){
         
          new AEThread2( "PM:check:", true )
          {
            public void
            run()
            {
              try{
                r.run();
              }finally{
               
                sem.release();
              }
            }
          }.start();
        }
       
        for (int i=0;i<to_do.size();i++){
         
          sem.reserve();
        }
      }
     
      for ( String excess: existing_checked ){
       
View Full Code Here

Examples of org.gudy.azureus2.core3.util.AESemaphore

      }
     
      final DistributedDatabase db = plugin_interface.getDistributedDatabase();
     
      final List      potential_contacts     = new ArrayList();
      final AESemaphore  potential_contacts_sem   = new AESemaphore( "MagnetPlugin:liveones" );
      final AEMonitor    potential_contacts_mon  = new AEMonitor( "MagnetPlugin:liveones" );
     
      final int[]      outstanding    = {0};
      final boolean[]    lookup_complete  = {false};
     
      listener.reportActivitygetMessageText( "report.searching" ));
     
      DistributedDatabaseListener  ddb_listener =
        new DistributedDatabaseListener()
        {
          private Set  found_set = new HashSet();
         
          public void
          event(
            DistributedDatabaseEvent     event )
          {
            int  type = event.getType();
 
            if ( type == DistributedDatabaseEvent.ET_OPERATION_STARTS ){

                // give live results a chance before kicking in explicit ones
             
              if ( sources.length > 0 ){
               
                new DelayedEvent(
                  "MP:sourceAdd",
                  10*1000,
                  new AERunnable()
                  {
                    public void
                    runSupport()
                    {
                      addExplicitSources();
                    }
                  });
              }
             
            }else if ( type == DistributedDatabaseEvent.ET_VALUE_READ ){
                         
              contactFound( event.getValue().getContact());
     
            }else if type == DistributedDatabaseEvent.ET_OPERATION_COMPLETE ||
                  type == DistributedDatabaseEvent.ET_OPERATION_TIMEOUT ){
               
              listener.reportActivity( getMessageText( "report.found", String.valueOf( found_set.size())));
             
                // now inject any explicit sources

              addExplicitSources();
             
              try{
                potential_contacts_mon.enter();                         

                lookup_complete[0] = true;
               
              }finally{
               
                potential_contacts_mon.exit();
              }
             
              potential_contacts_sem.release();
            }
          }
         
          protected void
          addExplicitSources()
          { 
            for (int i=0;i<sources.length;i++){
             
              try{
                contactFound( db.importContact(sources[i]));
               
              }catch( Throwable e ){
               
                Debug.printStackTrace(e);
              }
            }
          }
         
          public void
          contactFound(
            final DistributedDatabaseContact  contact )
          {
            String  key = contact.getAddress().toString();
           
            synchronized( found_set ){
             
              if ( found_set.contains( key )){
               
                return;
              }
             
              found_set.add( key );
            }
           
            if ( listener.verbose()){
           
              listener.reportActivity( getMessageText( "report.found", contact.getName()));
            }
           
            try{
              potential_contacts_mon.enter();                         

              outstanding[0]++;
             
            }finally{
             
              potential_contacts_mon.exit();
            }
           
            contact.isAlive(
              20*1000,
              new DistributedDatabaseListener()
              {
                public void
                event(
                  DistributedDatabaseEvent event)
                {
                  try{
                    boolean  alive = event.getType() == DistributedDatabaseEvent.ET_OPERATION_COMPLETE;
                     
                    if ( listener.verbose()){
                   
                      listener.reportActivity(
                        getMessageText( alive?"report.alive":"report.dead",  contact.getName()));
                    }
                   
                    try{
                      potential_contacts_mon.enter();
                     
                      Object[]  entry = new Object[]{ new Boolean( alive ), contact};
                     
                      boolean  added = false;
                     
                      if ( alive ){
                       
                          // try and place before first dead entry
                   
                        for (int i=0;i<potential_contacts.size();i++){
                         
                          if (!((Boolean)((Object[])potential_contacts.get(i))[0]).booleanValue()){
                           
                            potential_contacts.add(i, entry );
                           
                            added = true;
                           
                            break;
                          }
                        }
                      }
                     
                      if ( !added ){
                       
                        potential_contacts.add( entry )// dead at end
                      }
                       
                    }finally{
                       
                      potential_contacts_mon.exit();
                    }
                  }finally{
                   
                    try{
                      potential_contacts_mon.enter();                         

                      outstanding[0]--;
                     
                    }finally{
                     
                      potential_contacts_mon.exit();
                    }
                   
                    potential_contacts_sem.release();
                  }
                }
              });
          }
        };
       
      db.read(
        ddb_listener,
        db.createKey( hash, "Torrent download lookup for '" + ByteFormatter.encodeString( hash ) + "'" ),
        timeout,
        DistributedDatabase.OP_EXHAUSTIVE_READ | DistributedDatabase.OP_PRIORITY_HIGH );
     
      long  remaining  = timeout;
     
      long   overall_start       = SystemTime.getMonotonousTime();
      boolean  sl_enabled        = secondary_lookup.getValue() && FeatureAvailability.isMagnetSLEnabled();

      long  secondary_lookup_time   = -1;
     
      long last_found = -1;
     
      final Object[] secondary_result = { null };
     
      while( remaining > 0 ){
         
        try{
          potential_contacts_mon.enter();

          if (   lookup_complete[0] &&
              potential_contacts.size() == 0 &&
              outstanding[0] == 0 ){
           
            break;
          }
        }finally{
         
          potential_contacts_mon.exit();
        }
               
       
        while( remaining > 0 ){
       
          long wait_start = SystemTime.getMonotonousTime();

          boolean got_sem = potential_contacts_sem.reserve( 1000 );
   
          long now = SystemTime.getMonotonousTime();
         
          remaining -= ( now - wait_start );
       
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.