@Test
 public void testDisconnect() throws InterruptedException {
   TerminalConnection connection =
       terminalConnectionFactory.getConnection(new SimpleConnectionProperties());
   while (true) {
     TerminalSendAction action = new SimpleTerminalSendAction("[enter]");
     connection.doAction(action);
     System.out.println(connection.getSnapshot());
     Thread.sleep(5000);
   }
 }
 public void doAction(TerminalSendAction terminalSendAction) {
   lazyConnect();
   terminalSnapshot = null;
   handleRightAdjust(terminalSendAction);
   terminalConnection.doAction(terminalSendAction);
   handleSleep(terminalSendAction);
 }
  private int waitForNonEmptySnapshot() {
    int timer = 0;
    do {
      if (terminalSnapshot == null) {
        terminalSnapshot = terminalConnection.fetchSnapshot();
      }

      if (terminalSnapshot.getFields().size() == 0) {
        try {
          Thread.sleep(waitBetweenEmptyScreens);
          timer += waitBetweenEmptyScreens;
        } catch (InterruptedException e) {
          // do nothing
        }
        terminalSnapshot = null;
      }

    } while (isConnected() && terminalSnapshot == null && timer < maxWaitOnEmptyScreen);
    return timer;
  }
  public TerminalSnapshot getSnapshot() {
    lazyConnect();

    // clear the snapshot sequence is different from the session, clear it so it will re-build
    if (terminalSnapshot != null
        && terminalSnapshot.getSequence() != null
        && !terminalConnection.getSequence().equals(terminalSnapshot.getSequence())) {
      terminalSnapshot = null;
    }

    waitForNonEmptySnapshot();

    if (!isConnected()) {
      disconnect();
      throw (new OpenLegacyRuntimeException("Session is not connected"));
    }

    if (terminalSnapshot == null) {
      throw (new OpenLegacyProviderException(
          MessageFormat.format(
              "Current screen is empty for session after waiting for {0}", maxWaitOnEmptyScreen)));
    }
    return terminalSnapshot;
  }
 public boolean isRightToLeftState() {
   return terminalConnection.isRightToLeftState();
 }
 public void flip() {
   terminalConnection.flip();
 }
 public Integer getSequence() {
   if (!isConnected()) {
     return 0;
   }
   return terminalConnection.getSequence();
 }
 public boolean isConnected() {
   return terminalConnection != null && terminalConnection.isConnected();
 }
 public Object getDelegate() {
   lazyConnect();
   return terminalConnection.getDelegate();
 }