Esempio n. 1
0
 public static void main(String[] args) throws Exception {
   TypeServer ts = new TypeServer();
   ts.port = Integer.parseInt(args[0]);
   Thread t = new Thread(ts);
   t.start();
   System.out.println("Type server ready...Type CTRL-D to exit");
   while (System.in.read() > 0) ;
   ts.stopServer();
   t.join();
 }
 /**
  * @param log Logger.
  * @param time Time.
  * @param msg Message.
  */
 private static void log0(@Nullable IgniteLogger log, long time, String msg) {
   if (log != null) {
     if (log.isDebugEnabled()) log.debug(msg);
     else log.warning(msg);
   } else
     X.println(
         String.format(
             "[%s][%s]%s",
             DEBUG_DATE_FMT.get().format(time), Thread.currentThread().getName(), msg));
 }
Esempio n. 3
0
  public ShowComp() throws InterruptedException, IOException {
    super("CONNECTED COMPUTERS");
    int x = 0, d = 20;
    mb = new JMenuBar();
    File = new JMenu("File");
    mb.add(File);
    exit = new JMenuItem("Exit");
    exit.addActionListener(this);
    File.add(exit);
    ta = new JTextArea();
    ta.setBounds(20, 30, 315, 470);
    ta.setEditable(false);
    add(ta);

    setJMenuBar(mb);

    sel = new JLabel("The connected computers are..");
    sel.setBounds(15, 5, 300, 30);
    add(sel);
    b1 = new JButton("<< BACK");
    b1.setBounds(140, 510, 100, 30);
    b1.setToolTipText("Back to main page");
    b1.addActionListener(this);
    add(b1);
    setLayout(null);
    while (x < 360) {
      x = x + d;
      setBounds(675, 50, x, 600);
      this.show();
    }
    // setVisible(true);
    String s = "192.168.0.", temp = null;
    Printer printer = new Printer();
    printer.start();
    Connector connector = new Connector(printer);
    connector.start();

    LinkedList targets = new LinkedList();
    for (int i = 1; i <= 255; i++) {
      temp = s + Integer.toString(i);
      Target t = new Target(temp);
      targets.add(t);
      connector.add(t);
    }
    Thread.sleep(2000);
    connector.shutdown();
    connector.join();

    for (Iterator i = targets.iterator(); i.hasNext(); ) {
      Target t = (Target) i.next();
      if (!t.shown) t.show();
    }

    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  }
Esempio n. 4
0
  static void compare(Charset cs1, Charset cs2, char[] cc) throws Exception {
    System.gc(); // enqueue finalizable objects
    Thread.sleep(1000);
    System.gc(); // enqueue finalizable objects

    String csn1 = cs1.name();
    String csn2 = cs2.name();
    System.out.printf("Diff     <%s> <%s>...%n", csn1, csn2);

    Time t1 = new Time();
    Time t2 = new Time();

    byte[] bb1 = encode(cc, cs1, false, t1);
    byte[] bb2 = encode(cc, cs2, false, t2);

    System.out.printf(
        "    Encoding TimeRatio %s/%s: %d,%d :%f%n",
        csn2, csn1, t2.t, t1.t, (double) (t2.t) / (t1.t));
    if (!Arrays.equals(bb1, bb2)) {
      System.out.printf("        encoding failed%n");
    }

    char[] cc2 = decode(bb1, cs2, false, t2);
    char[] cc1 = decode(bb1, cs1, false, t1);
    System.out.printf(
        "    Decoding TimeRatio %s/%s: %d,%d :%f%n",
        csn2, csn1, t2.t, t1.t, (double) (t2.t) / (t1.t));
    if (!Arrays.equals(cc1, cc2)) {
      System.out.printf("        decoding failed%n");
    }

    bb1 = encode(cc, cs1, true, t1);
    bb2 = encode(cc, cs2, true, t2);

    System.out.printf(
        "    Encoding(dir) TimeRatio %s/%s: %d,%d :%f%n",
        csn2, csn1, t2.t, t1.t, (double) (t2.t) / (t1.t));

    if (!Arrays.equals(bb1, bb2)) System.out.printf("        encoding (direct) failed%n");

    cc1 = decode(bb1, cs1, true, t1);
    cc2 = decode(bb1, cs2, true, t2);
    System.out.printf(
        "    Decoding(dir) TimeRatio %s/%s: %d,%d :%f%n",
        csn2, csn1, t2.t, t1.t, (double) (t2.t) / (t1.t));
    if (!Arrays.equals(cc1, cc2)) {
      System.out.printf("        decoding (direct) failed%n");
    }
  }
  /**
   * Checks if address can be reached using one argument InetAddress.isReachable() version or ping
   * command if failed.
   *
   * @param addr Address to check.
   * @param reachTimeout Timeout for the check.
   * @return {@code True} if address is reachable.
   */
  public static boolean reachableByPing(InetAddress addr, int reachTimeout) {
    try {
      if (addr.isReachable(reachTimeout)) return true;

      String cmd = String.format("ping -%s 1 %s", U.isWindows() ? "n" : "c", addr.getHostAddress());

      Process myProc = Runtime.getRuntime().exec(cmd);

      myProc.waitFor();

      return myProc.exitValue() == 0;
    } catch (IOException ignore) {
      return false;
    } catch (InterruptedException ignored) {
      Thread.currentThread().interrupt();

      return false;
    }
  }
Esempio n. 6
0
  /** This method starts the thread and begins to download the file. */
  public void run() {
    int maxThreads = Integer.parseInt(mainApp.getPrefValue("ServerSettingsThreadCount"));
    int runningThreads = 0;
    HashMap<String, Integer> downloadedBytes = new HashMap<String, Integer>();
    HashMap<String, Integer> lastProgBarUpdate = new HashMap<String, Integer>();

    // loop at all segments of the download file
    while (!shutdown && (segQueue.hasMoreSegments() || runningThreads > 0)) {
      // more segments to go?
      while (segQueue.hasMoreSegments()
          && runningThreads < maxThreads
          && !pause
          && nioClient.hasFreeSlot()) {
        // get next download segment of the download file
        DownloadFileSegment seg = segQueue.nextSegment();
        if (seg == null) break;
        String filename = seg.getDlFile().getFilename();
        logger.msg("Downloading next segment of file: " + filename, MyLogger.SEV_DEBUG);

        // create new response handler
        RspHandler newHandler = new RspHandler(seg);
        activeRspHandlers.add(newHandler);

        // map the new response handler to the download file
        Vector<RspHandler> tmpVector = dlFileRspHandlerMap.get(seg.getDlFile());
        if (tmpVector == null) tmpVector = new Vector<RspHandler>();
        tmpVector.add(newHandler);
        dlFileRspHandlerMap.put(seg.getDlFile(), tmpVector);

        // start data download
        nioClient.fetchArticleData(seg.getGroups().firstElement(), seg.getArticleId(), newHandler);

        // increase thread counter
        runningThreads++;
      }

      // check if the next element of the result set is already finished
      Vector<RspHandler> toRemoveVector = new Vector<RspHandler>();
      for (int i = 0; i < activeRspHandlers.size(); i++) {
        RspHandler handler = activeRspHandlers.get(i);

        // handle error response from NNTP server
        if (handler.getError() == RspHandler.ERR_NONE) {
          // no error, do nothing
        } else if (handler.getError() == RspHandler.ERR_AUTH) {
          // do nothing for this error (?)
        } else if (handler.getError() == RspHandler.ERR_FETCH) {
          // TODO: handle "430 no such article" error (?)
          String msg =
              "no such article found: <"
                  + handler.dlFileSeg().getArticleId()
                  + "> ("
                  + handler.getErrorMsg()
                  + ")";
          logger.msg(msg, MyLogger.SEV_WARNING);
        } else {
          // all other errors
          shutdown = true;
        }

        // update downloaded byte counter ...
        DownloadFile dlFile = handler.dlFileSeg().getDlFile();
        String filename = dlFile.getFilename();
        int bytes = 0;
        Integer bytesInt = downloadedBytes.get(filename);
        if (bytesInt != null) bytes = bytesInt;
        bytes += handler.newByteCount();
        downloadedBytes.put(filename, bytes);

        // ... and progres bar in main window
        int last = 0;
        Integer lastInt = lastProgBarUpdate.get(filename);
        if (lastInt != null) last = lastInt;
        last = updateProgressBar(bytes, last, dlFile);
        lastProgBarUpdate.put(filename, last);

        // all data downloaded?
        if (handler.isFinished()) {
          toRemoveVector.add(handler);
          runningThreads--;
          decrSegCount(filename); // decrease main window segment
          // counter

          // segment done, so check if whole download file is finished
          // now
          dlFile.removeSegment(handler.dlFileSeg().getIndex());
          if (!dlFile.hasMoreSegments()) {
            try {
              handleFinishedDlFile(dlFile);
            } catch (Exception e) {
              logger.printStackTrace(e);
            }
          }
        }
      }
      activeRspHandlers.removeAll(toRemoveVector);
      toRemoveVector.removeAllElements();

      // all tasks done?
      if (!segQueue.hasMoreSegments() && runningThreads == 0) {
        break;
      }

      try {
        // let the thread sleep a bit
        Thread.sleep(10);
      } catch (InterruptedException e) {
        // shutdown if interrupted
        shutdown = true;
      }
    } // end of main loop

    logger.msg("FileDownloader has finished downloading all files", MyLogger.SEV_DEBUG);
  }
Esempio n. 7
0
  /**
   * This method is called when a whole download file has been finished downloading. It updates main
   * application window and starts the decoding thread.
   *
   * @param dlFile The DownloadFile object that is finished
   */
  private void handleFinishedDlFile(final DownloadFile dlFile) {
    final String filename = dlFile.getFilename();
    logger.msg("File downloading finished: " + filename, MyLogger.SEV_INFO);

    // notify application that download has finished
    SwingUtilities.invokeLater(
        new Runnable() {
          public void run() {
            mainApp.fileDownloadFinished(filename);
            mainApp.setProgBarToDecoding(filename, dlFile.getSegCount());
          }
        });

    // create result vector
    Vector<byte[]> articleData = new Vector<byte[]>();
    Vector<RspHandler> rspHandlers = dlFileRspHandlerMap.get(dlFile);
    for (int i = 0; i < rspHandlers.size(); i++) {
      byte[] tmpArray = removeFirstLine(rspHandlers.get(i).getData(true));
      articleData.add(tmpArray);
      rspHandlers.set(i, null); // free some memory
    }

    // call garbage collector
    rspHandlers = null;
    dlFileRspHandlerMap.remove(dlFile);
    Runtime.getRuntime().gc();

    logger.msg(
        "First line(s) dump:\n" + HelloNzbToolkit.firstLineFromByteData(articleData.get(0), 2),
        MyLogger.SEV_DEBUG);

    // determine data encoding (yenc or UU)
    String encoding = null;
    boolean bHasData = false;
    for (int i = 0; i < articleData.size(); i++) {
      byte[] abyteHelp = articleData.get(i);
      if (abyteHelp.length > 0) {
        bHasData = true;
        if (bytesEqualsString(abyteHelp, "=ybegin")) {
          encoding = "yenc";
          break;
        } else if (bytesEqualsString(abyteHelp, "begin ")) {
          encoding = "uu";
          break;
        }
      }
    }
    if (encoding == null) {
      if (bHasData) {
        encoding = "yenc";
        logger.msg(
            "No suitable decoder (no data) found for downloaded file: "
                + dlFile.getFilename()
                + " -- Assuming yenc.",
            MyLogger.SEV_WARNING);
      } else {
        // too bad, no decoder found for this file :(
        logger.msg(
            "No suitable decoder found for downloaded file (no data): " + dlFile.getFilename(),
            MyLogger.SEV_ERROR);

        // update main application window
        SwingUtilities.invokeLater(
            new Runnable() {
              public void run() {
                mainApp.fileDecodingFinished(dlFile.getFilename());
              }
            });

        return;
      }
    }

    /*
     * // determine data encoding String encoding = null;
     * if(bytesEqualsString(articleData.get(0), "=ybegin")) encoding =
     * "yenc"; else if(bytesEqualsString(articleData.get(0), "begin "))
     * encoding = "uu"; else { // too bad, no decoder found for this file :(
     * logger.msg("No suitable decoder found for downloaded file: " +
     * dlFile.getFilename(), MyLogger.SEV_ERROR);
     *
     * // update main application window SwingUtilities.invokeLater(new
     * Runnable() { public void run() {
     * mainApp.fileDecodingFinished(dlFile.getFilename()); } } );
     *
     * return; }
     */

    // start data decoding background thread
    FileDecoder fileDecoder = new FileDecoder(mainApp, dlDir, dlFile, articleData, encoding);
    Thread t = new Thread(fileDecoder);
    t.start();
  }