Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members  

jabberoo-component-session.cpp

00001 /*
00002  * jabberoo-component-session.cpp
00003  * Jabber Component Session management
00004  *
00005  * Original Code Copyright (C) 1999-2001 Dave Smith (dave@jabber.org)
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  * 
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  *
00021  * Contributor(s): Julian Missig (IBM)
00022  *
00023  */
00024 
00025 #include "jabberoo-component.hh"
00026 #include <sha.h>
00027 
00028 using namespace jabberoo;
00029 
00030 ComponentSession::ComponentSession() : 
00031     judo::ElementStream(this), _connState(csNotConnected)
00032 { }
00033 
00034 ComponentSession::~ComponentSession()
00035 {
00036     if (_connState != csNotConnected)
00037     {
00038         disconnect();
00039     }
00040 
00041     while (!_XPaths.empty())
00042     {
00043         judo::XPath::Query* query = _XPaths.front();
00044         delete query;
00045         _XPaths.pop_front();
00046     }
00047 }
00048 
00049 void ComponentSession::connect(const std::string& server, const std::string& componentID,
00050         const std::string& password)
00051 {
00052     _componentID = componentID;
00053     _server = server;
00054     _password = password;
00055 
00056     assert(_connState == csNotConnected);
00057 
00058     reset();
00059 
00060     *this << "<stream::stream to='" << _componentID.c_str() << "' xmlns='jabber:component:accept' xmlns:stream='http://etherx.jabber.org/streams'>";
00061 
00062     _connState = csAwaitingAuth;
00063 }
00064 
00065 void ComponentSession::disconnect()
00066 {
00067     assert(_connState != csNotConnected);
00068 
00069     *this << "</stream:stream>";
00070     
00071     _connState = csNotConnected;
00072 }
00073 
00074 void ComponentSession::push(const char* data, int datasz)
00075 {
00076     try 
00077     {
00078         judo::ElementStream::push(data, datasz);
00079     } 
00080     catch (const judo::ElementStream::exception::ParserError& error) 
00081     {
00082         disconnect();
00083     }
00084 }
00085 
00086 judo::XPath::Query* ComponentSession::registerXPath(const std::string& query, 
00087         ElementCallbackFunc f)
00088 {
00089     judo::XPath::Query* xpq = new judo::XPath::Query(query);
00090     _XPaths.push_front(xpq);
00091     _XPCallbacks.insert(std::make_pair(xpq, f));
00092 
00093     return xpq;
00094 }
00095 
00096 void ComponentSession::unregisterXPath(judo::XPath::Query* id)
00097 {
00098         std::remove(_XPaths.begin(), _XPaths.end(), id);
00099     _XPCallbacks.erase(id);
00100     delete id;
00101 }
00102 
00103 void ComponentSession::onDocumentStart(judo::Element* t)
00104 {
00105     // Retrieve the SID from the stream header
00106     _SessionID = t->getAttrib("id");
00107     
00108     std::string full = _SessionID + _password;
00109     *this << "<handshake>" << shahash(full.c_str()) << "</handshake>";
00110 }
00111 
00112 void ComponentSession::onElement(judo::Element* t) 
00113 {
00114     if (_connState != csConnected && t->getName() == "handshake")
00115     {
00116         _connState = csConnected;
00117         delete t;
00118         evtConnected();
00119         return;
00120     }
00121 
00122     judo::Element& tref = *t;
00123     // See if a judo::xpath handles this
00124     typedef std::list<judo::XPath::Query*>::iterator IT;
00125     for (IT it = _XPaths.begin(); it != _XPaths.end(); it++)
00126     {
00127         if ((*it)->check(tref))
00128         {
00129             _XPCallbacks[(*it)](tref);
00130         }
00131     }
00132 
00133     delete t;
00134 }
00135 
00136 void ComponentSession::onCDATA(judo::CDATA* c)
00137 {
00138      // If we ever want to deal with CDATA we receive
00139      // possibly message sizes and things like that
00140      // then we do it here.
00141      delete c;
00142 }
00143 
00144 void ComponentSession::onDocumentEnd() 
00145 {
00146     disconnect();
00147 }
00148 

Generated on Thu Jul 24 13:31:50 2003 for jabberoo by doxygen1.3-rc3