bool parseCommandLine(int argc, char **argv)
    {
        int tokenCount = 0;
        for (int i = 1; i < argc; ++i) {
            if (!std::strcmp(argv[i],"-s") && i + 1 < argc) {
                d_securities.push_back(argv[++i]);
                continue;
            }

            if (!std::strcmp(argv[i],"-f") && i + 1 < argc) {
                d_field = std::string(argv[++i]);
                continue;
            }

            if (!std::strcmp(argv[i],"-t") && i + 1 < argc) {
                d_tokens.push_back(argv[++i]);
                ++tokenCount;
                std::cout << "User #" << tokenCount
                          << " token: " << argv[i]
                          << std::endl;
                continue;
            }
            if (!std::strcmp(argv[i],"-ip") && i + 1 < argc) {
                d_host = argv[++i];
                continue;
            }
            if (!std::strcmp(argv[i],"-p") &&  i + 1 < argc) {
                d_port = std::atoi(argv[++i]);
                continue;
            }
            printUsage();
            return false;
        }

        if (!d_tokens.size()) {
            std::cout << "No tokens were specified" << std::endl;
            printUsage();
            return false;
        }

        if (!d_securities.size()) {
            d_securities.push_back("MSFT US Equity");
        }

        for (size_t i = 0; i < d_securities.size(); ++i) {
            d_subscriptions.add(d_securities[i].c_str(), d_field.c_str(), "",
                CorrelationId(i));
        }
        return true;
    }
Exemple #2
0
// [[Rcpp::export]]
SEXP subscribe_Impl(SEXP con_, std::vector<std::string> securities, std::vector<std::string> fields, Rcpp::Function fun,
                    SEXP options_, SEXP identity_) {

    // via Rcpp Attributes we get a try/catch block with error propagation to R "for free"
    Session* session =
        reinterpret_cast<Session*>(checkExternalPointer(con_, "blpapi::Session*"));

    const std::string mdsrv = "//blp/mktdata";
    if (!session->openService(mdsrv.c_str())) {
        Rcpp::stop("Failed to open " + mdsrv);
    }

    SubscriptionList subscriptions;

    // put fields into comma delimited format
    std::string fields_collapsed(vectorToCSVString(fields));
    std::string options_collapsed("");
    if(options_ != R_NilValue && Rf_length(options_)) {
        std::vector<std::string> options(Rcpp::as< std::vector<std::string> >(options_));
        options_collapsed = vectorToCSVString(options);
    }

    for(const std::string& security : securities) {
        CorrelationId cid(reinterpret_cast<void*>(const_cast<char *>(security.c_str())));
        subscriptions.add(security.c_str(),fields_collapsed.c_str(),options_collapsed.c_str(),cid);
    }

    // check if identity was passed, if so, use it
    if(identity_ != R_NilValue) {
        Identity* ip;
        ip = reinterpret_cast<Identity*>(checkExternalPointer(identity_,"blpapi::Identity*"));
        session->subscribe(subscriptions,*ip);
    } else {
        session->subscribe(subscriptions);
    }

    while (true) {
        Event event = session->nextEvent();
        Rcpp::checkUserInterrupt();
        MessageIterator msgIter(event);
        while (msgIter.next()) {
            Message msg = msgIter.message();
            const char *topic = (char *)msg.correlationId().asPointer();
            if (event.eventType() == Event::SUBSCRIPTION_STATUS ||
                    event.eventType() == Event::SUBSCRIPTION_DATA) {

                Rcpp::List ans;
                auto it = BlpapiEventToString.find(event.eventType());
                if(it==BlpapiEventToString.end()) {
                    throw Rcpp::exception("Unknown event type.");
                }
                ans["event.type"] = it->second;
                ans["topic"] = topic;
                ans["data"] = recursiveParse(msg.asElement());
                // call user function
                fun(ans);
            }
        }
    }
    return R_NilValue;
}