// Returns whether a card is present in this terminal.
 // Throws:
 //     CardException - if the status could not be determined
 public boolean isCardPresent() throws CardException {
   if (verbosity > 0) {
     System.out.println("JC terminal " + port + " isCardPresent");
   }
   try {
     ensure_terminal_is_open();
     if (jc_terminal.getState() == RemoteJCTerminal.CARD_PRESENT) {
       return true;
     } else {
       // When I tried it getState on the emulator gave either
       // RemoteJCTerminal.CARD_PRESENT or threw an exception.
       // So this is fishy here!
       ensure_terminal_is_closed();
       return false;
     }
   } catch (JCException e) {
     if (e.errorCode == JCException.TERMINAL_ERROR) {
       // No emulator listens on this port.
       ensure_terminal_is_closed();
       return false;
     } else {
       // Other exceptions are propagated.
       throw new CardException(e);
     }
   }
 }
 void ensure_terminal_is_open() {
   if (!terminal_is_open) {
     if (verbosity > 0) {
       System.out.println("JC open jc_terminal");
     }
     jc_terminal.open();
     terminal_is_open = true;
   }
 }
 void ensure_terminal_is_closed() {
   if (terminal_is_open) {
     if (verbosity > 0) {
       System.out.println("JC close jc_terminal");
     }
     try {
       jc_terminal.close();
     } finally {
       terminal_is_open = false;
     }
   }
 }
  // Establishes a connection to the card. If a connection has
  // previously established using the specified protocol, this method
  // returns the same Card object as the previous call.
  //
  // Parameters:
  //     protocol - the protocol to use ("T=0", "T=1", or "T=CL"), or
  //                "*" to connect using any available protocol.
  //
  // Throws:
  //     NullPointerException - if protocol is null
  //     IllegalArgumentException - if protocol is an invalid protocol
  //                                specification
  //     CardNotPresentException - if no card is present in this terminal
  //
  //     CardException - if a connection could not be established using
  //                     the specified protocol or if a connection has
  //                     previously been established using a different
  //                     protocol
  //
  //     SecurityException - if a SecurityManager exists and the caller
  //                         does not have the required permission
  public Card connect(String protocol_name) throws CardNotPresentException, CardException {
    try {
      if (verbosity > 0) {
        System.out.println("JC terminal " + port + " connect");
      }
      byte[] atrbytes;

      if (terminal_is_open) {
        try {
          atrbytes = jc_terminal.waitForCard(0);
        } catch (JCException e) {
          // Maybe someone restarted the emulator since when
          // we opened the connection. Reopen and try again
          // in this case.
          if (e.errorCode == JCException.TERMINAL_ERROR) {
            ensure_terminal_is_closed();
            ensure_terminal_is_open();
            atrbytes = jc_terminal.waitForCard(0);
          } else throw e;
        }
      } else {
        ensure_terminal_is_open();
        atrbytes = jc_terminal.waitForCard(0);
      }

      ATR smio_atr = new ATR(atrbytes);
      JCard card = new JCard(jc_terminal, null, 0);

      return new Emulator_connection_card(
          this, card,
          smio_atr, verbosity);
    } catch (JCException e) {
      if (e.errorCode == JCException.TERMINAL_ERROR) {
        throw new CardNotPresentException(e.getMessage(), e);
      } else {
        throw new CardException("connection failure", e);
      }
    }
  }
  Emulator_connection_terminal(int port, int verbosity) {
    this.port = port;
    this.verbosity = verbosity;
    jc_terminal = new RemoteJCTerminal();
    jc_terminal.init(String.format("localhost:%d", port));
    if (verbosity > 0) System.out.println("JC terminal " + port + " constructur");

    // try {
    //     jc_terminal.open();
    //     System.out.println("opened");
    //     try{Thread.sleep(5000);} catch(InterruptedException e) {}
    //     System.out.println("continue");
    //     byte[] atrbytes = jc_terminal.waitForCard(0);
    // }
    // catch(JCException e) {
    //     System.out.format("caught JCE %s code %d (%s)\n",
    //                    e.getMessage(),
    //                    e.errorCode,
    //                    e.toString());
    //     e.printStackTrace();
    // }
  }