diff options
author | Leslie Linden <leslie@lindenlab.com> | 2011-12-07 15:04:39 -0800 |
---|---|---|
committer | Leslie Linden <leslie@lindenlab.com> | 2011-12-07 15:04:39 -0800 |
commit | 35020db3a9310742c9759673e3aa1bbb1d7aa02f (patch) | |
tree | 90e769fc43b8112461035b0b26c9b9b9df50877d /indra/newview/llmarketplacefunctions.cpp | |
parent | e860925818fe9376fa9abb0520680dba986ab42e (diff) |
Refactored marketplace inventory import HTTP requests and surrounding support to remove it from the UI panel code.
Diffstat (limited to 'indra/newview/llmarketplacefunctions.cpp')
-rw-r--r-- | indra/newview/llmarketplacefunctions.cpp | 302 |
1 files changed, 281 insertions, 21 deletions
diff --git a/indra/newview/llmarketplacefunctions.cpp b/indra/newview/llmarketplacefunctions.cpp index 2f8c5bc9ee..b9e02a36b4 100644 --- a/indra/newview/llmarketplacefunctions.cpp +++ b/indra/newview/llmarketplacefunctions.cpp @@ -29,52 +29,312 @@ #include "llmarketplacefunctions.h" #include "llagent.h" +#include "llhttpclient.h" +#include "llviewermedia.h" #include "llviewernetwork.h" -std::string getMarketplaceBaseURL() +// +// Helpers +// + +namespace LLMarketplaceImport { - std::string url = "https://marketplace.secondlife.com/"; + // Basic interface for this namespace + + bool inProgress(); + bool resultPending(); + U32 getResultStatus(); + const LLSD& getResults(); + + void establishMarketplaceSessionCookie(); + void pollStatus(); + void triggerImport(); + + // Internal state variables + + static std::string sMarketplaceCookie = ""; + static bool sImportInProgress = false; + static bool sImportGetPending = false; + static U32 sImportResultStatus = 0; + static LLSD sImportResults = LLSD::emptyMap(); + + + // Internal helper functions + + std::string getBaseURL() + { + std::string url = "https://marketplace.secondlife.com/"; + + if (!LLGridManager::getInstance()->isInProductionGrid()) + { + std::string gridLabel = utf8str_tolower(LLGridManager::getInstance()->getGridLabel()); + + if (gridLabel == "damballah") + { + url = "https://marketplace.secondlife-staging.com/"; + } + else + { + url = llformat("https://marketplace.%s.lindenlab.com/", gridLabel.c_str()); + } + } + + url += "api/1/"; + url += gAgent.getID().getString(); + url += "/inventory/"; + + return url; + } + + std::string getInventoryImportURL() + { + std::string url = getBaseURL(); - if (!LLGridManager::getInstance()->isInProductionGrid()) + url += "import/"; + + return url; + } + + // Responders + + class LLImportPostResponder : public LLHTTPClient::Responder + { + public: + LLImportPostResponder() : LLCurl::Responder() {} + + void completed(U32 status, const std::string& reason, const LLSD& content) + { + sImportInProgress = (status == MarketplaceErrorCodes::IMPORT_DONE); + sImportResultStatus = status; + } + }; + + class LLImportGetResponder : public LLHTTPClient::Responder + { + public: + LLImportGetResponder() : LLCurl::Responder() {} + + void completedHeader(U32 status, const std::string& reason, const LLSD& content) + { + sMarketplaceCookie = content["set-cookie"].asString(); + } + + void completed(U32 status, const std::string& reason, const LLSD& content) + { + sImportInProgress = (status == MarketplaceErrorCodes::IMPORT_PROCESSING); + sImportGetPending = false; + sImportResultStatus = status; + sImportResults = content; + } + }; + + // Coroutine testing +/* + std::string gTimeDelayDebugFunc = ""; + + void timeDelay(LLCoros::self& self, LLPanelMarketplaceOutbox* outboxPanel) { - std::string gridLabel = utf8str_tolower(LLGridManager::getInstance()->getGridLabel()); + waitForEventOn(self, "mainloop"); + + LLTimer delayTimer; + delayTimer.reset(); + delayTimer.setTimerExpirySec(5.0f); - if (gridLabel == "damballah") + while (!delayTimer.hasExpired()) { - url = "https://marketplace.secondlife-staging.com/"; + waitForEventOn(self, "mainloop"); } - else + + outboxPanel->onImportPostComplete(MarketplaceErrorCodes::IMPORT_DONE, LLSD::emptyMap()); + + gTimeDelayDebugFunc = ""; + } + + std::string gImportPollingFunc = ""; + + void importPoll(LLCoros::self& self, LLPanelMarketplaceOutbox* outboxPanel) + { + waitForEventOn(self, "mainloop"); + + while (outboxPanel->isImportInProgress()) { - url = llformat("https://marketplace.%s.lindenlab.com/", gridLabel.c_str()); + LLTimer delayTimer; + delayTimer.reset(); + delayTimer.setTimerExpirySec(5.0f); + + while (!delayTimer.hasExpired()) + { + waitForEventOn(self, "mainloop"); + } + + //outboxPanel-> } + + gImportPollingFunc = ""; } + +*/ + + // Basic API - url += "api/1/"; - url += gAgent.getID().getString(); - url += "/inventory/"; + bool inProgress() + { + return sImportInProgress; + } + + bool resultPending() + { + return sImportGetPending; + } + + U32 getResultStatus() + { + return sImportResultStatus; + } + + const LLSD& getResults() + { + return sImportResults; + } + + void establishMarketplaceSessionCookie() + { + sImportInProgress = true; + sImportGetPending = true; + + std::string url = getInventoryImportURL(); + + LLHTTPClient::get(url, new LLImportGetResponder(), LLViewerMedia::getHeaders()); + } + + void pollStatus() + { + sImportGetPending = true; - return url; + std::string url = getInventoryImportURL(); + + // Make the headers for the post + LLSD headers = LLSD::emptyMap(); + headers["Accept"] = "*/*"; + headers["Cookie"] = sMarketplaceCookie; + headers["Content-Type"] = "application/xml"; + headers["User-Agent"] = LLViewerMedia::getCurrentUserAgent(); + + LLHTTPClient::get(url, new LLImportGetResponder(), headers); + } + + void triggerImport() + { + sImportInProgress = true; + sImportResultStatus = MarketplaceErrorCodes::IMPORT_PROCESSING; + sImportResults = LLSD::emptyMap(); + + std::string url = getInventoryImportURL(); + + // Make the headers for the post + LLSD headers = LLSD::emptyMap(); + headers["Accept"] = "*/*"; + headers["Connection"] = "Keep-Alive"; + headers["Cookie"] = sMarketplaceCookie; + headers["Content-Type"] = "application/xml"; + headers["User-Agent"] = LLViewerMedia::getCurrentUserAgent(); + + LLHTTPClient::post(url, LLSD(), new LLImportPostResponder(), headers); + + // Set a timer (for testing only) + //gTimeDelayDebugFunc = LLCoros::instance().launch("LLPanelMarketplaceOutbox timeDelay", boost::bind(&timeDelay, _1, this)); + } } -std::string getMarketplaceURL_InventoryImport() + +// +// Interface class +// + + +//static +void LLMarketplaceInventoryImporter::update() { - std::string url = getMarketplaceBaseURL(); + if (instanceExists()) + { + LLMarketplaceInventoryImporter::instance().updateImport(); + } +} - url += "import/"; +LLMarketplaceInventoryImporter::LLMarketplaceInventoryImporter() + : mImportInProgress(false) + , mInitialized(false) + , mStatusChangedSignal(NULL) + , mStatusReportSignal(NULL) +{ +} - return url; +void LLMarketplaceInventoryImporter::initialize() +{ + if (!mInitialized) + { + LLMarketplaceImport::establishMarketplaceSessionCookie(); + } } +boost::signals2::connection LLMarketplaceInventoryImporter::setStatusChangedCallback(const status_changed_signal_t::slot_type& cb) +{ + if (mStatusChangedSignal == NULL) + { + mStatusChangedSignal = new status_changed_signal_t(); + } -static bool gMarketplaceImportEnabled = true; + return mStatusChangedSignal->connect(cb); +} -bool getMarketplaceImportEnabled() +boost::signals2::connection LLMarketplaceInventoryImporter::setStatusReportCallback(const status_report_signal_t::slot_type& cb) { - return gMarketplaceImportEnabled; + if (mStatusReportSignal == NULL) + { + mStatusReportSignal = new status_report_signal_t(); + } + + return mStatusReportSignal->connect(cb); } -void setMarketplaceImportEnabled(bool importEnabled) +bool LLMarketplaceInventoryImporter::triggerImport() { - gMarketplaceImportEnabled = importEnabled; + LLMarketplaceImport::triggerImport(); + + return LLMarketplaceImport::inProgress(); } + +void LLMarketplaceInventoryImporter::updateImport() +{ + const bool in_progress = LLMarketplaceImport::inProgress(); + + if (in_progress && !LLMarketplaceImport::resultPending()) + { + LLMarketplaceImport::pollStatus(); + } + + if (mImportInProgress != in_progress) + { + mImportInProgress = in_progress; + + if (mStatusChangedSignal) + { + (*mStatusChangedSignal)(mImportInProgress); + } + + // If we are no longer in progress, report results + if (!mImportInProgress && mStatusReportSignal) + { + if (mInitialized) + { + (*mStatusReportSignal)(LLMarketplaceImport::getResultStatus(), LLMarketplaceImport::getResults()); + } + else + { + mInitialized = true; + } + } + } +} + |