示例#1
0
 private void send(Connection c, String message, int fromNode, int toNode) {
   int drop = rand.nextInt(100);
   if (drop >= links.getDropRate() || links.getDropRate() == 0) {
     String content = message;
     // TODO Change to only allowed matched responses.
     boolean shouldSend =
         (links.getOffset() == 0)
             || (!links.isCheckwhois())
             || ((content.length() > 12)
                 && (content.substring(0, 12).equals("WHOIS(Query,")
                     || content.subSequence(0, 13).equals("WHOIS(Answer,")));
     if (shouldSend) {
       // Corruption
       int corr = rand.nextInt(100);
       if (corr < links.getCorruptionRate()) {
         content = Texts.corrupt(content);
       }
       // Network delay
       int delay = (links.getDelay() > 0) ? rand.nextInt(links.getDelay()) : 0;
       synchronized (queue) {
         queue.add(new PacketMessage(c, delay, fromNode, toNode, content));
       }
     }
   }
 }
示例#2
0
  // This method can only be executed in the EDT so is safe from Connections
  // updates
  public void updateStatus() {
    // The first update triggers network building.
    if (!started) {
      started = true;
      int maxNets = Math.max(1, connections.size() / MIN_PER_NET);
      int numNets = Math.min((connections.size() + MAX_PER_NET - 1) / MAX_PER_NET, maxNets);
      createCycles(numNets);
      // Begin to check for messages.
      synchronized (queue) {
        this.start();
      }
    }
    int l = connections.size();
    List<String> texts = null;
    if (links.getOffset() != 0 && !links.isCheckwhois()) {
      texts = new ArrayList<String>();
      Texts.choose_messages(texts, l, links.getCorruptionRate() > 0);
    }
    // mark any current messages out of date
    startTime = System.currentTimeMillis();

    synchronized (queue) {
      for (int i = 0; i < l; i++) {
        Connection c = connections.get(i);
        if (c != null) {
          if (links.getOffset() == 0) {
            queue.add(new TaskMessage(c, -1, links));
          } else {
            int recipient = cycles.get(c.getNetwork()).offsetNode(c.getNode(), links.getOffset());
            System.out.println("Node:" + c.getNode() + "sending to: " + recipient);
            if (links.isCheckwhois()) {
              String unknown = connections.get(nodeToIndex(recipient)).getHostname();
              queue.add(new TaskMessage(c, -1, links, unknown));
            } else {
              queue.add(new TaskMessage(c, -1, links, recipient, texts.get(i)));
            }
          }
        }
      }
    }
  }