Пример #1
0
Settings parseCmdLine(CommandOptionParser& cp, int argc, char** argv)
{
    cp.parse(argc, argv);
    if (cp.getOption(optHelp).isPresent())
    {
        cp.displayOptionsHelp(std::cout);
        exit(0);
    }

    Settings s;

    s.basePath = cp.getOption(optPath).getParam(0, s.basePath);

    return s;
}
Пример #2
0
Settings parseCmdLine(CommandOptionParser& cp, int argc, char** argv)
{
    cp.parse(argc, argv);
    if (cp.getOption(optHelp).isPresent())
    {
        cp.displayOptionsHelp(std::cout);
        exit(0);
    }

    Settings s;

    s.dirPrefix = cp.getOption(optPrefix).getParam(0, s.dirPrefix);
    s.channel = cp.getOption(optChannel).getParam(0, s.channel);
    s.streamId = cp.getOption(optStreamId).getParamAsInt(0, 1, INT32_MAX, s.streamId);
    s.fragmentCountLimit = cp.getOption(optFrags).getParamAsInt(0, 1, INT32_MAX, s.fragmentCountLimit);
    return s;
}
Пример #3
0
Settings parseCmdLine(CommandOptionParser& cp, int argc, char** argv)
{
    cp.parse(argc, argv);
    if (cp.getOption(optHelp).isPresent())
    {
        cp.displayOptionsHelp(std::cout);
        exit(0);
    }

    Settings s;

    s.dirPrefix = cp.getOption(optPrefix).getParam(0, s.dirPrefix);
    s.channel = cp.getOption(optChannel).getParam(0, s.channel);
    s.streamId = cp.getOption(optStreamId).getParamAsInt(0, 1, INT32_MAX, s.streamId);
    s.numberOfMessages = cp.getOption(optMessages).getParamAsInt(0, 0, INT32_MAX, s.numberOfMessages);
    s.lingerTimeoutMs = cp.getOption(optLinger).getParamAsInt(0, 0, 60 * 60 * 1000, s.lingerTimeoutMs);

    return s;
}
Пример #4
0
Settings parseCmdLine(CommandOptionParser& cp, int argc, char** argv)
{
    cp.parse(argc, argv);
    if (cp.getOption(optHelp).isPresent())
    {
        cp.displayOptionsHelp(std::cout);
        exit(0);
    }

    Settings s;

    s.dirPrefix = cp.getOption(optPrefix).getParam(0, s.dirPrefix);
    s.pingChannel = cp.getOption(optPingChannel).getParam(0, s.pingChannel);
    s.pongChannel = cp.getOption(optPongChannel).getParam(0, s.pongChannel);
    s.pingStreamId = cp.getOption(optPingStreamId).getParamAsInt(0, 1, INT32_MAX, s.pingStreamId);
    s.pongStreamId = cp.getOption(optPongStreamId).getParamAsInt(0, 1, INT32_MAX, s.pongStreamId);
    s.numberOfMessages = cp.getOption(optMessages).getParamAsLong(0, 0, INT64_MAX, s.numberOfMessages);
    s.messageLength = cp.getOption(optLength).getParamAsInt(0, sizeof(std::int64_t), INT32_MAX, s.messageLength);
    s.fragmentCountLimit = cp.getOption(optFrags).getParamAsInt(0, 1, INT32_MAX, s.fragmentCountLimit);
    return s;
}
Пример #5
0
int main(int argc, char** argv)
{
    CommandOptionParser cp;
    cp.addOption(CommandOption(optHelp, 0, 0, "                Displays help information."));
    cp.addOption(CommandOption(optPrefix, 1, 1, "dir             Prefix directory for aeron driver."));

    signal(SIGINT, sigIntHandler);

    try
    {
        Settings settings = parseCmdLine(cp, argc, argv);

//        if (settings.dirPrefix != "")
//        {
//            context.aeronDir(settings.dirPrefix);
//        }

        while (running)
        {

        }

        std::cout << "Shutting Down..." << std::endl;
    }
    catch (CommandOptionException& e)
    {
        std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
        cp.displayOptionsHelp(std::cerr);
        return -1;
    }
    catch (std::exception& e)
    {
        std::cerr << "FAILED: " << e.what() << " : " << std::endl;
        return -1;
    }

    return 0;
}
Пример #6
0
int main(int argc, char **argv)
{
    CommandOptionParser cp;
    cp.addOption(CommandOption (optHelp,         0, 0, "                Displays help information."));
    cp.addOption(CommandOption (optPrefix,       1, 1, "dir             Prefix directory for aeron driver."));
    cp.addOption(CommandOption (optPingChannel,  1, 1, "channel         Ping Channel."));
    cp.addOption(CommandOption (optPongChannel,  1, 1, "channel         Pong Channel."));
    cp.addOption(CommandOption (optPingStreamId, 1, 1, "streamId        Ping Stream ID."));
    cp.addOption(CommandOption (optPongStreamId, 1, 1, "streamId        Pong Stream ID."));
    cp.addOption(CommandOption (optFrags,        1, 1, "limit           Fragment Count Limit."));

    signal (SIGINT, sigIntHandler);

    try
    {
        Settings settings = parseCmdLine(cp, argc, argv);

        std::cout << "Subscribing Ping at " << settings.pingChannel << " on Stream ID " << settings.pingStreamId << std::endl;
        std::cout << "Publishing Pong at " << settings.pongChannel << " on Stream ID " << settings.pongStreamId << std::endl;

        aeron::Context context;

        if (settings.dirPrefix != "")
        {
            context.aeronDir(settings.dirPrefix);
        }

        context.newSubscriptionHandler(
            [](const std::string& channel, std::int32_t streamId, std::int64_t correlationId)
            {
                std::cout << "Subscription: " << channel << " " << correlationId << ":" << streamId << std::endl;
            });

        context.newPublicationHandler(
            [](const std::string& channel, std::int32_t streamId, std::int32_t sessionId, std::int64_t correlationId)
            {
                std::cout << "Publication: " << channel << " " << correlationId << ":" << streamId << ":" << sessionId << std::endl;
            });

        context.availableImageHandler([](
            Image &image,
            const std::string &channel,
            std::int32_t streamId,
            std::int32_t sessionId,
            std::int64_t joiningPosition,
            const std::string &sourceIdentity)
        {
            std::cout << "Available image on " << channel << " streamId=" << streamId << " sessionId=" << sessionId;
            std::cout << " at position=" << joiningPosition << " from " << sourceIdentity << std::endl;
        });

        context.unavailableImageHandler(
            [](Image &image, const std::string &channel, std::int32_t streamId, std::int32_t sessionId,
                std::int64_t position)
            {
                std::cout << "Unavailable image on " << channel << "streamId=" << streamId << " sessionId=" << sessionId;
                std::cout << " at position=" << position << std::endl;
            });

        Aeron aeron(context);

        std::int64_t subscriptionId = aeron.addSubscription(settings.pingChannel, settings.pingStreamId);
        std::int64_t publicationId = aeron.addPublication(settings.pongChannel, settings.pongStreamId);

        std::shared_ptr<Subscription> pingSubscription = aeron.findSubscription(subscriptionId);
        while (!pingSubscription)
        {
            std::this_thread::yield();
            pingSubscription = aeron.findSubscription(subscriptionId);
        }

        std::shared_ptr<Publication> pongPublication = aeron.findPublication(publicationId);
        while (!pongPublication)
        {
            std::this_thread::yield();
            pongPublication = aeron.findPublication(publicationId);
        }

        BusySpinIdleStrategy idleStrategy;
        BusySpinIdleStrategy pingHandlerIdleStrategy;
        FragmentAssembler fragmentAssembler(
            [&](AtomicBuffer& buffer, index_t offset, index_t length, Header& header)
            {
                while (pongPublication->offer(buffer, offset, length) < 0L)
                {
                    pingHandlerIdleStrategy.idle(0);
                }
            });

        fragment_handler_t handler = fragmentAssembler.handler();

        while (running)
        {
            const int fragmentsRead = pingSubscription->poll(handler, settings.fragmentCountLimit);

            idleStrategy.idle(fragmentsRead);
        }

        std::cout << "Shutting down...\n";
    }
    catch (CommandOptionException& e)
    {
        std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
        cp.displayOptionsHelp(std::cerr);
        return -1;
    }
    catch (SourcedException& e)
    {
        std::cerr << "FAILED: " << e.what() << " : " << e.where() << std::endl;
        return -1;
    }
    catch (std::exception& e)
    {
        std::cerr << "FAILED: " << e.what() << " : " << std::endl;
        return -1;
    }

    return 0;
}
Пример #7
0
int main(int argc, char **argv)
{
    CommandOptionParser cp;
    cp.addOption(CommandOption (optHelp,     0, 0, "                Displays help information."));
    cp.addOption(CommandOption (optPrefix,   1, 1, "dir             Prefix directory for aeron driver."));
    cp.addOption(CommandOption (optChannel,  1, 1, "channel         Channel."));
    cp.addOption(CommandOption (optStreamId, 1, 1, "streamId        Stream ID."));
    cp.addOption(CommandOption (optFrags,    1, 1, "limit           Fragment Count Limit."));

    signal (SIGINT, sigIntHandler);

    try
    {
        Settings settings = parseCmdLine(cp, argc, argv);

        std::cout << "Subscribing to channel " << settings.channel << " on Stream ID " << settings.streamId << std::endl;

        aeron::Context context;

        if (settings.dirPrefix != "")
        {
            context.aeronDir(settings.dirPrefix);
        }

        context.newSubscriptionHandler(
            [](const std::string& channel, std::int32_t streamId, std::int64_t correlationId)
            {
                std::cout << "Subscription: " << channel << " " << correlationId << ":" << streamId << std::endl;
            });

        context.newConnectionHandler([](
            const std::string& channel,
            std::int32_t streamId,
            std::int32_t sessionId,
            std::int64_t joiningPosition,
            const std::string& sourceIdentity)
            {
                std::cout << "New connection on " << channel << " streamId=" << streamId << " sessionId=" << sessionId;
                std::cout << " at position=" << joiningPosition << " from " << sourceIdentity << std::endl;
            });

        context.inactiveConnectionHandler(
            [](const std::string& channel, std::int32_t streamId, std::int32_t sessionId, std::int64_t position)
            {
                std::cout << "Inactive connection on " << channel << "streamId=" << streamId << " sessionId=" << sessionId;
                std::cout << " at position=" << position << std::endl;
            });

        Aeron aeron(context);

        // add the subscription to start the process
        std::int64_t id = aeron.addSubscription(settings.channel, settings.streamId);

        std::shared_ptr<Subscription> subscription = aeron.findSubscription(id);
        // wait for the subscription to be valid
        while (!subscription)
        {
            std::this_thread::yield();
            subscription = aeron.findSubscription(id);
        }

        BusySpinIdleStrategy idleStrategy;
        RateReporter rateReporter(std::chrono::seconds(1), printRate);
        FragmentAssemblyAdapter fragmentAssemblyAdapter(rateReporterHandler(rateReporter));
        fragment_handler_t handler = fragmentAssemblyAdapter.handler();

        std::thread rateReporterThread([&](){ rateReporter.run(); });

        while (running)
        {
            const int fragmentsRead = subscription->poll(handler, settings.fragmentCountLimit);

            idleStrategy.idle(fragmentsRead);
        }

        std::cout << "Shutting down...\n";

        rateReporter.halt();
        rateReporterThread.join();
    }
    catch (CommandOptionException& e)
    {
        std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
        cp.displayOptionsHelp(std::cerr);
        return -1;
    }
    catch (SourcedException& e)
    {
        std::cerr << "FAILED: " << e.what() << " : " << e.where() << std::endl;
        return -1;
    }
    catch (std::exception& e)
    {
        std::cerr << "FAILED: " << e.what() << " : " << std::endl;
        return -1;
    }

    return 0;
}
Пример #8
0
int main (int argc, char** argv)
{
    CommandOptionParser cp;
    cp.addOption(CommandOption(optHelp,   0, 0, "                Displays help information."));
    cp.addOption(CommandOption(optPath,   1, 1, "basePath        Base Path to shared memory. Default: " + Context::defaultAeronPath()));

    try
    {
        Settings settings = parseCmdLine(cp, argc, argv);
        const std::string filename = LossReportDescriptor::file(settings.basePath);

        if (MemoryMappedFile::getFileSize(filename.c_str()) < 0)
        {
            std::cerr << "Loss report does not exist: " << filename << std::endl;
            std::exit(1);
        }

        MemoryMappedFile::ptr_t lossReportFile = MemoryMappedFile::mapExistingReadOnly(filename.c_str());
        AtomicBuffer buffer(lossReportFile->getMemoryPtr(), lossReportFile->getMemorySize());

        std::cout <<
            "OBSERVATION_COUNT, " <<
            "TOTAL_BYTES_LOST, " <<
            "FIRST_OBSERVATION, " <<
            "LAST_OBSERVATION, " <<
            "SESSION_ID, " <<
            "STREAM_ID, " <<
            "CHANNEL, " <<
            "SOURCE" << std::endl;

        const int entriesRead = LossReportReader::read(
            buffer,
            [](
                std::int64_t observationCount,
                std::int64_t totalBytesLost,
                std::int64_t firstObservationTimestamp,
                std::int64_t lastObservationTimestamp,
                std::int32_t sessionId,
                std::int32_t streamId,
                const std::string &channel,
                const std::string &source)
            {
                std::cout << std::to_string(observationCount) << ",";
                std::cout << std::to_string(totalBytesLost) << ",";
                std::cout << formatDate(firstObservationTimestamp) << ",";
                std::cout << formatDate(lastObservationTimestamp) << ",";
                std::cout << std::to_string(sessionId) << ",";
                std::cout << std::to_string(streamId) << ",";
                std::cout << channel << ",";
                std::cout << source << std::endl;
            });

        std::cout << std::to_string(entriesRead) << " entries read" << std::endl;
    }
    catch (const CommandOptionException& e)
    {
        std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
        cp.displayOptionsHelp(std::cerr);
        return -1;
    }
    catch (const SourcedException& e)
    {
        std::cerr << "FAILED: " << e.what() << " : " << e.where() << std::endl;
        return -1;
    }
    catch (const std::exception& e)
    {
        std::cerr << "FAILED: " << e.what() << " : " << std::endl;
        return -1;
    }

    return 0;
}
Пример #9
0
int main (int argc, char** argv)
{
    CommandOptionParser cp;
    cp.addOption(CommandOption(optHelp,   0, 0, "                Displays help information."));
    cp.addOption(CommandOption(optPath,   1, 1, "basePath        Base Path to shared memory. Default: " + Context::defaultAeronPath()));
    cp.addOption(CommandOption(optPeriod, 1, 1, "update period   Update period in milliseconds. Default: 1000ms"));

    signal (SIGINT, sigIntHandler);

    try
    {
        Settings settings = parseCmdLine(cp, argc, argv);

        MemoryMappedFile::ptr_t cncFile = MemoryMappedFile::mapExistingReadOnly(
            (settings.basePath + "/" + CncFileDescriptor::CNC_FILE).c_str());

        const std::int32_t cncVersion = CncFileDescriptor::cncVersionVolatile(cncFile);

        if (cncVersion != CncFileDescriptor::CNC_VERSION)
        {
            std::cerr << "CNC version not supported: file version=" << cncVersion << std::endl;
            return -1;
        }

        const std::int64_t clientLivenessTimeoutNs = CncFileDescriptor::clientLivenessTimeout(cncFile);
        const std::int64_t pid = CncFileDescriptor::pid(cncFile);

        AtomicBuffer metadataBuffer = CncFileDescriptor::createCounterMetadataBuffer(cncFile);
        AtomicBuffer valuesBuffer = CncFileDescriptor::createCounterValuesBuffer(cncFile);

        CountersReader counters(metadataBuffer, valuesBuffer);

        while(running)
        {
            time_t rawtime;
            char currentTime[80];

            ::time(&rawtime);
            struct tm localTm;

#ifdef _MSC_VER
            _localtime_s(&localTm, &rawTime);
#else
            ::localtime_r(&rawtime, &localTm);
#endif
            ::strftime(currentTime, sizeof(currentTime) - 1, "%H:%M:%S", &localTm);

            std::printf("\033[H\033[2J");

            std::printf(
                "%s - Aeron Stat (CnC v%" PRId32 "), pid %" PRId64 ", client liveness %s ns\n",
                currentTime, cncVersion, pid, toStringWithCommas(clientLivenessTimeoutNs).c_str());
            std::printf("===========================\n");

            counters.forEach([&](std::int32_t counterId, std::int32_t, const AtomicBuffer&, const std::string& l)
            {
                std::int64_t value = counters.getCounterValue(counterId);

                std::printf("%3d: %20s - %s\n", counterId, toStringWithCommas(value).c_str(), l.c_str());
            });

            std::this_thread::sleep_for(std::chrono::milliseconds(settings.updateIntervalMs));
        }

        std::cout << "Exiting..." << std::endl;
    }
    catch (const CommandOptionException& e)
    {
        std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
        cp.displayOptionsHelp(std::cerr);
        return -1;
    }
    catch (const SourcedException& e)
    {
        std::cerr << "FAILED: " << e.what() << " : " << e.where() << std::endl;
        return -1;
    }
    catch (const std::exception& e)
    {
        std::cerr << "FAILED: " << e.what() << " : " << std::endl;
        return -1;
    }

    return 0;
}
Пример #10
0
int main(int argc, char** argv)
{
    CommandOptionParser cp;
    cp.addOption(CommandOption (optHelp,     0, 0, "                Displays help information."));
    cp.addOption(CommandOption (optPrefix,   1, 1, "dir             Prefix directory for aeron driver."));
    cp.addOption(CommandOption (optChannel,  1, 1, "channel         Channel."));
    cp.addOption(CommandOption (optStreamId, 1, 1, "streamId        Stream ID."));

    signal (SIGINT, sigIntHandler);

    try
    {
        Settings settings = parseCmdLine(cp, argc, argv);

        std::cout << "Subscribing to channel " << settings.channel << " on Stream ID " << settings.streamId << std::endl;

        aeron::Context context;

        if (settings.dirPrefix != "")
        {
            context.aeronDir(settings.dirPrefix);
        }

        context.newSubscriptionHandler(
            [](const std::string& channel, std::int32_t streamId, std::int64_t correlationId)
            {
                std::cout << "Subscription: " << channel << " " << correlationId << ":" << streamId << std::endl;
            });

        context.availableImageHandler([](Image &image)
            {
                std::cout << "Available image correlationId=" << image.correlationId() << " sessionId=" << image.sessionId();
                std::cout << " at position=" << image.position() << " from " << image.sourceIdentity() << std::endl;
            });

        context.unavailableImageHandler([](Image &image)
            {
                std::cout << "Unavailable image on correlationId=" << image.correlationId() << " sessionId=" << image.sessionId();
                std::cout << " at position=" << image.position() << " from " << image.sourceIdentity() << std::endl;
            });

        std::shared_ptr<Aeron> aeron = Aeron::connect(context);

        // add the subscription to start the process
        std::int64_t id = aeron->addSubscription(settings.channel, settings.streamId);

        std::shared_ptr<Subscription> subscription = aeron->findSubscription(id);
        // wait for the subscription to be valid
        while (!subscription)
        {
            std::this_thread::yield();
            subscription = aeron->findSubscription(id);
        }

        fragment_handler_t handler = printStringMessage();
        SleepingIdleStrategy idleStrategy(IDLE_SLEEP_MS);

        while (running)
        {
            const int fragmentsRead = subscription->poll(handler, FRAGMENTS_LIMIT);

            idleStrategy.idle(fragmentsRead);
        }

    }
    catch (CommandOptionException& e)
    {
        std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
        cp.displayOptionsHelp(std::cerr);
        return -1;
    }
    catch (SourcedException& e)
    {
        std::cerr << "FAILED: " << e.what() << " : " << e.where() << std::endl;
        return -1;
    }
    catch (std::exception& e)
    {
        std::cerr << "FAILED: " << e.what() << " : " << std::endl;
        return -1;
    }

    return 0;
}
Пример #11
0
int main(int argc, char **argv)
{
    CommandOptionParser cp;
    cp.addOption(CommandOption (optHelp,     0, 0, "                Displays help information."));
    cp.addOption(CommandOption (optRandLen,  0, 0, "                Random Message Length."));
    cp.addOption(CommandOption (optProgress, 0, 0, "                Print rate progress while sending."));
    cp.addOption(CommandOption (optPrefix,   1, 1, "dir             Prefix directory for aeron driver."));
    cp.addOption(CommandOption (optChannel,  1, 1, "channel         Channel."));
    cp.addOption(CommandOption (optStreamId, 1, 1, "streamId        Stream ID."));
    cp.addOption(CommandOption (optMessages, 1, 1, "number          Number of Messages."));
    cp.addOption(CommandOption (optLength,   1, 1, "length          Length of Messages."));
    cp.addOption(CommandOption (optLinger,   1, 1, "milliseconds    Linger timeout in milliseconds."));
    cp.addOption(CommandOption (optFrags,    1, 1, "limit           Fragment Count Limit."));

    signal (SIGINT, sigIntHandler);

    try
    {
        Settings settings = parseCmdLine(cp, argc, argv);

        std::cout << "Subscribing to channel " << settings.channel << " on Stream ID " << settings.streamId << std::endl;

        ::setlocale(LC_NUMERIC, "");

        std::printf(
            "Streaming %'ld messages of%s size %d bytes to %s on stream ID %d\n",
            settings.numberOfMessages,
            settings.randomMessageLength ? " random" : "",
            settings.messageLength,
            settings.channel.c_str(),
            settings.streamId);

        aeron::Context context;

        if (settings.dirPrefix != "")
        {
            context.aeronDir(settings.dirPrefix);
        }

        context.newPublicationHandler(
            [](const std::string& channel, std::int32_t streamId, std::int32_t sessionId, std::int64_t correlationId)
            {
                std::cout << "Publication: " << channel << " " << correlationId << ":" << streamId << ":" << sessionId << std::endl;
            });

        context.newSubscriptionHandler(
            [](const std::string& channel, std::int32_t streamId, std::int64_t correlationId)
            {
                std::cout << "Subscription: " << channel << " " << correlationId << ":" << streamId << std::endl;
            });

        context.availableImageHandler([](
            Image &image,
            const std::string &channel,
            std::int32_t streamId,
            std::int32_t sessionId,
            std::int64_t joiningPosition,
            const std::string &sourceIdentity)
        {
            std::cout << "Available image on " << channel << " streamId=" << streamId << " sessionId=" << sessionId;
            std::cout << " at position=" << joiningPosition << " from " << sourceIdentity << std::endl;
        });

        context.unavailableImageHandler(
            [](Image &image, const std::string &channel, std::int32_t streamId, std::int32_t sessionId,
                std::int64_t position)
            {
                std::cout << "Unavailable image on " << channel << "streamId=" << streamId << " sessionId=" << sessionId;
                std::cout << " at position=" << position << std::endl;
            });

        Aeron aeron(context);

        std::int64_t subscriptionId = aeron.addSubscription(settings.channel, settings.streamId);
        std::int64_t publicationId = aeron.addPublication(settings.channel, settings.streamId);

        std::shared_ptr<Subscription> subscription = aeron.findSubscription(subscriptionId);
        while (!subscription)
        {
            std::this_thread::yield();
            subscription = aeron.findSubscription(subscriptionId);
        }

        std::shared_ptr<Publication> publication = aeron.findPublication(publicationId);
        while (!publication)
        {
            std::this_thread::yield();
            publication = aeron.findPublication(publicationId);
        }

        std::unique_ptr<std::uint8_t[]> buffer(new std::uint8_t[settings.messageLength]);
        concurrent::AtomicBuffer srcBuffer(buffer.get(), settings.messageLength);

        BusySpinIdleStrategy offerIdleStrategy;
        BusySpinIdleStrategy pollIdleStrategy;

        RateReporter rateReporter(std::chrono::seconds(1), printRate);
        FragmentAssembler fragmentAssembler(rateReporterHandler(rateReporter));
        fragment_handler_t handler = fragmentAssembler.handler();

        on_new_length_t lengthGenerator = composeLengthGenerator(settings.randomMessageLength, settings.messageLength);
        std::shared_ptr<std::thread> rateReporterThread;

        if (settings.progress)
        {
            rateReporterThread = std::make_shared<std::thread>([&](){ rateReporter.run(); });
        }

        std::thread pollThread([&]()
            {
                while (running)
                {
                    const int fragmentsRead = subscription->poll(handler, settings.fragmentCountLimit);

                    pollIdleStrategy.idle(fragmentsRead);
                }
            });

        do
        {
            printingActive = true;

            long backPressureCount = 0;

            if (nullptr == rateReporterThread)
            {
                rateReporter.reset();
            }

            for (long i = 0; i < settings.numberOfMessages && running; i++)
            {
                const int length = lengthGenerator();
                srcBuffer.putInt64(0, i);

                while (publication->offer(srcBuffer, 0, length) < 0L)
                {
                    backPressureCount++;
                    offerIdleStrategy.idle(0);
                }
            }

            if (nullptr == rateReporterThread)
            {
                rateReporter.report();
            }

            std::cout << "Done streaming. Back pressure ratio ";
            std::cout << ((double)backPressureCount / settings.numberOfMessages) << std::endl;

            if (running && settings.lingerTimeoutMs > 0)
            {
                std::cout << "Lingering for " << settings.lingerTimeoutMs << " milliseconds." << std::endl;
                std::this_thread::sleep_for(std::chrono::milliseconds(settings.lingerTimeoutMs));
            }

            printingActive = false;
        }
        while (running && continuationBarrier("Execute again?"));

        running = false;
        rateReporter.halt();

        pollThread.join();

        if (nullptr != rateReporterThread)
        {
            rateReporterThread->join();
        }
    }
    catch (CommandOptionException& e)
    {
        std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
        cp.displayOptionsHelp(std::cerr);
        return -1;
    }
    catch (SourcedException& e)
    {
        std::cerr << "FAILED: " << e.what() << " : " << e.where() << std::endl;
        return -1;
    }
    catch (std::exception& e)
    {
        std::cerr << "FAILED: " << e.what() << " : " << std::endl;
        return -1;
    }

    return 0;
}
Пример #12
0
int main(int argc, char** argv)
{
    CommandOptionParser cp;
    cp.addOption(CommandOption (optHelp,     0, 0, "                Displays help information."));
    cp.addOption(CommandOption (optPrefix,   1, 1, "dir             Prefix directory for aeron driver."));
    cp.addOption(CommandOption (optChannel,  1, 1, "channel         Channel."));
    cp.addOption(CommandOption (optStreamId, 1, 1, "streamId        Stream ID."));
    cp.addOption(CommandOption (optMessages, 1, 1, "number          Number of Messages."));
    cp.addOption(CommandOption (optLinger,   1, 1, "milliseconds    Linger timeout in milliseconds."));

    signal (SIGINT, sigIntHandler);

    try
    {
        Settings settings = parseCmdLine(cp, argc, argv);

        std::cout << "Publishing to channel " << settings.channel << " on Stream ID " << settings.streamId << std::endl;

        aeron::Context context;

        if (settings.dirPrefix != "")
        {
            context.aeronDir(settings.dirPrefix);
        }

        context.newPublicationHandler(
            [](const std::string& channel, std::int32_t streamId, std::int32_t sessionId, std::int64_t correlationId)
            {
                std::cout << "Publication: " << channel << " " << correlationId << ":" << streamId << ":" << sessionId << std::endl;
            });

        std::shared_ptr<Aeron> aeron = Aeron::connect(context);

        // add the publication to start the process
        std::int64_t id = aeron->addPublication(settings.channel, settings.streamId);

        std::shared_ptr<Publication> publication = aeron->findPublication(id);
        // wait for the publication to be valid
        while (!publication)
        {
            std::this_thread::yield();
            publication = aeron->findPublication(id);
        }

        AERON_DECL_ALIGNED(buffer_t buffer, 16);
        concurrent::AtomicBuffer srcBuffer(&buffer[0], buffer.size());
        char message[256];

        for (int i = 0; i < settings.numberOfMessages && running; i++)
        {
#if _MSC_VER
            const int messageLen = ::sprintf_s(message, sizeof(message), "Hello World! %d", i);
#else
            const int messageLen = ::snprintf(message, sizeof(message), "Hello World! %d", i);
#endif

            srcBuffer.putBytes(0, reinterpret_cast<std::uint8_t *>(message), messageLen);

            std::cout << "offering " << i << "/" << settings.numberOfMessages;
            std::cout.flush();

            const std::int64_t result = publication->offer(srcBuffer, 0, messageLen);

            if (result < 0)
            {
                if (PUBLICATION_NOT_CONNECTED == result)
                {
                    std::cout << " not connected yet." << std::endl;
                }
                else if (PUBLICATION_BACK_PRESSURE == result)
                {
                    std::cout << " back pressured." << std::endl;
                }
                else
                {
                    std::cout << " ah?! unknown " << result << std::endl;
                }
            }
            else
            {
                std::cout << " yay!" << std::endl;
            }

            std::this_thread::sleep_for(std::chrono::seconds(1));
        }

        std::cout << "Done sending." << std::endl;

        if (settings.lingerTimeoutMs > 0)
        {
            std::cout << "Lingering for " << settings.lingerTimeoutMs << " milliseconds." << std::endl;
            std::this_thread::sleep_for(std::chrono::milliseconds(settings.lingerTimeoutMs));
        }
    }
    catch (CommandOptionException& e)
    {
        std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
        cp.displayOptionsHelp(std::cerr);
        return -1;
    }
    catch (SourcedException& e)
    {
        std::cerr << "FAILED: " << e.what() << " : " << e.where() << std::endl;
        return -1;
    }
    catch (std::exception& e)
    {
        std::cerr << "FAILED: " << e.what() << " : " << std::endl;
        return -1;
    }

    return 0;
}
Пример #13
0
int main(int argc, char **argv)
{
    CommandOptionParser cp;
    cp.addOption(CommandOption (optHelp,          0, 0, "                Displays help information."));
    cp.addOption(CommandOption (optPrefix,        1, 1, "dir             Prefix directory for aeron driver."));
    cp.addOption(CommandOption (optPingChannel,   1, 1, "channel         Ping Channel."));
    cp.addOption(CommandOption (optPongChannel,   1, 1, "channel         Pong Channel."));
    cp.addOption(CommandOption (optPingStreamId,  1, 1, "streamId        Ping Stream ID."));
    cp.addOption(CommandOption (optPongStreamId,  1, 1, "streamId        Pong Stream ID."));
    cp.addOption(CommandOption (optMessages,      1, 1, "number          Number of Messages."));
    cp.addOption(CommandOption (optLength,        1, 1, "length          Length of Messages."));
    cp.addOption(CommandOption (optFrags,         1, 1, "limit           Fragment Count Limit."));
    cp.addOption(CommandOption (optWarmupMessages,1, 1, "number          Number of Messages for warmup."));

    signal (SIGINT, sigIntHandler);

    try
    {
        Settings settings = parseCmdLine(cp, argc, argv);

        std::cout << "Subscribing Pong at " << settings.pongChannel << " on Stream ID " << settings.pongStreamId << std::endl;
        std::cout << "Publishing Ping at " << settings.pingChannel << " on Stream ID " << settings.pingStreamId << std::endl;

        aeron::Context context;
        std::atomic<int> countDown(1);
        std::int64_t subscriptionId;
        std::int64_t publicationId;

        if (settings.dirPrefix != "")
        {
            context.aeronDir(settings.dirPrefix);
        }

        context.newSubscriptionHandler(
            [](const std::string& channel, std::int32_t streamId, std::int64_t correlationId)
            {
                std::cout << "Subscription: " << channel << " " << correlationId << ":" << streamId << std::endl;
            });

        context.newPublicationHandler(
            [](const std::string& channel, std::int32_t streamId, std::int32_t sessionId, std::int64_t correlationId)
            {
                std::cout << "Publication: " << channel << " " << correlationId << ":" << streamId << ":" << sessionId << std::endl;
            });

        context.availableImageHandler(
            [&](Image &image)
            {
                std::cout << "Available image correlationId=" << image.correlationId() << " sessionId=" << image.sessionId();
                std::cout << " at position=" << image.position() << " from " << image.sourceIdentity() << std::endl;

                if (image.subscriptionRegistrationId() == subscriptionId)
                {
                    countDown--;
                }
            });

        context.unavailableImageHandler([](Image &image)
            {
                std::cout << "Unavailable image on correlationId=" << image.correlationId() << " sessionId=" << image.sessionId();
                std::cout << " at position=" << image.position() << " from " << image.sourceIdentity() << std::endl;
            });

        Aeron aeron(context);

        subscriptionId = aeron.addSubscription(settings.pongChannel, settings.pongStreamId);
        publicationId = aeron.addPublication(settings.pingChannel, settings.pingStreamId);

        std::shared_ptr<Subscription> pongSubscription = aeron.findSubscription(subscriptionId);
        while (!pongSubscription)
        {
            std::this_thread::yield();
            pongSubscription = aeron.findSubscription(subscriptionId);
        }

        std::shared_ptr<Publication> pingPublication = aeron.findPublication(publicationId);
        while (!pingPublication)
        {
            std::this_thread::yield();
            pingPublication = aeron.findPublication(publicationId);
        }

        while (countDown > 0)
        {
            std::this_thread::yield();
        }

        ::setlocale(LC_NUMERIC, "");
        if (settings.numberOfWarmupMessages > 0)
        {
            Settings warmupSettings = settings;
            warmupSettings.numberOfMessages = warmupSettings.numberOfWarmupMessages;

            const steady_clock::time_point start = steady_clock::now();

            std::printf("Warming up the media driver with %'ld messages of length %d bytes\n", warmupSettings.numberOfWarmupMessages, warmupSettings.messageLength);
            sendPingAndReceivePong([](AtomicBuffer&, index_t, index_t, Header&){}, pingPublication, pongSubscription, warmupSettings);
            std::int64_t nanoDuration = duration<std::int64_t, std::nano>(steady_clock::now() - start).count();

            std::printf("Warmed up the media driver in %'" PRId64 " [ns]\n", nanoDuration);
        }

        hdr_histogram* histogram;
        hdr_init(1, 10 * 1000 * 1000 * 1000LL, 3, &histogram);

        do
        {
            hdr_reset(histogram);

            FragmentAssembler fragmentAssembler(
                [&](const AtomicBuffer& buffer, index_t offset, index_t length, const Header& header)
                {
                    steady_clock::time_point end = steady_clock::now();
                    steady_clock::time_point start;

                    buffer.getBytes(offset, (std::uint8_t*)&start, sizeof(steady_clock::time_point));
                    std::int64_t nanoRtt = duration<std::int64_t, std::nano>(end - start).count();

                    hdr_record_value(histogram, nanoRtt);
                });

            std::printf("Pinging %'ld messages of length %d bytes\n", settings.numberOfMessages, settings.messageLength);

            sendPingAndReceivePong(fragmentAssembler.handler(), pingPublication, pongSubscription, settings);

            hdr_percentiles_print(histogram, stdout, 5, 1000.0, CLASSIC);
            fflush(stdout);
        }
        while (running && continuationBarrier("Execute again?"));

    }
    catch (const CommandOptionException& e)
    {
        std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
        cp.displayOptionsHelp(std::cerr);
        return -1;
    }
    catch (const SourcedException& e)
    {
        std::cerr << "FAILED: " << e.what() << " : " << e.where() << std::endl;
        return -1;
    }
    catch (const std::exception& e)
    {
        std::cerr << "FAILED: " << e.what() << " : " << std::endl;
        return -1;
    }

    return 0;
}
Пример #14
0
int main (int argc, char** argv)
{
    CommandOptionParser cp;
    cp.addOption(CommandOption(optHelp,   0, 0, "                Displays help information."));
    cp.addOption(CommandOption(optPath,   1, 1, "basePath        Base Path to shared memory. Default: " + Context::defaultAeronPath()));

    try
    {
        Settings settings = parseCmdLine(cp, argc, argv);

        MemoryMappedFile::ptr_t cncFile = MemoryMappedFile::mapExistingReadOnly(
            (settings.basePath + "/" + CncFileDescriptor::CNC_FILE).c_str());

        const std::int32_t cncVersion = CncFileDescriptor::cncVersionVolatile(cncFile);

        if (cncVersion != CncFileDescriptor::CNC_VERSION)
        {
            std::cerr << "CNC version not supported: file version=" << cncVersion << std::endl;
            return -1;
        }

        AtomicBuffer errorBuffer = CncFileDescriptor::createErrorLogBuffer(cncFile);

        const int distinctErrorCount = ErrorLogReader::read(
            errorBuffer,
            [](
                std::int32_t observationCount,
                std::int64_t firstObservationTimestamp,
                std::int64_t lastObservationTimestamp,
                const std::string &encodedException)
                {
                    std::printf(
                        "***\n%d observations from %s to %s for:\n %s\n",
                        observationCount,
                        formatDate(firstObservationTimestamp).c_str(),
                        formatDate(lastObservationTimestamp).c_str(),
                        encodedException.c_str());
                },
            0);

        std::printf("\n%d distinct errors observed.\n", distinctErrorCount);
    }
    catch (const CommandOptionException& e)
    {
        std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
        cp.displayOptionsHelp(std::cerr);
        return -1;
    }
    catch (const SourcedException& e)
    {
        std::cerr << "FAILED: " << e.what() << " : " << e.where() << std::endl;
        return -1;
    }
    catch (const std::exception& e)
    {
        std::cerr << "FAILED: " << e.what() << " : " << std::endl;
        return -1;
    }

    return 0;
}