Examples of TOTorrent


Examples of org.gudy.azureus2.core3.torrent.TOTorrent

         
          setUserData( TPS_Key, new Object[]{ tps, tol });

            // tracker peer sources
         
          TOTorrent t = getTorrent();

          if ( t != null ){

            t.addListener( tol );
           
            TOTorrentAnnounceURLSet[] sets = t.getAnnounceURLGroup().getAnnounceURLSets();
           
            if ( sets.length == 0 ){
             
              sets = new TOTorrentAnnounceURLSet[]{ t.getAnnounceURLGroup().createAnnounceURLSet( new URL[]{ torrent.getAnnounceURL()})};
            }
             
              // source per set
           
          for ( final TOTorrentAnnounceURLSet set: sets ){
View Full Code Here

Examples of org.gudy.azureus2.core3.torrent.TOTorrent

           
              Object[] tps_data = (Object[])getUserData( TPS_Key );

              if ( tps_data != null ){
           
               TOTorrent t = getTorrent();

               if ( t != null ){
                
                 t.removeListener( (TOTorrentListener)tps_data[1] );
               }
              
                setUserData( TPS_Key, null );
              }
          }
View Full Code Here

Examples of org.gudy.azureus2.core3.torrent.TOTorrent

 

  public long
  getHashFailCount()
  {
    TOTorrent  t = download_manager.getTorrent();
   
    if ( t == null ){
     
      return(0);
    }
   
    long  total   = getHashFailBytes();
   
    long  res = total / t.getPieceLength();
   
    if ( res == 0 && total > 0 ){
     
      res = 1;
    }
View Full Code Here

Examples of org.gudy.azureus2.core3.torrent.TOTorrent

    String[]  args )
  {
    try{
      File file = new File  ( "C:\\temp\\test.torrent");
     
      TOTorrent  torrent = TOTorrentFactory.deserialiseFromBEncodedFile( file );
     
      Map  map = torrent.serialiseToMap();
     
      List  urls = new ArrayList();
     
      urls.add( "http://192.168.1.2:8080/test.dat" );
     
      map.put( "httpseeds", urls);
     
      torrent = TOTorrentFactory.deserialiseFromMap( map );
     
      torrent.serialiseToBEncodedFile( file );
     
    }catch( Throwable e ){
     
      e.printStackTrace();
    }
View Full Code Here

Examples of org.gudy.azureus2.core3.torrent.TOTorrent

    try{
      COConfigurationManager.preInitialise();
     
      File file = new File  ( "C:\\temp\\httpseed.torrent");
     
      TOTorrent  torrent = TOTorrentFactory.deserialiseFromBEncodedFile( file );
     
      Map  map = torrent.serialiseToMap();
     
      /*
      List  urls = (List)map.get( "url-list" );
       
      if ( urls == null ){
       
        urls = new ArrayList();
      }
         
      urls.add( "http://127.0.0.1:888/files/%DF%26%5B7w%C9%13I%88%8D%EC%E5b%2C9%0F%8D%0Co%BC/" );
     
      map.put( "url-list", urls);
      */
     
      /*
      Map params = new HashMap();
     
      map.put( "url-list-params", params );
      */
     
      List params2 = new ArrayList();
     
      map.put( "url-list-params2", params2 );
           
      Map x_map = new HashMap();
     
      x_map.put( "max_speed", new Long(5*1024));
     
      params2.add( new Long(0));
      params2.add( x_map );
     
      torrent = TOTorrentFactory.deserialiseFromMap( map );
     
      torrent.serialiseToBEncodedFile( file );
     
    }catch( Throwable e ){
     
      e.printStackTrace();
    }
View Full Code Here

Examples of org.gudy.azureus2.core3.torrent.TOTorrent

      Debug.out( "Disk manager is null" );
     
      throw( new IOException( "Disk manager unavailable" ));
    }
         
    TOTorrent  to_torrent = dm.getTorrent();
       
    char[]  chars = header.toCharArray();
   
    int  last_pos   = 0;
    int  line_num  = 0;
   
    String        target_str  = null;
   
    DiskManagerFileInfo  target_file = null;
   
    long  file_offset  = 0;
   
    List<long[]>  ranges = new ArrayList<long[]>();
   
    boolean  keep_alive  = false;
   
    for (int i=1;i<chars.length;i++){
     
      if ( chars[i-1] == '\r' && chars[i] == '\n' ){
       
        String  line = new String( chars, last_pos, i - last_pos ).trim();
       
        last_pos = i;
       
        line_num++;
       
        // System.out.println( "line " + line_num + " -> " + line );
       
        if ( line_num == 1 ){
         
          line = line.substring( line.indexOf( "files/" ) + 6 );
         
          int  hash_end = line.indexOf( "/" );
         
          final byte[] old_hash = control.getHash();

          final byte[] new_hash = URLDecoder.decode(line.substring(0, hash_end), "ISO-8859-1").getBytes( "ISO-8859-1" );
         
          if ( !Arrays.equals( new_hash, old_hash )){
           
            switching    = true;
           
            decoder.pauseInternally();
             
            flushRequests(
              new flushListener()
              {
                private boolean triggered;
               
                public void
                flushed()
                {
                  synchronized( this ){
                   
                    if ( triggered ){
                     
                      return;
                    }
                   
                    triggered = true;
                  }
                 
                  getManager().reRoute(
                      HTTPNetworkConnectionFile.this,
                      old_hash, new_hash, header );
                }
              });
             
            return;
          }
         
         
          line = line.substring( hash_end + 1 );
         
          line = line.substring( 0, line.lastIndexOf( ' ' ));
         
          String  file = line;

          if ( to_torrent.isSimpleTorrent()){
           
              // optimise for simple torrents. also support the case where
              // client has the hash but doesn't know the file name
           
            target_file = dm.getFiles()[0];
           
          }else{
           
            target_str  = file;
           
            StringTokenizer  tok = new StringTokenizer( file, "/" );
           
            List<byte[]>  bits = new ArrayList<byte[]>();
           
            while( tok.hasMoreTokens()){
             
              bits.add( URLDecoder.decode(tok.nextToken(), "ISO-8859-1").getBytes( "ISO-8859-1" ));
            }
           
              // latest spec has torrent file name encoded first for non-simple torrents
              // remove it if we find it so we have some backward compat
           
            if ( !to_torrent.isSimpleTorrent() && bits.size() > 1 ){
           
              if ( Arrays.equals( to_torrent.getName(), (byte[])bits.get(0))){
               
                bits.remove(0);
              }
            }
           
View Full Code Here

Examples of org.gudy.azureus2.core3.torrent.TOTorrent

    if ( piece_group_size == 0 ){
     
      piece_group_size  = 1;
    }
   
    TOTorrent  to_torrent = ((TorrentImpl)_torrent).getTorrent();

    String  ua = getUserAgent();
   
    if ( to_torrent.isSimpleTorrent()){
     
      http_downloaders =
        new ExternalSeedHTTPDownloader[]{
          linear_download?new ExternalSeedHTTPDownloaderLinear( url, ua ):new ExternalSeedHTTPDownloaderRange( url, ua )};
     
      downloader_offsets   = new long[]{ 0 };
      downloader_lengths  = new long[]{ to_torrent.getSize() };
     
    }else{
     
      TOTorrentFile[]  files = to_torrent.getFiles();
     
      http_downloaders = new ExternalSeedHTTPDownloader[ files.length ];
     
      downloader_offsets   = new long[ files.length ];
      downloader_lengths  = new long[ files.length ];

      long  offset  = 0;
     
        // encoding is a problem, assume ISO-8859-1
     
      String  base_url = url.toString();
     
      if ( base_url.endsWith( "/" )){
       
        base_url = base_url.substring( 0, base_url.length()-1 );
      }
     
      base_url += "/" + URLEncoder.encode( new String( to_torrent.getName(), "ISO-8859-1" ), "ISO-8859-1" ).replaceAll("\\+", "%20");
     
      for (int i=0;i<files.length;i++ ){
       
        TOTorrentFile  file = files[i];
       
View Full Code Here

Examples of org.gudy.azureus2.core3.torrent.TOTorrent

            try{
              while( it.hasNext()){
               
                DownloadManager  dm = (DownloadManager)it.next();
               
                TOTorrent  torrent = dm.getTorrent();
               
                if ( torrent == null ){
                 
                  continue;
                }
               
                byte[]  sha1_hash = (byte[])dm.getData( "AZInstanceManager::sha1_hash" );
               
                if ( sha1_hash == null ){     

                  sha1_hash  = new SHA1Simple().calculateHash( torrent.getHash());
                 
                  dm.setData( "AZInstanceManager::sha1_hash", sha1_hash );
                }
               
                if ( Arrays.equals( hash, sha1_hash )){
View Full Code Here

Examples of org.gudy.azureus2.core3.torrent.TOTorrent

         
          for ( DownloadManager download: initial_downloads ){
 
            if ( download.isForceStart()){
             
              TOTorrent torrent = download.getTorrent();
             
              if ( torrent == null ){
               
                continue;
              }
             
              try{
                byte[] hash = torrent.getHash();
               
                String  hash_str = ByteFormatter.encodeString( hash );
               
                Map m = xfer_cache.get( hash_str );
                 
                if ( m != null ){
                 
                  if ( m.containsKey( "f" )){
                   
                    log( download, "Resetting force-start" );
                   
                    download.setForceStart( false );
                  }
                }
              }catch( Throwable e ){
               
                Debug.printStackTrace(e);
              }
            }
          }
        }
       
        gm.addListener(
          new GlobalManagerAdapter()
          {
            public void
            downloadManagerAdded(
              DownloadManager  dm )
            {
              freq_lim_updater.dispatch();
            }
             
            public void
            downloadManagerRemoved(
              DownloadManager  dm )
            {
              freq_lim_updater.dispatch();
            }
          },
          false );
      }
     
      DeviceManager manager = getManager();
       
      DeviceOfflineDownloaderManager dodm = manager.getOfflineDownlaoderManager();
     
      List<DownloadManager> downloads;
     
      if ( dodm.isOfflineDownloadingEnabled() && isEnabled()){

        List<DownloadManager> initial_downloads = gm.getDownloadManagers();

        List<DownloadManager> relevant_downloads = new ArrayList<DownloadManager>( initial_downloads.size());
     
          // remove uninteresting ones
       
        for ( DownloadManager download: initial_downloads ){
       
          int  state = download.getState();
                   
          if ( state == DownloadManager.STATE_SEEDING ){
              // state == DownloadManager.STATE_ERROR ){  removed - might be out of disk space and fixable
           
            continue;
          }
         
            // don't include 'stopping' here as we go through stopping on way to queued

          if ( state == DownloadManager.STATE_STOPPED ){
           
              // don't remove from downloader if simply paused
           
            if ( !download.isPaused()){
                           
              continue;
            }
          }
         
            // if it is complete then of no interest
         
          if ( download.isDownloadComplete( false )){
             
            continue;
          }
         
          relevant_downloads.add( download );
        }
     
        downloads = new ArrayList<DownloadManager>( relevant_downloads.size());
     
        if ( dodm.getOfflineDownloadingIsAuto()){
         
          boolean  include_private = dodm.getOfflineDownloadingIncludePrivate();
         
          if ( include_private ){
           
            downloads.addAll( relevant_downloads );
           
          }else{
           
            for ( DownloadManager download: relevant_downloads ){
             
              TOTorrent torrent = download.getTorrent();
             
              if ( !TorrentUtils.isReallyPrivate( torrent )){
               
                downloads.add( download );
              }
            }
          }
        }else{
         
            // manual, just use the tagged downloads
         
          for ( DownloadManager download: relevant_downloads ){

            if ( dodm.isManualDownload( PluginCoreUtils.wrap( download ))){
             
              downloads.add( download );
            }
          }
        }
      }else{
       
        downloads = new ArrayList<DownloadManager>();
      }
     
      Map<DownloadManager,byte[]>  download_map = new HashMap<DownloadManager, byte[]>();
     
      for ( DownloadManager download: downloads ){
                     
        TOTorrent torrent = download.getTorrent();
 
        if ( torrent == null ){
         
          continue;
        }
       
        try{
          byte[] hash = torrent.getHash();
         
          String  hash_str = ByteFormatter.encodeString( hash );
         
          DiskManager disk = download.getDiskManager();
         
          if ( disk == null ){
           
            byte[] existing = old_cache.get( hash_str );
           
            if ( existing != null ){
             
              new_cache.put( hash_str, existing );
             
              download_map.put( download, existing );
             
            }else{
             
                // assume not yet started and just use the non-skipped files
             
              DiskManagerFileInfo[] files = download.getDiskManagerFileInfo();
             
              byte[] needed = new byte[( torrent.getNumberOfPieces() + 7 ) / 8];

              int  hits = 0;

              for ( DiskManagerFileInfo file: files ){
               
                if ( file.isSkipped()){
                 
                  continue;
                }
               
                int  first_piece   = file.getFirstPieceNumber();
                int  last_piece    = first_piece + file.getNbPieces() - 1;
                               
                int  needed_pos    = first_piece/8;
                int  current_byte  = 0;
                               
                for ( int pos=first_piece;pos<=last_piece;pos++ ){
                 
                  current_byte = current_byte << 1;
                                     
                  current_byte += 1;
                   
                  hits++;
                 
                  if (( pos %8 ) == 7 ){
                   
                    needed[needed_pos++] |= (byte)current_byte;
                   
                    current_byte = 0;
                  }
                }
               
                if ( current_byte != 0 ){
                 
                  needed[needed_pos++] |= (byte)(current_byte << (8 - (last_piece % 8)));
                }
              }
             
              if ( hits > 0 ){
                 
                new_cache.put( hash_str, needed );
                 
                download_map.put( download, needed );
              }
            }
          }else{
         
            DiskManagerPiece[] pieces = disk.getPieces();
           
            byte[] needed = new byte[( pieces.length + 7 ) / 8];
           
            int  needed_pos    = 0;
            int  current_byte  = 0;
            int  pos       = 0;
           
            int  hits = 0;
           
            for ( DiskManagerPiece piece: pieces ){
             
              current_byte = current_byte << 1;
             
              if ( piece.isNeeded() && !piece.isDone()){
               
                current_byte += 1;
               
                hits++;
              }
             
              if (( pos %8 ) == 7 ){
               
                needed[needed_pos++] = (byte)current_byte;
               
                current_byte = 0;
              }
              pos++;
            }
           
            if (( pos % 8 ) != 0 ){
             
              needed[needed_pos++] = (byte)(current_byte << (8 - (pos % 8)));
            }
           
            if ( hits > 0 ){
             
              new_cache.put( hash_str, needed );
             
              download_map.put( download, needed );
            }
          }
        }catch( Throwable e ){
         
          Debug.out( e );
        }
      }
     
        // store this so we have consistent record for downloads that queue/pause etc and therefore lose accessible piece details
     
      setPersistentMapProperty( PP_OD_STATE_CACHE, new_cache );
     
        // sort by download priority
     
      List<Map.Entry<DownloadManager, byte[]>> entries = new ArrayList<Map.Entry<DownloadManager,byte[]>>( download_map.entrySet());
     
      Collections.sort(
        entries,
        new Comparator<Map.Entry<DownloadManager, byte[]>>()
        {
          public int
          compare(
            Map.Entry<DownloadManager, byte[]> o1,
            Map.Entry<DownloadManager, byte[]> o2)
          {
            return( o1.getKey().getPosition() - o2.getKey().getPosition());
          }
        });
       
      String  download_hashes = "";
     
      Iterator<Map.Entry<DownloadManager, byte[]>> it = entries.iterator();
     
      while( it.hasNext()){
       
        Map.Entry<DownloadManager, byte[]> entry = it.next();
       
        DownloadManager  download = entry.getKey();
       
        try{
          String hash = ByteFormatter.encodeString( download.getTorrent().getHash());
         
          download_hashes += ( download_hashes.length()==0?"":"," ) + hash;
         
          new_offline_downloads.put( hash, download );
         
        }catch( Throwable e ){
         
          log( download, "Failed to get download hash", e );
         
          it.remove();
        }
      }
     
      try{
        String[] set_dl_results = service.setDownloads( client_id, download_hashes );
       
        String  set_dl_result  = set_dl_results[0].trim();
        String  set_dl_status   = set_dl_results[1];
       
        if ( !set_dl_status.equals( "OK" )){
         
          error_status = MessageText.getString( "device.od.error.opfailstatus", new String[]{ "SetDownloads", set_dl_status });

          throw( new Exception( "Failing result returned: " + set_dl_status ));
        }
       
        String[]  bits = Constants.PAT_SPLIT_COMMA.split(set_dl_result);
       
        int  num_bits = set_dl_result.length()==0?0:bits.length;
       
        if ( num_bits != entries.size()){
         
          log( "SetDownloads returned an invalid number of results (hashes=" + new_offline_downloads.size() + ",result=" + set_dl_result + ")");
         
        }else{
         
          it = entries.iterator();
         
          int  pos = 0;
         
          while( it.hasNext()){
           
            Map.Entry<DownloadManager, byte[]> entry = it.next();
           
            DownloadManager  download = entry.getKey();
           
            try{
              TOTorrent torrent = download.getTorrent();
 
              String hash_str = ByteFormatter.encodeString( torrent.getHash());
             
              int  status = Integer.parseInt( bits[ pos++ ]);
   
              boolean  do_update = false;
           
              if ( status == 0 ){
             
                do_update = true;
             
              }else if ( status == 1 ){
             
                  // need to add the torrent
             
                try{
                    // for vuze content add in the azid
                 
                  if ( PlatformTorrentUtils.isContent( torrent, true )){
                   
                    String ext = DownloadUtils.getTrackerExtensions( PluginCoreUtils.wrap( download ));
                   
                    if ( ext != null && ext.length() > 0 ){
                     
                      try{
                       
                        if ( ext.startsWith( "&" )){
                         
                          ext = ext.substring(1);
                        }
                     
                        torrent = TOTorrentFactory.deserialiseFromMap( torrent.serialiseToMap());
                       
                        torrent.setAnnounceURL( appendToURL( torrent.getAnnounceURL(), ext ));
                       
                        TOTorrentAnnounceURLSet[] sets = torrent.getAnnounceURLGroup().getAnnounceURLSets();
                       
                        for ( TOTorrentAnnounceURLSet set: sets ){
                         
                          URL[] urls = set.getAnnounceURLs();
                         
                          for (int i=0;i<urls.length;i++){
                           
                            urls[i] = appendToURL( urls[i], ext );
                          }
                        }
                       
                        torrent.getAnnounceURLGroup().setAnnounceURLSets( sets );
                       
                      }catch( Throwable e ){
                       
                        log( "Torrent modification failed", e );
                      }
                    }
                  }
                 
                  String add_result =
                    addTorrent(                    
                      hash_str,
                      ByteFormatter.encodeStringFully( BEncoder.encode( torrent.serialiseToMap())));
                 
                  log( download, "AddDownload succeeded" );
                 
                  if ( add_result.equals( "OK" )){
                   
                    do_update = true;
                   
                  }else{
                   
                    error_status = MessageText.getString( "device.od.error.opfailstatus", new String[]{ "AddDownload", add_result });
                 
                    throw( new Exception( "Failed to add download: " + add_result ));
                  }
                }catch( Throwable e ){
                 
                    // TODO: prevent continual attempts to add same torrent?
                 
                  error_status = MessageText.getString( "device.od.error.opfailexcep", new String[]{ "AddDownload", Debug.getNestedExceptionMessage( e )});

                  log( download, "Failed to add download", e );
                }
              }else{
             
                error_status = MessageText.getString( "device.od.error.opfailstatus", new String[]{ "SetDownloads", String.valueOf( status )});

                log( download, "SetDownloads: error status returned - " + status );
              }
         
              if ( do_update ){
         
                try{
                  byte[]  required_map = entry.getValue();
                 
                  String  required_bitfield = ByteFormatter.encodeStringFully( required_map );
                 
                  String[] update_results =
                    service.updateDownload(
                      client_id,
                      hash_str,
                      required_bitfield );
                   
                  String  have_bitfield  = update_results[0];
                  String  update_status   = update_results[1];
                 
                  if ( !update_status.equals( "OK" )){
                   
                    error_status = MessageText.getString( "device.od.error.opfailstatus", new String[]{ "UpdateDownload", update_status });

                    throw( new Exception( "UpdateDownload: Failing result returned: " + update_status ));
                  }
                       
                  int    useful_piece_count   = 0;
                 
                  if ( have_bitfield.length() > 0 ){
                   
                    byte[]  have_map = ByteFormatter.decodeString( have_bitfield );
                   
                    if ( have_map.length != required_map.length ){
                     
                      throw( new Exception( "UpdateDownload: Returned bitmap length invalid" ));
                    }
                   
                    for ( int i=0;i<required_map.length;i++){
                     
                      int x = ( required_map[i] & have_map[i] )&0xff;
                     
                      if ( x != 0 ){
                         
                        for (int j=0;j<8;j++){
                         
                          if ((x&0x01) != 0 ){
                           
                            useful_piece_count++;
                          }
                         
                          x >>= 1;
                        }
                      }
                    }
                   
                    if ( useful_piece_count > 0 ) {
                   
                      long  piece_size  = torrent.getPieceLength();

                      new_transferables.put( hash_str, new TransferableDownload( download, hash_str, have_map, useful_piece_count * piece_size ));
                    }
                  }
                 
View Full Code Here

Examples of org.gudy.azureus2.core3.torrent.TOTorrent

      }
     
      if ( new_web_seeds.size() > 0 ){
       
        try{
          TOTorrent torrent = TOTorrentFactory.deserialiseFromBEncodedByteArray( torrent_data );
 
          Object obj = torrent.getAdditionalProperty( "url-list" );
         
          List<String> existing = new ArrayList<String>();
         
          if ( obj instanceof byte[] ){
                   
            try{
              new_web_seeds.remove( new URL( new String((byte[])obj, "UTF-8" )).toExternalForm());
             
            }catch( Throwable e ){             
            }
          }else if ( obj instanceof List ){
           
            List<byte[]> l = (List<byte[]>)obj;
           
            for ( byte[] b: l ){
             
              try{
                existing.add( new URL( new String((byte[])b, "UTF-8" )).toExternalForm());
               
              }catch( Throwable e ){             
              }
            }
          }
         
          boolean update = false;
         
          for ( String e: new_web_seeds ){
           
            if ( !existing.contains( e )){
             
              existing.add( e );
             
              update = true;
            }
          }
         
          if ( update ){
         
            List<byte[]>  l = new ArrayList<byte[]>();
           
            for ( String s: existing ){
             
              l.add( s.getBytes( "UTF-8" ));
            }
           
            torrent.setAdditionalProperty( "url-list", l );
           
            torrent_data = BEncoder.encode( torrent.serialiseToMap());
          }
         
        }catch( Throwable e ){
        }
      }
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.