Ejemplo n.º 1
0
    void add(Target t) {
      SocketChannel sc = null;
      try {

        sc = SocketChannel.open();
        sc.configureBlocking(false);

        boolean connected = sc.connect(t.address);

        t.channel = sc;
        t.connectStart = System.currentTimeMillis();

        if (connected) {
          t.connectFinish = t.connectStart;
          sc.close();
          printer.add(t);
        } else {
          synchronized (pending) {
            pending.add(t);
          }

          sel.wakeup();
        }
      } catch (IOException x) {
        if (sc != null) {
          try {
            sc.close();
          } catch (IOException xx) {
          }
        }
        t.failure = x;
        printer.add(t);
      }
    }
 private static Budget loadBudget(int index, File[] foundBudgets) {
   FileInputStream newBudget = null;
   ObjectInputStream toLoad = null;
   Object loadedBudget = null;
   try {
     newBudget = new FileInputStream(foundBudgets[index].toString());
   } catch (FileNotFoundException e) {
     Printer.print("wTF");
     System.out.println("Error making the FileInputStream");
   }
   try {
     toLoad = new ObjectInputStream(newBudget);
   } catch (IOException e) {
     Printer.print("wTF");
     System.out.println("Error making the ObjectInputStream");
   }
   try {
     loadedBudget = toLoad.readObject();
   } catch (ClassNotFoundException | IOException e) {
     Printer.print("wTF");
     System.out.println("IO exception, maybe the file is bad?");
   }
   try {
     toLoad.close();
   } catch (IOException e) {
     Printer.print("wTF");
     System.out.println("Couldn't close the Object/File input streams.");
   }
   BudgetBoss.endLoadSavedBudget();
   return (Budget) loadedBudget;
 }
Ejemplo n.º 3
0
 public PingClient() throws IOException {
   selector = Selector.open();
   Connector connector = new Connector();
   Printer printer = new Printer();
   connector.start();
   printer.start();
   receiveTarget();
 }
 private static void searchDirectory(String directoryToSearch) {
   AnsiConsole.out.println(ansi().eraseScreen());
   Printer.print("searchingDirectory");
   File[] foundBudgets = Finder.findBudgets(directoryToSearch);
   if (foundBudgets.length > 0) {
     chooseBudget(foundBudgets);
     BudgetBoss.setDefaultDirectory(directoryToSearch);
   } else Printer.print("noBudgetFound");
 }
 private static void chooseBudget(File[] foundBudgets) {
   Printer.printFoundBudgets(foundBudgets);
   Printer.print("openBudget");
   int index = getChoiceNumber(foundBudgets);
   if (!(index == -5)) {
     System.out.println("Opening " + foundBudgets[index].getName() + "...\n");
     BudgetBoss.setCurrentBudget(loadBudget(index, foundBudgets));
     BudgetBoss.endNeedNewBudget();
   }
 }
Ejemplo n.º 6
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);
  }
 public static void getLoadDirectory() {
   AnsiConsole.out.println(ansi().eraseScreen());
   Printer.print("savedInDefault");
   System.out.println(BudgetBoss.getDefaultDirectory());
   Printer.print("whereSaved");
   String toCheck = Listener.getInput();
   while (InputValidator.pathIsInvalid(toCheck)) toCheck = Listener.getInput();
   if (!(toCheck.equals("exit"))) {
     if (toCheck.equalsIgnoreCase("y")) {
       searchDirectory(BudgetBoss.getDefaultDirectory());
     } else searchDirectory(toCheck);
   }
 }
Ejemplo n.º 8
0
    void processSelectedKeys() throws IOException {
      for (Iterator i = sel.selectedKeys().iterator(); i.hasNext(); ) {

        SelectionKey sk = (SelectionKey) i.next();
        i.remove();

        Target t = (Target) sk.attachment();
        SocketChannel sc = (SocketChannel) sk.channel();

        try {
          if (sc.finishConnect()) {
            sk.cancel();
            t.connectFinish = System.currentTimeMillis();
            sc.close();
            printer.add(t);
          }
        } catch (IOException x) {
          sc.close();
          t.failure = x;
          printer.add(t);
        }
      }
    }
Ejemplo n.º 9
0
    void processPendingTargets() throws IOException {
      synchronized (pending) {
        while (pending.size() > 0) {
          Target t = (Target) pending.removeFirst();
          try {

            t.channel.register(sel, SelectionKey.OP_CONNECT, t);

          } catch (IOException x) {

            t.channel.close();
            t.failure = x;
            printer.add(t);
          }
        }
      }
    }
Ejemplo n.º 10
0
 public void print(int n, boolean p) {
   Printer.printNil(n, p);
 }
Ejemplo n.º 11
0
 void print(Node t, int n, boolean p) {
   Printer.printSet(t, n, p);
 }