ncbi_conn_stream.cpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:14k
源码类别:

生物技术

开发平台:

C/C++

  1. /*
  2.  * ===========================================================================
  3.  * PRODUCTION $Log: ncbi_conn_stream.cpp,v $
  4.  * PRODUCTION Revision 1000.4  2004/06/01 18:44:41  gouriano
  5.  * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R6.31
  6.  * PRODUCTION
  7.  * ===========================================================================
  8.  */
  9. /*  $Id: ncbi_conn_stream.cpp,v 1000.4 2004/06/01 18:44:41 gouriano Exp $
  10.  * ===========================================================================
  11.  *
  12.  *                            PUBLIC DOMAIN NOTICE
  13.  *               National Center for Biotechnology Information
  14.  *
  15.  *  This software/database is a "United States Government Work" under the
  16.  *  terms of the United States Copyright Act.  It was written as part of
  17.  *  the author's official duties as a United States Government employee and
  18.  *  thus cannot be copyrighted.  This software/database is freely available
  19.  *  to the public for use. The National Library of Medicine and the U.S.
  20.  *  Government have not placed any restriction on its use or reproduction.
  21.  *
  22.  *  Although all reasonable efforts have been taken to ensure the accuracy
  23.  *  and reliability of the software and data, the NLM and the U.S.
  24.  *  Government do not and cannot warrant the performance or results that
  25.  *  may be obtained by using this software or data. The NLM and the U.S.
  26.  *  Government disclaim all warranties, express or implied, including
  27.  *  warranties of performance, merchantability or fitness for any particular
  28.  *  purpose.
  29.  *
  30.  *  Please cite the author in any work or product based on this material.
  31.  *
  32.  * ===========================================================================
  33.  *
  34.  * Authors:  Anton Lavrentiev,  Denis Vakatov
  35.  *
  36.  * File Description:
  37.  *   CONN-based C++ streams
  38.  *
  39.  *   See file <connect/ncbi_conn_stream.hpp> for more detailed information.
  40.  *
  41.  */
  42. #include <ncbi_pch.hpp>
  43. #include "ncbi_ansi_ext.h"
  44. #include "ncbi_conn_streambuf.hpp"
  45. #include <connect/ncbi_conn_stream.hpp>
  46. #include <connect/ncbi_core_cxx.hpp>
  47. #include <corelib/ncbithr.hpp>
  48. #include <memory>
  49. BEGIN_NCBI_SCOPE
  50. CConn_IOStream::CConn_IOStream(CONNECTOR connector, const STimeout* timeout,
  51.                                streamsize buf_size, bool do_tie) :
  52.     CNcbiIostream(0), m_CSb(0)
  53. {
  54.     auto_ptr<CConn_Streambuf>
  55.         csb(new CConn_Streambuf(connector, timeout, buf_size, do_tie));
  56.     if ( csb->GetCONN() ) {
  57.         init(csb.get());
  58.         m_CSb = csb.release();
  59.     } else
  60.         init(0); // according to the standard (27.4.4.1.3), badbit is set here
  61. }
  62. CConn_IOStream::~CConn_IOStream()
  63. {
  64.     Cleanup();
  65. }
  66. CONN CConn_IOStream::GetCONN(void) const
  67. {
  68.     return m_CSb ? m_CSb->GetCONN() : 0;
  69. }
  70. void CConn_IOStream::Cleanup(void)
  71. {
  72.     streambuf* sb = rdbuf();
  73.     delete sb;
  74.     if (sb != m_CSb)
  75.         delete m_CSb;
  76.     m_CSb = 0;
  77. #ifdef AUTOMATIC_STREAMBUF_DESTRUCTION
  78.     rdbuf(0);
  79. #endif
  80. }
  81. CConn_SocketStream::CConn_SocketStream(const string&   host,
  82.                                        unsigned short  port,
  83.                                        unsigned int    max_try,
  84.                                        const STimeout* timeout,
  85.                                        streamsize      buf_size)
  86.     : CConn_IOStream(SOCK_CreateConnector(host.c_str(), port, max_try),
  87.                      timeout, buf_size)
  88. {
  89.     return;
  90. }
  91. CConn_SocketStream::CConn_SocketStream(SOCK            sock,
  92.                                        unsigned int    max_try,
  93.                                        const STimeout* timeout,
  94.                                        streamsize      buf_size)
  95.     : CConn_IOStream(SOCK_CreateConnectorOnTop(sock, max_try),
  96.                      timeout, buf_size)
  97. {
  98.     return;
  99. }
  100. static CONNECTOR s_HttpConnectorBuilder(const SConnNetInfo* net_info_in,
  101.                                         const char*         url,
  102.                                         const char*         host,
  103.                                         unsigned short      port,
  104.                                         const char*         path,
  105.                                         const char*         args,
  106.                                         const char*         user_hdr,
  107.                                         THCC_Flags          flags,
  108.                                         const STimeout*     timeout)
  109. {
  110.     SConnNetInfo* net_info = net_info_in ?
  111.         ConnNetInfo_Clone(net_info_in) : ConnNetInfo_Create(0);
  112.     if (!net_info)
  113.         return 0;
  114.     if (url && !ConnNetInfo_ParseURL(net_info, url))
  115.         return 0;
  116.     if (host) {
  117.         strncpy0(net_info->host, host, sizeof(net_info->host) - 1);
  118.         net_info->port = port;
  119.     }
  120.     if (path)
  121.         strncpy0(net_info->path, path, sizeof(net_info->path) - 1);
  122.     if (args)
  123.         strncpy0(net_info->args, args, sizeof(net_info->args) - 1);
  124.     if (timeout && timeout != kDefaultTimeout) {
  125.         net_info->tmo     = *timeout;
  126.         net_info->timeout = &net_info->tmo;
  127.     } else if (!timeout)
  128.         net_info->timeout = 0;
  129.     CONNECTOR c = HTTP_CreateConnector(net_info, user_hdr, flags);
  130.     ConnNetInfo_Destroy(net_info);
  131.     return c;
  132. }
  133. CConn_HttpStream::CConn_HttpStream(const string&   host,
  134.                                    const string&   path,
  135.                                    const string&   args,
  136.                                    const string&   user_header,
  137.                                    unsigned short  port,
  138.                                    THCC_Flags      flags,
  139.                                    const STimeout* timeout,
  140.                                    streamsize      buf_size)
  141.     : CConn_IOStream(s_HttpConnectorBuilder(0,
  142.                                             0,
  143.                                             host.c_str(),
  144.                                             port,
  145.                                             path.c_str(),
  146.                                             args.c_str(),
  147.                                             user_header.c_str(),
  148.                                             flags,
  149.                                             timeout),
  150.                      timeout, buf_size)
  151. {
  152.     return;
  153. }
  154. CConn_HttpStream::CConn_HttpStream(const string&       url,
  155.                                    THCC_Flags          flags,
  156.                                    const STimeout*     timeout,
  157.                                    streamsize          buf_size)
  158.     : CConn_IOStream(s_HttpConnectorBuilder(0,
  159.                                             url.c_str(),
  160.                                             0,
  161.                                             0,
  162.                                             0,
  163.                                             0,
  164.                                             0,
  165.                                             flags,
  166.                                             timeout),
  167.                      timeout, buf_size)
  168. {
  169.     return;
  170. }
  171. CConn_HttpStream::CConn_HttpStream(const SConnNetInfo* net_info,
  172.                                    const string&       user_header,
  173.                                    THCC_Flags          flags,
  174.                                    const STimeout*     timeout,
  175.                                    streamsize          buf_size)
  176.     : CConn_IOStream(s_HttpConnectorBuilder(net_info,
  177.                                             0,
  178.                                             0,
  179.                                             0,
  180.                                             0,
  181.                                             0,
  182.                                             user_header.c_str(),
  183.                                             flags,
  184.                                             timeout),
  185.                      timeout, buf_size)
  186. {
  187.     return;
  188. }
  189. static CONNECTOR s_ServiceConnectorBuilder(const char*           service,
  190.                                            TSERV_Type            types,
  191.                                            const SConnNetInfo*   net_info_in,
  192.                                            const SSERVICE_Extra* params,
  193.                                            const STimeout*       timeout)
  194. {
  195.     SConnNetInfo* net_info = net_info_in ?
  196.         ConnNetInfo_Clone(net_info_in) : ConnNetInfo_Create(service);
  197.     if (!net_info)
  198.         return 0;
  199.     if (timeout && timeout != kDefaultTimeout) {
  200.         net_info->tmo     = *timeout;
  201.         net_info->timeout = &net_info->tmo;
  202.     } else if (!timeout)
  203.         net_info->timeout = 0;
  204.     CONNECTOR c = SERVICE_CreateConnectorEx(service, types, net_info, params);
  205.     ConnNetInfo_Destroy(net_info);
  206.     return c;
  207. }
  208. CConn_ServiceStream::CConn_ServiceStream(const string&         service,
  209.                                          TSERV_Type            types,
  210.                                          const SConnNetInfo*   net_info,
  211.                                          const SSERVICE_Extra* params,
  212.                                          const STimeout*       timeout,
  213.                                          streamsize            buf_size)
  214.     : CConn_IOStream(s_ServiceConnectorBuilder(service.c_str(),
  215.                                                types,
  216.                                                net_info,
  217.                                                params,
  218.                                                timeout),
  219.                      timeout, buf_size)
  220. {
  221.     return;
  222. }
  223. CConn_MemoryStream::CConn_MemoryStream(CRWLock*   lk,
  224.                                        bool       pass_lk_ownership,
  225.                                        streamsize buf_size)
  226.     : CConn_IOStream(MEMORY_CreateConnector
  227.                      (MT_LOCK_cxx2c(lk, pass_lk_ownership)),
  228.                      0, buf_size)
  229. {
  230.     return;
  231. }
  232. CConn_PipeStream::CConn_PipeStream(const string&         cmd,
  233.                                    const vector<string>& args,
  234.                                    CPipe::TCreateFlags   create_flags,
  235.                                    const STimeout*       timeout,
  236.                                    streamsize            buf_size)
  237.     : CConn_IOStream(PIPE_CreateConnector(cmd, args, create_flags, &m_Pipe),
  238.                      timeout, buf_size), m_Pipe()
  239. {
  240.     return;
  241. }
  242. CConn_PipeStream::~CConn_PipeStream()
  243. {
  244.     // Explicitly call Cleanup() to avoid using dead m_Pipe otherwise.
  245.     Cleanup();
  246. #ifndef AUTOMATIC_STREAMBUF_DESTRUCTION
  247.     rdbuf(0);
  248. #endif
  249. }
  250. CConn_NamedPipeStream::CConn_NamedPipeStream(const string&   pipename,
  251.                                              size_t          pipebufsize,
  252.                                              const STimeout* timeout,
  253.                                              streamsize      buf_size)
  254.     : CConn_IOStream(NAMEDPIPE_CreateConnector(pipename, pipebufsize),
  255.                      timeout, buf_size)
  256. {
  257.     return;
  258. }
  259. END_NCBI_SCOPE
  260. /*
  261.  * ---------------------------------------------------------------------------
  262.  * $Log: ncbi_conn_stream.cpp,v $
  263.  * Revision 1000.4  2004/06/01 18:44:41  gouriano
  264.  * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R6.31
  265.  *
  266.  * Revision 6.31  2004/05/17 20:58:13  gorelenk
  267.  * Added include of PCH ncbi_pch.hpp
  268.  *
  269.  * Revision 6.30  2004/03/22 16:54:32  ivanov
  270.  * Cosmetic changes
  271.  *
  272.  * Revision 6.29  2004/01/20 20:36:05  lavr
  273.  * Cease using HAVE_BUGGY_IOS_CALLBACKS in this file
  274.  *
  275.  * Revision 6.28  2003/11/12 17:45:08  lavr
  276.  * Change log fixed to reflect real changes in previous commit
  277.  *
  278.  * Revision 6.27  2003/11/12 16:37:50  ivanov
  279.  * CConn_PipeStream:: dtor added
  280.  *
  281.  * Revision 6.26  2003/10/23 12:16:27  lavr
  282.  * CConn_IOStream:: base class is now CNcbiIostream
  283.  *
  284.  * Revision 6.25  2003/09/23 21:05:23  lavr
  285.  * +CConn_PipeStream, +CConn_NamedPipeStream
  286.  *
  287.  * Revision 6.24  2003/08/25 14:40:43  lavr
  288.  * Employ new k..Timeout constants
  289.  *
  290.  * Revision 6.23  2003/05/20 19:08:28  lavr
  291.  * Do not use clear() in constructor ('cause it may throw an exception)
  292.  *
  293.  * Revision 6.22  2003/05/20 16:57:56  lavr
  294.  * Fix typo in log
  295.  *
  296.  * Revision 6.21  2003/05/20 16:47:19  lavr
  297.  * GetCONN() to check for NULL; constructor to init(0) if connection is bad
  298.  *
  299.  * Revision 6.20  2003/05/12 18:32:27  lavr
  300.  * Modified not to throw exceptions from stream buffer; few more improvements
  301.  *
  302.  * Revision 6.19  2003/04/29 19:58:24  lavr
  303.  * Constructor taking a URL added in CConn_HttpStream
  304.  *
  305.  * Revision 6.18  2003/04/14 21:08:15  lavr
  306.  * Take advantage of HAVE_BUGGY_IOS_CALLBACKS
  307.  *
  308.  * Revision 6.17  2003/04/11 17:57:29  lavr
  309.  * Take advantage of HAVE_IOS_XALLOC
  310.  *
  311.  * Revision 6.16  2003/03/25 22:17:18  lavr
  312.  * Set timeouts from ctors in SConnNetInfo, too, for display unambigously
  313.  *
  314.  * Revision 6.15  2002/10/28 15:42:18  lavr
  315.  * Use "ncbi_ansi_ext.h" privately and use strncpy0()
  316.  *
  317.  * Revision 6.14  2002/06/06 19:02:32  lavr
  318.  * Some housekeeping: log moved to the end
  319.  *
  320.  * Revision 6.13  2002/02/21 18:04:25  lavr
  321.  * +class CConn_MemoryStream
  322.  *
  323.  * Revision 6.12  2002/02/05 22:04:12  lavr
  324.  * Included header files rearranged
  325.  *
  326.  * Revision 6.11  2002/02/05 16:05:26  lavr
  327.  * List of included header files revised
  328.  *
  329.  * Revision 6.10  2002/01/28 21:29:25  lavr
  330.  * Use iostream(0) as a base class constructor
  331.  *
  332.  * Revision 6.9  2002/01/28 20:19:11  lavr
  333.  * Clean destruction of streambuf sub-object; no exception throw in GetCONN()
  334.  *
  335.  * Revision 6.8  2001/12/10 19:41:18  vakatov
  336.  * + CConn_SocketStream::CConn_SocketStream(SOCK sock, ....)
  337.  *
  338.  * Revision 6.7  2001/12/07 22:58:01  lavr
  339.  * GetCONN(): throw exception if the underlying streambuf is not CONN-based
  340.  *
  341.  * Revision 6.6  2001/09/24 20:26:17  lavr
  342.  * +SSERVICE_Extra* parameter for CConn_ServiceStream::CConn_ServiceStream()
  343.  *
  344.  * Revision 6.5  2001/01/12 23:49:19  lavr
  345.  * Timeout and GetCONN method added
  346.  *
  347.  * Revision 6.4  2001/01/11 23:04:06  lavr
  348.  * Bugfixes; tie is now done at streambuf level, not in iostream
  349.  *
  350.  * Revision 6.3  2001/01/11 17:22:41  thiessen
  351.  * fix args copy in s_HttpConnectorBuilder()
  352.  *
  353.  * Revision 6.2  2001/01/10 21:41:10  lavr
  354.  * Added classes: CConn_SocketStream, CConn_HttpStream, CConn_ServiceStream.
  355.  * Everything is now wordly documented.
  356.  *
  357.  * Revision 6.1  2001/01/09 23:35:25  vakatov
  358.  * Initial revision (draft, not tested in run-time)
  359.  *
  360.  * ===========================================================================
  361.  */