@Override
    public void run() {
      try {
        Thread.sleep(myReconnectTimeout);

        if (myFacade.connect() != null && myFacade.getMyAccount().isLoginAllowed()) {
          myReconnectProcess = myIdeFacade.runOnPooledThread(this);
        }
      } catch (InterruptedException ignored) {
        // return
      }
    }
  protected void setUp() throws Exception {
    super.setUp();
    try {
      getConnection(0)
          .getRoster()
          .createEntry(getBareJID(2), "gato5", new String[] {"Friends, Coworker"});
      getConnection(0).getRoster().createEntry(getBareJID(3), "gato6", null);
      Thread.sleep(100);

    } catch (Exception e) {
      fail(e.getMessage());
    }
  }
  /**
   * High level API test. 1. User_1 will send his/her roster to user_2 2. User_2 will automatically
   * add the entries that receives to his/her roster in the corresponding group 3. User_1 will wait
   * several seconds for an ACK from user_2, if none is received then something is wrong
   */
  public void testSendAndAcceptRoster() {
    RosterExchangeManager rosterExchangeManager1 = new RosterExchangeManager(getConnection(0));
    RosterExchangeManager rosterExchangeManager2 = new RosterExchangeManager(getConnection(1));

    // Create a RosterExchangeListener that will accept all the received roster entries
    RosterExchangeListener rosterExchangeListener =
        new RosterExchangeListener() {
          public void entriesReceived(String from, Iterator remoteRosterEntries) {
            int received = 0;
            assertNotNull("From is null", from);
            assertNotNull("remoteRosterEntries is null", remoteRosterEntries);
            assertTrue("Roster without entries", remoteRosterEntries.hasNext());
            while (remoteRosterEntries.hasNext()) {
              received++;
              try {
                RemoteRosterEntry remoteRosterEntry =
                    (RemoteRosterEntry) remoteRosterEntries.next();
                getConnection(1)
                    .getRoster()
                    .createEntry(
                        remoteRosterEntry.getUser(),
                        remoteRosterEntry.getName(),
                        remoteRosterEntry.getGroupArrayNames());
              } catch (Exception e) {
                fail(e.toString());
              }
            }
            entriesReceived = received;
          }
        };
    rosterExchangeManager2.addRosterListener(rosterExchangeListener);

    // Send user1's roster to user2
    try {
      entriesSent = getConnection(0).getRoster().getEntryCount();
      entriesReceived = 0;
      rosterExchangeManager1.send(getConnection(0).getRoster(), getBareJID(1));
      // Wait up to 2 seconds
      long initial = System.currentTimeMillis();
      while (System.currentTimeMillis() - initial < 2000 && (entriesSent != entriesReceived)) {
        Thread.sleep(100);
      }
    } catch (Exception e) {
      fail("An error occured sending the message with the roster");
    }
    assertEquals(
        "Number of sent and received entries does not match", entriesSent, entriesReceived);
    assertTrue("Roster2 has no entries", getConnection(1).getRoster().getEntryCount() > 0);
  }
Ejemplo n.º 4
0
  public void run() throws XMPPException, IOException, InterruptedException {
    logger.debug("Trying to connect to " + host + ":" + port);
    ConnectionConfiguration configuration = new ConnectionConfiguration(host, port, "gmail.com");
    final XMPPConnection connection = new XMPPConnection(configuration);
    connection.connect();
    logger.debug("...connected");
    SASLAuthentication.supportSASLMechanism("PLAIN", 0);
    logger.debug("Trying to log in with credentials " + login + ":" + password);
    connection.login(login, password, "MyXmppBot");
    logger.debug("...logged in");

    final Process process = Runtime.getRuntime().exec("/bin/bash");
    final InputStream inputStream = process.getInputStream();
    final OutputStream outputStream = process.getOutputStream();

    Thread inputThread =
        new Thread(
            new Runnable() {
              @Override
              public void run() {
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                try {
                  while (true) {
                    String line = bufferedReader.readLine();
                    Message message = new Message(to);
                    message.setBody(line);
                    connection.sendPacket(message);
                  }
                } catch (IOException e) {
                  e.printStackTrace();
                }
              }
            });
    inputThread.start();

    final PrintWriter commandWriter = new PrintWriter(outputStream, true);
    PacketListener listener =
        new PacketListener() {
          @Override
          public void processPacket(Packet packet) {
            if (packet instanceof Message) {
              Message message = (Message) packet;
              to = message.getFrom();
              String command = message.getBody();
              if (command != null) {
                logger.debug("Command received:" + command);
                if (command.equalsIgnoreCase("exit")) {
                  process.destroy();
                  System.exit(0);
                } else {
                  commandWriter.println(command);
                }
              }
            }
          }
        };

    PacketFilter filter = new FromContainsFilter("carlos.prados");
    Thread.sleep(1000);
    connection.addPacketListener(listener, null);
  }
    /** Thread entry point. */
    @Override
    public void run() {
      int status;
      long progress;
      String statusReason = "";

      while (true) {
        try {
          Thread.sleep(10);

          status = parseJabberStatus(jabberTransfer.getStatus());
          progress = fileTransfer.getTransferedBytes();

          if (status == FileTransferStatusChangeEvent.FAILED
              || status == FileTransferStatusChangeEvent.COMPLETED
              || status == FileTransferStatusChangeEvent.CANCELED
              || status == FileTransferStatusChangeEvent.REFUSED) {
            if (fileTransfer instanceof OutgoingFileTransferJabberImpl) {
              ((OutgoingFileTransferJabberImpl) fileTransfer).removeThumbnailRequestListener();
            }

            // sometimes a filetransfer can be preparing
            // and than completed :
            // transfered in one iteration of current thread
            // so it won't go through intermediate state - inProgress
            // make sure this won't happen
            if (status == FileTransferStatusChangeEvent.COMPLETED
                && fileTransfer.getStatus() == FileTransferStatusChangeEvent.PREPARING) {
              fileTransfer.fireStatusChangeEvent(
                  FileTransferStatusChangeEvent.IN_PROGRESS, "Status changed");
              fileTransfer.fireProgressChangeEvent(System.currentTimeMillis(), progress);
            }

            break;
          }

          fileTransfer.fireStatusChangeEvent(status, "Status changed");
          fileTransfer.fireProgressChangeEvent(System.currentTimeMillis(), progress);
        } catch (InterruptedException e) {
          if (logger.isDebugEnabled()) logger.debug("Unable to sleep thread.", e);
        }
      }

      if (jabberTransfer.getError() != null) {
        logger.error(
            "An error occured while transfering file: " + jabberTransfer.getError().getMessage());
      }

      if (jabberTransfer.getException() != null) {
        logger.error(
            "An exception occured while transfering file: ", jabberTransfer.getException());

        if (jabberTransfer.getException() instanceof XMPPException) {
          XMPPError error = ((XMPPException) jabberTransfer.getException()).getXMPPError();
          if (error != null)
            if (error.getCode() == 406 || error.getCode() == 403)
              status = FileTransferStatusChangeEvent.REFUSED;
        }

        statusReason = jabberTransfer.getException().getMessage();
      }

      if (initialFileSize > 0
          && status == FileTransferStatusChangeEvent.COMPLETED
          && fileTransfer.getTransferedBytes() < initialFileSize) {
        status = FileTransferStatusChangeEvent.CANCELED;
      }

      fileTransfer.fireStatusChangeEvent(status, statusReason);
      fileTransfer.fireProgressChangeEvent(System.currentTimeMillis(), progress);
    }