/** * Will a user recieve a message from another after only sending the user a directed presence, * or will Wildfire intercept for offline storage? * * User1 becomes lines. User0 never sent an available presence to the server but * instead sent one to User1. User1 sends a message to User0. Should User0 get the * message? */ public void testDirectPresence() { getConnection(1).sendStanza(new Presence(Presence.Type.available)); Presence presence = new Presence(Presence.Type.available); presence.setTo(getBareJID(1)); getConnection(0).sendStanza(presence); PacketCollector collector = getConnection(0) .createPacketCollector(new MessageTypeFilter(Message.Type.chat)); try { getConnection(1).getChatManager().createChat(getBareJID(0), null).sendMessage("Test 1"); } catch (XMPPException e) { e.printStackTrace(); fail(e.getMessage()); } Message message = (Message) collector.nextResult(2500); assertNotNull("Message not recieved from remote user", message); }
/** * Initialize PacketListener for a given {@link org.jivesoftware.smack.XMPPConnection} * and a Command prefix * * @param prefix the command prefix used to filter message * @param connection the connection on which PacketListener will be registered */ private void initListeners(final String prefix, final XMPPConnection connection){ StanzaFilter filter = new AndFilter( new OrFilter(MessageTypeFilter.GROUPCHAT,MessageTypeFilter.CHAT), new StanzaFilter() { @Override public boolean accept(Stanza stanza) { return stanza instanceof Message && ((Message) stanza).getBody().startsWith(prefix); } } ); XmppMessageListener commandListener = new XmppMessageListener(this,listeners); connection.addAsyncStanzaListener(commandListener,filter); MultiUserChatManager.getInstanceFor(connection).addInvitationListener(new InvitationListener(this,listeners)); }
/** * Will a user recieve a message from another after only sending the user a directed presence, * or will Wildfire intercept for offline storage? * * User1 becomes lines. User0 never sent an available presence to the server but * instead sent one to User1. User1 sends a message to User0. Should User0 get the * message? */ public void testDirectPresence() { getConnection(1).sendPacket(new Presence(Presence.Type.available)); Presence presence = new Presence(Presence.Type.available); presence.setTo(getBareJID(1)); getConnection(0).sendPacket(presence); PacketCollector collector = getConnection(0) .createPacketCollector(new MessageTypeFilter(Message.Type.chat)); try { getConnection(1).getChatManager().createChat(getBareJID(0), null).sendMessage("Test 1"); } catch (XMPPException e) { e.printStackTrace(); fail(e.getMessage()); } Message message = (Message) collector.nextResult(2500); assertNotNull("Message not recieved from remote user", message); }
private void initChatManager() { notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); XMPPConnection conn = XmppConnectionManager.getInstance() .getConnection(); conn.addPacketListener(pListener, new MessageTypeFilter( Message.Type.chat)); }
private void initSysTemMsgManager() { initSoundPool(); XMPPConnection con = XmppConnectionManager.getInstance() .getConnection(); con.addPacketListener(pListener, new MessageTypeFilter( Message.Type.normal)); }
private void init() { //数据业务类 mIMMsgDao = new IMMsgDao(context); XMPPConnection conn = XmppConnectionManager.getInstance() .getConnection(); conn.addPacketListener(pListener, new MessageTypeFilter( Message.Type.chat)); }
private void init() { //数据业务类 mIMMsgDao = new IMMsgDao(context); XMPPConnection con = XmppConnectionManager.getInstance() .getConnection(); con.addPacketListener(pListener, new MessageTypeFilter( Message.Type.normal)); }
private void setMessageListener(final Connection connection) { mPacketListener = new PacketListener() { public void processPacket(Packet packet) { try { Message mes = (Message) packet; Log.i("got personal message", "xml: " + mes.toXML()); logs.info("Got personal xmpp message: " + mes.toXML()); parseXml(mes.toXML()); // check if event is in acceptable distance and if so show it Location location = LocationService.locationListener.getLastLocation(false); Long radius = VICTIM_DATA.getRadius(); if (location == null || radius == 0 || location.distanceTo(VICTIM_DATA.getLocation()) <= radius) { if (!processing_event && !EventManager.getInstance(XmppService.this).isEventActive()) { openAcceptEventScreen(); processing_event = true; } } } catch (Exception e) {} } }; connection.addPacketListener(mPacketListener, new MessageTypeFilter(Message.Type.chat)); }
/** * Check that when a client becomes unavailable all messages sent to the client are stored offline. So that when * the client becomes available again the offline messages are received. */ public void testOfflineMessage() { getConnection(0).sendPacket(new Presence(Presence.Type.available)); getConnection(1).sendPacket(new Presence(Presence.Type.available)); // Make user2 unavailable getConnection(1).sendPacket(new Presence(Presence.Type.unavailable)); try { Thread.sleep(500); // User1 sends some messages to User2 which is not available at the moment Chat chat = getConnection(0).getChatManager().createChat(getBareJID(1), null); PacketCollector collector = getConnection(1).createPacketCollector( new MessageTypeFilter(Message.Type.chat)); chat.sendMessage("Test 1"); chat.sendMessage("Test 2"); Thread.sleep(500); // User2 becomes available again getConnection(1).sendPacket(new Presence(Presence.Type.available)); // Check that offline messages are retrieved by user2 which is now available Message message = (Message) collector.nextResult(2500); assertNotNull(message); message = (Message) collector.nextResult(2000); assertNotNull(message); message = (Message) collector.nextResult(1000); assertNull(message); } catch (Exception e) { e.printStackTrace(); fail(e.getMessage()); } }
/** * Check that two clients are able to send messages with a body of 4K characters and their * connections are not being closed. */ public void testHugeMessage() { getConnection(0).sendPacket(new Presence(Presence.Type.available)); getConnection(1).sendPacket(new Presence(Presence.Type.available)); // User2 becomes available again PacketCollector collector = getConnection(1).createPacketCollector( new MessageTypeFilter(Message.Type.chat)); // Create message with a body of 4K characters Message msg = new Message(getFullJID(1), Message.Type.chat); StringBuilder sb = new StringBuilder(5000); for (int i = 0; i <= 4000; i++) { sb.append("X"); } msg.setBody(sb.toString()); // Send the first message getConnection(0).sendPacket(msg); // Check that the connection that sent the message is still connected assertTrue("Connection was closed", getConnection(0).isConnected()); // Check that the message was received Message rcv = (Message) collector.nextResult(1000); assertNotNull("No Message was received", rcv); // Send the second message getConnection(0).sendPacket(msg); // Check that the connection that sent the message is still connected assertTrue("Connection was closed", getConnection(0).isConnected()); // Check that the second message was received rcv = (Message) collector.nextResult(1000); assertNotNull("No Message was received", rcv); }
/** * Ensures that sending and receiving of packets is ok. */ public void testSending() throws XMPPException { PacketCollector collector1 = getConnection(1).createPacketCollector(new MessageTypeFilter(Message.Type.normal)); PacketCollector collector2 = getConnection(2).createPacketCollector(new MessageTypeFilter(Message.Type.normal)); PacketCollector collector3 = getConnection(3).createPacketCollector(new MessageTypeFilter(Message.Type.normal)); Message message = new Message(); message.setBody("Hola"); List<String> to = Arrays.asList(new String[]{getBareJID(1)}); List<String> cc = Arrays.asList(new String[]{getBareJID(2)}); List<String> bcc = Arrays.asList(new String[]{getBareJID(3)}); MultipleRecipientManager.send(getConnection(0), message, to, cc, bcc); Packet message1 = collector1.nextResult(SmackConfiguration.getPacketReplyTimeout()); assertNotNull("XMPPConnection 1 never received the message", message1); MultipleRecipientInfo info1 = MultipleRecipientManager.getMultipleRecipientInfo(message1); assertNotNull("Message 1 does not contain MultipleRecipientInfo", info1); assertFalse("Message 1 should be 'replyable'", info1.shouldNotReply()); List<?> addresses1 = info1.getTOAddresses(); assertEquals("Incorrect number of TO addresses", 1, addresses1.size()); String address1 = ((MultipleAddresses.Address) addresses1.get(0)).getJid(); assertEquals("Incorrect TO address", getBareJID(1), address1); addresses1 = info1.getCCAddresses(); assertEquals("Incorrect number of CC addresses", 1, addresses1.size()); address1 = ((MultipleAddresses.Address) addresses1.get(0)).getJid(); assertEquals("Incorrect CC address", getBareJID(2), address1); Packet message2 = collector2.nextResult(SmackConfiguration.getPacketReplyTimeout()); assertNotNull("XMPPConnection 2 never received the message", message2); MultipleRecipientInfo info2 = MultipleRecipientManager.getMultipleRecipientInfo(message2); assertNotNull("Message 2 does not contain MultipleRecipientInfo", info2); assertFalse("Message 2 should be 'replyable'", info2.shouldNotReply()); List<MultipleAddresses.Address> addresses2 = info2.getTOAddresses(); assertEquals("Incorrect number of TO addresses", 1, addresses2.size()); String address2 = ((MultipleAddresses.Address) addresses2.get(0)).getJid(); assertEquals("Incorrect TO address", getBareJID(1), address2); addresses2 = info2.getCCAddresses(); assertEquals("Incorrect number of CC addresses", 1, addresses2.size()); address2 = ((MultipleAddresses.Address) addresses2.get(0)).getJid(); assertEquals("Incorrect CC address", getBareJID(2), address2); Packet message3 = collector3.nextResult(SmackConfiguration.getPacketReplyTimeout()); assertNotNull("XMPPConnection 3 never received the message", message3); MultipleRecipientInfo info3 = MultipleRecipientManager.getMultipleRecipientInfo(message3); assertNotNull("Message 3 does not contain MultipleRecipientInfo", info3); assertFalse("Message 3 should be 'replyable'", info3.shouldNotReply()); List<MultipleAddresses.Address> addresses3 = info3.getTOAddresses(); assertEquals("Incorrect number of TO addresses", 1, addresses3.size()); String address3 = ((MultipleAddresses.Address) addresses3.get(0)).getJid(); assertEquals("Incorrect TO address", getBareJID(1), address3); addresses3 = info3.getCCAddresses(); assertEquals("Incorrect number of CC addresses", 1, addresses3.size()); address3 = ((MultipleAddresses.Address) addresses3.get(0)).getJid(); assertEquals("Incorrect CC address", getBareJID(2), address3); collector1.cancel(); collector2.cancel(); collector3.cancel(); }
@Override protected void doStart() throws Exception { try { connection = endpoint.createConnection(); } catch (SmackException e) { if (endpoint.isTestConnectionOnStartup()) { throw new RuntimeException("Could not connect to XMPP server.", e); } else { LOG.warn(e.getMessage()); if (getExceptionHandler() != null) { getExceptionHandler().handleException(e.getMessage(), e); } scheduleDelayedStart(); return; } } chatManager = ChatManager.getInstanceFor(connection); chatManager.addChatListener(this); OrFilter pubsubPacketFilter = new OrFilter(); if (endpoint.isPubsub()) { //xep-0060: pubsub#notification_type can be 'headline' or 'normal' pubsubPacketFilter.addFilter(new MessageTypeFilter(Type.headline)); pubsubPacketFilter.addFilter(new MessageTypeFilter(Type.normal)); connection.addPacketListener(this, pubsubPacketFilter); } if (endpoint.getRoom() == null) { privateChat = chatManager.getThreadChat(endpoint.getChatId()); if (privateChat != null) { if (LOG.isDebugEnabled()) { LOG.debug("Adding listener to existing chat opened to " + privateChat.getParticipant()); } privateChat.addMessageListener(this); } else { privateChat = ChatManager.getInstanceFor(connection).createChat(endpoint.getParticipant(), endpoint.getChatId(), this); if (LOG.isDebugEnabled()) { LOG.debug("Opening private chat to " + privateChat.getParticipant()); } } } else { // add the presence packet listener to the connection so we only get packets that concerns us // we must add the listener before creating the muc final AndFilter packetFilter = new AndFilter(new PacketTypeFilter(Presence.class)); connection.addPacketListener(this, packetFilter); muc = new MultiUserChat(connection, endpoint.resolveRoom(connection)); muc.addMessageListener(this); DiscussionHistory history = new DiscussionHistory(); history.setMaxChars(0); // we do not want any historical messages muc.join(endpoint.getNickname(), null, history, SmackConfiguration.getDefaultPacketReplyTimeout()); if (LOG.isInfoEnabled()) { LOG.info("Joined room: {} as: {}", muc.getRoom(), endpoint.getNickname()); } } this.startRobustConnectionMonitor(); super.doStart(); }
@Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub if (null != con && con.isConnected()) { PacketFilter filter = new AndFilter(new PacketTypeFilter( Presence.class), new MessageTypeFilter(Message.Type.chat)); PacketListener listener = new PacketListener() { @Override public void processPacket(Packet packet) { SLog.i("Presence", "PresenceService------" + packet.toXML()); // 看API可知�?Presence是Packet的子�? if (packet instanceof Presence) { Presence presence = (Presence) packet; // Presence还有很多方法,可查看API String from = presence.getFrom();// 发�?�? String to = presence.getTo();// 接收�? // Presence.Type�?中状�? if (presence.getType().equals(Presence.Type.subscribe)) {// 好友申请 } else if (presence.getType().equals( Presence.Type.subscribed)) {// 同意添加好友 } else if (presence.getType().equals( Presence.Type.unsubscribe)) {// 拒绝添加好友 �?删除好友 } else if (presence.getType().equals( Presence.Type.unsubscribed)) {// 这个我没用到 } else if (presence.getType().equals( Presence.Type.unavailable)) {// 好友下线 // 要更新好友列表,可以在这收到包后,发广播到指定页�? // 更新列表 } else if (presence.getType().equals( Presence.Type.available)) {// 好友上线 } } else if (packet instanceof Message) { Message msg = (Message) packet; Toast.makeText(getApplicationContext(), msg.getFrom() + " 说:" + msg.getBody(), Toast.LENGTH_SHORT).show(); } } }; con.addPacketListener(listener, filter); } return super.onStartCommand(intent, flags, startId); }
@Override public void newConnection(XMPPConnection connection) { connection.addAsyncStanzaListener(mChatPacketListener, MessageTypeFilter.NORMAL_OR_CHAT); }
/** * Ensures that sending and receiving of packets is ok. */ public void testSending() throws XMPPException { PacketCollector collector1 = getConnection(1).createPacketCollector(new MessageTypeFilter(Message.Type.normal)); PacketCollector collector2 = getConnection(2).createPacketCollector(new MessageTypeFilter(Message.Type.normal)); PacketCollector collector3 = getConnection(3).createPacketCollector(new MessageTypeFilter(Message.Type.normal)); Message message = new Message(); message.setBody("Hola"); List<String> to = Arrays.asList(new String[]{getBareJID(1)}); List<String> cc = Arrays.asList(new String[]{getBareJID(2)}); List<String> bcc = Arrays.asList(new String[]{getBareJID(3)}); MultipleRecipientManager.send(getConnection(0), message, to, cc, bcc); Packet message1 = collector1.nextResult(SmackConfiguration.getPacketReplyTimeout()); assertNotNull("Connection 1 never received the message", message1); MultipleRecipientInfo info1 = MultipleRecipientManager.getMultipleRecipientInfo(message1); assertNotNull("Message 1 does not contain MultipleRecipientInfo", info1); assertFalse("Message 1 should be 'replyable'", info1.shouldNotReply()); List<?> addresses1 = info1.getTOAddresses(); assertEquals("Incorrect number of TO addresses", 1, addresses1.size()); String address1 = ((MultipleAddresses.Address) addresses1.get(0)).getJid(); assertEquals("Incorrect TO address", getBareJID(1), address1); addresses1 = info1.getCCAddresses(); assertEquals("Incorrect number of CC addresses", 1, addresses1.size()); address1 = ((MultipleAddresses.Address) addresses1.get(0)).getJid(); assertEquals("Incorrect CC address", getBareJID(2), address1); Packet message2 = collector2.nextResult(SmackConfiguration.getPacketReplyTimeout()); assertNotNull("Connection 2 never received the message", message2); MultipleRecipientInfo info2 = MultipleRecipientManager.getMultipleRecipientInfo(message2); assertNotNull("Message 2 does not contain MultipleRecipientInfo", info2); assertFalse("Message 2 should be 'replyable'", info2.shouldNotReply()); List<MultipleAddresses.Address> addresses2 = info2.getTOAddresses(); assertEquals("Incorrect number of TO addresses", 1, addresses2.size()); String address2 = ((MultipleAddresses.Address) addresses2.get(0)).getJid(); assertEquals("Incorrect TO address", getBareJID(1), address2); addresses2 = info2.getCCAddresses(); assertEquals("Incorrect number of CC addresses", 1, addresses2.size()); address2 = ((MultipleAddresses.Address) addresses2.get(0)).getJid(); assertEquals("Incorrect CC address", getBareJID(2), address2); Packet message3 = collector3.nextResult(SmackConfiguration.getPacketReplyTimeout()); assertNotNull("Connection 3 never received the message", message3); MultipleRecipientInfo info3 = MultipleRecipientManager.getMultipleRecipientInfo(message3); assertNotNull("Message 3 does not contain MultipleRecipientInfo", info3); assertFalse("Message 3 should be 'replyable'", info3.shouldNotReply()); List<MultipleAddresses.Address> addresses3 = info3.getTOAddresses(); assertEquals("Incorrect number of TO addresses", 1, addresses3.size()); String address3 = ((MultipleAddresses.Address) addresses3.get(0)).getJid(); assertEquals("Incorrect TO address", getBareJID(1), address3); addresses3 = info3.getCCAddresses(); assertEquals("Incorrect number of CC addresses", 1, addresses3.size()); address3 = ((MultipleAddresses.Address) addresses3.get(0)).getJid(); assertEquals("Incorrect CC address", getBareJID(2), address3); collector1.cancel(); collector2.cancel(); collector3.cancel(); }
/** * Ensures that replying to packets is ok. */ public void testReplying() throws XMPPException { PacketCollector collector0 = getConnection(0).createPacketCollector(new MessageTypeFilter(Message.Type.normal)); PacketCollector collector1 = getConnection(1).createPacketCollector(new MessageTypeFilter(Message.Type.normal)); PacketCollector collector2 = getConnection(2).createPacketCollector(new MessageTypeFilter(Message.Type.normal)); PacketCollector collector3 = getConnection(3).createPacketCollector(new MessageTypeFilter(Message.Type.normal)); // Send the intial message with multiple recipients Message message = new Message(); message.setBody("Hola"); List<String> to = Arrays.asList(new String[]{getBareJID(1)}); List<String> cc = Arrays.asList(new String[]{getBareJID(2)}); List<String> bcc = Arrays.asList(new String[]{getBareJID(3)}); MultipleRecipientManager.send(getConnection(0), message, to, cc, bcc); // Get the message and ensure it's ok Message message1 = (Message) collector1.nextResult(SmackConfiguration.getPacketReplyTimeout()); assertNotNull("Connection 1 never received the message", message1); MultipleRecipientInfo info = MultipleRecipientManager.getMultipleRecipientInfo(message1); assertNotNull("Message 1 does not contain MultipleRecipientInfo", info); assertFalse("Message 1 should be 'replyable'", info.shouldNotReply()); assertEquals("Incorrect number of TO addresses", 1, info.getTOAddresses().size()); assertEquals("Incorrect number of CC addresses", 1, info.getCCAddresses().size()); // Prepare and send the reply Message reply1 = new Message(); reply1.setBody("This is my reply"); MultipleRecipientManager.reply(getConnection(1), message1, reply1); // Get the reply and ensure it's ok reply1 = (Message) collector0.nextResult(SmackConfiguration.getPacketReplyTimeout()); assertNotNull("Connection 0 never received the reply", reply1); info = MultipleRecipientManager.getMultipleRecipientInfo(reply1); assertNotNull("Replied message does not contain MultipleRecipientInfo", info); assertFalse("Replied message should be 'replyable'", info.shouldNotReply()); assertEquals("Incorrect number of TO addresses", 1, info.getTOAddresses().size()); assertEquals("Incorrect number of CC addresses", 1, info.getCCAddresses().size()); // Send a reply to the reply Message reply2 = new Message(); reply2.setBody("This is my reply to your reply"); reply2.setFrom(getBareJID(0)); MultipleRecipientManager.reply(getConnection(0), reply1, reply2); // Get the reply and ensure it's ok reply2 = (Message) collector1.nextResult(SmackConfiguration.getPacketReplyTimeout()); assertNotNull("Connection 1 never received the reply", reply2); info = MultipleRecipientManager.getMultipleRecipientInfo(reply2); assertNotNull("Replied message does not contain MultipleRecipientInfo", info); assertFalse("Replied message should be 'replyable'", info.shouldNotReply()); assertEquals("Incorrect number of TO addresses", 1, info.getTOAddresses().size()); assertEquals("Incorrect number of CC addresses", 1, info.getCCAddresses().size()); // Check that connection2 recevied 3 messages message1 = (Message) collector2.nextResult(SmackConfiguration.getPacketReplyTimeout()); assertNotNull("Connection2 didn't receive the 1 message", message1); message1 = (Message) collector2.nextResult(SmackConfiguration.getPacketReplyTimeout()); assertNotNull("Connection2 didn't receive the 2 message", message1); message1 = (Message) collector2.nextResult(SmackConfiguration.getPacketReplyTimeout()); assertNotNull("Connection2 didn't receive the 3 message", message1); message1 = (Message) collector2.nextResult(SmackConfiguration.getPacketReplyTimeout()); assertNull("Connection2 received 4 messages", message1); // Check that connection3 recevied only 1 message (was BCC in the first message) message1 = (Message) collector3.nextResult(SmackConfiguration.getPacketReplyTimeout()); assertNotNull("Connection3 didn't receive the 1 message", message1); message1 = (Message) collector3.nextResult(SmackConfiguration.getPacketReplyTimeout()); assertNull("Connection2 received 2 messages", message1); collector0.cancel(); collector1.cancel(); collector2.cancel(); collector3.cancel(); }
/** * Ensures that replying is not allowed when disabled. */ public void testNoReply() throws XMPPException { PacketCollector collector1 = getConnection(1).createPacketCollector(new MessageTypeFilter(Message.Type.normal)); PacketCollector collector2 = getConnection(2).createPacketCollector(new MessageTypeFilter(Message.Type.normal)); PacketCollector collector3 = getConnection(3).createPacketCollector(new MessageTypeFilter(Message.Type.normal)); // Send the intial message with multiple recipients Message message = new Message(); message.setBody("Hola"); List<String> to = Arrays.asList(new String[]{getBareJID(1)}); List<String> cc = Arrays.asList(new String[]{getBareJID(2)}); List<String> bcc = Arrays.asList(new String[]{getBareJID(3)}); MultipleRecipientManager.send(getConnection(0), message, to, cc, bcc, null, null, true); // Get the message and ensure it's ok Message message1 = (Message) collector1.nextResult(SmackConfiguration.getPacketReplyTimeout()); assertNotNull("Connection 1 never received the message", message1); MultipleRecipientInfo info = MultipleRecipientManager.getMultipleRecipientInfo(message1); assertNotNull("Message 1 does not contain MultipleRecipientInfo", info); assertTrue("Message 1 should be not 'replyable'", info.shouldNotReply()); assertEquals("Incorrect number of TO addresses", 1, info.getTOAddresses().size()); assertEquals("Incorrect number of CC addresses", 1, info.getCCAddresses().size()); // Prepare and send the reply Message reply1 = new Message(); reply1.setBody("This is my reply"); try { MultipleRecipientManager.reply(getConnection(1), message1, reply1); fail("It was possible to send a reply to a not replyable message"); } catch (XMPPException e) { // Exception was expected since replying was not allowed } // Check that connection2 recevied 1 messages message1 = (Message) collector2.nextResult(SmackConfiguration.getPacketReplyTimeout()); assertNotNull("Connection2 didn't receive the 1 message", message1); message1 = (Message) collector2.nextResult(SmackConfiguration.getPacketReplyTimeout()); assertNull("Connection2 received 2 messages", message1); // Check that connection3 recevied only 1 message (was BCC in the first message) message1 = (Message) collector3.nextResult(SmackConfiguration.getPacketReplyTimeout()); assertNotNull("Connection3 didn't receive the 1 message", message1); message1 = (Message) collector3.nextResult(SmackConfiguration.getPacketReplyTimeout()); assertNull("Connection2 received 2 messages", message1); collector1.cancel(); collector2.cancel(); collector3.cancel(); }