Author: keith
Date: Sun Jan 20 21:57:30 2008
New Revision: 12558

Log:

Implementaing handling of inflight messages



Modified:
   
trunk/commons/im-wrapper/src/main/java/org/wso2/imwrapper/oscar/OscarWrapperImpl.java

Modified: 
trunk/commons/im-wrapper/src/main/java/org/wso2/imwrapper/oscar/OscarWrapperImpl.java
==============================================================================
--- 
trunk/commons/im-wrapper/src/main/java/org/wso2/imwrapper/oscar/OscarWrapperImpl.java
       (original)
+++ 
trunk/commons/im-wrapper/src/main/java/org/wso2/imwrapper/oscar/OscarWrapperImpl.java
       Sun Jan 20 21:57:30 2008
@@ -25,6 +25,10 @@
 import net.kano.joustsim.oscar.StateListener;
 import net.kano.joustsim.oscar.oscar.service.icbm.ImConversation;
 import net.kano.joustsim.oscar.oscar.service.icbm.SimpleMessage;
+import net.kano.joustsim.oscar.oscar.service.icbm.ConversationListener;
+import net.kano.joustsim.oscar.oscar.service.icbm.Conversation;
+import net.kano.joustsim.oscar.oscar.service.icbm.MessageInfo;
+import net.kano.joustsim.oscar.oscar.service.icbm.ConversationEventInfo;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.wso2.imwrapper.core.IMException;
@@ -39,6 +43,9 @@
     boolean loginProcessed = false;
     boolean loggedIn = false;
 
+    int messageCount = 0;
+    boolean disconnectCalled = false;
+
     public void login(String userID, String password) throws IMException {
         DefaultAppSession session = new DefaultAppSession();
 
@@ -66,11 +73,17 @@
     }
 
     public void sendMessage(String to, String message) throws IMException {
+        if (disconnectCalled) {
+            throw new IMException("Unable to send message cause the IM 
connection is been " +
+                    "disconnected");
+        }
         if (loginProcessed && loggedIn) {
             ImConversation imConversation = aimConnection.getIcbmService()
                     .getImConversation(new Screenname(to));
             imConversation.open();
+            imConversation.addConversationListener(new 
AimConversationListener());
             if (imConversation.canSendMessage()) {
+                messageCount++;
                 imConversation.sendMessage(new SimpleMessage(message));
             } else {
                 log.error("Cound not send the message to: " + to);
@@ -85,6 +98,13 @@
 
     public void disconnect() throws IMException {
         if (aimConnection != null) {
+            do {
+                try {
+                    Thread.sleep(1000);
+                } catch (InterruptedException e) {
+                    log.error(e);
+                }
+            } while (messageCount > 0);
             aimConnection.disconnect();
             if (log.isDebugEnabled()) {
                 log.debug("Connection disconnected");
@@ -119,4 +139,32 @@
             }
         }
     }
+
+    private class AimConversationListener implements ConversationListener {
+
+        public void conversationOpened(Conversation conversation) {
+        }
+
+        public void conversationClosed(Conversation conversation) {
+        }
+
+        public void gotMessage(Conversation conversation, MessageInfo 
messageInfo) {
+        }
+
+        public void sentMessage(Conversation conversation, MessageInfo 
messageInfo) {
+            messageCount--;
+        }
+
+        public void canSendMessageChanged(Conversation conversation, boolean 
b) {
+
+        }
+
+        public void gotOtherEvent(Conversation conversation,
+                                  ConversationEventInfo conversationEventInfo) 
{
+        }
+
+        public void sentOtherEvent(Conversation conversation,
+                                   ConversationEventInfo 
conversationEventInfo) {
+        }
+    }
 }

_______________________________________________
Commons-dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/commons-dev

Reply via email to