Code

added StateHandling to whiteboard
authordaleharvey <daleharvey@users.sourceforge.net>
Thu, 3 Aug 2006 01:55:09 +0000 (01:55 +0000)
committerdaleharvey <daleharvey@users.sourceforge.net>
Thu, 3 Aug 2006 01:55:09 +0000 (01:55 +0000)
src/jabber_whiteboard/defines.h
src/jabber_whiteboard/inkboard-document.cpp
src/jabber_whiteboard/inkboard-document.h
src/jabber_whiteboard/inkboard-session.h
src/jabber_whiteboard/session-manager.cpp

index e08d876a680de0ec5f126fec65f6529b3205f5a3..e1cc8cb8b3e7bd07d4b3cb316ede47cbda5d7c41 100644 (file)
@@ -98,16 +98,28 @@ namespace State {
     extern SessionType WHITEBOARD_MUC; 
     extern SessionType WHITEBOARD_PEER;
 
-    typedef char const* SessionState;
+    enum SessionState {
+
+        INITIAL =                   0,
+        AWAITING_INVITATION_REPLY = 1,
+        CONNECTING =                2,
+        INVITATION_RECIEVED =       3,
+        AWAITING_CONNECTED =        4,
+        CONNECTED =                 5,
+        AWAITING_DOCUMENT_BEGIN =   6,
+        SYNCHRONISING =             7,
+        IN_WHITEBOARD =             8
+
+    };
 }
 
 namespace Dialog {
 
-enum DialogReply {
+    enum DialogReply {
 
-    ACCEPT_INVITATION =     0,
-    DECLINE_INVITATION =    1
-};
+        ACCEPT_INVITATION =     0,
+        DECLINE_INVITATION =    1
+    };
 
 }
 
index bf72d3c2af445b6a30f8f1b06b0f48082e540753..514b3acc414dc936de84292ec2b868e7cae2e299 100644 (file)
@@ -33,6 +33,7 @@ void
 InkboardDocument::_initBindings()
 {
     this->_sm = &SessionManager::instance();
+    this->state = State::INITIAL;
     _bindDocument(*this);
     _bindLogger(*(new XML::SimpleSession()));
 }
@@ -50,13 +51,13 @@ InkboardDocument::getRecipient() const
 }
 
 void
-InkboardDocument::setSessionIdent(Glib::ustring const& val)
+InkboardDocument::setSessionId(Glib::ustring const& val)
 {
     this->_session = val;
 }
 
 Glib::ustring 
-InkboardDocument::getSessionIdent() const
+InkboardDocument::getSessionId() const
 {
     return this->_session;
 }
@@ -77,29 +78,108 @@ void
 InkboardDocument::processInkboardEvent(Message::Wrapper mtype, Glib::ustring const& data)
 {
     g_log(NULL, G_LOG_LEVEL_DEBUG, "Processing Inkboard event: mtype=%s data=%s\n",mtype,data.c_str());
+    
 }
 
 bool
 InkboardDocument::sendProtocol(const Glib::ustring &destJid, Message::Wrapper wrapper,
      Message::Message message)
 {
-    char *fmt=
-        "<message type='%s' from='%s' to='%s'>"
-            "<wb xmlns='%s' session='%s'>"
-                "<%s>"
-                    "<%s />"
-                "</%s>"
-            "</wb>"
-            "<body> </body>"
-        "</message>";
-    if (!_sm->getClient().write(
-        fmt,_type,_sm->getClient().getJid().c_str(),destJid.c_str(),
-        Vars::INKBOARD_XMLNS,this->getSessionIdent().c_str(),wrapper,message,wrapper))
+    if(this->handleOutgoingState(wrapper,message))
+    {
+        char *fmt=
+            "<message type='%s' from='%s' to='%s'>"
+                "<wb xmlns='%s' session='%s'>"
+                    "<%s>"
+                        "<%s />"
+                    "</%s>"
+                "</wb>"
+                "<body> </body>"
+            "</message>";
+
+        if (!_sm->getClient().write(fmt,
+                _type,_sm->getClient().getJid().c_str(),destJid.c_str(),Vars::INKBOARD_XMLNS,
+                this->getSessionId().c_str(),wrapper,message,wrapper))
+            return false;
+
+        return true;
+
+    }else
         return false;
+}
+
+bool
+InkboardDocument::handleOutgoingState(Message::Wrapper wrapper,Message::Message message)
+{
+    g_warning("state %d, message %s",this->state,message);
+    if(wrapper == Message::PROTOCOL) 
+    {
+        if(message == Message::CONNECT_REQUEST) 
+            return this->handleState(State::INITIAL,State::AWAITING_INVITATION_REPLY);
+
+        else if(message == Message::ACCEPT_INVITATION)
+            return this->handleState(State::CONNECTING,State::AWAITING_CONNECTED);
+
+        else if(message == Message::CONNECTED)
+            return this->handleState(State::INVITATION_RECIEVED,State::CONNECTED);
+
+        else if(message == Message::DOCUMENT_BEGIN)
+            return this->handleState(State::CONNECTED,State::SYNCHRONISING);
 
-    return true;
+        else if(message == Message::DOCUMENT_END)
+            return this->handleState(State::SYNCHRONISING,State::IN_WHITEBOARD);
+
+        else 
+            return false;
+
+    } else 
+        if(this->state == State::SYNCHRONISING && wrapper == Message::NEW)
+            return true;
+
+    return this->state == State::IN_WHITEBOARD;
 }
 
+bool
+InkboardDocument::handleIncomingState(Message::Wrapper wrapper,Glib::ustring const& message)
+{
+    if(wrapper == Message::PROTOCOL) 
+    {
+        // Connect Requests are handled in SessionManager
+        if(message == Message::ACCEPT_INVITATION)
+            return this->handleState(State::AWAITING_INVITATION_REPLY,State::INVITATION_RECIEVED);
+
+        else if(message == Message::CONNECTED)
+            return this->handleState(State::AWAITING_CONNECTED,State::AWAITING_DOCUMENT_BEGIN);
+
+        else if(message == Message::DOCUMENT_BEGIN)
+            return this->handleState(State::AWAITING_DOCUMENT_BEGIN,State::SYNCHRONISING);
+
+        else if(message == Message::DOCUMENT_END)
+            return this->handleState(State::SYNCHRONISING,State::IN_WHITEBOARD);
+
+        else 
+            return false;
+
+    } else 
+        if(this->state == State::SYNCHRONISING && wrapper == Message::NEW)
+            return true;
+
+    return this->state == State::IN_WHITEBOARD;
+}
+
+bool 
+InkboardDocument::handleState(State::SessionState expectedState, State::SessionState newState)
+{
+    if(this->state == expectedState)
+    {
+        this->state = newState;
+        return true;
+    }
+
+    return false;
+}
+
+
 } // namespace Whiteboard
 } // namespace Inkscape
 
index 71de75e19c05b24dcd9bb4b1224a964e19bdcfd4..c09640852f3550abb8c03caf7245922feaa177fc 100644 (file)
@@ -38,8 +38,8 @@ public:
     void setRecipient(Glib::ustring const& val);
     Glib::ustring getRecipient() const;
 
-    void setSessionIdent(Glib::ustring const& val);
-    Glib::ustring getSessionIdent() const;
+    void setSessionId(Glib::ustring const& val);
+    Glib::ustring getSessionId() const;
 
     void startSessionNegotiation();
     void terminateSession();
@@ -48,6 +48,10 @@ public:
     bool sendProtocol(const Glib::ustring &destJid, Message::Wrapper mwrapper, 
         Message::Message message);
 
+    bool handleOutgoingState(Message::Wrapper wrapper,Message::Message message);
+    bool handleIncomingState(Message::Wrapper wrapper,Glib::ustring const& message);
+
+    bool handleState(State::SessionState expectedState, State::SessionState newstate);
 
 protected:
        /**
@@ -72,6 +76,7 @@ private:
 
     SessionManager *_sm;
     State::SessionType _type;
+    State::SessionState state;
 
     Glib::ustring _session;
     Glib::ustring _recipient;
index 28e844f23c2eef13d7f128107636fc78ef05f271..9b1209e050a47df4770db288e340757f520f8f15 100644 (file)
@@ -49,46 +49,46 @@ public:
     virtual void setName(const Glib::ustring &val)
         { _name = val; }
 
-       /**
-        * Returns status attributes of this session.
-        *
-        * \return Status of this session.
-        */
-       virtual std::bitset< NUM_FLAGS > const& getStatus() const
-       {
-               return status;
-       }
-
-       //
-       // XML::TransactionLogger methods
-       //
-       Session& session()
-       {
-               return *this;
-       }
-
-       // 
-       // XML::Session methods
-       // 
-       bool inTransaction() 
-       {
-               return _in_transaction;
-       }
-
-       void beginTransaction();
-       void rollback();
-       void commit();
-
-       XML::Event* commitUndoable();
-
-       XML::Node* createElementNode(char const* name);
-       XML::Node* createTextNode(char const* content);
-       XML::Node* createCommentNode(char const* content);
-
-       //
-       // XML::NodeObserver methods
-       // (inherited from XML::TransactionLogger)
-       //
+    /**
+     * Returns status attributes of this session.
+     *
+     * \return Status of this session.
+     */
+    virtual std::bitset< NUM_FLAGS > const& getStatus() const
+    {
+       return status;
+    }
+
+    //
+    // XML::TransactionLogger methods
+    //
+    Session& session()
+    {
+       return *this;
+    }
+
+    // 
+    // XML::Session methods
+    // 
+    bool inTransaction() 
+    {
+       return _in_transaction;
+    }
+
+    void beginTransaction();
+    void rollback();
+    void commit();
+
+    XML::Event* commitUndoable();
+
+    XML::Node* createElementNode(char const* name);
+    XML::Node* createTextNode(char const* content);
+    XML::Node* createCommentNode(char const* content);
+
+    //
+    // XML::NodeObserver methods
+    // (inherited from XML::TransactionLogger)
+    //
     void notifyChildAdded(Inkscape::XML::Node &parent, Inkscape::XML::Node &child, Inkscape::XML::Node *prev);
 
     void notifyChildRemoved(Inkscape::XML::Node &parent, Inkscape::XML::Node &child, Inkscape::XML::Node *prev);
@@ -105,13 +105,14 @@ public:
                                 Util::ptr_shared<char> new_value);
 
 private:
+
     InkboardSession(InkboardSession const &); // no copy
     void operator=(InkboardSession const &); // no assign
 
-       bool _in_transaction;
+    bool _in_transaction;
 
     std::bitset< NUM_FLAGS > status;
-       Glib::ustring _name;
+    Glib::ustring _name;
 };
 
 }
index dd392c845b4d18940f2bb274504eec50a6ff6d19..7ca4902121fc08fe3d8cd3ac0710d3dd8856a09c 100644 (file)
@@ -175,7 +175,7 @@ SessionManager::initialiseSession(Glib::ustring const& to, State::SessionType ty
 
     char * sessionId = createSessionId(10);
 
-    inkdoc->setSessionIdent(sessionId);
+    inkdoc->setSessionId(sessionId);
 
     addSession(WhiteboardRecord(sessionId, inkdoc));
 
@@ -315,9 +315,12 @@ SessionManager::checkInvitationQueue()
 
 
         SPDocument* doc = makeInkboardDocument(g_quark_from_static_string("xml"), "svg:svg", State::WHITEBOARD_PEER, from);
+
         InkboardDocument* inkdoc = dynamic_cast< InkboardDocument* >(doc->rdoc);
         if(inkdoc == NULL) return true;
 
+        inkdoc->handleState(State::INITIAL,State::CONNECTING);
+        inkdoc->setSessionId(sessionId);
         addSession(WhiteboardRecord(sessionId, inkdoc));
 
         switch (reply) {