void run(int argc, char **argv)
    {
        if (!parseCommandLine(argc, argv))
            return;

        SessionOptions sessionOptions;
        for (size_t i = 0; i < d_hosts.size(); ++i) {
            sessionOptions.setServerAddress(d_hosts[i].c_str(), d_port, i);
        }
        sessionOptions.setAuthenticationOptions(d_authOptions.c_str());
        sessionOptions.setAutoRestartOnDisconnection(true);
        sessionOptions.setNumStartAttempts(d_hosts.size());

        std::cout << "Connecting to port " << d_port
                  << " on ";
        std::copy(d_hosts.begin(), d_hosts.end(), std::ostream_iterator<std::string>(std::cout, " "));
        std::cout << std::endl;

        MyProviderEventHandler providerEventHandler(d_service);
        ProviderSession providerSession(
                sessionOptions, &providerEventHandler, 0);

        MyRequesterEventHandler requesterEventHandler;
        Session requesterSession(sessionOptions, &requesterEventHandler, 0);

        if (d_role == SERVER || d_role == BOTH) {
            serverRun(&providerSession);
        }
        if (d_role == CLIENT || d_role == BOTH) {
            clientRun(&requesterSession);
        }

        // wait for enter key to exit application
        std::cout << "Press ENTER to quit" << std::endl;
        char dummy[2];
        std::cin.getline(dummy, 2);
        if (d_role == SERVER || d_role == BOTH) {
            providerSession.stop();
        }
        if (d_role == CLIENT || d_role == BOTH) {
            requesterSession.stop();
        }
    }
    void run(int argc, char **argv)
    {
        if (!parseCommandLine(argc, argv))
            return;

        SessionOptions sessionOptions;
        for (size_t i = 0; i < d_hosts.size(); ++i) {
            sessionOptions.setServerAddress(d_hosts[i].c_str(), d_port, i);
        }
        sessionOptions.setAuthenticationOptions(d_authOptions.c_str());
        sessionOptions.setAutoRestartOnDisconnection(true);
        sessionOptions.setNumStartAttempts(d_hosts.size());

        std::cout << "Connecting to port " << d_port
                  << " on ";
        std::copy(d_hosts.begin(),
                  d_hosts.end(),
                  std::ostream_iterator<std::string>(std::cout, " "));
        std::cout << std::endl;

        MyEventHandler myEventHandler(d_service);
        ProviderSession session(sessionOptions, &myEventHandler, 0);
        if (!session.start()) {
            std::cerr <<"Failed to start session." << std::endl;
            return;
        }

        Identity providerIdentity = session.createIdentity();
        if (!d_authOptions.empty()) {
            bool isAuthorized = false;
            const char* authServiceName = "//blp/apiauth";
            if (session.openService(authServiceName)) {
                Service authService = session.getService(authServiceName);
                isAuthorized = authorize(authService, &providerIdentity,
                        &session, CorrelationId((void *)"auth"));
            }
            if (!isAuthorized) {
                std::cerr << "No authorization" << std::endl;
                return;
            }
        }

        ServiceRegistrationOptions serviceOptions;
        serviceOptions.setGroupId(d_groupId.c_str(), d_groupId.size());
        serviceOptions.setServicePriority(d_priority);
        if (!session.registerService(d_service.c_str(), providerIdentity, serviceOptions)) {
            std::cerr <<"Failed to register " << d_service << std::endl;
            return;
        }

        Service service = session.getService(d_service.c_str());

        // Now we will start publishing
        int value=1;
        while (g_running) {
            Event event = service.createPublishEvent();
            {
                MutexGuard guard(&g_mutex);
                if (0 == g_availableTopicCount) {
                    guard.release()->unlock();
                    SLEEP(1);
                    continue;
                }

                EventFormatter eventFormatter(event);
                for (MyStreams::iterator iter = g_streams.begin();
                    iter != g_streams.end(); ++iter) {
                    if (!iter->second->isAvailable()) {
                        continue;
                    }
                    std::ostringstream os;
                    os << ++value;

                    if (!iter->second->isInitialPaintSent()) {
                        eventFormatter.appendRecapMessage(
                                                        iter->second->topic());
                        eventFormatter.setElement("numRows", 25);
                        eventFormatter.setElement("numCols", 80);
                        eventFormatter.pushElement("rowUpdate");
                        for (int i = 1; i < 6; ++i) {
                            eventFormatter.appendElement();
                            eventFormatter.setElement("rowNum", i);
                            eventFormatter.pushElement("spanUpdate");
                            eventFormatter.appendElement();
                            eventFormatter.setElement("startCol", 1);
                            eventFormatter.setElement("length", 10);
                            eventFormatter.setElement("text", "INITIAL");
                            eventFormatter.setElement("fgColor", "RED");
                            eventFormatter.popElement();
                            eventFormatter.popElement();
                            eventFormatter.popElement();
                        }
                        eventFormatter.popElement();
                        iter->second->setIsInitialPaintSent(true);
                    }

                    eventFormatter.appendMessage("RowUpdate",
                                                 iter->second->topic());
                    eventFormatter.setElement("rowNum", 1);
                    eventFormatter.pushElement("spanUpdate");
                    eventFormatter.appendElement();
                    Name START_COL("startCol");
                    eventFormatter.setElement(START_COL, 1);
                    eventFormatter.setElement("length", int(os.str().size()));
                    eventFormatter.setElement("text", os.str().c_str());
                    eventFormatter.popElement();
                    eventFormatter.popElement();
                }
            }

            printMessages(event);
            session.publish(event);
            SLEEP(10);
        }
        session.stop();
    }
    void run(int argc, char **argv)
    {
        if (!parseCommandLine(argc, argv))
            return;

        SessionOptions sessionOptions;
        for (size_t i = 0; i < d_hosts.size(); ++i) {
            sessionOptions.setServerAddress(d_hosts[i].c_str(), d_port, i);
        }
        sessionOptions.setServerPort(d_port);
        sessionOptions.setAuthenticationOptions(d_authOptions.c_str());
        sessionOptions.setAutoRestartOnDisconnection(true);
        sessionOptions.setNumStartAttempts(d_hosts.size());

        MyEventHandler myEventHandler;
        ProviderSession session(sessionOptions, &myEventHandler, 0);

        std::cout << "Connecting to port " << d_port
                  << " on ";
        std::copy(d_hosts.begin(), d_hosts.end(), std::ostream_iterator<std::string>(std::cout, " "));
        std::cout << std::endl;

        if (!session.start()) {
            std::cerr <<"Failed to start session." << std::endl;
            return;
        }

        Identity providerIdentity = session.createIdentity();
        if (!d_authOptions.empty()) {
            bool isAuthorized = false;
            const char* authServiceName = "//blp/apiauth";
            if (session.openService(authServiceName)) {
                Service authService = session.getService(authServiceName);
                isAuthorized = authorize(authService, &providerIdentity,
                        &session, CorrelationId((void *)"auth"));
            }
            if (!isAuthorized) {
                std::cerr << "No authorization" << std::endl;
                return;
            }
        }

        TopicList topicList;
        topicList.add((d_service + d_topic).c_str(),
            CorrelationId(new MyStream(d_topic)));

        session.createTopics(
            &topicList,
            ProviderSession::AUTO_REGISTER_SERVICES,
            providerIdentity);
        // createTopics() is synchronous, topicList will be updated
        // with the results of topic creation (resolution will happen
        // under the covers)

        MyStreams myStreams;

        for (size_t i = 0; i < topicList.size(); ++i) {
            MyStream *stream = reinterpret_cast<MyStream*>(
                topicList.correlationIdAt(i).asPointer());
            int resolutionStatus = topicList.statusAt(i);
            if (resolutionStatus == TopicList::CREATED) {
                Topic topic = session.getTopic(topicList.messageAt(i));
                stream->setTopic(topic);
                myStreams.push_back(stream);
            }
            else {
                std::cout
                    << "Stream '"
                    << stream->getId()
                    << "': topic not resolved, status = "
                    << resolutionStatus
                    << std::endl;
            }
        }

        Service service = session.getService(d_service.c_str());

        // Now we will start publishing
        int value = 1;
        while (myStreams.size() > 0 && g_running) {
            Event event = service.createPublishEvent();
            EventFormatter eventFormatter(event);

            for (MyStreams::iterator iter = myStreams.begin();
                 iter != myStreams.end(); ++iter)
            {
                eventFormatter.appendMessage(MARKET_DATA, (*iter)->getTopic());
                eventFormatter.setElement("BID", 0.5 * ++value);
                eventFormatter.setElement("ASK", value);
            }

            MessageIterator iter(event);
            while (iter.next()) {
                Message msg = iter.message();
                msg.print(std::cout);
            }

            session.publish(event);
            SLEEP(10);
        }

        session.stop();
    }
    void run(int argc, char **argv)
    {
        if (!parseCommandLine(argc, argv)) return;

        SessionOptions sessionOptions;
        for (size_t i = 0; i < d_hosts.size(); ++i) {
            sessionOptions.setServerAddress(d_hosts[i].c_str(), d_port, i);
        }
        sessionOptions.setServerPort(d_port);
        sessionOptions.setAuthenticationOptions(d_authOptions.c_str());
        sessionOptions.setAutoRestartOnDisconnection(true);
        sessionOptions.setNumStartAttempts(d_hosts.size());

        std::cout << "Connecting to port " << d_port
                  << " on ";
        std::copy(d_hosts.begin(), d_hosts.end(), std::ostream_iterator<std::string>(std::cout, " "));
        std::cout << std::endl;

        MyEventHandler myEventHandler;
        ProviderSession session(sessionOptions, &myEventHandler, 0);
        if (!session.start()) {
            std::cerr <<"Failed to start session." << std::endl;
            return;
        }

        Identity providerIdentity = session.createIdentity();
        if (!d_authOptions.empty()) {
            bool isAuthorized = false;
            const char* authServiceName = "//blp/apiauth";
            if (session.openService(authServiceName)) {
                Service authService = session.getService(authServiceName);
                isAuthorized = authorize(authService, &providerIdentity,
                        &session, CorrelationId((void *)"auth"));
            }
            if (!isAuthorized) {
                std::cerr << "No authorization" << std::endl;
                return;
            }
        }

        TopicList topicList;
        topicList.add(((d_service + "/") + d_topic).c_str(),
            CorrelationId(new MyStream(d_topic)));

        session.createTopics(
            &topicList,
            ProviderSession::AUTO_REGISTER_SERVICES,
            providerIdentity);

        MyStreams myStreams;

        for (size_t i = 0; i < topicList.size(); ++i) {
            MyStream *stream = reinterpret_cast<MyStream*>(
                topicList.correlationIdAt(i).asPointer());
            int resolutionStatus = topicList.statusAt(i);
            if (resolutionStatus == TopicList::CREATED) {
                Topic topic = session.getTopic(topicList.messageAt(i));
                stream->setTopic(topic);
                myStreams.push_back(stream);
            }
            else {
                std::cout
                    << "Stream '"
                    << stream->getId()
                    << "': topic not resolved, status = "
                    << resolutionStatus
                    << std::endl;
            }
        }

        Service service = session.getService(d_service.c_str());

        // Now we will start publishing
        while (g_running) {
            Event event = service.createPublishEvent();
            EventFormatter eventFormatter(event);

            for (MyStreams::iterator iter = myStreams.begin();
                 iter != myStreams.end(); ++iter) {
                eventFormatter.appendMessage("PageData", (*iter)->getTopic());
                eventFormatter.pushElement("rowUpdate");

                eventFormatter.appendElement();
                eventFormatter.setElement("rowNum", 1);
                eventFormatter.pushElement("spanUpdate");

                eventFormatter.appendElement();
                eventFormatter.setElement("startCol", 20);
                eventFormatter.setElement("length", 4);
                eventFormatter.setElement("text", "TEST");
                eventFormatter.popElement();

                eventFormatter.appendElement();
                eventFormatter.setElement("startCol", 25);
                eventFormatter.setElement("length", 4);
                eventFormatter.setElement("text", "PAGE");
                eventFormatter.popElement();

                char buffer[10];
                time_t rawtime;
                std::time(&rawtime);
                int length = (int)std::strftime(buffer, 10, "%X", std::localtime(&rawtime));
                eventFormatter.appendElement();
                eventFormatter.setElement("startCol", 30);
                eventFormatter.setElement("length", length);
                eventFormatter.setElement("text", buffer);
                eventFormatter.setElement("attr", "BLINK");
                eventFormatter.popElement();

                eventFormatter.popElement();
                eventFormatter.popElement();

                eventFormatter.appendElement();
                eventFormatter.setElement("rowNum", 2);
                eventFormatter.pushElement("spanUpdate");
                eventFormatter.appendElement();
                eventFormatter.setElement("startCol", 20);
                eventFormatter.setElement("length", 9);
                eventFormatter.setElement("text", "---------");
                eventFormatter.setElement("attr", "UNDERLINE");
                eventFormatter.popElement();
                eventFormatter.popElement();
                eventFormatter.popElement();

                eventFormatter.appendElement();
                eventFormatter.setElement("rowNum", 3);
                eventFormatter.pushElement("spanUpdate");
                eventFormatter.appendElement();
                eventFormatter.setElement("startCol", 10);
                eventFormatter.setElement("length", 9);
                eventFormatter.setElement("text", "TEST LINE");
                eventFormatter.popElement();
                eventFormatter.appendElement();
                eventFormatter.setElement("startCol", 23);
                eventFormatter.setElement("length", 5);
                eventFormatter.setElement("text", "THREE");
                eventFormatter.popElement();
                eventFormatter.popElement();
                eventFormatter.popElement();
                eventFormatter.popElement();

                eventFormatter.setElement("contributorId", d_contributorId);
                eventFormatter.setElement("productCode", 1);
                eventFormatter.setElement("pageNumber", 1);
            }

            printMessages(event);

            session.publish(event);
            SLEEP(10);
        }

        session.stop();
    }