PageRenderTime 30ms CodeModel.GetById 13ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llmessage/tests/networkio.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 110 lines | 58 code | 10 blank | 42 comment | 6 complexity | 0d80accf86ec487e633272380c921a49 MD5 | raw file
  1/**
  2 * @file   networkio.h
  3 * @author Nat Goodspeed
  4 * @date   2009-01-09
  5 * @brief  
  6 * 
  7 * $LicenseInfo:firstyear=2009&license=viewerlgpl$
  8 * Second Life Viewer Source Code
  9 * Copyright (C) 2010, Linden Research, Inc.
 10 * 
 11 * This library is free software; you can redistribute it and/or
 12 * modify it under the terms of the GNU Lesser General Public
 13 * License as published by the Free Software Foundation;
 14 * version 2.1 of the License only.
 15 * 
 16 * This library is distributed in the hope that it will be useful,
 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 19 * Lesser General Public License for more details.
 20 * 
 21 * You should have received a copy of the GNU Lesser General Public
 22 * License along with this library; if not, write to the Free Software
 23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 24 * 
 25 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 26 * $/LicenseInfo$
 27 */
 28
 29#if ! defined(LL_NETWORKIO_H)
 30#define LL_NETWORKIO_H
 31
 32#include "llmemory.h"               // LLSingleton
 33#include "llapr.h"
 34#include "llares.h"
 35#include "llpumpio.h"
 36#include "llhttpclient.h"
 37
 38/*****************************************************************************
 39*   NetworkIO
 40*****************************************************************************/
 41// Doing this initialization in a class constructor makes sense. But we don't
 42// want to redo it for each different test. Nor do we want to do it at static-
 43// init time. Use the lazy, on-demand initialization we get from LLSingleton.
 44class NetworkIO: public LLSingleton<NetworkIO>
 45{
 46public:
 47    NetworkIO():
 48        mServicePump(NULL),
 49        mDone(false)
 50    {
 51        ll_init_apr();
 52        if (! gAPRPoolp)
 53        {
 54            throw std::runtime_error("Can't initialize APR");
 55        }
 56
 57        // Create IO Pump to use for HTTP Requests.
 58        mServicePump = new LLPumpIO(gAPRPoolp);
 59        LLHTTPClient::setPump(*mServicePump);
 60        if (ll_init_ares() == NULL || !gAres->isInitialized())
 61        {
 62            throw std::runtime_error("Can't start DNS resolver");
 63        }
 64
 65        // You can interrupt pump() without waiting the full timeout duration
 66        // by posting an event to the LLEventPump named "done".
 67        LLEventPumps::instance().obtain("done").listen("self",
 68                                                       boost::bind(&NetworkIO::done, this, _1));
 69    }
 70
 71    bool pump(F32 timeout=10)
 72    {
 73        // Reset the done flag so we don't pop out prematurely
 74        mDone = false;
 75        // Evidently the IO structures underlying LLHTTPClient need to be
 76        // "pumped". Do some stuff normally performed in the viewer's main
 77        // loop.
 78        LLTimer timer;
 79        while (timer.getElapsedTimeF32() < timeout)
 80        {
 81            if (mDone)
 82            {
 83//              std::cout << "NetworkIO::pump(" << timeout << "): breaking loop after "
 84//                        << timer.getElapsedTimeF32() << " seconds\n";
 85                return true;
 86            }
 87            pumpOnce();
 88        }
 89        return false;
 90    }
 91
 92    void pumpOnce()
 93    {
 94        gAres->process();
 95        mServicePump->pump();
 96        mServicePump->callback();
 97    }
 98
 99    bool done(const LLSD&)
100    {
101        mDone = true;
102        return false;
103    }
104
105private:
106    LLPumpIO* mServicePump;
107    bool mDone;
108};
109
110#endif /* ! defined(LL_NETWORKIO_H) */