RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
core/monitor/monitor_provider.cc
00001 /* monitor_provider.cc
00002    Wolfgang Sourdeau, January 2013
00003    Copyright (c) 2013 Datacratic.  All rights reserved.
00004    
00005    Rest endpoint queried by the monitor in the provider processes
00006 */
00007 
00008 #include <iostream>
00009 #include <boost/algorithm/string/trim.hpp>
00010 #include <jml/arch/exception_handler.h>
00011 #include "soa/service/service_base.h"
00012 #include "monitor_provider.h"
00013 
00014 using namespace std;
00015 
00016 namespace RTBKIT {
00017 
00018 MonitorProviderClient::
00019 MonitorProviderClient(const std::shared_ptr<zmq::context_t> & context,
00020                       MonitorProvider & provider)
00021         : RestProxy(context),
00022           provider_(provider),
00023           inhibit_(false),
00024           pendingRequest(false)
00025 {
00026     restUrlPath_ = "/v1/services/" + provider.getProviderName();
00027     onDone = std::bind(&MonitorProviderClient::onResponseReceived, this,
00028                        placeholders::_1, placeholders::_2, placeholders::_3);
00029 }
00030 
00031 MonitorProviderClient::
00032 ~MonitorProviderClient()
00033 {
00034     shutdown();
00035 }
00036 
00037 void
00038 MonitorProviderClient::
00039 init(std::shared_ptr<ConfigurationService> & config,
00040      const std::string & serviceName)
00041 {
00042     addPeriodic("MonitorProviderClient::postStatus", 1.0,
00043                 std::bind(&MonitorProviderClient::postStatus, this),
00044                 true);
00045 
00046     RestProxy::init(config, serviceName);
00047 }
00048 
00049 void
00050 MonitorProviderClient::
00051 shutdown()
00052 {
00053     sleepUntilIdle();
00054     RestProxy::shutdown();
00055 }
00056 
00057 void
00058 MonitorProviderClient::
00059 postStatus()
00060 {
00061     if (!inhibit_) {
00062         Guard(requestLock);
00063 
00064         if (pendingRequest) {
00065             fprintf(stderr, "MonitorProviderClient::checkStatus: last request is"
00066                     " still active\n");
00067         }
00068         else {
00069             string payload = provider_.getProviderIndicators().toString();
00070             pendingRequest = true;
00071             push(onDone, "POST", restUrlPath_, RestParams(), payload);
00072         }
00073     }
00074 }
00075 
00076 void
00077 MonitorProviderClient::
00078 onResponseReceived(exception_ptr ext, int responseCode, const string & body)
00079 {
00080     bool newStatus(false);
00081 
00082     if (responseCode == 200) {
00083         ML::Set_Trace_Exceptions notrace(false);
00084         try {
00085             Json::Value parsedBody = Json::parse(body);
00086             if (parsedBody.isMember("status") && parsedBody["status"] == "ok") {
00087                 newStatus = true;
00088             }
00089         }
00090         catch (const Json::Exception & exc) {
00091         }
00092     }
00093 
00094     pendingRequest = false;
00095 }
00096 
00097 } // namespace RTBKIT
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator