![]() |
RTBKit
0.9
Open-source framework to create real-time ad bidding systems.
|
00001 /* opstats_js.cc 00002 Jeremy Barnes, 5 August 2011 00003 Copyright (c) 2011 Datacratic. All rights reserved. 00004 00005 Operational stats JS wrapping 00006 */ 00007 00008 #include "soa/service/carbon_connector.h" 00009 #include "v8.h" 00010 #include "node.h" 00011 #include "soa/js/js_value.h" 00012 #include "soa/js/js_utils.h" 00013 #include "soa/js/js_wrapped.h" 00014 #include "soa/js/js_call.h" 00015 #include "soa/js/js_registry.h" 00016 #include "jml/arch/timers.h" 00017 #include "jml/utils/guard.h" 00018 #include "soa/sigslot/slot.h" 00019 #include "jml/utils/guard.h" 00020 #include "soa/sigslot/slot_js.h" 00021 #include <boost/static_assert.hpp> 00022 #include "soa/js/js_call.h" 00023 00024 00025 using namespace std; 00026 using namespace v8; 00027 using namespace node; 00028 00029 00030 namespace Datacratic { 00031 namespace JS { 00032 00033 extern Registry registry; 00034 00035 extern const char * const opstatsModule; 00036 00037 const char * const opstatsModule = "opstats"; 00038 00039 // Node.js initialization function; called to set up the OPSTATS object 00040 extern "C" void 00041 init(Handle<v8::Object> target) 00042 { 00043 Datacratic::JS::registry.init(target, opstatsModule); 00044 } 00045 00046 00047 /*****************************************************************************/ 00048 /* CARBON CONNECTOR JS */ 00049 /*****************************************************************************/ 00050 00051 const char * CarbonConnectorName = "CarbonConnector"; 00052 00053 struct CarbonConnectorJS 00054 : public JSWrapped2<CarbonConnector, CarbonConnectorJS, 00055 CarbonConnectorName, 00056 opstatsModule, true> { 00057 00058 CarbonConnectorJS() 00059 { 00060 } 00061 00062 CarbonConnectorJS(const v8::Handle<v8::Object> & This, 00063 const std::shared_ptr<CarbonConnector> & logger 00064 = std::shared_ptr<CarbonConnector>()) 00065 { 00066 wrap(This, logger); 00067 } 00068 00069 static Handle<v8::Value> 00070 New(const Arguments & args) 00071 { 00072 try { 00073 if (args.Length() > 0) { 00074 new CarbonConnectorJS(args.This(), 00075 std::shared_ptr<CarbonConnector> 00076 (new CarbonConnector())); 00077 return open(args); 00078 } 00079 else { 00080 new CarbonConnectorJS(args.This(), 00081 std::shared_ptr<CarbonConnector>()); 00082 } 00083 00084 return args.This(); 00085 } HANDLE_JS_EXCEPTIONS; 00086 } 00087 00088 static void Initialize() 00089 { 00090 Persistent<FunctionTemplate> t = Register(New); 00091 00092 // Instance methods 00093 NODE_SET_PROTOTYPE_METHOD(t, "open", open); 00094 00095 registerMemberFn(&CarbonConnector::stop, "close"); 00096 registerMemberFn(&CarbonConnector::stop, "stop"); 00097 registerMemberFn(&CarbonConnector::dump, "dump"); 00098 00099 registerMemberFn(&CarbonConnector::recordLevel, "recordLevel"); 00100 registerMemberFn(&CarbonConnector::recordOccurrence, 00101 "recordOccurrence"); 00102 registerMemberFn(&CarbonConnector::recordQuantity, "recordQuantity"); 00103 registerMemberFn(&CarbonConnector::recordOutcome, "recordOutcome"); 00104 } 00105 00106 static Handle<v8::Value> 00107 open(const Arguments & args) 00108 { 00109 try { 00110 // Make sure Node doesn't exit and we don't get GCd when the 00111 // event loop is running. 00112 00113 struct ev_loop * loop = ev_default_loop(0); 00114 ev_ref(loop); 00115 v8::Persistent<v8::Object> phandle 00116 = v8::Persistent<v8::Object>::New(args.This()); 00117 00118 auto cleanup = [=] () 00119 { 00120 v8::Persistent<v8::Object> handle = phandle; 00121 ev_unref(loop); 00122 handle.Clear(); 00123 handle.Dispose(); 00124 }; 00125 00126 ML::Call_Guard doCleanup(cleanup); // cleanup on exception 00127 00128 if (!args[0].IsEmpty() 00129 && args[0]->IsArray()) { 00130 getShared(args)->open(getArg<vector<string> > 00131 (args, 0, "carbonAddr"), 00132 getArg(args, 1, "statsPath"), 00133 cleanup); 00134 } 00135 else { 00136 getShared(args)->open(getArg<string>(args, 0, "carbonAddr"), 00137 getArg(args, 1, "statsPath"), 00138 cleanup); 00139 } 00140 00141 doCleanup.clear(); 00142 00143 return args.This(); 00144 } HANDLE_JS_EXCEPTIONS; 00145 } 00146 }; 00147 00148 std::shared_ptr<CarbonConnector> 00149 from_js(const JSValue & value, std::shared_ptr<CarbonConnector> *) 00150 { 00151 return CarbonConnectorJS::fromJS(value); 00152 } 00153 00154 CarbonConnector * 00155 from_js(const JSValue & value, CarbonConnector **) 00156 { 00157 return CarbonConnectorJS::fromJS(value).get(); 00158 } 00159 00160 std::shared_ptr<CarbonConnector> 00161 from_js_ref(const JSValue& value, std::shared_ptr<CarbonConnector>*) 00162 { 00163 return CarbonConnectorJS::fromJS(value); 00164 } 00165 00166 void 00167 to_js(JS::JSValue& value, const std::shared_ptr<CarbonConnector>& proxy) 00168 { 00169 value = CarbonConnectorJS::toJS(proxy); 00170 } 00171 00172 00173 } // namespace JS 00174 } // namespace Datacratic