Advertisement
rahul0611

Im client111111

Jun 11th, 2012
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.59 KB | None | 0 0
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.activemq.transport.xmpp;
  18.  
  19. import junit.framework.TestCase;
  20. import junit.textui.TestRunner;
  21.  
  22. import org.apache.activemq.util.Wait;
  23. import org.jivesoftware.smack.Chat;
  24. import org.jivesoftware.smack.ChatManager;
  25. import org.jivesoftware.smack.ChatManagerListener;
  26. import org.jivesoftware.smack.ConnectionConfiguration;
  27. import org.jivesoftware.smack.MessageListener;
  28. import org.jivesoftware.smack.PacketListener;
  29. import org.jivesoftware.smack.XMPPConnection;
  30. import org.jivesoftware.smack.XMPPException;
  31. import org.jivesoftware.smack.filter.PacketFilter;
  32. import org.jivesoftware.smack.packet.Message;
  33. import org.jivesoftware.smack.packet.Packet;
  34. import org.jivesoftware.smackx.muc.MultiUserChat;
  35. import org.slf4j.Logger;
  36. import org.slf4j.LoggerFactory;
  37.  
  38. public class XmppTest extends TestCase {
  39.  
  40. private static final Logger LOG = LoggerFactory.getLogger(XmppTest.class);
  41.  
  42. protected static boolean block;
  43.  
  44. private final XmppBroker broker = new XmppBroker();
  45.  
  46. private final long sleepTime = 5000;
  47.  
  48. public static void main(String[] args) {
  49. block = true;
  50. TestRunner.run(XmppTest.class);
  51. }
  52.  
  53. public void testConnect() throws Exception {
  54. ConnectionConfiguration config = new
  55. ConnectionConfiguration("localhost", 61222);
  56. // config.setDebuggerEnabled(true);
  57.  
  58. try {
  59. // SmackConfiguration.setPacketReplyTimeout(1000);
  60. XMPPConnection con = new XMPPConnection(config);
  61. con.connect();
  62. con.login("amq-user", "amq-pwd");
  63. ChatManager chatManager = con.getChatManager();
  64. Chat chat = chatManager.createChat("test@localhost", new MessageListener() {
  65. public void processMessage(Chat chat, Message message) {
  66. LOG.info("Got XMPP message from chat " + chat.getParticipant() + " message - " + message.getBody());
  67. }
  68. });
  69. for (int i = 0; i < 10; i++) {
  70. LOG.info("Sending message: " + i);
  71. chat.sendMessage("Hello from Message: " + i);
  72. }
  73. LOG.info("Sent all messages!");
  74. con.disconnect();
  75. } catch (XMPPException e) {
  76. if (block) {
  77. LOG.info("Caught: " + e);
  78. e.printStackTrace();
  79. } else {
  80. throw e;
  81. }
  82. }
  83. if (block) {
  84. Thread.sleep(20000);
  85. LOG.info("Press any key to quit!: ");
  86. System.in.read();
  87. }
  88. LOG.info("Done!");
  89. }
  90.  
  91. public void testChat() throws Exception {
  92. ConnectionConfiguration config = new ConnectionConfiguration("localhost", 61222);
  93. //config.setDebuggerEnabled(true);
  94.  
  95. XMPPConnection consumerCon = new XMPPConnection(config);
  96. consumerCon.connect();
  97. consumerCon.login("consumer", "consumer");
  98. consumerCon.addPacketListener(new XmppLogger("CONSUMER INBOUND"), new PacketFilter() {
  99. public boolean accept(Packet packet) {
  100. return true;
  101. }
  102. });
  103. consumerCon.addPacketWriterListener(new XmppLogger("CONSUMER OUTBOUND"), new PacketFilter() {
  104. public boolean accept(Packet packet) {
  105. return true;
  106. }
  107. });
  108. final ConsumerMessageListener listener = new ConsumerMessageListener();
  109.  
  110. consumerCon.getChatManager().addChatListener(new ChatManagerListener() {
  111. public void chatCreated(Chat chat, boolean createdLocally) {
  112. chat.addMessageListener(listener);
  113. }
  114. });
  115.  
  116. XMPPConnection producerCon = new XMPPConnection(config);
  117. producerCon.connect();
  118. producerCon.login("producer", "producer");
  119. producerCon.addPacketListener(new XmppLogger("PRODUCER INBOUND"), new PacketFilter() {
  120. public boolean accept(Packet packet) {
  121. return true;
  122. }
  123. });
  124.  
  125. producerCon.addPacketWriterListener(new XmppLogger("PRODUCER OUTBOUND"), new PacketFilter() {
  126. public boolean accept(Packet packet) {
  127. return true;
  128. }
  129. });
  130.  
  131. Chat chat = producerCon.getChatManager().createChat("consumer", new MessageListener() {
  132. public void processMessage(Chat chat, Message message) {
  133. LOG.info("Got XMPP message from chat " + chat.getParticipant() + " message - " + message.getBody());
  134. }
  135. });
  136.  
  137. for (int i = 0; i < 10; i++) {
  138. LOG.info("Sending message: " + i);
  139. Message message = new Message("consumer");
  140. message.setType(Message.Type.chat);
  141. message.setBody("Hello from producer, message # " + i);
  142. chat.sendMessage(message);
  143. }
  144. LOG.info("Sent all messages!");
  145.  
  146. assertTrue("Consumer received - " + listener.getMessageCount(), Wait.waitFor(new Wait.Condition() {
  147. @Override
  148. public boolean isSatisified() throws Exception {
  149. return listener.getMessageCount() == 10;
  150. }
  151. }));
  152.  
  153. LOG.info("Consumer received - " + listener.getMessageCount());
  154. }
  155.  
  156. public void testMultiUserChat() throws Exception {
  157. LOG.info("\n\n\n\n\n\n");
  158. ConnectionConfiguration config = new ConnectionConfiguration("localhost", 61222);
  159. //config.setDebuggerEnabled(true);
  160. //
  161. XMPPConnection consumerCon = new XMPPConnection(config);
  162. consumerCon.connect();
  163. consumerCon.login("consumer", "consumer");
  164. MultiUserChat consumerMuc = new MultiUserChat(consumerCon, "muc-test");
  165. consumerMuc.join("consumer");
  166.  
  167. final ConsumerMUCMessageListener listener = new ConsumerMUCMessageListener();
  168. consumerMuc.addMessageListener(listener);
  169.  
  170. XMPPConnection producerCon = new XMPPConnection(config);
  171. producerCon.connect();
  172. producerCon.login("producer", "producer");
  173. MultiUserChat producerMuc = new MultiUserChat(producerCon, "muc-test");
  174. producerMuc.join("producer");
  175.  
  176. for (int i = 0; i < 10; i++) {
  177. LOG.info("Sending message: " + i);
  178. Message message = producerMuc.createMessage();
  179. message.setBody("Hello from producer, message # " + i);
  180. producerMuc.sendMessage(message);
  181. }
  182. LOG.info("Sent all messages!");
  183.  
  184. assertTrue("Consumer received - " + listener.getMessageCount(), Wait.waitFor(new Wait.Condition() {
  185. @Override
  186. public boolean isSatisified() throws Exception {
  187. return listener.getMessageCount() == 10;
  188. }
  189. }));
  190.  
  191. LOG.info("Consumer received - " + listener.getMessageCount());
  192. }
  193.  
  194. public void addLoggingListeners(String name, XMPPConnection connection) {
  195. connection.addPacketListener(new XmppLogger(name + " INBOUND"), new PacketFilter() {
  196. public boolean accept(Packet packet) {
  197. return true;
  198. }
  199. });
  200. connection.addPacketWriterListener(new XmppLogger(name + " OUTBOUND"), new PacketFilter() {
  201. public boolean accept(Packet packet) {
  202. return true;
  203. }
  204. });
  205. }
  206.  
  207. public void testTwoConnections() throws Exception {
  208. LOG.info("\n\n\n\n\n\n");
  209. ConnectionConfiguration config = new ConnectionConfiguration("localhost", 61222);
  210. //config.setDebuggerEnabled(true);
  211.  
  212. //create the consumer first...
  213. XMPPConnection consumerCon = new XMPPConnection(config);
  214. consumerCon.connect();
  215. addLoggingListeners("CONSUMER", consumerCon);
  216. consumerCon.login("consumer", "consumer");
  217.  
  218. final ConsumerMessageListener listener1 = new ConsumerMessageListener();
  219. consumerCon.getChatManager().addChatListener(new ChatManagerListener() {
  220. public void chatCreated(Chat chat, boolean createdLocally) {
  221. chat.addMessageListener(listener1);
  222. }
  223. });
  224.  
  225. //now create the producer
  226. XMPPConnection producerCon = new XMPPConnection(config);
  227. LOG.info("Connecting producer and consumer");
  228. producerCon.connect();
  229. addLoggingListeners("PRODUCER", producerCon);
  230. producerCon.login("producer", "producer");
  231.  
  232. //create the chat and send some messages
  233. Chat chat = producerCon.getChatManager().createChat("consumer", new MessageListener() {
  234. public void processMessage(Chat chat, Message message) {
  235. LOG.info("Got XMPP message from chat " + chat.getParticipant() + " message - " + message.getBody());
  236. }
  237. });
  238.  
  239. for (int i = 0; i < 10; i++) {
  240. LOG.info("Sending message: " + i);
  241. Message message = new Message("consumer");
  242. message.setType(Message.Type.chat);
  243. message.setBody("Hello from producer, message # " + i);
  244. chat.sendMessage(message);
  245. }
  246.  
  247. //make sure the consumer has time to receive all the messages...
  248. Thread.sleep(sleepTime);
  249.  
  250. //create an identical 2nd consumer
  251. XMPPConnection lastguyCon = new XMPPConnection(config);
  252. lastguyCon.connect();
  253. addLoggingListeners("LASTGUY", consumerCon);
  254. lastguyCon.login("consumer", "consumer");
  255. final ConsumerMessageListener listener2 = new ConsumerMessageListener();
  256. lastguyCon.getChatManager().addChatListener(new ChatManagerListener() {
  257. public void chatCreated(Chat chat, boolean createdLocally) {
  258. chat.addMessageListener(listener2);
  259. }
  260. });
  261.  
  262. for (int i = 0; i < 10; i++) {
  263. LOG.info("Sending message: " + i);
  264. Message message = new Message("consumer");
  265. message.setType(Message.Type.chat);
  266. message.setBody("Hello from producer, message # " + i);
  267. chat.sendMessage(message);
  268. }
  269.  
  270. LOG.info("Sent all messages!");
  271.  
  272. assertTrue("Consumer received - " + listener1.getMessageCount(), Wait.waitFor(new Wait.Condition() {
  273. @Override
  274. public boolean isSatisified() throws Exception {
  275. return listener1.getMessageCount() == 20;
  276. }
  277. }));
  278.  
  279. assertTrue("Consumer received - " + listener2.getMessageCount(), Wait.waitFor(new Wait.Condition() {
  280. @Override
  281. public boolean isSatisified() throws Exception {
  282. return listener2.getMessageCount() == 10;
  283. }
  284. }));
  285. }
  286.  
  287. class XmppLogger implements PacketListener {
  288.  
  289. private final String direction;
  290.  
  291. public XmppLogger(String direction) {
  292. this.direction = direction;
  293. }
  294.  
  295. public void processPacket(Packet packet) {
  296. LOG.info(direction + " : " + packet.toXML());
  297. }
  298. }
  299.  
  300. class ConsumerMUCMessageListener implements PacketListener {
  301. private int messageCount=0;
  302.  
  303. public void processPacket(Packet packet) {
  304. if ( packet instanceof Message) {
  305. LOG.info("Received message number : " + (messageCount++));
  306. }
  307. }
  308. public int getMessageCount() {
  309. return messageCount;
  310. }
  311. }
  312.  
  313. class ConsumerMessageListener implements MessageListener {
  314. private int messageCount=0;
  315.  
  316. public void processMessage(Chat chat, Message message) {
  317. LOG.info("Received message number : " + (messageCount++));
  318. }
  319.  
  320. public int getMessageCount() {
  321. return messageCount;
  322. }
  323. }
  324.  
  325. @Override
  326. protected void setUp() throws Exception {
  327. broker.start();
  328. }
  329.  
  330. @Override
  331. protected void tearDown() throws Exception {
  332. broker.stop();
  333. }
  334. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement