/**
   * Validate streams generate the same output.
   *
   * @param expIn Expected input stream.
   * @param actIn Actual input stream.
   * @param expSize Expected size of the streams.
   * @param seek Seek to use async position-based reading or {@code null} to use simple continuous
   *     reading.
   * @throws IOException In case of any IO exception.
   */
  private void assertEqualStreams(
      InputStream expIn, GridGgfsInputStream actIn, @Nullable Long expSize, @Nullable Long seek)
      throws IOException {
    if (seek != null) expIn.skip(seek);

    int bufSize = 2345;
    byte buf1[] = new byte[bufSize];
    byte buf2[] = new byte[bufSize];
    long pos = 0;

    long start = System.currentTimeMillis();

    while (true) {
      int read = (int) Math.min(bufSize, expSize - pos);

      int i1;

      if (seek == null) i1 = actIn.read(buf1, 0, read);
      else if (seek % 2 == 0) i1 = actIn.read(pos + seek, buf1, 0, read);
      else {
        i1 = read;

        actIn.readFully(pos + seek, buf1, 0, read);
      }

      // Read at least 0 byte, but don't read more then 'i1' or 'read'.
      int i2 = expIn.read(buf2, 0, Math.max(0, Math.min(i1, read)));

      if (i1 != i2) {
        fail(
            "Expects the same data [read="
                + read
                + ", pos="
                + pos
                + ", seek="
                + seek
                + ", i1="
                + i1
                + ", i2="
                + i2
                + ']');
      }

      if (i1 == -1) break; // EOF

      // i1 == bufSize => compare buffers.
      // i1 <  bufSize => Compare part of buffers, rest of buffers are equal from previous
      // iteration.
      assertTrue(
          "Expects the same data [read="
              + read
              + ", pos="
              + pos
              + ", seek="
              + seek
              + ", i1="
              + i1
              + ", i2="
              + i2
              + ']',
          Arrays.equals(buf1, buf2));

      if (read == 0) break; // Nothing more to read.

      pos += i1;
    }

    if (expSize != null) assertEquals(expSize.longValue(), pos);

    long time = System.currentTimeMillis() - start;

    if (time != 0 && log.isInfoEnabled()) {
      log.info(
          String.format(
              "Streams were compared in continuous reading " + "[size=%7d, rate=%3.1f MB/sec]",
              expSize, expSize * 1000. / time / 1024 / 1024));
    }
  }
  public synchronized void logUnavailableHost(URL url) {
    if (this.offlineMode) return;

    if (url == null) {
      String message = Logging.getMessage("nullValue.URLIsNull");
      Logging.error(message);
      throw new IllegalArgumentException(message);
    }

    String hostName = url.getHost();
    HostInfo hi = this.hostMap.get(hostName);
    if (hi != null) {
      if (!hi.isUnavailable()) {
        hi.logCount.incrementAndGet();
        if (hi.isUnavailable()) // host just became unavailable
        this.firePropertyChange(NetworkStatus.HOST_UNAVAILABLE, null, url);
      }
      hi.lastLogTime.set(System.currentTimeMillis());
    } else {
      hi = new HostInfo(this.attemptLimit.get(), this.tryAgainInterval.get());
      hi.logCount.set(1);
      if (hi.isUnavailable()) // the attempt limit may be as low as 1, so handle that case here
      this.firePropertyChange(NetworkStatus.HOST_UNAVAILABLE, null, url);
      this.hostMap.put(hostName, hi);
    }

    this.lastUnavailableLogTime.set(System.currentTimeMillis());
  }
 private static void nodeCopy(
     double[] srcKeys,
     Object[] srcVals,
     int srcBegin,
     double[] targetKeys,
     Object[] targetVals,
     int targetBegin,
     int copyLen) {
   System.arraycopy(srcKeys, srcBegin, targetKeys, targetBegin, copyLen);
   System.arraycopy(srcVals, srcBegin, targetVals, targetBegin, copyLen);
 }
Esempio n. 4
0
    @Override
    public void onGetDSLAMPortFailure(String LID, Throwable ex) {
      // Update counters:
      {
        invokeAsyncEndCount.incrementAndGet();
        invokeAsyncRunningCount.decrementAndGet();
        invokeAsyncEndFailureCount.incrementAndGet();

        synchronized (invokeEndFrequencyCounter) {
          invokeEndFrequencyCounter.increment();
        }
      }

      { // HACK! HACK! HACK!
        long t1 = System.currentTimeMillis();
        long t = t1 - t0;
        /*
        System.out.println("@@@@@ StatMXManClientListener.onSetAlarmsActiveFailure:> "+
                           "time="+
                           t+
                           " ms");
                           */
      }

      l.onGetDSLAMPortFailure(LID, ex);
    }
Esempio n. 5
0
    @Override
    public void onGetDSLAMPort(DSLAMSource source, String LID) {

      // Update counters:
      {
        invokeAsyncEndCount.incrementAndGet();
        invokeAsyncRunningCount.decrementAndGet();

        synchronized (invokeEndFrequencyCounter) {
          invokeEndFrequencyCounter.increment();
        }
      }

      { // HACK! HACK! HACK!
        long t1 = System.currentTimeMillis();
        long t = t1 - t0;

        // System.out.println("@@@@@ StatMXManClientListener.onSetAlarmsActive:> "+
        //                   "time="+
        //                   t+
        //                   " ms");
        //
      }

      l.onGetLID(source, LID);
    }
  /**
   * Initiates a retrieval of the model referenced by this placemark. Once the resource is retrieved
   * and loaded, this calls <code>{@link #setColladaRoot(ColladaRoot)}</code> to specify this link's
   * new network resource, and sends an <code>
   * {@link gov.nasa.worldwind.avlist.AVKey#RETRIEVAL_STATE_SUCCESSFUL}</code> property change event
   * to this link's property change listeners.
   *
   * <p>This does nothing if this <code>KMLNetworkLink</code> has no <code>KMLLink</code>.
   *
   * @param address the address of the resource to retrieve
   */
  protected void retrieveModel(String address) throws IOException, XMLStreamException {
    Object o = this.parent.getRoot().resolveReference(address);
    if (o == null) return;

    ColladaRoot root = ColladaRoot.createAndParse(o);
    if (root == null) return;

    this.setColladaRoot(root);
    this.resourceRetrievalTime.set(System.currentTimeMillis());
    this.parent.getRoot().requestRedraw();
  }
  static void oneRun(BlockingQueue<Runnable> q, int nThreads, int iters, boolean print)
      throws Exception {

    ThreadPoolExecutor pool =
        new ThreadPoolExecutor(nThreads + 1, Integer.MAX_VALUE, 1L, TimeUnit.SECONDS, q);

    CountDownLatch done = new CountDownLatch(iters);
    remaining.set(nThreads - 1);
    pool.prestartAllCoreThreads();
    Task t = new Task(pool, done);
    long start = System.nanoTime();
    pool.execute(t);
    done.await();
    long time = System.nanoTime() - start;
    if (print) System.out.println("\t: " + LoopHelpers.rightJustify(time / iters) + " ns per task");
    q.clear();
    Thread.sleep(100);
    pool.shutdown();
    Thread.sleep(100);
    pool.shutdownNow();
  }
Esempio n. 8
0
  @Test(timeout = 10000)
  public void testOnBackpressureDrop() {
    long t = System.currentTimeMillis();
    for (int i = 0; i < 100; i++) {
      // stop the test if we are getting close to the timeout because slow machines
      // may not get through 100 iterations
      if (System.currentTimeMillis() - t > TimeUnit.SECONDS.toMillis(9)) {
        break;
      }
      int NUM =
          (int) (Observable.bufferSize() * 1.1); // > 1 so that take doesn't prevent buffer overflow
      AtomicInteger c = new AtomicInteger();
      TestSubscriber<Integer> ts = new TestSubscriber<>();
      firehose(c)
          .onBackpressureDrop()
          .observeOn(Schedulers.computation())
          .map(SLOW_PASS_THRU)
          .take(NUM)
          .subscribe(ts);
      ts.awaitTerminalEvent();
      ts.assertNoErrors();

      List<Integer> onNextEvents = ts.values();
      assertEquals(NUM, onNextEvents.size());

      Integer lastEvent = onNextEvents.get(NUM - 1);

      System.out.println(
          "testOnBackpressureDrop => Received: "
              + onNextEvents.size()
              + "  Emitted: "
              + c.get()
              + " Last value: "
              + lastEvent);
      // it drop, so we should get some number far higher than what would have sequentially
      // incremented
      assertTrue(NUM - 1 <= lastEvent.intValue());
    }
  }
  // TODO: Consider implementing this method using Android's ConnectivityManager
  public synchronized boolean isNetworkUnavailable(long checkInterval) {
    if (this.offlineMode) return true;

    // If there's been success since failure, network assumed to be reachable.
    if (this.lastAvailableLogTime.get() > this.lastUnavailableLogTime.get()) {
      this.lastNetworkUnavailableResult.set(false);
      return this.lastNetworkUnavailableResult.get();
    }

    long now = System.currentTimeMillis();

    // If there's been success recently, network assumed to be reachable.
    if (!this.lastNetworkUnavailableResult.get()
        && now - this.lastAvailableLogTime.get() < checkInterval) {
      return this.lastNetworkUnavailableResult.get();
    }

    // If query comes too soon after an earlier one that addressed the network, return the earlier
    // result.
    if (now - this.lastNetworkCheckTime.get() < checkInterval) {
      return this.lastNetworkUnavailableResult.get();
    }

    this.lastNetworkCheckTime.set(now);

    if (!this.isWorldWindServerUnavailable()) {
      this.lastNetworkUnavailableResult.set(false); // network not unreachable
      return this.lastNetworkUnavailableResult.get();
    }

    for (String testHost : networkTestSites) {
      if (isHostReachable(testHost)) {
        {
          this.lastNetworkUnavailableResult.set(false); // network not unreachable
          return this.lastNetworkUnavailableResult.get();
        }
      }
    }

    if (now - this.lastNetworkStatusReportTime.get() > NETWORK_STATUS_REPORT_INTERVAL) {
      this.lastNetworkStatusReportTime.set(now);
      String message = Logging.getMessage("NetworkStatus.NetworkUnreachable");
      Logging.info(message);
    }

    this.lastNetworkUnavailableResult.set(
        true); // if no successful contact then network is unreachable
    return this.lastNetworkUnavailableResult.get();
  }
  @BeforeClass
  public void beforeClass() throws IOException {

    OGlobalConfiguration.FILE_LOCK.setValue(Boolean.FALSE);

    String buildDirectory = System.getProperty("buildDirectory");
    if (buildDirectory == null) buildDirectory = ".";

    storageLocal =
        (OLocalPaginatedStorage)
            Orient.instance()
                .loadStorage("plocal:" + buildDirectory + "/ReadWriteCacheConcurrentTest");
    storageLocal.create(null);

    prepareFilesForTest(FILE_COUNT);
  }
Esempio n. 11
0
  /**
   * Concat arrays in one.
   *
   * @param arrays Arrays.
   * @return Summary array.
   */
  public static int[] concat(int[]... arrays) {
    assert arrays != null;
    assert arrays.length > 1;

    int len = 0;

    for (int[] a : arrays) len += a.length;

    int[] r = Arrays.copyOf(arrays[0], len);

    for (int i = 1, shift = 0; i < arrays.length; i++) {
      shift += arrays[i - 1].length;
      System.arraycopy(arrays[i], 0, r, shift, arrays[i].length);
    }

    return r;
  }
  public synchronized void logAvailableHost(URL url) {
    if (this.offlineMode) return;

    if (url == null) {
      String message = Logging.getMessage("nullValue.URLIsNull");
      Logging.error(message);
      throw new IllegalArgumentException(message);
    }

    String hostName = url.getHost();
    HostInfo hi = this.hostMap.get(hostName);
    if (hi != null) {
      this.hostMap.remove(hostName); // host is available again
      firePropertyChange(NetworkStatus.HOST_AVAILABLE, null, url);
    }

    this.lastAvailableLogTime.set(System.currentTimeMillis());
  }
Esempio n. 13
0
  private File create(String bsn, Version v) throws Exception {
    String name = bsn + "-" + v;
    Builder b = new Builder();
    b.setBundleSymbolicName(bsn);
    b.setBundleVersion(v);
    b.setProperty("Random", random++ + "");
    b.setProperty("-resourceonly", true + "");
    b.setIncludeResource("foo;literal='foo'");
    Jar jar = b.build();
    assertTrue(b.check());

    File file = IO.getFile(tmp, name + ".jar");
    file.getParentFile().mkdirs();
    jar.updateModified(System.currentTimeMillis(), "Force it to now");
    jar.write(file);
    b.close();
    return file;
  }
  /**
   * Determines and stores the network sites to test for public network connectivity. The sites are
   * drawn from the JVM's gov.nasa.worldwind.avkey.NetworkStatusTestSites property ({@link
   * AVKey#NETWORK_STATUS_TEST_SITES}). If that property is not defined, the sites are drawn from
   * the same property in the World Wind or application configuration file. If the sites are not
   * specified there, the set of sites specified in {@link #DEFAULT_NETWORK_TEST_SITES} are used. To
   * indicate an empty list in the JVM property or configuration file property, specify an empty
   * site list, "".
   */
  protected void establishNetworkTestSites() {
    String testSites = System.getProperty(AVKey.NETWORK_STATUS_TEST_SITES);

    if (testSites == null)
      testSites = Configuration.getStringValue(AVKey.NETWORK_STATUS_TEST_SITES);

    if (testSites == null) {
      this.networkTestSites.addAll(Arrays.asList(DEFAULT_NETWORK_TEST_SITES));
    } else {
      String[] sites = testSites.split(",");
      List<String> actualSites = new ArrayList<String>(sites.length);

      for (String site : sites) {
        site = WWUtil.removeWhiteSpace(site);
        if (!WWUtil.isEmpty(site)) actualSites.add(site);
      }

      this.setNetworkTestSites(actualSites);
    }
  }
Esempio n. 15
0
 @Override
 protected FilterNode any(final FilterNode node0, boolean signor) {
   Objects.requireNonNull(node0);
   if (!(node0 instanceof FilterJoinNode)) {
     throw new IllegalArgumentException(
         this
             + (signor ? " or " : " and ")
             + " a node but "
             + String.valueOf(node0)
             + "is not a "
             + FilterJoinNode.class.getSimpleName());
   }
   final FilterJoinNode node = (FilterJoinNode) node0;
   if (this.nodes == null) {
     this.nodes = new FilterNode[] {node};
     this.or = signor;
     return this;
   }
   if (or == signor || this.column == null) {
     FilterNode[] newsiblings = new FilterNode[nodes.length + 1];
     System.arraycopy(nodes, 0, newsiblings, 0, nodes.length);
     newsiblings[nodes.length] = node;
     this.nodes = newsiblings;
     if (this.column == null) this.or = signor;
     return this;
   }
   this.nodes = new FilterNode[] {new FilterJoinNode(node), node};
   this.column = null;
   this.express = null;
   this.value = null;
   this.joinClass = null;
   this.joinEntity = null;
   this.joinColumns = null;
   this.or = signor;
   return this;
 }
  static void parTest() {
    final int N = 100000;
    final ConcurrentDoubleOrderedListMap<String> map = new ConcurrentDoubleOrderedListMap<String>();
    final double[] keys = new double[N];

    final Random r = new Random();
    for (int i = 0; i < N; i++) {
      int x = r.nextInt();
      keys[i] = x;
      map.put(x, "" + x);
    }
    map.doSanityCheck();
    System.out.println("Start parallel...");

    long s = System.currentTimeMillis();
    for (int i = 0; i < N; i++) {
      map.get(keys[i]);
    }
    long e = System.currentTimeMillis();
    System.out.println("Exec time:" + (e - s) + "ms");

    final int nReader = 8;
    final int nReplacer = 4;
    final int nWriter = 4;
    final int nThread = nReader + nReplacer + nWriter;
    for (int i = 0; i < 5; i++) {
      ParUtil.parallel(
          nThread,
          new ParUtil.CallableBlock() {
            @Override
            public void call(int index) {
              if (index < nReader) {
                for (int j = 0; j < 35; j++) {
                  for (int i = index; i < N; i += nReader) {
                    String val = map.get(keys[i]);
                    assert val != null && val.equals("" + keys[i])
                        : "key:" + keys[i] + ", val:" + val;
                  }
                }
                // System.out.println("Finished reader thread:" + index);
              } else if (index < nReader + nReplacer) {
                for (int j = 0; j < 5; j++) {
                  for (int i = index; i < N; i += nReplacer) {
                    boolean replaced = map.replace(keys[i], "" + keys[i], "" + keys[i]);
                    assert replaced;
                  }
                }
                // System.out.println("Finished writer(replace) thread:" + index);
              } else {
                for (int i = 0; i < 2 * N; i++) {
                  int x = r.nextInt();
                  map.putAtomic(x, "" + x);
                }
                // System.out.println("Finished writer thread:" + index);
              }
            }
          });
      System.out.println("Finished all parallel... i:" + i);
      map.doSanityCheck();
    }
    /*
    System.out.println("Node.pool size:"+Node.pool.size());
    while(true) {
        Node n = Node.pool.poll();
        if (n==null) break;
        System.out.println("n.refcnt:"+n.refcnt);
    }
    System.out.println("fill ratio:"+map._fillRatio());
    */
  }
Esempio n. 17
0
  public void run() {

    long start, end;

    // Start timer
    start = System.currentTimeMillis();

    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);
    myint.set(50);

    end = System.currentTimeMillis();

    System.out.println(
        "W contention on atomic "
            + (end - start)
            + " ms BID: "
            + VM.getIsland()
            + " CID: "
            + VM.getCore());
  }
Esempio n. 18
0
  private DarkBotMCSpambot(
      DarkBot darkBot,
      String server,
      String username,
      String password,
      String sessionId,
      String loginProxy,
      String proxy,
      String owner) {
    synchronized (bots) {
      bots.add(this);
      // slotsTaken.incrementAndGet();
      synchronized (slotsTaken) {
        slotsTaken.notifyAll();
      }
    }
    MinecraftBotData.Builder builder = MinecraftBotData.builder();
    // botData.nickname = "";
    // for(int i = 0; i < 10; i++)
    // botData.nickname += alphas[random.nextInt(alphas.length)];
    if (proxy != null && !proxy.isEmpty()) {
      int port = 80;
      ProxyType type = ProxyType.SOCKS;
      if (proxy.contains(":")) {
        String[] parts = proxy.split(":");
        proxy = parts[0];
        port = Integer.parseInt(parts[1]);
        if (parts.length > 2) type = ProxyType.values()[Integer.parseInt(parts[2]) - 1];
      }
      builder.withSocksProxy(new ProxyData(proxy, port, type));
      this.proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(proxy, port));
    }
    if (loginProxy != null && !loginProxy.isEmpty()) {
      int port = 80;
      if (loginProxy.contains(":")) {
        String[] parts = loginProxy.split(":");
        loginProxy = parts[0];
        port = Integer.parseInt(parts[1]);
      }
      builder.withHttpProxy(new ProxyData(loginProxy, port, ProxyType.HTTP));
      this.loginProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(loginProxy, port));
    }
    builder.withUsername(username);
    if (sessionId != null) builder.withSessionId(sessionId);
    else builder.withPassword(password);
    if (server != null && !server.isEmpty()) {
      int port = 25565;
      if (server.contains(":")) {
        String[] parts = server.split(":");
        server = parts[0];
        port = Integer.parseInt(parts[1]);
      }
      builder.withServer(server).withPort(port);
    } else throw new IllegalArgumentException("Unknown server!");

    this.owner = owner;
    MinecraftBotData botData = builder.build();
    System.setProperty("socksProxyHost", "");
    System.setProperty("socksProxyPort", "");
    System.out.println("[" + username + "] Connecting...");
    bot = new MinecraftBot(darkBot, botData);
    bot.setMovementDisabled(true);
    connectionHandler = bot.getConnectionHandler();
    Session session = bot.getSession();
    // System.gc();
    System.out.println("[" + username + "] Done! (" + amountJoined.incrementAndGet() + ")");
    bot.getEventManager().registerListener(this);
    bot.getGameHandler().registerListener(this);

    long lastShoutTime = System.currentTimeMillis();
    while (bot.isConnected()) {
      if (die) {
        connectionHandler.sendPacket(new Packet255KickDisconnect("Goodbye"));
        return;
      }
      try {
        Thread.sleep(3000 + random.nextInt(1000));
      } catch (InterruptedException exception) {
        exception.printStackTrace();
      }
      if (!bot.hasSpawned()) continue;
      connectionHandler.sendPacket(new Packet0KeepAlive(random.nextInt()));
      if (spamMessage == null || !canSpam) continue;
      String message = spamMessage;
      if (message.contains("%skill")) message = message.replace("%skill", skills[nextSkill++]);
      if (nextSkill >= skills.length) nextSkill = 0;
      if (message.contains("%bot")) {
        synchronized (bots) {
          message =
              message.replace(
                  "%bot",
                  bots.get(nextBot > bots.size() ? (nextBot = 0) * 0 : nextBot++)
                      .bot
                      .getSession()
                      .getUsername());
        }
      }
      if (message.contains("%spamlist"))
        message = message.replace("%spamlist", spamList[nextSpamList++]);
      if (nextSpamList >= spamList.length) nextSpamList = 0;
      if (message.contains("%rnd")) {
        int length = 1;
        int index = message.indexOf("%rnd") + "%rnd".length();
        int lastIndex;
        for (lastIndex = index; lastIndex < message.length(); lastIndex++)
          if (Character.isDigit(message.charAt(lastIndex))) lastIndex++;
          else break;
        if (lastIndex > message.length()) lastIndex--;
        try {
          System.out.println(index + "," + lastIndex + "," + message.length());
          length = Integer.parseInt(message.substring(index, lastIndex));
        } catch (Exception exception) {
        }

        String randomChars = "";
        for (int i = 0; i < length; i++) randomChars += alphas[random.nextInt(alphas.length)];
        message = message.replace("%rnd", randomChars);
      }
      if (message.contains("%msg"))
        message = "/msg " + msgChars[nextMsgChar++] + " " + message.replace("%msg", "");
      if (message.contains("%ernd")) {
        message = message.replace("%ernd", "");
        int extraMessageLength = 15 + random.nextInt(6);
        message = message.substring(0, Math.min(100 - extraMessageLength, message.length())) + " [";
        extraMessageLength -= 3;
        for (int i = 0; i < extraMessageLength; i++)
          message += alphas[random.nextInt(alphas.length)];
        message += "]";
      } else message = message.substring(0, Math.min(100, message.length()));
      connectionHandler.sendPacket(new Packet3Chat(message));
    }
    synchronized (bots) {
      bots.remove(this);
    }
    amountJoined.decrementAndGet();
    slotsTaken.decrementAndGet();
    synchronized (slotsTaken) {
      slotsTaken.notifyAll();
    }
  }
Esempio n. 19
0
    /**
     * @param time Test time.
     * @param writers Number of writers threads.
     * @throws Exception If failed.
     */
    public void testQueue(long time, int writers) throws Exception {
      System.out.println("Start test [writers=" + writers + ", time=" + time + "]");

      Thread rdr =
          new Thread() {
            @SuppressWarnings({"InfiniteLoopStatement", "unchecked"})
            @Override
            public void run() {
              try {
                while (true) {
                  Future fut;

                  while ((fut = queue.poll()) != null) {
                    qSize.decrementAndGet();

                    fut.onDone(true);
                  }

                  long size = qSize.get();

                  if (size == 0) {
                    synchronized (mux) {
                      while (queue.isEmpty()) {
                        mux.wait();
                      }
                    }
                  }
                }
              } catch (InterruptedException ignore) {
              }
            }
          };

      rdr.start();

      List<Thread> putThreads = new ArrayList<>();

      for (int i = 0; i < writers; i++) {
        putThreads.add(
            new Thread() {
              @SuppressWarnings("CallToNotifyInsteadOfNotifyAll")
              @Override
              public void run() {
                try {
                  while (!stop) {
                    long id = cnt.incrementAndGet();

                    Future<Boolean> fut = new Future<Boolean>(new Message(id));

                    queue.offer(fut);

                    long size = qSize.incrementAndGet();

                    if (size == 1) {
                      synchronized (mux) {
                        mux.notify();
                      }
                    }

                    Boolean res = fut.get();

                    if (!res) {
                      System.out.println("Error");
                    }
                  }
                } catch (Exception e) {
                  e.printStackTrace();
                }
              }
            });
      }

      for (Thread t : putThreads) t.start();

      Thread.sleep(time);

      stop = true;

      for (Thread t : putThreads) t.join();

      rdr.interrupt();

      rdr.join();

      System.out.println("Total: " + cnt.get());

      System.gc();
      System.gc();
      System.gc();
    }
  /**
   * @param cacheMode Cache mode.
   * @param sameAff If {@code false} uses different number of partitions for caches.
   * @param concurrency Transaction concurrency.
   * @param isolation Transaction isolation.
   * @throws Exception If failed.
   */
  private void crossCacheTxFailover(
      CacheMode cacheMode,
      boolean sameAff,
      final TransactionConcurrency concurrency,
      final TransactionIsolation isolation)
      throws Exception {
    IgniteKernal ignite0 = (IgniteKernal) ignite(0);

    final AtomicBoolean stop = new AtomicBoolean();

    try {
      ignite0.createCache(cacheConfiguration(CACHE1, cacheMode, 256));
      ignite0.createCache(cacheConfiguration(CACHE2, cacheMode, sameAff ? 256 : 128));

      final AtomicInteger threadIdx = new AtomicInteger();

      IgniteInternalFuture<?> fut =
          GridTestUtils.runMultiThreadedAsync(
              new Callable<Void>() {
                @Override
                public Void call() throws Exception {
                  int idx = threadIdx.getAndIncrement();

                  Ignite ignite = ignite(idx % GRID_CNT);

                  log.info(
                      "Started update thread [node="
                          + ignite.name()
                          + ", client="
                          + ignite.configuration().isClientMode()
                          + ']');

                  IgniteCache<TestKey, TestValue> cache1 = ignite.cache(CACHE1);
                  IgniteCache<TestKey, TestValue> cache2 = ignite.cache(CACHE2);

                  assertNotSame(cache1, cache2);

                  IgniteTransactions txs = ignite.transactions();

                  ThreadLocalRandom rnd = ThreadLocalRandom.current();

                  long iter = 0;

                  while (!stop.get()) {
                    boolean sameKey = rnd.nextBoolean();

                    try {
                      try (Transaction tx = txs.txStart(concurrency, isolation)) {
                        if (sameKey) {
                          TestKey key = new TestKey(rnd.nextLong(KEY_RANGE));

                          cacheOperation(rnd, cache1, key);
                          cacheOperation(rnd, cache2, key);
                        } else {
                          TestKey key1 = new TestKey(rnd.nextLong(KEY_RANGE));
                          TestKey key2 = new TestKey(key1.key() + 1);

                          cacheOperation(rnd, cache1, key1);
                          cacheOperation(rnd, cache2, key2);
                        }

                        tx.commit();
                      }
                    } catch (CacheException | IgniteException e) {
                      log.info("Update error: " + e);
                    }

                    if (iter++ % 500 == 0) log.info("Iteration: " + iter);
                  }

                  return null;
                }

                /**
                 * @param rnd Random.
                 * @param cache Cache.
                 * @param key Key.
                 */
                private void cacheOperation(
                    ThreadLocalRandom rnd, IgniteCache<TestKey, TestValue> cache, TestKey key) {
                  switch (rnd.nextInt(4)) {
                    case 0:
                      cache.put(key, new TestValue(rnd.nextLong()));

                      break;

                    case 1:
                      cache.remove(key);

                      break;

                    case 2:
                      cache.invoke(key, new TestEntryProcessor(rnd.nextBoolean() ? 1L : null));

                      break;

                    case 3:
                      cache.get(key);

                      break;

                    default:
                      assert false;
                  }
                }
              },
              10,
              "tx-thread");

      long stopTime = System.currentTimeMillis() + 3 * 60_000;

      long topVer = ignite0.cluster().topologyVersion();

      boolean failed = false;

      while (System.currentTimeMillis() < stopTime) {
        log.info("Start node.");

        IgniteKernal ignite = (IgniteKernal) startGrid(GRID_CNT);

        assertFalse(ignite.configuration().isClientMode());

        topVer++;

        IgniteInternalFuture<?> affFut =
            ignite
                .context()
                .cache()
                .context()
                .exchange()
                .affinityReadyFuture(new AffinityTopologyVersion(topVer));

        try {
          if (affFut != null) affFut.get(30_000);
        } catch (IgniteFutureTimeoutCheckedException e) {
          log.error("Failed to wait for affinity future after start: " + topVer);

          failed = true;

          break;
        }

        Thread.sleep(500);

        log.info("Stop node.");

        stopGrid(GRID_CNT);

        topVer++;

        affFut =
            ignite0
                .context()
                .cache()
                .context()
                .exchange()
                .affinityReadyFuture(new AffinityTopologyVersion(topVer));

        try {
          if (affFut != null) affFut.get(30_000);
        } catch (IgniteFutureTimeoutCheckedException e) {
          log.error("Failed to wait for affinity future after stop: " + topVer);

          failed = true;

          break;
        }
      }

      stop.set(true);

      fut.get();

      assertFalse("Test failed, see log for details.", failed);
    } finally {
      stop.set(true);

      ignite0.destroyCache(CACHE1);
      ignite0.destroyCache(CACHE2);

      awaitPartitionMapExchange();
    }
  }
 protected boolean isTimeToTryAgain() {
   return System.currentTimeMillis() - this.lastLogTime.get() >= this.tryAgainInterval;
 }
 protected HostInfo(int attemptLimit, long tryAgainInterval) {
   this.lastLogTime.set(System.currentTimeMillis());
   this.logCount.set(1);
   this.tryAgainInterval = tryAgainInterval;
   this.attemptLimit = attemptLimit;
 }
// TODO: Android provides a way to test for network connectivity. We could use this to implement
// isNetworkUnavailable.
public class BasicNetworkStatus extends AVListImpl implements NetworkStatus {
  protected static final long DEFAULT_TRY_AGAIN_INTERVAL = (long) 60e3; // seconds
  protected static final long NETWORK_STATUS_REPORT_INTERVAL = (long) 120e3;
  protected static final int DEFAULT_ATTEMPT_LIMIT =
      7; // number of unavailable events to declare host unavailable
  protected static final String[] DEFAULT_NETWORK_TEST_SITES =
      new String[] {
        "www.nasa.gov", "worldwind.arc.nasa.gov", "google.com", "microsoft.com", "yahoo.com"
      };

  protected static class HostInfo {
    protected final long tryAgainInterval;
    protected final int attemptLimit;
    protected AtomicInteger logCount = new AtomicInteger();
    protected AtomicLong lastLogTime = new AtomicLong();

    protected HostInfo(int attemptLimit, long tryAgainInterval) {
      this.lastLogTime.set(System.currentTimeMillis());
      this.logCount.set(1);
      this.tryAgainInterval = tryAgainInterval;
      this.attemptLimit = attemptLimit;
    }

    protected boolean isUnavailable() {
      return this.logCount.get() >= this.attemptLimit;
    }

    protected boolean isTimeToTryAgain() {
      return System.currentTimeMillis() - this.lastLogTime.get() >= this.tryAgainInterval;
    }
  }

  // Values exposed to the application.
  // Values exposed to the application.
  protected CopyOnWriteArrayList<String> networkTestSites = new CopyOnWriteArrayList<String>();
  protected AtomicLong tryAgainInterval = new AtomicLong(DEFAULT_TRY_AGAIN_INTERVAL);
  protected AtomicInteger attemptLimit = new AtomicInteger(DEFAULT_ATTEMPT_LIMIT);
  protected boolean offlineMode;

  // Fields for determining and remembering overall network status.
  protected ConcurrentHashMap<String, HostInfo> hostMap = new ConcurrentHashMap<String, HostInfo>();
  protected AtomicLong lastUnavailableLogTime = new AtomicLong(System.currentTimeMillis());
  protected AtomicLong lastAvailableLogTime = new AtomicLong(System.currentTimeMillis() + 1);
  protected AtomicLong lastNetworkCheckTime = new AtomicLong(System.currentTimeMillis());
  protected AtomicLong lastNetworkStatusReportTime = new AtomicLong(0);
  protected AtomicBoolean lastNetworkUnavailableResult = new AtomicBoolean(false);

  public BasicNetworkStatus() {
    String oms = Configuration.getStringValue(AVKey.OFFLINE_MODE, "false");
    this.offlineMode = oms.startsWith("t") || oms.startsWith("T");

    this.establishNetworkTestSites();
  }

  /**
   * Determines and stores the network sites to test for public network connectivity. The sites are
   * drawn from the JVM's gov.nasa.worldwind.avkey.NetworkStatusTestSites property ({@link
   * AVKey#NETWORK_STATUS_TEST_SITES}). If that property is not defined, the sites are drawn from
   * the same property in the World Wind or application configuration file. If the sites are not
   * specified there, the set of sites specified in {@link #DEFAULT_NETWORK_TEST_SITES} are used. To
   * indicate an empty list in the JVM property or configuration file property, specify an empty
   * site list, "".
   */
  protected void establishNetworkTestSites() {
    String testSites = System.getProperty(AVKey.NETWORK_STATUS_TEST_SITES);

    if (testSites == null)
      testSites = Configuration.getStringValue(AVKey.NETWORK_STATUS_TEST_SITES);

    if (testSites == null) {
      this.networkTestSites.addAll(Arrays.asList(DEFAULT_NETWORK_TEST_SITES));
    } else {
      String[] sites = testSites.split(",");
      List<String> actualSites = new ArrayList<String>(sites.length);

      for (String site : sites) {
        site = WWUtil.removeWhiteSpace(site);
        if (!WWUtil.isEmpty(site)) actualSites.add(site);
      }

      this.setNetworkTestSites(actualSites);
    }
  }

  public int getAttemptLimit() {
    return this.attemptLimit.get();
  }

  public long getTryAgainInterval() {
    return this.tryAgainInterval.get();
  }

  public boolean isOfflineMode() {
    return this.offlineMode;
  }

  public void setOfflineMode(boolean offlineMode) {
    this.offlineMode = offlineMode;
  }

  public void setAttemptLimit(int limit) {
    if (limit < 1) {
      String message = Logging.getMessage("NetworkStatus.InvalidAttemptLimit");
      Logging.error(message);
      throw new IllegalArgumentException(message);
    }

    this.attemptLimit.set(limit);
  }

  public void setTryAgainInterval(long interval) {
    if (interval < 0) {
      String message = Logging.getMessage("NetworkStatus.InvalidTryAgainInterval");
      Logging.error(message);
      throw new IllegalArgumentException(message);
    }

    this.tryAgainInterval.set(interval);
  }

  public List<String> getNetworkTestSites() {
    return new ArrayList<String>(networkTestSites);
  }

  public void setNetworkTestSites(List<String> networkTestSites) {
    this.networkTestSites.clear();

    if (networkTestSites != null) this.networkTestSites.addAll(networkTestSites);
  }

  public synchronized void logUnavailableHost(URL url) {
    if (this.offlineMode) return;

    if (url == null) {
      String message = Logging.getMessage("nullValue.URLIsNull");
      Logging.error(message);
      throw new IllegalArgumentException(message);
    }

    String hostName = url.getHost();
    HostInfo hi = this.hostMap.get(hostName);
    if (hi != null) {
      if (!hi.isUnavailable()) {
        hi.logCount.incrementAndGet();
        if (hi.isUnavailable()) // host just became unavailable
        this.firePropertyChange(NetworkStatus.HOST_UNAVAILABLE, null, url);
      }
      hi.lastLogTime.set(System.currentTimeMillis());
    } else {
      hi = new HostInfo(this.attemptLimit.get(), this.tryAgainInterval.get());
      hi.logCount.set(1);
      if (hi.isUnavailable()) // the attempt limit may be as low as 1, so handle that case here
      this.firePropertyChange(NetworkStatus.HOST_UNAVAILABLE, null, url);
      this.hostMap.put(hostName, hi);
    }

    this.lastUnavailableLogTime.set(System.currentTimeMillis());
  }

  public synchronized void logAvailableHost(URL url) {
    if (this.offlineMode) return;

    if (url == null) {
      String message = Logging.getMessage("nullValue.URLIsNull");
      Logging.error(message);
      throw new IllegalArgumentException(message);
    }

    String hostName = url.getHost();
    HostInfo hi = this.hostMap.get(hostName);
    if (hi != null) {
      this.hostMap.remove(hostName); // host is available again
      firePropertyChange(NetworkStatus.HOST_AVAILABLE, null, url);
    }

    this.lastAvailableLogTime.set(System.currentTimeMillis());
  }

  public boolean isHostUnavailable(URL url) {
    if (this.offlineMode) return true;

    if (url == null) {
      String message = Logging.getMessage("nullValue.URLIsNull");
      Logging.error(message);
      throw new IllegalArgumentException(message);
    }

    String hostName = url.getHost();
    HostInfo hi = this.hostMap.get(hostName);
    if (hi == null) return false;

    if (hi.isTimeToTryAgain()) {
      hi.logCount.set(0); // info removed from table in logAvailableHost
      return false;
    }

    return hi.isUnavailable();
  }

  public boolean isNetworkUnavailable() {
    return this.offlineMode || this.isNetworkUnavailable(10000L);
  }

  // TODO: Consider implementing this method using Android's ConnectivityManager
  public synchronized boolean isNetworkUnavailable(long checkInterval) {
    if (this.offlineMode) return true;

    // If there's been success since failure, network assumed to be reachable.
    if (this.lastAvailableLogTime.get() > this.lastUnavailableLogTime.get()) {
      this.lastNetworkUnavailableResult.set(false);
      return this.lastNetworkUnavailableResult.get();
    }

    long now = System.currentTimeMillis();

    // If there's been success recently, network assumed to be reachable.
    if (!this.lastNetworkUnavailableResult.get()
        && now - this.lastAvailableLogTime.get() < checkInterval) {
      return this.lastNetworkUnavailableResult.get();
    }

    // If query comes too soon after an earlier one that addressed the network, return the earlier
    // result.
    if (now - this.lastNetworkCheckTime.get() < checkInterval) {
      return this.lastNetworkUnavailableResult.get();
    }

    this.lastNetworkCheckTime.set(now);

    if (!this.isWorldWindServerUnavailable()) {
      this.lastNetworkUnavailableResult.set(false); // network not unreachable
      return this.lastNetworkUnavailableResult.get();
    }

    for (String testHost : networkTestSites) {
      if (isHostReachable(testHost)) {
        {
          this.lastNetworkUnavailableResult.set(false); // network not unreachable
          return this.lastNetworkUnavailableResult.get();
        }
      }
    }

    if (now - this.lastNetworkStatusReportTime.get() > NETWORK_STATUS_REPORT_INTERVAL) {
      this.lastNetworkStatusReportTime.set(now);
      String message = Logging.getMessage("NetworkStatus.NetworkUnreachable");
      Logging.info(message);
    }

    this.lastNetworkUnavailableResult.set(
        true); // if no successful contact then network is unreachable
    return this.lastNetworkUnavailableResult.get();
  }

  public boolean isWorldWindServerUnavailable() {
    return this.offlineMode || !isHostReachable("worldwind.arc.nasa.gov");
  }

  /**
   * Determine if a host is reachable by attempting to resolve the host name, and then attempting to
   * open a connection.
   *
   * @param hostName Name of the host to connect to.
   * @return {@code true} if a the host is reachable, {@code false} if the host name cannot be
   *     resolved, or if opening a connection to the host fails.
   */
  protected static boolean isHostReachable(String hostName) {
    try {
      // Assume host is unreachable if we can't get its dns entry without getting an exception
      //noinspection ResultOfMethodCallIgnored
      InetAddress.getByName(hostName);
    } catch (UnknownHostException e) {
      String message = Logging.getMessage("NetworkStatus.UnreachableTestHost", hostName);
      Logging.verbose(message);
      return false;
    } catch (Exception e) {
      String message = Logging.getMessage("NetworkStatus.ExceptionTestingHost", hostName);
      Logging.verbose(message);
      return false;
    }

    // Was able to get internet address, but host still might not be reachable because the address
    // might have been
    // cached earlier when it was available. So need to try something else.

    URLConnection connection = null;
    try {
      URL url = new URL("http://" + hostName);
      Proxy proxy = WWIO.configureProxy();
      if (proxy != null) connection = url.openConnection(proxy);
      else connection = url.openConnection();

      connection.setConnectTimeout(2000);
      connection.setReadTimeout(2000);
      String ct = connection.getContentType();
      if (ct != null) return true;
    } catch (IOException e) {
      String message = Logging.getMessage("NetworkStatus.ExceptionTestingHost", hostName);
      Logging.info(message);
    } finally {
      if (connection instanceof HttpURLConnection) ((HttpURLConnection) connection).disconnect();
    }

    return false;
  }
}
  // For each Tweet that is read, check threshold of the stock and apply
  @Override
  public void execute(Tuple input) {
    String userId = input.getString(0);
    String displayname = input.getString(1);
    String hashtag_all = input.getString(2);
    String tweet = input.getString(3);
    String created = input.getString(4);
    String longitude = input.getString(5);
    String latitude = input.getString(6);
    String language = input.getString(7);
    String fullTweet = input.getString(8);

    if (hashtag_all.length() == 0) {
      System.out.println("Skipping tweet...unable to find hashtag from it:" + tweet);
      collector.ack(input);
      return;
    }

    String hashtags[] = hashtag_all.split(" ");

    for (String hashtag : hashtags) {

      System.out.println(
          "RuleBolt received event displayname: "
              + displayname
              + " hashtag: "
              + hashtag
              + " tweet: "
              + tweet);
      // double latitude = input.getDoubleByField("latitude");
      // long correlationId = input.getLongByField("correlationId");
      // int truckId = input.getIntegerByField("truckId");

      // save event to our Map of events and retrive how many times its been mentioned in tweets
      // tweeted
      twitterEvents.putIfAbsent(hashtag, new AtomicInteger(0));
      int numTimesStockTweeted = twitterEvents.get(hashtag).incrementAndGet();

      // query HBase table for threshold for the stock symbol that was tweeted about
      int threshold = findThresholdForStock(hashtag);
      // int threshold = DEFAULT_ALERT_THRESHOLD;

      System.out.println(
          "\n\n\n\n\n\n\nStock: "
              + hashtag
              + " now has count: "
              + numTimesStockTweeted
              + ", threshold = "
              + threshold
              + " structure: "
              + twitterEvents
              + "\n\n\n\n\n\n\n");

      // check if this event takes the tweet volume for this stock above threshold
      if (numTimesStockTweeted > threshold) {
        int unixTime = (int) (System.currentTimeMillis() / 1000L);
        String timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        String upsert =
            String.format(
                "upsert into alerts values(%d, '%s', '%s', %d)",
                unixTime, hashtag, timeStamp, numTimesStockTweeted);
        System.out.println(
            "ALERT!!! Stock: "
                + hashtag
                + " exceeded limit: "
                + threshold
                + " as it has count: "
                + numTimesStockTweeted
                + " on: "
                + timeStamp);

        runHbaseUpsert(upsert);

        createSolrAlert(userId, created, hashtag);
      }
    }

    collector.ack(input);
  }
Esempio n. 25
0
  /**
   * Grabs local events and detects if events was lost since last poll.
   *
   * @param ignite Target grid.
   * @param evtOrderKey Unique key to take last order key from node local map.
   * @param evtThrottleCntrKey Unique key to take throttle count from node local map.
   * @param evtTypes Event types to collect.
   * @param evtMapper Closure to map grid events to Visor data transfer objects.
   * @return Collections of node events
   */
  public static Collection<VisorGridEvent> collectEvents(
      Ignite ignite,
      String evtOrderKey,
      String evtThrottleCntrKey,
      final int[] evtTypes,
      IgniteClosure<Event, VisorGridEvent> evtMapper) {
    assert ignite != null;
    assert evtTypes != null && evtTypes.length > 0;

    ConcurrentMap<String, Long> nl = ignite.cluster().nodeLocalMap();

    final long lastOrder = getOrElse(nl, evtOrderKey, -1L);
    final long throttle = getOrElse(nl, evtThrottleCntrKey, 0L);

    // When we first time arrive onto a node to get its local events,
    // we'll grab only last those events that not older than given period to make sure we are
    // not grabbing GBs of data accidentally.
    final long notOlderThan = System.currentTimeMillis() - EVENTS_COLLECT_TIME_WINDOW;

    // Flag for detecting gaps between events.
    final AtomicBoolean lastFound = new AtomicBoolean(lastOrder < 0);

    IgnitePredicate<Event> p =
        new IgnitePredicate<Event>() {
          /** */
          private static final long serialVersionUID = 0L;

          @Override
          public boolean apply(Event e) {
            // Detects that events were lost.
            if (!lastFound.get() && (lastOrder == e.localOrder())) lastFound.set(true);

            // Retains events by lastOrder, period and type.
            return e.localOrder() > lastOrder
                && e.timestamp() > notOlderThan
                && F.contains(evtTypes, e.type());
          }
        };

    Collection<Event> evts = ignite.events().localQuery(p);

    // Update latest order in node local, if not empty.
    if (!evts.isEmpty()) {
      Event maxEvt = Collections.max(evts, EVTS_ORDER_COMPARATOR);

      nl.put(evtOrderKey, maxEvt.localOrder());
    }

    // Update throttle counter.
    if (!lastFound.get())
      nl.put(evtThrottleCntrKey, throttle == 0 ? EVENTS_LOST_THROTTLE : throttle - 1);

    boolean lost = !lastFound.get() && throttle == 0;

    Collection<VisorGridEvent> res = new ArrayList<>(evts.size() + (lost ? 1 : 0));

    if (lost) res.add(new VisorGridEventsLost(ignite.cluster().localNode().id()));

    for (Event e : evts) {
      VisorGridEvent visorEvt = evtMapper.apply(e);

      if (visorEvt != null) res.add(visorEvt);
    }

    return res;
  }
Esempio n. 26
0
    public StatADManClientListener(ADManClientListener l) {
      super();

      this.l = l;
      t0 = System.currentTimeMillis();
    }
Esempio n. 27
0
  public static void main(String[] args) {
    // TODO main
    OptionParser parser = new OptionParser();
    parser.acceptsAll(Arrays.asList("h", "help"), "Show this help dialog.");
    OptionSpec<String> serverOption =
        parser
            .acceptsAll(Arrays.asList("s", "server"), "Server to join.")
            .withRequiredArg()
            .describedAs("server-address[:port]");
    OptionSpec<String> proxyOption =
        parser
            .acceptsAll(
                Arrays.asList("P", "proxy"),
                "SOCKS proxy to use. Ignored in presence of 'socks-proxy-list'.")
            .withRequiredArg()
            .describedAs("proxy-address");
    OptionSpec<String> ownerOption =
        parser
            .acceptsAll(
                Arrays.asList("o", "owner"), "Owner of the bot (username of in-game control).")
            .withRequiredArg()
            .describedAs("username");
    OptionSpec<?> offlineOption =
        parser.acceptsAll(
            Arrays.asList("O", "offline"),
            "Offline-mode. Ignores 'password' and 'account-list' (will "
                + "generate random usernames if 'username' is not supplied).");
    OptionSpec<?> autoRejoinOption =
        parser.acceptsAll(Arrays.asList("a", "auto-rejoin"), "Auto-rejoin a server on disconnect.");
    OptionSpec<Integer> loginDelayOption =
        parser
            .acceptsAll(
                Arrays.asList("d", "login-delay"),
                "Delay between bot joins, in milliseconds. 5000 is "
                    + "recommended if not using socks proxies.")
            .withRequiredArg()
            .describedAs("delay")
            .ofType(Integer.class);
    OptionSpec<Integer> botAmountOption =
        parser
            .acceptsAll(
                Arrays.asList("b", "bot-amount"),
                "Amount of bots to join. Must be <= amount of accounts.")
            .withRequiredArg()
            .describedAs("amount")
            .ofType(Integer.class);

    OptionSpec<String> accountListOption =
        parser
            .accepts(
                "account-list",
                "File containing a list of accounts, in username/email:password format.")
            .withRequiredArg()
            .describedAs("file");
    OptionSpec<String> socksProxyListOption =
        parser
            .accepts(
                "socks-proxy-list",
                "File containing a list of SOCKS proxies, in address:port format.")
            .withRequiredArg()
            .describedAs("file");
    OptionSpec<String> httpProxyListOption =
        parser
            .accepts(
                "http-proxy-list",
                "File containing a list of HTTP proxies, in address:port format.")
            .withRequiredArg()
            .describedAs("file");

    OptionSet options;
    try {
      options = parser.parse(args);
    } catch (OptionException exception) {
      try {
        parser.printHelpOn(System.out);
      } catch (Exception exception1) {
        exception1.printStackTrace();
      }
      return;
    }

    if (options.has("help")) {
      printHelp(parser);
      return;
    }

    final boolean offline = options.has(offlineOption);
    final boolean autoRejoin = options.has(autoRejoinOption);

    final List<String> accounts;
    if (options.has(accountListOption)) {
      accounts = loadAccounts(options.valueOf(accountListOption));
    } else if (!offline) {
      System.out.println("Option 'accounts' must be supplied in " + "absence of option 'offline'.");
      printHelp(parser);
      return;
    } else accounts = null;

    final String server;
    if (!options.has(serverOption)) {
      System.out.println("Option 'server' required.");
      printHelp(parser);
      return;
    } else server = options.valueOf(serverOption);

    final String owner;
    if (!options.has(ownerOption)) {
      System.out.println("Option 'owner' required.");
      printHelp(parser);
      return;
    } else owner = options.valueOf(ownerOption);

    final List<String> socksProxies;
    if (options.has(socksProxyListOption))
      socksProxies = loadProxies(options.valueOf(socksProxyListOption));
    else socksProxies = null;
    final boolean useProxy = socksProxies != null;

    final List<String> httpProxies;
    if (options.has(httpProxyListOption))
      httpProxies = loadLoginProxies(options.valueOf(httpProxyListOption));
    else if (!offline && accounts != null) {
      System.out.println(
          "Option 'http-proxy-list' required if " + "option 'account-list' is supplied.");
      printHelp(parser);
      return;
    } else httpProxies = null;

    final int loginDelay;
    if (options.has(loginDelayOption)) loginDelay = options.valueOf(loginDelayOption);
    else loginDelay = 0;

    final int botAmount;
    if (!options.has(botAmountOption)) {
      System.out.println("Option 'bot-amount' required.");
      printHelp(parser);
      return;
    } else botAmount = options.valueOf(botAmountOption);

    initGui();
    while (!sessions.get()) {
      synchronized (sessions) {
        try {
          sessions.wait(5000);
        } catch (InterruptedException exception) {
        }
      }
    }

    final Queue<Runnable> lockQueue = new ArrayDeque<Runnable>();

    ExecutorService service = Executors.newFixedThreadPool(botAmount + (loginDelay > 0 ? 1 : 0));
    final Object firstWait = new Object();
    if (loginDelay > 0) {
      service.execute(
          new Runnable() {
            @Override
            public void run() {
              synchronized (firstWait) {
                try {
                  firstWait.wait();
                } catch (InterruptedException exception) {
                }
              }
              while (true) {
                if (die) return;
                while (slotsTaken.get() >= 2) {
                  synchronized (slotsTaken) {
                    try {
                      slotsTaken.wait(500);
                    } catch (InterruptedException exception) {
                    }
                  }
                }
                synchronized (lockQueue) {
                  if (lockQueue.size() > 0) {
                    Runnable thread = lockQueue.poll();
                    synchronized (thread) {
                      thread.notifyAll();
                    }
                    lockQueue.offer(thread);
                  } else continue;
                }
                try {
                  Thread.sleep(loginDelay);
                } catch (InterruptedException exception) {
                }
                while (!sessions.get()) {
                  synchronized (sessions) {
                    try {
                      sessions.wait(5000);
                    } catch (InterruptedException exception) {
                    }
                  }
                }
              }
            }
          });
    }
    final List<String> accountsInUse = new ArrayList<String>();
    for (int i = 0; i < botAmount; i++) {
      final int botNumber = i;
      Runnable runnable =
          new Runnable() {
            @Override
            public void run() {
              if (loginDelay > 0)
                synchronized (lockQueue) {
                  lockQueue.add(this);
                }
              Random random = new Random();

              if (!offline) {
                boolean authenticated = false;
                user:
                while (true) {
                  if (authenticated) {
                    authenticated = false;
                    sessionCount.decrementAndGet();
                  }
                  Session session = null;
                  String loginProxy;
                  String account = accounts.get(random.nextInt(accounts.size()));
                  synchronized (accountsInUse) {
                    if (accountsInUse.size() == accounts.size()) System.exit(0);
                    while (accountsInUse.contains(account))
                      account = accounts.get(random.nextInt(accounts.size()));
                    accountsInUse.add(account);
                  }
                  String[] accountParts = account.split(":");
                  while (true) {
                    while (!sessions.get()) {
                      synchronized (sessions) {
                        try {
                          sessions.wait(5000);
                        } catch (InterruptedException exception) {
                        }
                      }
                    }
                    loginProxy = httpProxies.get(random.nextInt(httpProxies.size()));
                    try {
                      session = Util.retrieveSession(accountParts[0], accountParts[1], loginProxy);
                      // addAccount(session);
                      sessionCount.incrementAndGet();
                      authenticated = true;
                      break;
                    } catch (AuthenticationException exception) {
                      System.err.println("[Bot" + botNumber + "] " + exception);
                      if (!exception.getMessage().startsWith("Exception"))
                        // && !exception.getMessage().equals(
                        // "Too many failed logins"))
                        continue user;
                    }
                  }
                  System.out.println(
                      "["
                          + session.getUsername()
                          + "] Password: "******", Session ID: "
                          + session.getSessionId());
                  while (!joins.get()) {
                    synchronized (joins) {
                      try {
                        joins.wait(5000);
                      } catch (InterruptedException exception) {
                      }
                    }
                  }
                  if (loginDelay > 0) {
                    synchronized (this) {
                      try {
                        synchronized (firstWait) {
                          firstWait.notifyAll();
                        }
                        wait();
                      } catch (InterruptedException exception) {
                      }
                    }
                  }

                  while (true) {
                    String proxy =
                        useProxy ? socksProxies.get(random.nextInt(socksProxies.size())) : null;
                    try {
                      new DarkBotMCSpambot(
                          DARK_BOT,
                          server,
                          session.getUsername(),
                          session.getPassword(),
                          session.getSessionId(),
                          null,
                          proxy,
                          owner);
                      if (die) break user;
                      else if (!autoRejoin) break;
                    } catch (Exception exception) {
                      exception.printStackTrace();
                      System.out.println(
                          "["
                              + session.getUsername()
                              + "] Error connecting: "
                              + exception.getCause().toString());
                    }
                  }
                  System.out.println("[" + session.getUsername() + "] Account failed");
                }
              } else {
                while (true) {
                  String proxy =
                      useProxy ? socksProxies.get(random.nextInt(socksProxies.size())) : null;
                  try {
                    String username = "";
                    if (accounts != null) {
                      username = accounts.get(random.nextInt(accounts.size())).split(":")[0];
                      synchronized (accountsInUse) {
                        while (accountsInUse.contains(username))
                          username = accounts.get(random.nextInt(accounts.size()));
                        accountsInUse.add(username);
                      }
                    } else
                      for (int i = 0; i < 10 + random.nextInt(6); i++)
                        username += alphas[random.nextInt(alphas.length)];
                    if (loginDelay > 0) {
                      synchronized (this) {
                        try {
                          synchronized (firstWait) {
                            firstWait.notifyAll();
                          }
                          wait();
                        } catch (InterruptedException exception) {
                        }
                      }
                    }
                    new DarkBotMCSpambot(DARK_BOT, server, username, "", "", null, proxy, owner);
                    if (die || !autoRejoin) break;
                    else continue;
                  } catch (Exception exception) {
                    System.out.println(
                        "[Bot" + botNumber + "] Error connecting: " + exception.toString());
                  }
                }
              }
            }
          };
      service.execute(runnable);
    }
    service.shutdown();
    while (!service.isTerminated()) {
      try {
        service.awaitTermination(9000, TimeUnit.DAYS);
      } catch (InterruptedException exception) {
        exception.printStackTrace();
      }
    }
    System.exit(0);
  }
Esempio n. 28
0
  public static void main(String[] args) {
    // TODO main
    OptionParser parser = new OptionParser();
    parser.acceptsAll(Arrays.asList("h", "help"), "Show this help dialog.");
    OptionSpec<String> serverOption =
        parser
            .acceptsAll(Arrays.asList("s", "server"), "Server to join.")
            .withRequiredArg()
            .describedAs("server-address[:port]");
    OptionSpec<String> proxyOption =
        parser
            .acceptsAll(
                Arrays.asList("P", "proxy"),
                "SOCKS proxy to use. Ignored in presence of 'socks-proxy-list'.")
            .withRequiredArg()
            .describedAs("proxy-address");
    OptionSpec<String> ownerOption =
        parser
            .acceptsAll(
                Arrays.asList("o", "owner"), "Owner of the bot (username of in-game control).")
            .withRequiredArg()
            .describedAs("username");
    OptionSpec<?> offlineOption =
        parser.acceptsAll(
            Arrays.asList("O", "offline"),
            "Offline-mode. Ignores 'password' and 'account-list' (will "
                + "generate random usernames if 'username' is not supplied).");
    OptionSpec<?> autoRejoinOption =
        parser.acceptsAll(Arrays.asList("a", "auto-rejoin"), "Auto-rejoin a server on disconnect.");
    OptionSpec<Integer> loginDelayOption =
        parser
            .acceptsAll(
                Arrays.asList("d", "login-delay"),
                "Delay between bot joins, in milliseconds. 5000 is "
                    + "recommended if not using socks proxies.")
            .withRequiredArg()
            .describedAs("delay")
            .ofType(Integer.class);
    OptionSpec<Integer> botAmountOption =
        parser
            .acceptsAll(
                Arrays.asList("b", "bot-amount"),
                "Amount of bots to join. Must be <= amount of accounts.")
            .withRequiredArg()
            .describedAs("amount")
            .ofType(Integer.class);
    OptionSpec<String> protocolOption =
        parser
            .accepts(
                "protocol",
                "Protocol version to use. Can be either protocol number or Minecraft version.")
            .withRequiredArg();
    OptionSpec<?> protocolsOption =
        parser.accepts("protocols", "List available protocols and exit.");

    OptionSpec<String> accountListOption =
        parser
            .accepts(
                "account-list",
                "File containing a list of accounts, in username/email:password format.")
            .withRequiredArg()
            .describedAs("file");
    OptionSpec<String> socksProxyListOption =
        parser
            .accepts(
                "socks-proxy-list",
                "File containing a list of SOCKS proxies, in address:port format.")
            .withRequiredArg()
            .describedAs("file");
    OptionSpec<String> httpProxyListOption =
        parser
            .accepts(
                "http-proxy-list",
                "File containing a list of HTTP proxies, in address:port format.")
            .withRequiredArg()
            .describedAs("file");
    OptionSpec<String> captchaListOption =
        parser
            .accepts("captcha-list", "File containing a list of chat baised captcha to bypass.")
            .withRequiredArg()
            .describedAs("file");

    OptionSet options;
    try {
      options = parser.parse(args);
    } catch (OptionException exception) {
      try {
        parser.printHelpOn(System.out);
      } catch (Exception exception1) {
        exception1.printStackTrace();
      }
      return;
    }

    if (options.has("help")) {
      printHelp(parser);
      return;
    }
    if (options.has(protocolsOption)) {
      System.out.println("Available protocols:");
      for (ProtocolProvider provider : ProtocolProvider.getProviders())
        System.out.println(
            "\t"
                + provider.getMinecraftVersion()
                + " ("
                + provider.getSupportedVersion()
                + "): "
                + provider.getClass().getName());
      System.out.println(
          "If no protocols are listed above, you may attempt to specify a protocol version in case the provider is actually in the class-path.");
      return;
    }

    final boolean offline = options.has(offlineOption);
    final boolean autoRejoin = options.has(autoRejoinOption);

    final List<String> accounts;
    if (options.has(accountListOption)) {
      accounts = loadAccounts(options.valueOf(accountListOption));
    } else if (!offline) {
      System.out.println("Option 'accounts' must be supplied in " + "absence of option 'offline'.");
      printHelp(parser);
      return;
    } else accounts = null;

    final List<String> captcha;
    if (options.has(captchaListOption)) readCaptchaFile(options.valueOf(captchaListOption));

    final String server;
    if (!options.has(serverOption)) {
      System.out.println("Option 'server' required.");
      printHelp(parser);
      return;
    } else server = options.valueOf(serverOption);

    final String owner;
    if (!options.has(ownerOption)) {
      System.out.println("Option 'owner' required.");
      printHelp(parser);
      return;
    } else owner = options.valueOf(ownerOption);

    final int protocol;
    if (options.has(protocolOption)) {
      String protocolString = options.valueOf(protocolOption);
      int parsedProtocol;
      try {
        parsedProtocol = Integer.parseInt(protocolString);
      } catch (NumberFormatException exception) {
        ProtocolProvider foundProvider = null;
        for (ProtocolProvider provider : ProtocolProvider.getProviders())
          if (protocolString.equals(provider.getMinecraftVersion())) foundProvider = provider;
        if (foundProvider == null) {
          System.out.println("No provider found for Minecraft version '" + protocolString + "'.");
          return;
        } else parsedProtocol = foundProvider.getSupportedVersion();
      }
      protocol = parsedProtocol;
    } else protocol = MinecraftBot.LATEST_PROTOCOL;

    final List<String> socksProxies;
    if (options.has(socksProxyListOption))
      socksProxies = loadProxies(options.valueOf(socksProxyListOption));
    else socksProxies = null;
    final boolean useProxy = socksProxies != null;

    final List<String> httpProxies;
    if (options.has(httpProxyListOption))
      httpProxies = loadLoginProxies(options.valueOf(httpProxyListOption));
    else if (!offline && accounts != null) {
      System.out.println(
          "Option 'http-proxy-list' required if " + "option 'account-list' is supplied.");
      printHelp(parser);
      return;
    } else httpProxies = null;

    final int loginDelay;
    if (options.has(loginDelayOption)) loginDelay = options.valueOf(loginDelayOption);
    else loginDelay = 0;

    final int botAmount;
    if (!options.has(botAmountOption)) {
      System.out.println("Option 'bot-amount' required.");
      printHelp(parser);
      return;
    } else botAmount = options.valueOf(botAmountOption);

    initGui();
    while (!sessions.get()) {
      synchronized (sessions) {
        try {
          sessions.wait(5000);
        } catch (InterruptedException exception) {
        }
      }
    }

    final Queue<Runnable> lockQueue = new ArrayDeque<Runnable>();

    ExecutorService service = Executors.newFixedThreadPool(botAmount + (loginDelay > 0 ? 1 : 0));
    final Object firstWait = new Object();
    if (loginDelay > 0) {
      service.execute(
          new Runnable() {
            @Override
            public void run() {
              synchronized (firstWait) {
                try {
                  firstWait.wait();
                } catch (InterruptedException exception) {
                }
              }
              while (true) {
                try {
                  Thread.sleep(loginDelay);
                } catch (InterruptedException exception) {
                }
                synchronized (lockQueue) {
                  if (lockQueue.size() > 0) {
                    Runnable thread = lockQueue.poll();
                    synchronized (thread) {
                      thread.notifyAll();
                    }
                    lockQueue.offer(thread);
                  } else continue;
                }
                while (!sessions.get()) {
                  synchronized (sessions) {
                    try {
                      sessions.wait(5000);
                    } catch (InterruptedException exception) {
                    }
                  }
                }
              }
            }
          });
    }
    final List<String> accountsInUse = new ArrayList<String>();
    final Map<String, AtomicInteger> workingProxies = new HashMap<String, AtomicInteger>();
    for (int i = 0; i < botAmount; i++) {
      final int botNumber = i;
      Runnable runnable =
          new Runnable() {
            @Override
            public void run() {
              if (loginDelay > 0)
                synchronized (lockQueue) {
                  lockQueue.add(this);
                }
              Random random = new Random();

              if (!offline) {
                AuthService authService = new LegacyAuthService();
                boolean authenticated = false;
                user:
                while (true) {
                  if (authenticated) {
                    authenticated = false;
                    sessionCount.decrementAndGet();
                  }
                  Session session = null;
                  String loginProxy;
                  String account = accounts.get(random.nextInt(accounts.size()));
                  synchronized (accountsInUse) {
                    if (accountsInUse.size() == accounts.size()) System.exit(0);
                    while (accountsInUse.contains(account))
                      account = accounts.get(random.nextInt(accounts.size()));
                    accountsInUse.add(account);
                  }
                  String[] accountParts = account.split(":");
                  while (true) {
                    while (!sessions.get()) {
                      synchronized (sessions) {
                        try {
                          sessions.wait(5000);
                        } catch (InterruptedException exception) {
                        }
                      }
                    }
                    synchronized (workingProxies) {
                      Iterator<String> iterator = workingProxies.keySet().iterator();
                      if (iterator.hasNext()) loginProxy = iterator.next();
                      else loginProxy = httpProxies.get(random.nextInt(httpProxies.size()));
                      ;
                    }
                    try {
                      session =
                          authService.login(
                              accountParts[0],
                              accountParts[1],
                              toProxy(loginProxy, Proxy.Type.HTTP));
                      // addAccount(session);
                      synchronized (workingProxies) {
                        AtomicInteger count = workingProxies.get(loginProxy);
                        if (count != null) count.set(0);
                        else workingProxies.put(loginProxy, new AtomicInteger());
                      }
                      sessionCount.incrementAndGet();
                      authenticated = true;
                      break;
                    } catch (IOException exception) {
                      synchronized (workingProxies) {
                        workingProxies.remove(loginProxy);
                      }
                      System.err.println("[Bot" + botNumber + "] " + loginProxy + ": " + exception);
                    } catch (AuthenticationException exception) {
                      if (exception.getMessage().contains("Too many failed logins")) {
                        synchronized (workingProxies) {
                          AtomicInteger count = workingProxies.get(loginProxy);
                          if (count != null && count.incrementAndGet() >= 5)
                            workingProxies.remove(loginProxy);
                        }
                      }
                      System.err.println("[Bot" + botNumber + "] " + loginProxy + ": " + exception);
                      continue user;
                    }
                  }
                  System.out.println("[" + session.getUsername() + "] " + session);
                  while (!joins.get()) {
                    synchronized (joins) {
                      try {
                        joins.wait(5000);
                      } catch (InterruptedException exception) {
                      }
                    }
                  }
                  if (loginDelay > 0) {
                    synchronized (this) {
                      try {
                        synchronized (firstWait) {
                          firstWait.notifyAll();
                        }
                        wait();
                      } catch (InterruptedException exception) {
                      }
                    }
                  }

                  while (true) {
                    String proxy =
                        useProxy ? socksProxies.get(random.nextInt(socksProxies.size())) : null;
                    try {
                      DarkBotMCSpambot bot =
                          new DarkBotMCSpambot(
                              generateData(
                                  server,
                                  session.getUsername(),
                                  session.getPassword(),
                                  authService,
                                  session,
                                  protocol,
                                  null,
                                  proxy),
                              owner);
                      while (bot.getBot().isConnected()) {
                        try {
                          Thread.sleep(500);
                        } catch (InterruptedException exception) {
                          exception.printStackTrace();
                        }
                      }
                      if (!autoRejoin) break;
                    } catch (Exception exception) {
                      exception.printStackTrace();
                      System.out.println(
                          "["
                              + session.getUsername()
                              + "] Error connecting: "
                              + exception.getCause().toString());
                    }
                  }
                  System.out.println("[" + session.getUsername() + "] Account failed");
                }
              } else {
                while (true) {
                  String proxy =
                      useProxy ? socksProxies.get(random.nextInt(socksProxies.size())) : null;
                  try {
                    String username;
                    if (accounts != null) {
                      username = accounts.get(random.nextInt(accounts.size())).split(":")[0];
                      synchronized (accountsInUse) {
                        while (accountsInUse.contains(username))
                          username = accounts.get(random.nextInt(accounts.size()));
                        accountsInUse.add(username);
                      }
                    } else username = Util.generateRandomString(10 + random.nextInt(6));
                    if (loginDelay > 0) {
                      synchronized (this) {
                        try {
                          synchronized (firstWait) {
                            firstWait.notifyAll();
                          }
                          wait();
                        } catch (InterruptedException exception) {
                        }
                      }
                    }
                    DarkBotMCSpambot bot =
                        new DarkBotMCSpambot(
                            generateData(server, username, null, null, null, protocol, null, proxy),
                            owner);
                    while (bot.getBot().isConnected()) {
                      try {
                        Thread.sleep(500);
                      } catch (InterruptedException exception) {
                        exception.printStackTrace();
                      }
                    }
                    if (!autoRejoin) break;
                    else continue;
                  } catch (Exception exception) {
                    System.out.println(
                        "[Bot" + botNumber + "] Error connecting: " + exception.toString());
                  }
                }
              }
            }
          };
      service.execute(runnable);
    }
    service.shutdown();
    while (!service.isTerminated()) {
      try {
        service.awaitTermination(9000, TimeUnit.DAYS);
      } catch (InterruptedException exception) {
        exception.printStackTrace();
      }
    }
    System.exit(0);
  }