/** @param error null if connection is closed by user */
 void logoutImpl(Exception error) {
   synchronized (this) {
     if (isLoggedIn()) {
       loggedIn.set(false);
       LOG.infof(
           "disconnecting from {0} at {1}:{2} ...",
           connection.getServiceName(), connection.getHost(), connection.getPort());
       connection.disconnect();
       synchronized (friends) {
         friends.clear();
       }
       XMPPConnection.removeConnectionCreationListener(smackConnectionListener);
       connection = null;
       LOG.info("disconnected.");
       connectionMulticaster.broadcast(
           new FriendConnectionEvent(
               XMPPFriendConnectionImpl.this, FriendConnectionEvent.Type.DISCONNECTED, error));
       ChatStateManager.remove(connection);
       if (discoInfoListener != null) {
         discoInfoListener.cleanup();
       }
       if (noSaveFeatureInitializer != null) {
         noSaveFeatureInitializer.cleanup();
       }
       if (idleStatusMonitor != null) {
         idleStatusMonitor.stop();
       }
       if (xmppActivityListener != null) {
         xmppActivitySupport.removeListener(xmppActivityListener);
       }
       featureRegistry.deregisterInitializer(NoSaveFeature.ID);
     }
   }
 }
Ejemplo n.º 2
0
  /*
   * (non-Javadoc)
   *
   * @see
   * fr.univartois.ili.fsnet.commons.talk.ITalk#initConnexion(java.lang.String
   * , int, java.lang.String, java.lang.String, java.util.Map)
   */
  @Override
  public void initConnexion(
      String xmppServer, int port, String login, String pssword, Map<String, String> map)
      throws TalkException {
    config = new ConnectionConfiguration(xmppServer, port);

    connection = new XMPPConnection(config);
    try {
      if (!connection.isConnected()) {
        connection.connect();
      }

      accountManager = connection.getAccountManager();
      connection.login(login, pssword);

    } catch (XMPPException e) {
      if ((e.getLocalizedMessage().contains("authentication failed")
              || e.getLocalizedMessage().contains("SASL authentication"))
          && accountManager.supportsAccountCreation()) {
        createAccount(login, pssword, map);

      } else {
        Logger.getAnonymousLogger().log(Level.SEVERE, "", e);
      }
    }

    /* It is only at that moment where the Listener is correctly initialized */
    ChatStateManager.getInstance(connection);
  }
  /** Initialize the features provided by beem. */
  private void initFeatures() {
    ServiceDiscoveryManager.setIdentityName("Beem");
    ServiceDiscoveryManager.setIdentityType("phone");
    ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(mAdaptee);
    if (sdm == null) sdm = new ServiceDiscoveryManager(mAdaptee);

    sdm.addFeature("http://jabber.org/protocol/disco#info");
    // nikita: must be uncommented when the feature will be enabled
    // sdm.addFeature("jabber:iq:privacy");
    sdm.addFeature("http://jabber.org/protocol/caps");
    sdm.addFeature("urn:xmpp:avatar:metadata");
    sdm.addFeature("urn:xmpp:avatar:metadata+notify");
    sdm.addFeature("urn:xmpp:avatar:data");
    sdm.addFeature("http://jabber.org/protocol/nick");
    sdm.addFeature("http://jabber.org/protocol/nick+notify");
    sdm.addFeature(PingExtension.NAMESPACE);

    mChatStateManager = ChatStateManager.getInstance(mAdaptee);
    BeemCapsManager caps = new BeemCapsManager(sdm, mAdaptee, mService);
    caps.setNode("http://www.beem-project.com");
  }
    @Override
    public void connectionCreated(XMPPConnection connection) {
      if (XMPPFriendConnectionImpl.this.connection != connection) {
        return;
      }

      if (LOG.isDebugEnabled()) {
        LOG.debug("connection created for " + connection.toString());
      }
      connection.addConnectionListener(this);

      synchronized (ProviderManager.getInstance()) {
        if (ProviderManager.getInstance().getIQProvider("address", "jabber:iq:lw-address")
            == null) {
          ProviderManager.getInstance()
              .addIQProvider(
                  "address", "jabber:iq:lw-address", new AddressIQProvider(addressFactory));
        }
        if (ProviderManager.getInstance()
                .getIQProvider("file-transfer", "jabber:iq:lw-file-transfer")
            == null) {
          ProviderManager.getInstance()
              .addIQProvider(
                  "file-transfer", "jabber:iq:lw-file-transfer", FileTransferIQ.getIQProvider());
        }
        if (ProviderManager.getInstance().getIQProvider("auth-token", "jabber:iq:lw-auth-token")
            == null) {
          ProviderManager.getInstance()
              .addIQProvider("auth-token", "jabber:iq:lw-auth-token", new AuthTokenIQProvider());
        }
        if (ProviderManager.getInstance()
                .getIQProvider("library-changed", "jabber:iq:lw-lib-change")
            == null) {
          ProviderManager.getInstance()
              .addIQProvider(
                  "library-changed", "jabber:iq:lw-lib-change", LibraryChangedIQ.getIQProvider());
        }
        if (ProviderManager.getInstance()
                .getIQProvider(ConnectBackRequestIQ.ELEMENT_NAME, ConnectBackRequestIQ.NAME_SPACE)
            == null) {
          ProviderManager.getInstance()
              .addIQProvider(
                  ConnectBackRequestIQ.ELEMENT_NAME,
                  ConnectBackRequestIQ.NAME_SPACE,
                  new ConnectBackRequestIQProvider());
        }
        if (ProviderManager.getInstance().getIQProvider(NoSaveIQ.ELEMENT_NAME, NoSaveIQ.NAME_SPACE)
            == null) {
          ProviderManager.getInstance()
              .addIQProvider(NoSaveIQ.ELEMENT_NAME, NoSaveIQ.NAME_SPACE, NoSaveIQ.getIQProvider());
        }
      }

      ChatStateManager.getInstance(connection);

      discoInfoListener =
          new DiscoInfoListener(XMPPFriendConnectionImpl.this, connection, featureRegistry);
      discoInfoListener.addListeners(connectionMulticaster, friendPresenceSupport);

      addressIQListener =
          addressIQListenerFactory.create(XMPPFriendConnectionImpl.this, addressFactory);
      connection.addPacketListener(addressIQListener, addressIQListener.getPacketFilter());

      fileTransferIQListener = fileTransferIQListenerFactory.create(XMPPFriendConnectionImpl.this);
      connection.addPacketListener(
          fileTransferIQListener, fileTransferIQListener.getPacketFilter());

      authTokenIQListener = authTokenIQListenerFactory.create(XMPPFriendConnectionImpl.this);
      connection.addPacketListener(authTokenIQListener, authTokenIQListener.getPacketFilter());

      libraryChangedIQListener =
          libraryChangedIQListenerFactory.create(XMPPFriendConnectionImpl.this);
      connection.addPacketListener(
          libraryChangedIQListener, libraryChangedIQListener.getPacketFilter());

      connectRequestIQListener =
          connectBackRequestIQListenerFactory.create(XMPPFriendConnectionImpl.this);
      connection.addPacketListener(
          connectRequestIQListener, connectRequestIQListener.getPacketFilter());

      new LimewireFeatureInitializer().register(featureRegistry);

      noSaveFeatureInitializer =
          new NoSaveFeatureInitializer(
              connection, XMPPFriendConnectionImpl.this, rosterListeners, friendPresenceSupport);
      noSaveFeatureInitializer.register(featureRegistry);

      SubscriptionListener sub = new SubscriptionListener(connection, friendRequestBroadcaster);
      connection.addPacketListener(sub, sub);

      for (URI feature : featureRegistry.getPublicFeatureUris()) {
        ServiceDiscoveryManager.getInstanceFor(connection).addFeature(feature.toASCIIString());
      }
      if (xmppActivityListener == null) {
        xmppActivityListener = new XmppActivityEventListener();
      }
      xmppActivitySupport.addListener(xmppActivityListener);

      if (idleStatusMonitor == null) {
        idleStatusMonitor = idleStatusMonitorFactory.create();
      }
      idleStatusMonitor.start();
    }