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()); }
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()); }
@Override public void onClose() { ArrayList<ConnectionThread> connections = new ArrayList<ConnectionThread>(managedConnections); managedConnections.clear(); for (ConnectionThread connectionThread : connections) connectionThread.getConnectionItem().disconnect(connectionThread); }
/** * 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); } }
@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(); } } }
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); }
public void onConnected(ConnectionThread connectionThread) { if (!managedConnections.contains(connectionThread)) return; for (OnConnectedListener listener : Application.getInstance().getManagers(OnConnectedListener.class)) listener.onConnected(connectionThread.getConnectionItem()); }
public void onConnection(ConnectionThread connectionThread) { managedConnections.add(connectionThread); for (OnConnectionListener listener : Application.getInstance().getManagers(OnConnectionListener.class)) listener.onConnection(connectionThread.getConnectionItem()); }