• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

RealmConnection.h

Go to the documentation of this file.
00001 /* Copyright (C) 2008 AbiSource Corporation B.V.
00002  *
00003  * This program is free software; you can redistribute it and/or
00004  * modify it under the terms of the GNU General Public License
00005  * as published by the Free Software Foundation; either version 2
00006  * of the License, or (at your option) any later version.
00007  *
00008  * This program is distributed in the hope that it will be useful,
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  * GNU General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * along with this program; if not, write to the Free Software
00015  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00016  * 02111-1307, USA.
00017  */
00018 
00019 #ifndef __REALM_CONNECTION__
00020 #define __REALM_CONNECTION__
00021 
00022 #include <boost/shared_ptr.hpp>
00023 #include <boost/enable_shared_from_this.hpp>
00024 #include <asio.hpp>
00025 #include <vector>
00026 #include <string>
00027 #include "ut_types.h"
00028 #include "RealmBuddy.h"
00029 #include "RealmGrowBuffer.h"
00030 #include "RealmProtocol.h"
00031 #include "tls_tunnel.h"
00032 #include <sync/xp/lock.h>
00033 #include <sync/xp/SynchronizedQueue.h>
00034 
00035 class AP_Dialog_GenericProgress;
00036 
00037 using realm::protocolv1::PacketPtr;
00038 
00039 struct PendingDocumentProperties
00040 {
00041     PendingDocumentProperties(AP_Dialog_GenericProgress* pDlg_,
00042                                 PD_Document** pDoc_, XAP_Frame* pFrame_,
00043                                 const std::string& filename_, bool bLocallyOwned_)
00044     : pDlg(pDlg_),
00045     pDoc(pDoc_),
00046     pFrame(pFrame_),
00047     filename(filename_),
00048     bLocallyOwned(bLocallyOwned_)
00049     {}
00050 
00051     AP_Dialog_GenericProgress* pDlg;
00052     PD_Document** pDoc;
00053     XAP_Frame* pFrame;
00054     std::string filename;
00055     bool bLocallyOwned;
00056 };
00057 
00058 class UserJoinedPacket;
00059 typedef boost::shared_ptr<realm::protocolv1::UserJoinedPacket> UserJoinedPacketPtr;
00060 
00061 class RealmConnection : public boost::enable_shared_from_this<RealmConnection>
00062 {
00063 public:
00064     RealmConnection(const std::string& ca_file, const std::string& address, int port, bool tls,
00065                     const std::string& cookie, UT_uint64 doc_id, bool master, const std::string& session_id,
00066                     boost::function<void (boost::shared_ptr<RealmConnection>)> sig);
00067 
00068     bool                                connect();
00069     void                                disconnect();
00070     bool                                isConnected();
00071 
00072     void                                addBuddy(RealmBuddyPtr buddy_ptr);
00073     void                                removeBuddy(UT_uint8 realm_connection_id);
00074     RealmBuddyPtr                       getBuddy(UT_uint8 realm_connection_id);
00075     std::vector<RealmBuddyPtr>&         getBuddies()
00076         { return m_buddies; }
00077 
00078     UT_uint64                           user_id()
00079         { return m_user_id; }
00080     UT_uint8                            connection_id()
00081         { return m_connection_id; }
00082     UT_uint64                           doc_id()
00083         { return m_doc_id; }
00084     bool                                master()
00085         { return m_master; }
00086     void                                promote();
00087     const std::string&                  session_id()
00088         { return m_session_id; }
00089     PD_Document*                        getDocument()
00090         { return m_pDoc; }
00091     void                                setDocument(PD_Document* pDoc)
00092         { m_pDoc = pDoc; }
00093     SynchronizedQueue<PacketPtr>&       queue()
00094         { return m_packet_queue; }
00095     asio::ip::tcp::socket&              socket()
00096         { return m_socket; }
00097 
00098     void                                loadDocumentStart(AP_Dialog_GenericProgress* pDlg,
00099                                                           PD_Document** pDoc_, XAP_Frame* pFrame_, const std::string& filename_,
00100                                                           bool bLocallyOwned_)
00101     {
00102         UT_return_if_fail(!m_pdp_ptr);
00103         m_pdp_ptr.reset(new PendingDocumentProperties(pDlg, pDoc_, pFrame_, filename_, bLocallyOwned_));
00104     }
00105     boost::shared_ptr<PendingDocumentProperties>
00106                                         getPendingDocProps()
00107         { return m_pdp_ptr; }
00108     void                                loadDocumentEnd()
00109         { m_pdp_ptr.reset(); }
00110 
00111 
00112 private:
00113     void                                _disconnect();
00114     void                                _signal();
00115     bool                                _login();
00116     UserJoinedPacketPtr                 _receiveUserJoinedPacket();
00117     void                                _receive();
00118     void                                _message(const asio::error_code& e,
00119                                                 std::size_t bytes_transferred,
00120                                                 boost::shared_ptr<std::string> msg_ptr);
00121 
00122     void                                _complete_packet(PacketPtr packet_ptr);
00123     void                                _complete(const asio::error_code& e, std::size_t bytes_transferred,
00124                                                 PacketPtr packet_ptr);
00125 
00126     asio::io_service                    m_io_service;
00127     std::string                         m_ca_file;
00128     std::string                         m_address;
00129     int                                 m_port;
00130     int                                 m_tls;
00131     asio::ip::tcp::socket               m_socket;
00132     boost::shared_ptr<asio::thread>     m_thread_ptr;
00133     std::string                         m_cookie;
00134     UT_uint64                           m_user_id; // only valid after login
00135     UT_uint8                            m_connection_id; // only valid after login
00136     UT_uint64                           m_doc_id;
00137     bool                                m_master;
00138     std::string                         m_session_id;
00139     PD_Document*                        m_pDoc;
00140     realm::GrowBuffer                   m_buf;
00141     SynchronizedQueue<PacketPtr>        m_packet_queue;
00142     boost::function<void (boost::shared_ptr<RealmConnection>)> m_sig;
00143     std::vector<RealmBuddyPtr>          m_buddies;
00144 
00145     boost::shared_ptr<PendingDocumentProperties>
00146                                         m_pdp_ptr;
00147     boost::shared_ptr<tls_tunnel::ClientProxy>
00148                                         m_tls_tunnel_ptr;
00149 
00150     abicollab::mutex                    m_mutex;
00151 };
00152 
00153 typedef boost::shared_ptr<RealmConnection> ConnectionPtr;
00154 
00155 #endif /* __REALM_CONNECTION__ */

Generated on Sun May 27 2012 for AbiWord by  doxygen 1.7.1