public void onAuthorized(ConnectionThread connectionThread) {
   if (!managedConnections.contains(connectionThread)) return;
   LogManager.i(this, "onAuthorized: " + connectionThread.getConnectionItem());
   for (OnAuthorizedListener listener :
       Application.getInstance().getManagers(OnAuthorizedListener.class))
     listener.onAuthorized(connectionThread.getConnectionItem());
 }
 /**
  * Connect or disconnect from server depending on internal flags.
  *
  * @param userRequest action was requested by user.
  * @return Whether state has been changed.
  */
 public boolean updateConnection(boolean userRequest) {
   boolean available = isConnectionAvailable(userRequest);
   if (NetworkManager.getInstance().getState() != NetworkState.available
       || !available
       || disconnectionRequested) {
     ConnectionState target = available ? ConnectionState.waiting : ConnectionState.offline;
     if (state == ConnectionState.connected
         || state == ConnectionState.authentication
         || state == ConnectionState.connecting) {
       if (userRequest) connectionRequest = false;
       if (connectionThread != null) {
         disconnect(connectionThread);
         // Force remove managed connection thread.
         onClose(connectionThread);
         connectionThread = null;
       }
     } else if (state == target) {
       return false;
     }
     state = target;
     return true;
   } else {
     if (state == ConnectionState.offline || state == ConnectionState.waiting) {
       if (userRequest) connectionRequest = true;
       state = ConnectionState.connecting;
       connectionThread = new ConnectionThread(this);
       if (connectionSettings.isCustom())
         connectionThread.start(connectionSettings.getHost(), connectionSettings.getPort(), false);
       else connectionThread.start(connectionSettings.getServerName(), 5222, true);
       return true;
     } else {
       return false;
     }
   }
 }
 @Override
 public void onClose() {
   ArrayList<ConnectionThread> connections = new ArrayList<ConnectionThread>(managedConnections);
   managedConnections.clear();
   for (ConnectionThread connectionThread : connections)
     connectionThread.getConnectionItem().disconnect(connectionThread);
 }
 /**
  * Returns real full jid, that was assigned while login.
  *
  * @return <code>null</code> if connection is not established.
  */
 public String getRealJid() {
   ConnectionThread connectionThread = getConnectionThread();
   if (connectionThread == null) return null;
   XMPPConnection xmppConnection = connectionThread.getXMPPConnection();
   if (xmppConnection == null) return null;
   String user = xmppConnection.getUser();
   if (user == null) return null;
   return user;
 }
 // Begin BT connection thread
 private void beginConnection() {
   Log.i(TAG, "BonitorActivity - beginConnection()");
   int element = getIntent().getIntExtra("DEVICE", -1);
   Log.v(TAG, "BT element = " + element);
   // Begin Bluetooth Connection
   if (connectionThread != null) {
     connectionThread.cancel();
   }
   connectionThread = new ConnectionThread(bt.getElement(element));
   connectionThread.start();
 }
 public void onDisconnect(ConnectionThread connectionThread) {
   if (!managedConnections.remove(connectionThread)) return;
   ConnectionItem connectionItem = connectionThread.getConnectionItem();
   if (connectionItem instanceof AccountItem) {
     String account = ((AccountItem) connectionItem).getAccount();
     for (Entry<String, RequestHolder> entry : requests.getNested(account).entrySet())
       entry.getValue().getListener().onDisconnect(account, entry.getKey());
     requests.clear(account);
   }
   for (OnDisconnectListener listener :
       Application.getInstance().getManagers(OnDisconnectListener.class))
     listener.onDisconnect(connectionThread.getConnectionItem());
 }
示例#7
0
  /**
   * Start the ConnectionThread to initiate a connection to a remote device.
   *
   * @param address The address of the remote device to connect.
   * @throws IllegalArgumentException If address is not a valid bluetooth mac address.
   */
  public synchronized void connect(final String address) {
    if (!BluetoothAdapter.checkBluetoothAddress(address)) {
      throw new IllegalArgumentException("Address: " + address + " is not valid");
    }

    if (mBtAdapter == null) {
      Log.e(TAG, "Can't connect because bluetooth is not supported by this platform");
      return;
    }

    // Cancel current connection or reconnection
    mStopped = true;
    cancelTimer();
    cancelThreads();

    // Convert mac address in BluetoothDevice
    BluetoothDevice device = mBtAdapter.getRemoteDevice(address);
    Log.d(TAG, "will connect to: " + device);

    // Keep address
    mDeviceAddress = address;

    // Start the thread to connect with the given device
    mStopped = false;
    mConnectionThread = new ConnectionThread(device);
    mConnectionThread.start();

    setState(STATE_CONNECTING, true);
  }
 /**
  * Called when disconnect should occur.
  *
  * @param connectionThread
  * @return <code>true</code> if connection thread was managed.
  */
 private boolean onDisconnect(ConnectionThread connectionThread) {
   XMPPConnection xmppConnection = connectionThread.getXMPPConnection();
   boolean acceptable = isManaged(connectionThread);
   if (xmppConnection == null) LogManager.i(this, "onClose " + acceptable);
   else
     LogManager.i(
         this,
         "onClose "
             + xmppConnection.hashCode()
             + " - "
             + xmppConnection.connectionCounterValue
             + ", "
             + acceptable);
   ConnectionManager.getInstance().onDisconnect(connectionThread);
   if (acceptable) connectionThread.shutdown();
   return acceptable;
 }
示例#9
0
  private void cancelThreads() {
    // Cancel any thread attempting to make a connection
    if (mConnectionThread != null) {
      mConnectionThread.cancel();
      mConnectionThread = null;
    }

    // Cancel any thread currently running a connection
    if (mConnectedThread != null) {
      mConnectedThread.cancel();
      mConnectedThread = null;
    }
  }
 @Override
 public void onTimer() {
   if (NetworkManager.getInstance().getState() != NetworkState.suspended) {
     Collection<ConnectionItem> reconnect = new ArrayList<ConnectionItem>();
     for (ConnectionThread connectionThread : managedConnections)
       if (connectionThread.getConnectionItem().getState().isConnected()
           // XMPPConnection can`t be null here
           && !connectionThread.getXMPPConnection().isAlive()) {
         LogManager.i(connectionThread.getConnectionItem(), "forceReconnect on checkAlive");
         reconnect.add(connectionThread.getConnectionItem());
       }
     for (ConnectionItem connection : reconnect) connection.forceReconnect();
   }
   long now = new Date().getTime();
   Iterator<NestedMap.Entry<RequestHolder>> iterator = requests.iterator();
   while (iterator.hasNext()) {
     NestedMap.Entry<RequestHolder> entry = iterator.next();
     if (entry.getValue().isExpired(now)) {
       entry.getValue().getListener().onTimeout(entry.getFirst(), entry.getSecond());
       iterator.remove();
     }
   }
 }
 // Back button pressed
 @Override
 public void onBackPressed() {
   Log.e(TAG, "BonitorActivity - onBackPressed()");
   if (connectionThread != null) {
     connectionThread.cancel();
   }
   if (communicationThread != null) {
     communicationThread.cancel();
   }
   if (fileWriter != null) {
     try {
       fileWriter.close();
     } catch (IOException e) {
       e.printStackTrace();
     }
   }
   finish();
 }
 public void processPacket(ConnectionThread connectionThread, Packet packet) {
   if (!managedConnections.contains(connectionThread)) return;
   ConnectionItem connectionItem = connectionThread.getConnectionItem();
   if (packet instanceof IQ && connectionItem instanceof AccountItem) {
     IQ iq = (IQ) packet;
     String packetId = iq.getPacketID();
     if (packetId != null && (iq.getType() == Type.RESULT || iq.getType() == Type.ERROR)) {
       String account = ((AccountItem) connectionItem).getAccount();
       RequestHolder requestHolder = requests.remove(account, packetId);
       if (requestHolder != null) {
         if (iq.getType() == Type.RESULT)
           requestHolder.getListener().onReceived(account, packetId, iq);
         else requestHolder.getListener().onError(account, packetId, iq);
       }
     }
   }
   for (OnPacketListener listener : Application.getInstance().getManagers(OnPacketListener.class))
     listener.onPacket(connectionItem, Jid.getBareAddress(packet.getFrom()), packet);
 }
 // onDestroy
 @Override
 protected void onDestroy() {
   Log.e(TAG, "BonitorActivity - onDestroy()");
   super.onDestroy();
   if (connectionThread != null) {
     connectionThread.cancel();
   }
   if (communicationThread != null) {
     communicationThread.cancel();
   }
   if (fileWriter != null) {
     try {
       fileWriter.close();
     } catch (IOException e) {
       e.printStackTrace();
     }
   }
   finish();
 }
 /**
  * Send packet to authenticated connection.
  *
  * @param account
  * @param packet
  */
 public void sendPacket(String account, Packet packet) throws NetworkException {
   ConnectionThread connectionThread = null;
   for (ConnectionThread check : managedConnections)
     if (check.getConnectionItem() instanceof AccountItem
         && ((AccountItem) check.getConnectionItem()).getAccount().equals(account)) {
       connectionThread = check;
       break;
     }
   if (connectionThread == null || !connectionThread.getConnectionItem().getState().isConnected())
     throw new NetworkException(R.string.NOT_CONNECTED);
   XMPPConnection xmppConnection = connectionThread.getXMPPConnection();
   try {
     xmppConnection.sendPacket(packet);
   } catch (IllegalStateException e) {
     throw new NetworkException(R.string.XMPP_EXCEPTION);
   }
 }
示例#15
0
 private void updateRoles() {
   serverAttacker.changeConnection(connToAttacker);
   serverDefender.changeConnection(connToDefender);
 }
 public void onConnection(ConnectionThread connectionThread) {
   managedConnections.add(connectionThread);
   for (OnConnectionListener listener :
       Application.getInstance().getManagers(OnConnectionListener.class))
     listener.onConnection(connectionThread.getConnectionItem());
 }
 public void onConnected(ConnectionThread connectionThread) {
   if (!managedConnections.contains(connectionThread)) return;
   for (OnConnectedListener listener :
       Application.getInstance().getManagers(OnConnectedListener.class))
     listener.onConnected(connectionThread.getConnectionItem());
 }