RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
js/bids_js.cc
00001 
00008 #include "currency_js.h"
00009 #include "bids_js.h"
00010 #include "soa/js/js_wrapped.h"
00011 #include "soa/js/js_call.h"
00012 #include "soa/js/js_utils.h"
00013 #include "soa/js/js_registry.h"
00014 #include "soa/sigslot/slot.h"
00015 #include "jml/utils/smart_ptr_utils.h"
00016 
00017 
00018 using namespace std;
00019 using namespace v8;
00020 using namespace node;
00021 using namespace RTBKIT;
00022 
00023 
00024 namespace Datacratic {
00025 namespace JS {
00026 
00027 
00028 /******************************************************************************/
00029 /* BID                                                                        */
00030 /******************************************************************************/
00031 
00032 extern const char* const BidName;
00033 const char * const BidName = "Bid";
00034 
00035 struct BidJS : public JSWrapped2<Bid, BidJS, BidName, rtbModule>
00036 {
00037     BidJS() {}
00038 
00039     BidJS(  v8::Handle<v8::Object> This,
00040             const std::shared_ptr<Bid>& bid = std::shared_ptr<Bid>())
00041     {
00042         HandleScope scope;
00043         wrap(This, bid);
00044     }
00045 
00046     static Handle<v8::Value>
00047     New(const Arguments& args)
00048     {
00049         try {
00050             new BidJS(args.This(), make_shared<Bid>());
00051             return args.This();
00052         }
00053         HANDLE_JS_EXCEPTIONS;
00054     }
00055 
00056     static void
00057     Initialize()
00058     {
00059         Persistent<FunctionTemplate> t = Register(New);
00060 
00061         registerROProperty(&Bid::spotIndex, "spotIndex");
00062         registerROProperty(&Bid::availableCreatives, "availableCreatives");
00063         registerROProperty(&Bid::price, "price");
00064         registerROProperty(&Bid::creativeIndex, "creativeIndex");
00065         registerROProperty(&Bid::priority, "priority");
00066         registerROProperty(&Bid::account, "account");
00067 
00068     }
00069 };
00070 
00071 Bid* from_js(const JSValue& value, Bid**)
00072 {
00073     return BidJS::fromJS(value).get();
00074 }
00075 
00076 Bid from_js(const JSValue& value, Bid*)
00077 {
00078     return *BidJS::fromJS(value);
00079 }
00080 
00081 void to_js(JS::JSValue& value, const Bid& bid)
00082 {
00083     value = BidJS::toJS(make_shared<Bid>(bid));
00084 }
00085 
00086 
00087 /******************************************************************************/
00088 /* BIDS                                                                       */
00089 /******************************************************************************/
00090 
00091 extern const char* const BidsName;
00092 const char * const BidsName = "Bids";
00093 
00094 struct BidsJS : public JSWrapped2<Bids, BidsJS, BidsName, rtbModule>
00095 {
00096     BidsJS() {}
00097 
00098     BidsJS( v8::Handle<v8::Object> This,
00099             const std::shared_ptr<Bids>& bids = std::shared_ptr<Bids>())
00100     {
00101         HandleScope scope;
00102         wrap(This, bids);
00103     }
00104 
00105     static Handle<v8::Value>
00106     New(const Arguments& args)
00107     {
00108         try {
00109             new BidsJS(args.This(), make_shared<Bids>());
00110             return args.This();
00111         }
00112         HANDLE_JS_EXCEPTIONS;
00113     }
00114 
00115 
00116     static void
00117     Initialize()
00118     {
00119         Persistent<FunctionTemplate> t = Register(New);
00120 
00121         NODE_SET_PROTOTYPE_METHOD(t, "bid", bid);
00122         NODE_SET_PROTOTYPE_METHOD(t, "addSource", addSource);
00123 
00124         registerROProperty(&Bids::size, "length");
00125         t->InstanceTemplate()->SetIndexedPropertyHandler(at, 0, check, 0, list);
00126     }
00127 
00133     static Handle<Value>
00134     bid(const Arguments& args)
00135     {
00136         try {
00137             unsigned spot     = getArg<unsigned> (args, 0, "spot");
00138             int creativeIndex = getArg<int>      (args, 1, "creativeIndex");
00139             Amount price      = getArg<Amount>   (args, 2, "price");
00140             double priority   = getArg<double>   (args, 3, 0.0, "priority");
00141 
00142             auto obj = getShared(args);
00143             (*obj)[spot].bid(creativeIndex, price, priority);
00144 
00145             return Handle<Value>();
00146         }
00147         HANDLE_JS_EXCEPTIONS;
00148     }
00149 
00152     static Handle<Value>
00153     addSource(const Arguments& args)
00154     {
00155         try {
00156             string source = getArg<string>(args, 0, "source");
00157             getShared(args)->dataSources.insert(source);
00158             return Handle<Value>();
00159         }
00160         HANDLE_JS_EXCEPTIONS;
00161     }
00162 
00163 
00164     static Handle<Value>
00165     at(uint32_t index, const v8::AccessorInfo & info)
00166     {
00167         try {
00168             auto data = getSharedPtr(info.This());
00169 
00170             if (index >= data->size())
00171                 return Handle<Value>();
00172 
00173             return Datacratic::JS::toJS((*data)[index]);
00174         } HANDLE_JS_EXCEPTIONS;
00175     }
00176 
00177     static Handle<Integer>
00178     check(uint32_t index, const v8::AccessorInfo & info)
00179     {
00180         try {
00181             auto data = getSharedPtr(info.This());
00182             return Integer::New(index >= data->size());
00183         }
00184         catch (...) {
00185             translateCurrentException();
00186             return Handle<Integer>();
00187         }
00188     }
00189 
00190     static Handle<Array>
00191     list(const v8::AccessorInfo & info)
00192     {
00193         try {
00194             auto data = getSharedPtr(info.This());
00195 
00196             auto array = Array::New(data->size());
00197             for (size_t i = 0; i < data->size(); ++i)
00198                 array->Set(i, Integer::New(i));
00199 
00200             return array;
00201         }
00202         catch (...) {
00203             translateCurrentException();
00204             return Handle<Array>();
00205         }
00206     }
00207 
00208 };
00209 
00210 Bids from_js(const JSValue& value, Bids*)
00211 {
00212     return *BidsJS::fromJS(value);
00213 }
00214 
00215 Bids* from_js(const JSValue& value, Bids**)
00216 {
00217     return BidsJS::fromJS(value).get();
00218 }
00219 
00220 void to_js(JS::JSValue& value, const Bids& bids)
00221 {
00222     value = BidsJS::toJS(std::make_shared<Bids>(bids));
00223 }
00224 
00225 } // namepsace JS
00226 } // namespace Datacratic
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator