![]() |
RTBKit
0.9
Open-source framework to create real-time ad bidding systems.
|
00001 /* auction_js.cc 00002 Jeremy Barnes, 6 April 2011 00003 Copyright (c) 2011 Datacratic. All rights reserved. 00004 00005 Auctions. 00006 */ 00007 00008 00009 #include "auction_js.h" 00010 #include "bid_request_js.h" 00011 #include "soa/js/js_wrapped.h" 00012 #include "jml/utils/smart_ptr_utils.h" 00013 #include "soa/types/js/id_js.h" 00014 00015 using namespace std; 00016 using namespace v8; 00017 using namespace node; 00018 00019 namespace Datacratic { 00020 namespace JS { 00021 00022 /*****************************************************************************/ 00023 /* AUCTION JS */ 00024 /*****************************************************************************/ 00025 00026 const char * AuctionName = "Auction"; 00027 00028 struct AuctionJS 00029 : public JSWrapped2<Auction, AuctionJS, AuctionName, 00030 rtbModule> { 00031 00032 AuctionJS(v8::Handle<v8::Object> This, 00033 const std::shared_ptr<Auction> & bid 00034 = std::shared_ptr<Auction>()) 00035 { 00036 HandleScope scope; 00037 wrap(This, bid); 00038 } 00039 00040 static Handle<v8::Value> 00041 New(const Arguments & args) 00042 { 00043 try { 00044 new AuctionJS(args.This(), ML::make_std_sp(new Auction())); 00045 return args.This(); 00046 } HANDLE_JS_EXCEPTIONS; 00047 } 00048 00049 static void 00050 Initialize() 00051 { 00052 Persistent<FunctionTemplate> t = Register(New); 00053 NODE_SET_PROTOTYPE_METHOD(t, "setRequest", setRequest); 00054 //NODE_SET_PROTOTYPE_METHOD(t, "getResponse", getResponse); 00055 NODE_SET_PROTOTYPE_METHOD(t, "notifyNoMoreBids", finish); 00056 NODE_SET_PROTOTYPE_METHOD(t, "finish", finish); 00057 00058 RWPropertyHandler<AuctionJS, Auction, Date, &Auction::start> 00059 handleStart(t, "start"); 00060 RWPropertyHandler<AuctionJS, Auction, Date, &Auction::expiry> 00061 handleExpiry(t, "expiry"); 00062 RWPropertyHandler<AuctionJS, Auction, Id, &Auction::id> 00063 handleId(t, "id"); 00064 00065 t->InstanceTemplate() 00066 ->SetAccessor(String::NewSymbol("request"), requestGetter, 00067 requestSetter, v8::Handle<v8::Value>(), DEFAULT, 00068 PropertyAttribute(DontDelete)); 00069 00070 t->InstanceTemplate() 00071 ->SetAccessor(String::NewSymbol("requestStr"), requestStrGetter, 00072 requestStrSetter, v8::Handle<v8::Value>(), DEFAULT, 00073 PropertyAttribute(DontDelete)); 00074 00075 t->InstanceTemplate() 00076 ->SetAccessor(String::NewSymbol("tooLate"), tooLateGetter, 00077 0, v8::Handle<v8::Value>(), DEFAULT, 00078 PropertyAttribute(ReadOnly | DontDelete)); 00079 00080 t->InstanceTemplate() 00081 ->SetAccessor(String::NewSymbol("responses"), responseGetter, 00082 0, v8::Handle<v8::Value>(), DEFAULT, 00083 PropertyAttribute(ReadOnly | DontDelete)); 00084 } 00085 00086 static v8::Handle<v8::Value> 00087 requestGetter(v8::Local<v8::String> property, 00088 const v8::AccessorInfo & info) 00089 { 00090 try { 00091 JSValue result; 00092 to_js(result, getShared(info.This())->request); 00093 return result; 00094 } HANDLE_JS_EXCEPTIONS; 00095 } 00096 00097 static void 00098 requestSetter(v8::Local<v8::String> property, 00099 v8::Local<v8::Value> value, 00100 const v8::AccessorInfo & info) 00101 { 00102 try { 00103 std::shared_ptr<BidRequest> br 00104 = getBidRequestSharedPointer(value); 00105 if (br) { 00106 getShared(info.This())->request = br; 00107 } 00108 else if (value->IsString()) { 00109 00110 string s = cstr(value); 00111 getShared(info.This())->request 00112 .reset(BidRequest::parse("datacratic", s)); 00113 getShared(info.This())->requestStr = s; 00114 } 00115 else { 00116 Json::Value request = JS::fromJS(value); 00117 00118 string s = request.toString(); 00119 getShared(info.This())->request 00120 .reset(BidRequest::parse("datacratic", s)); 00121 getShared(info.This())->requestStr = s; 00122 00123 } 00124 } HANDLE_JS_EXCEPTIONS_SETTER; 00125 } 00126 00127 static v8::Handle<v8::Value> 00128 requestStrGetter(v8::Local<v8::String> property, 00129 const v8::AccessorInfo & info) 00130 { 00131 try { 00132 return JS::toJS(getShared(info.This())->requestStr); 00133 } HANDLE_JS_EXCEPTIONS; 00134 } 00135 00136 static void 00137 requestStrSetter(v8::Local<v8::String> property, 00138 v8::Local<v8::Value> value, 00139 const v8::AccessorInfo & info) 00140 { 00141 try { 00142 Json::Value request = JS::fromJS(value); 00143 string s = request.toString(); 00144 getShared(info.This())->request 00145 .reset(BidRequest::parse("datacratic", s)); 00146 getShared(info.This())->requestStr = s; 00147 00148 } HANDLE_JS_EXCEPTIONS_SETTER; 00149 } 00150 00151 static v8::Handle<v8::Value> 00152 tooLateGetter(v8::Local<v8::String> property, 00153 const v8::AccessorInfo & info) 00154 { 00155 try { 00156 return v8::Boolean::New(getShared(info.This())->tooLate()); 00157 } HANDLE_JS_EXCEPTIONS; 00158 } 00159 00160 static v8::Handle<v8::Value> 00161 responseGetter(v8::Local<v8::String> property, 00162 const v8::AccessorInfo & info) 00163 { 00164 HandleScope scope; 00165 try { 00166 const auto & auction = *getShared(info.This()); 00167 vector<vector<Auction::Response> > responses 00168 = auction.getResponses(); 00169 00170 vector<vector<Json::Value> > respjson(responses.size()); 00171 for (unsigned spotNum = 0; spotNum < responses.size(); 00172 ++spotNum) { 00173 for (auto it = responses[spotNum].begin(), 00174 end = responses[spotNum].end(); 00175 it != end; ++it) 00176 respjson[spotNum].push_back(it->toJson()); 00177 } 00178 return scope.Close(JS::toJS(respjson)); 00179 } HANDLE_JS_EXCEPTIONS; 00180 } 00181 00182 static Handle<v8::Value> 00183 setRequest(const Arguments & args) 00184 { 00185 try { 00186 Json::Value request = getArg(args, 0, "request"); 00187 string s = request.toString(); 00188 std::shared_ptr<BidRequest> newRequest 00189 (BidRequest::parse("datacratic", s)); 00190 getShared(args)->request = newRequest; 00191 getShared(args)->requestStr = s; 00192 return args.This(); 00193 } HANDLE_JS_EXCEPTIONS; 00194 } 00195 00196 static Handle<v8::Value> 00197 getResponse(const Arguments & args) 00198 { 00199 try { 00200 return JS::toJS(getShared(args)->getResponseJson(getArg(args, 0, "slotNum"))); 00201 } HANDLE_JS_EXCEPTIONS; 00202 } 00203 00204 static Handle<v8::Value> 00205 finish(const Arguments & args) 00206 { 00207 try { 00208 getShared(args)->finish(); 00209 return args.This(); 00210 } HANDLE_JS_EXCEPTIONS; 00211 } 00212 }; 00213 00214 00215 std::shared_ptr<Auction> 00216 from_js(const JSValue & value, std::shared_ptr<Auction> *) 00217 { 00218 return AuctionJS::fromJS(value); 00219 } 00220 00221 Auction * 00222 from_js(const JSValue & value, Auction **) 00223 { 00224 return AuctionJS::fromJS(value).get(); 00225 } 00226 00227 00228 } // namespace JS 00229 } // namespace Datacratic
1.7.6.1