• 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., 51 Franklin Street, Fifth Floor, Boston, MA
00016  * 02110-1301 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 #if defined(HAVE_BOOST_ASIO_HPP)
00025 # include <boost/asio.hpp>
00026 #else
00027 # include <asio.hpp>
00028 #endif
00029 #include <vector>
00030 #include <string>
00031 #include "ut_types.h"
00032 #include "RealmBuddy.h"
00033 #include "RealmGrowBuffer.h"
00034 #include "RealmProtocol.h"
00035 #include "tls_tunnel.h"
00036 #include <sync/xp/lock.h>
00037 #include <sync/xp/SynchronizedQueue.h>
00038 
00039 class AP_Dialog_GenericProgress;
00040 
00041 struct PendingDocumentProperties
00042 {
00043     PendingDocumentProperties(AP_Dialog_GenericProgress* pDlg_,
00044                                 PD_Document** pDoc_, XAP_Frame* pFrame_,
00045                                 const std::string& filename_, bool bLocallyOwned_)
00046     : pDlg(pDlg_),
00047     pDoc(pDoc_),
00048     pFrame(pFrame_),
00049     filename(filename_),
00050     bLocallyOwned(bLocallyOwned_)
00051     {}
00052 
00053     AP_Dialog_GenericProgress* pDlg;
00054     PD_Document** pDoc;
00055     XAP_Frame* pFrame;
00056     std::string filename;
00057     bool bLocallyOwned;
00058 };
00059 
00060 class UserJoinedPacket;
00061 typedef boost::shared_ptr<realm::protocolv1::UserJoinedPacket> UserJoinedPacketPtr;
00062 
00063 class RealmConnection : public boost::enable_shared_from_this<RealmConnection>
00064 {
00065 public:
00066     RealmConnection(const std::string& ca_file, const std::string& address, int port, bool tls,
00067                     const std::string& cookie, UT_uint64 doc_id, bool master, const std::string& session_id,
00068                     boost::function<void (boost::shared_ptr<RealmConnection>)> sig);
00069 
00070     bool                                connect();
00071     void                                disconnect();
00072     bool                                isConnected();
00073 
00074     void                                addBuddy(RealmBuddyPtr buddy_ptr);
00075     void                                removeBuddy(UT_uint8 realm_connection_id);
00076     RealmBuddyPtr                       getBuddy(UT_uint8 realm_connection_id);
00077     std::vector<RealmBuddyPtr>&         getBuddies()
00078         { return m_buddies; }
00079 
00080     UT_uint64                           user_id()
00081         { return m_user_id; }
00082     UT_uint8                            connection_id()
00083         { return m_connection_id; }
00084     UT_uint64                           doc_id()
00085         { return m_doc_id; }
00086     bool                                master()
00087         { return m_master; }
00088     void                                promote();
00089     const std::string&                  session_id()
00090         { return m_session_id; }
00091     PD_Document*                        getDocument()
00092         { return m_pDoc; }
00093     void                                setDocument(PD_Document* pDoc)
00094         { m_pDoc = pDoc; }
00095     SynchronizedQueue<realm::protocolv1::PacketPtr>&        queue()
00096         { return m_packet_queue; }
00097     asio::ip::tcp::socket&              socket()
00098         { return m_socket; }
00099 
00100     void                                loadDocumentStart(AP_Dialog_GenericProgress* pDlg,
00101                                                           PD_Document** pDoc_, XAP_Frame* pFrame_, const std::string& filename_,
00102                                                           bool bLocallyOwned_)
00103     {
00104         UT_return_if_fail(!m_pdp_ptr);
00105         m_pdp_ptr.reset(new PendingDocumentProperties(pDlg, pDoc_, pFrame_, filename_, bLocallyOwned_));
00106     }
00107     boost::shared_ptr<PendingDocumentProperties>
00108                                         getPendingDocProps()
00109         { return m_pdp_ptr; }
00110     void                                loadDocumentEnd()
00111         { m_pdp_ptr.reset(); }
00112 
00113 
00114 private:
00115     void                                _disconnect();
00116     void                                _signal();
00117     bool                                _login();
00118     UserJoinedPacketPtr                 _receiveUserJoinedPacket();
00119     void                                _receive();
00120     void                                _message(const asio::error_code& e,
00121                                                 std::size_t bytes_transferred,
00122                                                 boost::shared_ptr<std::string> msg_ptr);
00123 
00124     void                                _complete_packet(realm::protocolv1::PacketPtr packet_ptr);
00125     void                                _complete(const asio::error_code& e, std::size_t bytes_transferred,
00126                                                 realm::protocolv1::PacketPtr packet_ptr);
00127 
00128     asio::io_service                    m_io_service;
00129     std::string                         m_ca_file;
00130     std::string                         m_address;
00131     int                                 m_port;
00132     int                                 m_tls;
00133     asio::ip::tcp::socket               m_socket;
00134     boost::shared_ptr<asio::thread>     m_thread_ptr;
00135     std::string                         m_cookie;
00136     UT_uint64                           m_user_id; // only valid after login
00137     UT_uint8                            m_connection_id; // only valid after login
00138     UT_uint64                           m_doc_id;
00139     bool                                m_master;
00140     std::string                         m_session_id;
00141     PD_Document*                        m_pDoc;
00142     realm::GrowBuffer                   m_buf;
00143     SynchronizedQueue<realm::protocolv1::PacketPtr>     m_packet_queue;
00144     boost::function<void (boost::shared_ptr<RealmConnection>)> m_sig;
00145     std::vector<RealmBuddyPtr>          m_buddies;
00146 
00147     boost::shared_ptr<PendingDocumentProperties>
00148                                         m_pdp_ptr;
00149     boost::shared_ptr<tls_tunnel::ClientProxy>
00150                                         m_tls_tunnel_ptr;
00151 
00152     abicollab::mutex                    m_mutex;
00153 };
00154 
00155 typedef boost::shared_ptr<RealmConnection> ConnectionPtr;
00156 
00157 #endif /* __REALM_CONNECTION__ */

Generated on Sun Feb 14 2021 for AbiWord by  doxygen 1.7.1