summaryrefslogtreecommitdiff
path: root/indra/newview/llcoproceduremanager.cpp
blob: 3ecb323cab9307585cb15f89c26964b3bb28b32f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* @file llcoproceduremanager.cpp
* @author Rider Linden
* @brief Singleton class for managing asset uploads to the sim.
*
* $LicenseInfo:firstyear=2015&license=viewerlgpl$
* Second Life Viewer Source Code
* Copyright (C) 2015, Linden Research, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License only.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*
* Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
* $/LicenseInfo$
*/

#include "llviewerprecompiledheaders.h"
#include "linden_common.h" 

#include "llviewercontrol.h"

#include "llcoproceduremanager.h"

//=========================================================================
#define COROCOUNT 1

//=========================================================================
LLCoprocedureManager::LLCoprocedureManager():
    LLSingleton<LLCoprocedureManager>(),
    mPendingCoprocs(),
    mShutdown(false),
    mWakeupTrigger("CoprocedureManager", true),
    mCoroMapping(),
    mHTTPPolicy(LLCore::HttpRequest::DEFAULT_POLICY_ID)
{

    // *TODO: Retrieve the actual number of concurrent coroutines fro gSavedSettings and
    // clamp to a "reasonable" number.
    for (int count = 0; count < COROCOUNT; ++count)
    {
        LLCoreHttpUtil::HttpCoroutineAdapter::ptr_t httpAdapter =
            LLCoreHttpUtil::HttpCoroutineAdapter::ptr_t(
            new LLCoreHttpUtil::HttpCoroutineAdapter("uploadPostAdapter", mHTTPPolicy));

        std::string uploadCoro = LLCoros::instance().launch("LLCoprocedureManager::coprocedureInvokerCoro",
            boost::bind(&LLCoprocedureManager::coprocedureInvokerCoro, this, _1, httpAdapter));

        mCoroMapping.insert(CoroAdapterMap_t::value_type(uploadCoro, httpAdapter));
    }

    mWakeupTrigger.post(LLSD());
}

LLCoprocedureManager::~LLCoprocedureManager() 
{
    shutdown();
}

//=========================================================================

void LLCoprocedureManager::shutdown(bool hardShutdown)
{
    CoroAdapterMap_t::iterator it;

    for (it = mCoroMapping.begin(); it != mCoroMapping.end(); ++it)
    {
        if (!(*it).first.empty())
        {
            if (hardShutdown)
            {
                LLCoros::instance().kill((*it).first);
            }
        }
        if ((*it).second)
        {
            (*it).second->cancelYieldingOperation();
        }
    }

    mShutdown = true;
    mCoroMapping.clear();
    mPendingCoprocs.clear();
}

//=========================================================================
LLUUID LLCoprocedureManager::enqueueCoprocedure(const std::string &name, LLCoprocedureManager::CoProcedure_t proc)
{
    LLUUID id(LLUUID::generateNewID());

    mPendingCoprocs.push_back(QueuedCoproc::ptr_t(new QueuedCoproc(name, id, proc)));
    LL_INFOS() << "Coprocedure(" << name << ") enqueued with id=" << id.asString() << LL_ENDL;

    mWakeupTrigger.post(LLSD());

    return id;
}

void LLCoprocedureManager::cancelCoprocedure(const LLUUID &id)
{
    // first check the active coroutines.  If there, remove it and return.
    ActiveCoproc_t::iterator itActive = mActiveCoprocs.find(id);
    if (itActive != mActiveCoprocs.end())
    {
        LL_INFOS() << "Found and canceling active coprocedure with id=" << id.asString() << LL_ENDL;
        (*itActive).second->cancelYieldingOperation();
        mActiveCoprocs.erase(itActive);
        return;
    }

    for (CoprocQueue_t::iterator it = mPendingCoprocs.begin(); it != mPendingCoprocs.end(); ++it)
    {
        if ((*it)->mId == id)
        {
            LL_INFOS() << "Found and removing queued coroutine(" << (*it)->mName << ") with Id=" << id.asString() << LL_ENDL;
            mPendingCoprocs.erase(it);
            return;
        }
    }

    LL_INFOS() << "Coprocedure with Id=" << id.asString() << " was not found." << LL_ENDL;
}

//=========================================================================
void LLCoprocedureManager::coprocedureInvokerCoro(LLCoros::self& self, LLCoreHttpUtil::HttpCoroutineAdapter::ptr_t httpAdapter)
{
    LLCore::HttpRequest::ptr_t httpRequest(new LLCore::HttpRequest);

    while (!mShutdown)
    {
        waitForEventOn(self, mWakeupTrigger);
        if (mShutdown)
            break;
        
        while (!mPendingCoprocs.empty())
        {
            QueuedCoproc::ptr_t coproc = mPendingCoprocs.front();
            mPendingCoprocs.pop_front();
            mActiveCoprocs.insert(ActiveCoproc_t::value_type(coproc->mId, httpAdapter));

            LL_INFOS() << "Dequeued and invoking coprocedure(" << coproc->mName << ") with id=" << coproc->mId.asString() << LL_ENDL;

            try
            {
                coproc->mProc(self, httpAdapter, coproc->mId);
            }
            catch (std::exception &e)
            {
                LL_WARNS() << "Coprocedure(" << coproc->mName << ") id=" << coproc->mId.asString() <<
                    " threw an exception! Message=\"" << e.what() << "\"" << LL_ENDL;
            }
            catch (...)
            {
                LL_WARNS() << "A non std::exception was thrown from " << coproc->mName << " with id=" << coproc->mId << "." << LL_ENDL;
            }

            LL_INFOS() << "Finished coprocedure(" << coproc->mName << ")" << LL_ENDL;

            ActiveCoproc_t::iterator itActive = mActiveCoprocs.find(coproc->mId);
            if (itActive != mActiveCoprocs.end())
            {
                mActiveCoprocs.erase(itActive);
            }
        }
    }
}