/**
  * Convert a ClientKeyBlock to a Bucket. If an error occurs, report it via onFailure and return
  * null.
  */
 protected Bucket extract(
     ClientKeyBlock block, Object token, ObjectContainer container, ClientContext context) {
   Bucket data;
   try {
     data =
         block.decode(
             context.getBucketFactory(persistent),
             (int) (Math.min(ctx.maxOutputLength, Integer.MAX_VALUE)),
             false);
   } catch (KeyDecodeException e1) {
     if (Logger.shouldLog(Logger.MINOR, this)) Logger.minor(this, "Decode failure: " + e1, e1);
     onFailure(
         new FetchException(FetchException.BLOCK_DECODE_ERROR, e1.getMessage()),
         token,
         container,
         context);
     return null;
   } catch (TooBigException e) {
     onFailure(
         new FetchException(FetchException.TOO_BIG, e.getMessage()), token, container, context);
     return null;
   } catch (IOException e) {
     Logger.error(this, "Could not capture data - disk full?: " + e, e);
     onFailure(new FetchException(FetchException.BUCKET_ERROR, e), token, container, context);
     return null;
   }
   if (Logger.shouldLog(Logger.MINOR, this))
     Logger.minor(
         this, data == null ? "Could not decode: null" : ("Decoded " + data.size() + " bytes"));
   return data;
 }
 /** Handle an FNPRoutedRejected message. */
 private boolean handleRoutedRejected(Message m) {
   long id = m.getLong(DMT.UID);
   Long lid = Long.valueOf(id);
   RoutedContext rc = routedContexts.get(lid);
   if (rc == null) {
     // Gah
     Logger.error(this, "Unrecognized FNPRoutedRejected");
     return false; // locally originated??
   }
   short htl = rc.lastHtl;
   if (rc.source != null) htl = rc.source.decrementHTL(htl);
   short ohtl = m.getShort(DMT.HTL);
   if (ohtl < htl) htl = ohtl;
   if (htl == 0) {
     // Equivalent to DNF.
     // Relay.
     if (rc.source != null) {
       try {
         rc.source.sendAsync(
             DMT.createFNPRoutedRejected(id, (short) 0), null, nodeStats.routedMessageCtr);
       } catch (NotConnectedException e) {
         // Ouch.
         Logger.error(this, "Unable to relay probe DNF: peer disconnected: " + rc.source);
       }
     }
   } else {
     // Try routing to the next node
     forward(rc.msg, id, rc.source, htl, rc.msg.getDouble(DMT.TARGET_LOCATION), rc, rc.identity);
   }
   return true;
 }
 @Override
 public void run() {
   try {
     freenet.support.Logger.OSThread.logPID(this);
     // FIXME ? key is not known for inserts here
     if (key != null) stats.reportOutgoingLocalRequestLocation(key.toNormalizedDouble());
     if (!req.send(core, sched)) {
       if (!((!req.isPersistent()) && req.isCancelled()))
         Logger.error(this, "run() not able to send a request on " + req);
       else
         Logger.normal(
             this, "run() not able to send a request on " + req + " - request was cancelled");
     }
     if (logMINOR) Logger.minor(this, "Finished " + req);
   } finally {
     if (req.sendIsBlocking()) {
       if (key != null) sched.removeFetchingKey(key);
       else if ((!req.isPersistent())
           && ((TransientChosenBlock) req).request instanceof SendableInsert)
         sched.removeTransientInsertFetching(
             (SendableInsert) (((TransientChosenBlock) req).request), req.token);
       // Something might be waiting for a request to complete (e.g. if we have two requests for
       // the same key),
       // so wake the starter thread.
       wakeUp();
     }
   }
 }
Example #4
0
 public boolean removePendingKeys(HasKeyListener hasListener) {
   ArrayList<KeyListener> matches = new ArrayList<KeyListener>();
   synchronized (this) {
     for (KeyListener listener : keyListeners) {
       HasKeyListener hkl;
       try {
         hkl = listener.getHasKeyListener();
       } catch (Throwable t) {
         Logger.error(this, format("Error in getHasKeyListener callback for %s", listener), t);
         continue;
       }
       if (hkl == hasListener) {
         matches.add(listener);
       }
     }
   }
   if (matches.isEmpty()) {
     return false;
   }
   for (KeyListener listener : matches) {
     try {
       removePendingKeys(listener);
     } catch (Throwable t) {
       Logger.error(this, format("Error while removing %s", listener), t);
     }
   }
   return true;
 }
 public void onGeneratedURI(FreenetURI uri, BaseClientPutter state, ObjectContainer container) {
   if (logMINOR) Logger.minor(this, "Generated URI for " + darknetOpennetString + " ARK: " + uri);
   long l = uri.getSuggestedEdition();
   if (l < crypto.myARKNumber) {
     Logger.error(
         this,
         "Inserted "
             + darknetOpennetString
             + " ARK edition # lower than attempted: "
             + l
             + " expected "
             + crypto.myARKNumber);
   } else if (l > crypto.myARKNumber) {
     if (logMINOR)
       Logger.minor(
           this,
           darknetOpennetString + " ARK number moving from " + crypto.myARKNumber + " to " + l);
     crypto.myARKNumber = l;
     if (crypto.isOpennet) node.writeOpennetFile();
     else node.writeNodeFile();
     // We'll broadcast the new ARK edition to our connected peers via a differential node
     // reference
     SimpleFieldSet fs = new SimpleFieldSet(true);
     fs.putSingle("ark.number", Long.toString(crypto.myARKNumber));
     node.peers.locallyBroadcastDiffNodeRef(fs, !crypto.isOpennet, crypto.isOpennet);
   }
 }
 /** Called on failed/canceled fetch */
 public void onFailure(FetchException e, ClientGetter state, ObjectContainer container) {
   fetchFailures++;
   if (fetchFailures < 20 && e.newURI != null) {
     try {
       if (logMINOR) Logger.minor(this, "Trying new URI: " + e.newURI);
       indexuri = e.newURI.setMetaString(new String[] {""}).toString();
       if (origEdition != -1 && e.newURI.getEdition() < origEdition) {
         Logger.error(
             this,
             "Redirect to earlier edition?!?!?!?: "
                 + e.newURI.getEdition()
                 + " from "
                 + origEdition);
       } else {
         if (logMINOR) Logger.minor(this, "Trying new URI: " + e.newURI + " : " + indexuri);
         startFetch(true);
         if (updateHook != null) updateHook.update(updateContext, indexuri);
         return;
       }
     } catch (FetchException ex) {
       e = ex;
     } catch (MalformedURLException ex) {
       Logger.error(this, "what?", ex);
     }
   }
   fetchStatus = FetchStatus.FAILED;
   for (FindRequest findRequest : waitingOnMainIndex) {
     findRequest.setError(
         new TaskAbortException("Failure fetching rootindex of " + toString(), e));
   }
   Logger.error(this, "Fetch failed on " + toString() + " -- state = " + state, e);
 }
    public synchronized void run() {
      try {
        while (waitingOnSubindex.size() > 0) {
          if (fetchStatus == FetchStatus.UNFETCHED || fetchStatus == FetchStatus.FAILED) {
            try {
              fetchStatus = FetchStatus.FETCHING;
              // TODO tidy the fetch stuff
              bucket = Util.fetchBucket(indexuri + filename, hlsc);
              fetchStatus = FetchStatus.FETCHED;

            } catch (Exception e) { // TODO tidy the exceptions
              // java.net.MalformedURLException
              // freenet.client.FetchException
              String msg = indexuri + filename + " could not be opened: " + e.toString();
              Logger.error(this, msg, e);
              throw new TaskAbortException(msg, e);
            }
          } else if (fetchStatus == FetchStatus.FETCHED) {
            parseSubIndex();
          } else {
            break;
          }
        }
      } catch (TaskAbortException e) {
        fetchStatus = FetchStatus.FAILED;
        this.error = e;
        Logger.error(this, "Dropping from subindex run loop", e);
        for (FindRequest r : parsingSubindex) r.setError(e);
        for (FindRequest r : waitingOnSubindex) r.setError(e);
      }
    }
Example #8
0
 @Override
 public void requeueAfterCooldown(
     Key key, long time, ObjectContainer container, ClientContext context) {
   MyCooldownTrackerItem tracker = makeCooldownTrackerItem(container, context);
   if (tracker.cooldownWakeupTime > time) {
     if (logMINOR) Logger.minor(this, "Not requeueing as deadline has not passed yet");
     return;
   }
   if (isEmpty(container)) {
     if (logMINOR) Logger.minor(this, "Not requeueing as cancelled or finished");
     return;
   }
   if (persistent) container.activate(this.key, 5);
   if (!(key.equals(this.key.getNodeKey(false)))) {
     Logger.error(
         this,
         "Got requeueAfterCooldown for wrong key: "
             + key
             + " but mine is "
             + this.key.getNodeKey(false)
             + " for "
             + this.key);
     return;
   }
   if (logMINOR) Logger.minor(this, "Requeueing after cooldown " + key + " for " + this);
   reschedule(container, context);
   if (persistent) container.deactivate(this.key, 5);
 }
Example #9
0
 /**
  * Adds the node reference in the resources as a friend to the node this plugin is loaded.
  *
  * @return the corresponding PeerAdditionReturnCode indicating whether the bridge was added
  *     successfully as a friend
  */
 private PeerAdditionReturnCodes addFriendBridge() {
   SimpleFieldSet bridgeNodeFS;
   try {
     bridgeNodeFS = nodeRefHelper.getBridgeNodeRefFS();
   } catch (IOException e) {
     Logger.error(this, "IO Exception while parsing bridge reference resource file");
     return PeerAdditionReturnCodes.INTERNAL_ERROR;
   }
   PeerNode pn;
   try {
     pn = node.createNewDarknetNode(bridgeNodeFS, FRIEND_TRUST.HIGH, FRIEND_VISIBILITY.NO);
     ((DarknetPeerNode) pn).setPrivateDarknetCommentNote("Master Bridge");
   } catch (FSParseException e) {
     return PeerAdditionReturnCodes.CANT_PARSE;
   } catch (PeerParseException e) {
     return PeerAdditionReturnCodes.CANT_PARSE;
   } catch (ReferenceSignatureVerificationException e) {
     return PeerAdditionReturnCodes.INVALID_SIGNATURE;
   } catch (Throwable t) {
     Logger.error(this, "Internal error adding reference :" + t.getMessage(), t);
     return PeerAdditionReturnCodes.INTERNAL_ERROR;
   }
   if (Arrays.equals(pn.getPubKeyHash(), node.getDarknetPubKeyHash())) {
     Logger.warning(this, "The bridge  node reference file belongs to this node.");
     return PeerAdditionReturnCodes.TRY_TO_ADD_SELF;
   }
   if (!node.addPeerConnection(pn)) {
     return PeerAdditionReturnCodes.ALREADY_IN_REFERENCE;
   }
   return PeerAdditionReturnCodes.OK;
 }
Example #10
0
  public static boolean writeTo(InputStream input, File target)
      throws FileNotFoundException, IOException {
    DataInputStream dis = null;
    FileOutputStream fos = null;
    File file = File.createTempFile("temp", ".tmp", target.getParentFile());
    if (Logger.shouldLog(Logger.MINOR, FileUtil.class))
      Logger.minor(FileUtil.class, "Writing to " + file + " to be renamed to " + target);

    try {
      dis = new DataInputStream(input);
      fos = new FileOutputStream(file);

      int len = 0;
      byte[] buffer = new byte[4096];
      while ((len = dis.read(buffer)) > 0) {
        fos.write(buffer, 0, len);
      }
    } catch (IOException e) {
      throw e;
    } finally {
      if (dis != null) dis.close();
      if (fos != null) fos.close();
    }

    if (FileUtil.renameTo(file, target)) return true;
    else {
      file.delete();
      return false;
    }
  }
Example #11
0
 public void onGotKey(Key key, KeyBlock block, ObjectContainer container, ClientContext context) {
   if (persistent) {
     container.activate(this, 1);
     container.activate(key, 5);
     container.activate(this.key, 5);
   }
   synchronized (this) {
     if (finished) {
       if (logMINOR)
         Logger.minor(this, "onGotKey() called twice on " + this, new Exception("debug"));
       return;
     }
     finished = true;
     if (persistent) container.store(this);
     if (isCancelled(container)) return;
     if (key == null) throw new NullPointerException();
     if (this.key == null) throw new NullPointerException("Key is null on " + this);
     if (!key.equals(this.key.getNodeKey(false))) {
       Logger.normal(this, "Got sent key " + key + " but want " + this.key + " for " + this);
       return;
     }
   }
   unregister(
       container,
       context,
       getPriorityClass(container)); // Key has already been removed from pendingKeys
   onSuccess(block, false, null, container, context);
   if (persistent) {
     container.deactivate(this, 1);
     container.deactivate(this.key, 1);
   }
 }
Example #12
0
 static {
   Logger.minor(NativeThread.class, "Running init()");
   // Loading the NativeThread library isn't useful on macos
   boolean maybeLoadNative =
       ("Linux".equalsIgnoreCase(System.getProperty("os.name")))
           && (NodeStarter.extBuildNumber > 18);
   Logger.debug(NativeThread.class, "Run init(): should loadNative=" + maybeLoadNative);
   if (maybeLoadNative && LibraryLoader.loadNative("/freenet/support/io/", "NativeThread")) {
     NATIVE_PRIORITY_BASE = getLinuxPriority();
     NATIVE_PRIORITY_RANGE = 20 - NATIVE_PRIORITY_BASE;
     System.out.println(
         "Using the NativeThread implementation (base nice level is "
             + NATIVE_PRIORITY_BASE
             + ')');
     // they are 3 main prio levels
     HAS_THREE_NICE_LEVELS = NATIVE_PRIORITY_RANGE >= 3;
     HAS_ENOUGH_NICE_LEVELS = NATIVE_PRIORITY_RANGE >= ENOUGH_NICE_LEVELS;
     HAS_PLENTY_NICE_LEVELS = NATIVE_PRIORITY_RANGE >= JAVA_PRIORITY_RANGE;
     if (!(HAS_ENOUGH_NICE_LEVELS && HAS_THREE_NICE_LEVELS))
       System.err.println(
           "WARNING!!! The JVM has been niced down to a level which won't allow it to schedule threads properly! LOWER THE NICE LEVEL!!");
     _loadNative = true;
   } else {
     // unused anyway
     NATIVE_PRIORITY_BASE = 0;
     NATIVE_PRIORITY_RANGE = 19;
     HAS_THREE_NICE_LEVELS = true;
     HAS_ENOUGH_NICE_LEVELS = true;
     HAS_PLENTY_NICE_LEVELS = true;
     _loadNative = false;
   }
   Logger.minor(NativeThread.class, "Run init(): _loadNative = " + _loadNative);
 }
 @Override
 public ClientKey getKey(Object token, ObjectContainer container) {
   if (persistent) {
     container.activate(this, 1);
     container.activate(segment, 1);
   }
   synchronized (segment) {
     if (cancelled) {
       if (logMINOR)
         Logger.minor(this, "Segment is finishing when getting key " + token + " on " + this);
       return null;
     }
     ClientKey key = segment.getBlockKey(((MySendableRequestItem) token).x, container);
     if (key == null) {
       if (segment.isFinished(container)) {
         Logger.error(this, "Segment finished but didn't tell us! " + this);
       } else if (segment.isFinishing(container)) {
         Logger.error(this, "Segment finishing but didn't tell us! " + this);
       } else {
         Logger.error(
             this,
             "Segment not finishing yet still returns null for getKey()!: "
                 + token
                 + " for "
                 + this,
             new Exception("debug"));
       }
     }
     return key;
   }
 }
 /**
  * Terminate a subsegment. Called by the segment, which will have already removed the subsegment
  * from the list. Will delete the object from the database if persistent.
  */
 public void kill(
     ObjectContainer container,
     ClientContext context,
     boolean dontDeactivateSeg,
     boolean cancelledAlready) {
   if (persistent) {
     container.activate(segment, 1);
     container.activate(blockNums, 1);
   }
   if (logMINOR) Logger.minor(this, "Killing " + this);
   // Do unregister() first so can get and unregister each key and avoid a memory leak
   unregister(container, context, getPriorityClass(container));
   Integer[] oldNums = null;
   synchronized (segment) {
     if (cancelledAlready) {
       if (!cancelled) {
         Logger.error(this, "Should be cancelled already! " + this, new Exception("error"));
         cancelled = true;
       }
       if (!blockNums.isEmpty())
         Logger.error(
             this, "Block nums not empty! on " + this + " : " + blockNums, new Exception("error"));
     } else {
       if (cancelled) return;
       cancelled = true;
     }
     if (persistent) oldNums = blockNums.toArray(new Integer[blockNums.size()]);
     blockNums.clear();
   }
   if (persistent && oldNums != null && oldNums.length > 0) {
     for (Integer i : oldNums) container.delete(i);
   }
   if (persistent) removeFrom(container, context, dontDeactivateSeg);
 }
Example #15
0
  void updateSlot(final USK origUSK, final long number, final ClientContext context) {
    if (logMINOR) Logger.minor(this, "Updating (slot) " + origUSK.getURI() + " : " + number);
    USK clear = origUSK.clearCopy();
    final USKCallback[] callbacks;
    synchronized (this) {
      Long l = latestSlotByClearUSK.get(clear);
      if (logMINOR) Logger.minor(this, "Old slot: " + l);
      if ((l == null) || (number > l.longValue())) {
        l = Long.valueOf(number);
        latestSlotByClearUSK.put(clear, l);
        if (logMINOR) Logger.minor(this, "Put " + number);
      } else return;

      callbacks = subscribersByClearUSK.get(clear);
      if (temporaryBackgroundFetchersPrefetch.containsKey(clear)) {
        temporaryBackgroundFetchersPrefetch.put(clear, System.currentTimeMillis());
        schedulePrefetchChecker();
      }
    }
    if (callbacks != null) {
      // Run off-thread, because of locking, and because client callbacks may take some time
      final USK usk = origUSK.copy(number);
      for (final USKCallback callback : callbacks)
        context.mainExecutor.execute(
            new Runnable() {
              @Override
              public void run() {
                callback.onFoundEdition(
                    number, usk, null, // non-persistent
                    context, false, (short) -1, null, false, false);
              }
            },
            "USKManager callback executor for " + callback);
    }
  }
Example #16
0
  /**
   * Detects the operating system in which the JVM is running. Returns OperatingSystem.Unknown if
   * the OS is unknown or an error occured. Therefore this function should never throw.
   */
  private static OperatingSystem detectOperatingSystem() { // TODO: Move to the proper class
    try {
      final String name = System.getProperty("os.name").toLowerCase();

      // Order the if() by probability instead alphabetically to decrease the false-positive rate in
      // case they decide to call it "Windows Mac" or whatever

      // Please adapt sanitizeFileName when adding new OS.

      if (name.indexOf("win") >= 0) return OperatingSystem.Windows;

      if (name.indexOf("mac") >= 0) return OperatingSystem.MacOS;

      if (name.indexOf("linux") >= 0) return OperatingSystem.Linux;

      if (name.indexOf("freebsd") >= 0) return OperatingSystem.FreeBSD;

      if (name.indexOf("unix") >= 0) return OperatingSystem.GenericUnix;
      else if (File.separatorChar == '/') return OperatingSystem.GenericUnix;
      else if (File.separatorChar == '\\') return OperatingSystem.Windows;

      Logger.error(FileUtil.class, "Unknown operating system:" + name);
    } catch (Throwable t) {
      Logger.error(FileUtil.class, "Operating system detection failed", t);
    }

    return OperatingSystem.Unknown;
  }
Example #17
0
 public void checkUSK(
     FreenetURI uri, boolean persistent, ObjectContainer container, boolean isMetadata) {
   try {
     if (persistent) container.activate(uri, 5);
     FreenetURI uu;
     if (uri.isSSK() && uri.isSSKForUSK()) {
       uu = uri.setMetaString(null).uskForSSK();
     } else if (uri.isUSK()) {
       uu = uri;
     } else {
       return;
     }
     USK usk = USK.create(uu);
     if (!isMetadata) context.uskManager.updateKnownGood(usk, uu.getSuggestedEdition(), context);
     else
       // We don't know whether the metadata is fetchable.
       // FIXME add a callback so if the rest of the request completes we updateKnownGood().
       context.uskManager.updateSlot(usk, uu.getSuggestedEdition(), context);
   } catch (MalformedURLException e) {
     Logger.error(this, "Caught " + e, e);
   } catch (Throwable t) {
     // Don't let the USK hint cause us to not succeed on the block.
     Logger.error(this, "Caught " + t, t);
   }
 }
Example #18
0
  @Override
  public void sendPendingMessages(
      FCPConnectionOutputHandler handler,
      boolean includePersistentRequest,
      boolean includeData,
      boolean onlyData,
      ObjectContainer container) {
    if (persistenceType == ClientRequest.PERSIST_CONNECTION) {
      Logger.error(this, "WTF? persistenceType=" + persistenceType, new Exception("error"));
      return;
    }
    if (!onlyData) {
      if (includePersistentRequest) {
        FCPMessage msg = persistentTagMessage(container);
        handler.queue(msg);
      }
      if (progressPending != null) {
        if (persistenceType == PERSIST_FOREVER) container.activate(progressPending, 5);
        handler.queue(progressPending);
      }
      if (sentToNetwork) handler.queue(new SendingToNetworkMessage(identifier, global));
      if (finished) trySendDataFoundOrGetFailed(handler, container);
    }

    if (onlyData && allDataPending == null) {
      Logger.error(this, "No data pending !");
    }

    if (includeData && (allDataPending != null)) {
      if (persistenceType == PERSIST_FOREVER) container.activate(allDataPending, 5);
      handler.queue(allDataPending);
    }
  }
  /**
   * Fetch main index & process if local or fetch in background with callback if Freenet URI
   *
   * @throws freenet.client.FetchException
   * @throws java.net.MalformedURLException
   */
  private synchronized void startFetch(boolean retry) throws FetchException, MalformedURLException {
    if ((!retry) && (fetchStatus != FetchStatus.UNFETCHED && fetchStatus != FetchStatus.FAILED))
      return;
    fetchStatus = FetchStatus.FETCHING;
    String uri = indexuri + DEFAULT_FILE;

    // try local file first
    File file = new File(uri);
    if (file.exists() && file.canRead()) {
      processRequests(new FileBucket(file, true, false, false, false, false));
      return;
    }

    if (logMINOR) Logger.minor(this, "Fetching " + uri);
    // FreenetURI, try to fetch from freenet
    FreenetURI u = new FreenetURI(uri);
    while (true) {
      try {
        rootGetter = hlsc.fetch(u, -1, this, this, hlsc.getFetchContext().clone());
        Logger.normal(this, "Fetch started : " + toString());
        break;
      } catch (FetchException e) {
        if (e.newURI != null) {
          u = e.newURI;
          if (logMINOR) Logger.minor(this, "New URI: " + uri);
          continue;
        } else throw e;
      }
    }
  }
 protected ClientKeyBlock innerEncode(RandomSource random, ObjectContainer container)
     throws InsertException {
   if (persistent) {
     container.activate(uri, 1);
     container.activate(sourceData, 1);
   }
   try {
     return innerEncode(
         random,
         uri,
         sourceData,
         isMetadata,
         compressionCodec,
         sourceLength,
         ctx.compressorDescriptor);
   } catch (KeyEncodeException e) {
     Logger.error(SingleBlockInserter.class, "Caught " + e, e);
     throw new InsertException(InsertException.INTERNAL_ERROR, e, null);
   } catch (MalformedURLException e) {
     throw new InsertException(InsertException.INVALID_URI, e, null);
   } catch (IOException e) {
     Logger.error(SingleBlockInserter.class, "Caught " + e + " encoding data " + sourceData, e);
     throw new InsertException(InsertException.BUCKET_ERROR, e, null);
   } catch (InvalidCompressionCodecException e) {
     throw new InsertException(InsertException.INTERNAL_ERROR, e, null);
   }
 }
      @Override
      public void endElement(String namespaceURI, String localName, String qName) {
        if (processingWord && wordMatches != null && qName.equals("file")) {
          HashMap<Integer, String> termpositions = null;
          if (characters != null) {
            String[] termposs = characters.toString().split(",");
            termpositions = new HashMap<Integer, String>();
            for (String pos : termposs) {
              try {
                termpositions.put(Integer.valueOf(pos), null);
              } catch (NumberFormatException e) {
                Logger.error(this, "Position in index not an integer :" + pos, e);
              }
            }
            characters = null;
          }

          FileMatch thisFile = new FileMatch(id, termpositions, thisWordMatch);

          ArrayList<FileMatch> matchList = idToFileMatches.get(id);
          if (matchList == null) {
            matchList = new ArrayList<FileMatch>();
            idToFileMatches.put(id, matchList);
          }
          if (logMINOR) Logger.minor(this, "Match: id=" + id + " for word " + match);
          matchList.add(thisFile);
        }
      }
 protected ClientKeyBlock encode(
     ObjectContainer container, ClientContext context, boolean calledByCB) throws InsertException {
   if (persistent) {
     container.activate(sourceData, 1);
     container.activate(cb, 1);
   }
   ClientKeyBlock block;
   boolean shouldSend;
   synchronized (this) {
     if (finished) return null;
     if (sourceData == null) {
       Logger.error(this, "Source data is null on " + this + " but not finished!");
       return null;
     }
     block = innerEncode(context.random, container);
     shouldSend = (resultingURI == null);
     resultingURI = block.getClientKey().getURI();
   }
   if (logMINOR)
     Logger.minor(
         this,
         "Encoded "
             + resultingURI
             + " for "
             + this
             + " shouldSend="
             + shouldSend
             + " dontSendEncoded="
             + dontSendEncoded);
   if (shouldSend && !dontSendEncoded) cb.onEncode(block.getClientKey(), this, container, context);
   if (shouldSend && persistent) container.store(this);
   if (persistent && !calledByCB) container.deactivate(cb, 1);
   return block;
 }
Example #23
0
 public boolean tripPendingKey(Key key, KeyBlock block, ClientContext context) {
   if ((key instanceof NodeSSK) != isSSKScheduler) {
     Logger.error(
         this, "Key " + key + " on scheduler ssk=" + isSSKScheduler, new Exception("debug"));
     return false;
   }
   assert (key instanceof NodeSSK == isSSKScheduler);
   byte[] saltedKey = saltKey(key);
   List<KeyListener> matches = probablyWantKey(key, saltedKey);
   boolean ret = false;
   for (KeyListener listener : matches) {
     try {
       if (listener.handleBlock(key, saltedKey, block, context)) {
         ret = true;
       }
     } catch (Throwable t) {
       Logger.error(this, format("Error in handleBlock callback for %s", listener), t);
     }
     if (listener.isEmpty()) {
       try {
         removePendingKeys(listener);
       } catch (Throwable t) {
         Logger.error(this, format("Error while removing %s", listener), t);
       }
     }
   }
   return ret;
 }
 public static void loadKeyListeners(final ObjectContainer container, ClientContext context) {
   ObjectSet<HasKeyListener> results = Db4oBugs.query(container, HasKeyListener.class);
   for (HasKeyListener l : results) {
     container.activate(l, 1);
     try {
       if (l.isCancelled(container)) continue;
       KeyListener listener = l.makeKeyListener(container, context, true);
       if (listener != null) {
         if (listener.isSSK())
           context.getSskFetchScheduler(listener.isRealTime()).addPersistentPendingKeys(listener);
         else
           context.getChkFetchScheduler(listener.isRealTime()).addPersistentPendingKeys(listener);
         if (logMINOR)
           Logger.minor(
               ClientRequestScheduler.class,
               "Loaded request key listener: " + listener + " for " + l);
       }
     } catch (KeyListenerConstructionException e) {
       System.err.println("FAILED TO LOAD REQUEST BLOOM FILTERS:");
       e.printStackTrace();
       Logger.error(
           ClientRequestSchedulerCore.class, "FAILED TO LOAD REQUEST BLOOM FILTERS: " + e, e);
     } catch (Throwable t) {
       // Probably an error on last startup???
       Logger.error(ClientRequestSchedulerCore.class, "FAILED TO LOAD REQUEST: " + t, t);
       System.err.println("FAILED TO LOAD REQUEST: " + t);
       t.printStackTrace();
     }
     container.deactivate(l, 1);
   }
 }
Example #25
0
 public static boolean renameTo(File orig, File dest) {
   // Try an atomic rename
   // Shall we prevent symlink-race-conditions here ?
   if (orig.equals(dest))
     throw new IllegalArgumentException("Huh? the two file descriptors are the same!");
   if (!orig.exists()) {
     throw new IllegalArgumentException("Original doesn't exist!");
   }
   if (!orig.renameTo(dest)) {
     // Not supported on some systems (Windows)
     if (!dest.delete()) {
       if (dest.exists()) {
         Logger.error("FileUtil", "Could not delete " + dest + " - check permissions");
         System.err.println("Could not delete " + dest + " - check permissions");
       }
     }
     if (!orig.renameTo(dest)) {
       String err =
           "Could not rename "
               + orig
               + " to "
               + dest
               + (dest.exists() ? " (target exists)" : "")
               + (orig.exists() ? " (source exists)" : "")
               + " - check permissions";
       Logger.error(FileUtil.class, err);
       System.err.println(err);
       return false;
     }
   }
   return true;
 }
 /**
  * Register a group of requests (not inserts): a GotKeyListener and/or one or more SendableGet's.
  *
  * @param listener Listens for specific keys. Can be null if the listener is already registered
  *     i.e. on retrying.
  * @param getters The actual requests to register to the request sender queue.
  * @param persistent True if the request is persistent.
  * @param onDatabaseThread True if we are running on the database thread. NOTE:
  *     delayedStoreCheck/probablyNotInStore is unnecessary because we only register the listener
  *     once.
  * @throws FetchException
  */
 public void register(
     final HasKeyListener hasListener,
     final SendableGet[] getters,
     final boolean persistent,
     ObjectContainer container,
     final BlockSet blocks,
     final boolean noCheckStore)
     throws KeyListenerConstructionException {
   if (logMINOR) Logger.minor(this, "register(" + persistent + "," + hasListener + "," + getters);
   if (isInsertScheduler) {
     IllegalStateException e = new IllegalStateException("finishRegister on an insert scheduler");
     throw e;
   }
   if (persistent) {
     innerRegister(hasListener, getters, blocks, noCheckStore, container);
   } else {
     final KeyListener listener;
     if (hasListener != null) {
       listener = hasListener.makeKeyListener(container, clientContext, false);
       if (listener != null) schedTransient.addPendingKeys(listener);
       else Logger.normal(this, "No KeyListener for " + hasListener);
     } else listener = null;
     if (getters != null && !noCheckStore) {
       for (SendableGet getter : getters) datastoreChecker.queueTransientRequest(getter, blocks);
     } else {
       boolean anyValid = false;
       for (int i = 0; i < getters.length; i++) {
         if (!(getters[i].isCancelled(null)
             || getters[i].getCooldownTime(container, clientContext, System.currentTimeMillis())
                 != 0)) anyValid = true;
       }
       finishRegister(getters, false, container, anyValid, null);
     }
   }
 }
Example #27
0
 /*
 Borrowed from mrogers simulation code (February 6, 2008)
 */
 static void makeKleinbergNetwork(Node[] nodes) {
   for (int i = 0; i < nodes.length; i++) {
     Node a = nodes[i];
     // Normalise the probabilities
     double norm = 0.0;
     for (int j = 0; j < nodes.length; j++) {
       Node b = nodes[j];
       if (a.getLocation() == b.getLocation()) continue;
       norm += 1.0 / distance(a, b);
     }
     // Create DEGREE/2 outgoing connections
     for (int k = 0; k < nodes.length; k++) {
       Node b = nodes[k];
       if (a.getLocation() == b.getLocation()) continue;
       double p = 1.0 / distance(a, b) / norm;
       for (int n = 0; n < DEGREE / 2; n++) {
         if (Math.random() < p) {
           try {
             a.connect(b, trust, visibility);
             b.connect(a, trust, visibility);
           } catch (FSParseException e) {
             Logger.error(RealNodeSecretPingTest.class, "cannot connect!!!!", e);
           } catch (PeerParseException e) {
             Logger.error(RealNodeSecretPingTest.class, "cannot connect #2!!!!", e);
           } catch (freenet.io.comm.ReferenceSignatureVerificationException e) {
             Logger.error(RealNodeSecretPingTest.class, "cannot connect #3!!!!", e);
           }
           break;
         }
       }
     }
   }
 }
Example #28
0
 /**
  * A non-authoritative hint that a specific edition *might* exist. At the moment, we just fetch
  * the block. We do not fetch the contents, and it is possible that USKFetcher's are also fetching
  * the block. FIXME would it be more efficient to pass it along to a USKFetcher?
  *
  * @param context
  * @throws MalformedURLException If the uri passed in is not a USK.
  */
 public void hintUpdate(FreenetURI uri, ClientContext context, short priority)
     throws MalformedURLException {
   if (uri.getSuggestedEdition() < lookupLatestSlot(USK.create(uri))) {
     if (logMINOR)
       Logger.minor(
           this,
           "Ignoring hint because edition is "
               + uri.getSuggestedEdition()
               + " but latest is "
               + lookupLatestSlot(USK.create(uri)));
     return;
   }
   uri = uri.sskForUSK();
   if (logMINOR) Logger.minor(this, "Doing hint fetch for " + uri);
   final ClientGetter get =
       new ClientGetter(
           new NullClientCallback(),
           uri,
           new FetchContext(backgroundFetchContext, FetchContext.IDENTICAL_MASK, false, null),
           priority,
           rcBulk,
           new NullBucket(),
           null,
           null);
   try {
     get.start(null, context);
   } catch (FetchException e) {
     if (logMINOR) Logger.minor(this, "Cannot start hint fetch for " + uri + " : " + e, e);
     // Ignore
   }
 }
  public void run() {
    Logger.debug(this, "Main loop running...");

    try {
      mConnectedToWoT = connectToWoT();

      if (mConnectedToWoT) {
        try {
          fetchOwnIdentities(); // Fetch the own identities first to prevent own-identities from
                                // being imported as normal identity...
          fetchIdentities();
          garbageCollectIdentities();
        } catch (Exception e) {
          Logger.error(this, "Fetching identities failed.", e);
        }
      }
    } finally {
      final long sleepTime =
          mConnectedToWoT
              ? (THREAD_PERIOD / 2 + mRandom.nextInt(THREAD_PERIOD))
              : WOT_RECONNECT_DELAY;
      Logger.debug(this, "Sleeping for " + (sleepTime / (60 * 1000)) + " minutes.");
      mTicker.queueTimedJob(
          this, "Freetalk " + this.getClass().getSimpleName(), sleepTime, false, true);
    }

    Logger.debug(this, "Main loop finished.");
  }
 // Real onFailure
 protected void onFailure(
     FetchException e, Object token, ObjectContainer container, ClientContext context) {
   if (persistent) {
     container.activate(segment, 1);
     container.activate(parent, 1);
     container.activate(segment.errors, 1);
   }
   boolean forceFatal = false;
   if (parent.isCancelled()) {
     if (Logger.shouldLog(Logger.MINOR, this)) Logger.minor(this, "Failing: cancelled");
     e = new FetchException(FetchException.CANCELLED);
     forceFatal = true;
   }
   segment.errors.inc(e.getMode());
   if (e.isFatal() && token == null) {
     segment.fail(e, container, context, false);
   } else if (e.isFatal() || forceFatal) {
     segment.onFatalFailure(e, ((MySendableRequestItem) token).x, this, container, context);
   } else {
     segment.onNonFatalFailure(e, ((MySendableRequestItem) token).x, this, container, context);
   }
   removeBlockNum(((MySendableRequestItem) token).x, container, false);
   if (persistent) {
     container.deactivate(segment, 1);
     container.deactivate(parent, 1);
     container.deactivate(segment.errors, 1);
   }
 }