private <R extends OAStanza> R sendOAStanza(XMPPTCPConnection authConnection, OAStanza stanza, Class<R> replyClass, long replyTimeout) { StanzaCollector collector = authConnection.createStanzaCollector(new OAReplyFilter(stanza, authConnection)); messageLock.lock(); try { authConnection.sendStanza(stanza); return replyClass.cast(getNextStanzaSkipContinues(collector, replyTimeout, authConnection)); } catch (InterruptedException | SmackException | XMPPErrorException e) { throw new RuntimeException("Failed communicating with Harmony Hub", e); } finally { messageLock.unlock(); collector.cancel(); } }
private Stanza sendOAStanza(XMPPTCPConnection authConnection, OAStanza stanza, long replyTimeout) { StanzaCollector collector = authConnection .createStanzaCollector(new EmptyIncrementedIdReplyFilter(stanza, authConnection)); messageLock.lock(); try { authConnection.sendStanza(stanza); return getNextStanzaSkipContinues(collector, replyTimeout, authConnection); } catch (InterruptedException | SmackException | XMPPErrorException e) { throw new RuntimeException("Failed communicating with Harmony Hub", e); } finally { messageLock.unlock(); collector.cancel(); } }
@Override public void sendStanza(Stanza stanza) throws NotConnectedException, InterruptedException { if (stanza.getError() == null || stanza.getError().getCondition() != Condition.service_unavailable) { super.sendStanza(stanza); } }
/** * 拒绝好友申请 * * @param userId 用户id */ public void refuse(String userId) { Presence presence = new Presence(Presence.Type.unsubscribe); presence.setTo(userId); try { connection.sendStanza(presence); } catch (SmackException.NotConnectedException e) { e.printStackTrace(); } }
/** * 接收好友申请 * * @param userId 用户id */ public void accept(String userId) { Presence presence = new Presence(Presence.Type.subscribe); presence.setTo(userId); try { connection.sendStanza(presence); } catch (SmackException.NotConnectedException e) { e.printStackTrace(); } }
/** * 一上线获取离线消息 * 设置登录状态为在线 */ private void getOfflineMessage() { OfflineMessageManager offlineManager = new OfflineMessageManager(connection); try { List<Message> list = offlineManager.getMessages(); for (Message message : list) { message.setFrom(message.getFrom().split("/")[0]); JSONObject object = new JSONObject(message.getBody()); String type = object.getString("type"); String data = object.getString("data"); //保存离线信息 dbHelper.insertOneMsg(user.getUser_id(), message.getFrom(), data, System.currentTimeMillis() + "", message.getFrom(), 2); } //删除离线消息 offlineManager.deleteMessages(); //将状态设置成在线 Presence presence = new Presence(Presence.Type.available); connection.sendStanza(presence); } catch (Exception e) { e.printStackTrace(); } }
/** * Sends an ACK to FCM with back off strategy * * @param jsonRequest */ public void sendAck(String jsonRequest) { logger.info("Sending ack."); final Stanza packet = new FcmPacketExtension(jsonRequest).toPacket(); final BackOffStrategy backoff = new BackOffStrategy(); while (backoff.shouldRetry()) { try { xmppConn.sendStanza(packet); backoff.doNotRetry(); } catch (NotConnectedException | InterruptedException e) { logger.info("The packet could not be sent due to a connection problem. Backing off the packet: {}", packet.toXML(null)); backoff.errorOccured(); } } }
/** * Sends a downstream message to FCM with back off strategy */ private void sendDownstreamMessageInternal(String messageId, String jsonRequest) { final Stanza request = new FcmPacketExtension(jsonRequest).toPacket(); final BackOffStrategy backoff = new BackOffStrategy(); while (backoff.shouldRetry()) { try { xmppConn.sendStanza(request); backoff.doNotRetry(); } catch (NotConnectedException | InterruptedException e) { logger.info("The packet could not be sent due to a connection problem. Backing off the packet: {}", request.toXML(null)); try { backoff.errorOccured2(); } catch (Exception e2) { // all the attempts failed removeMessageFromSyncMessages(messageId); pendingMessages.put(messageId, Message.from(jsonRequest)); } } } }