Ejemplo n.º 1
0
        public static void Main()
        {
            var ctx = new Aeron.Context()
                      .AvailableImageHandler(SamplesUtil.PrintAvailableImage)
                      .UnavailableImageHandler(SamplesUtil.PrintUnavailableImage);

            IIdleStrategy idleStrategy = new BusySpinIdleStrategy();

            Console.WriteLine("Subscribing Ping at " + PingChannel + " on stream Id " + PingStreamID);
            Console.WriteLine("Publishing Pong at " + PongChannel + " on stream Id " + PongStreamID);

            var running = new AtomicBoolean(true);

            Console.CancelKeyPress += (_, e) => running.Set(false);

            using (var aeron = Aeron.Connect(ctx))
                using (var pongPublication = aeron.AddPublication(PongChannel, PongStreamID))
                    using (var pingSubscription = aeron.AddSubscription(PingChannel, PingStreamID))
                    {
                        FragmentHandler dataHandler = (buffer, offset, length, header) => PingHandler(pongPublication, buffer, offset, length);

                        while (running.Get())
                        {
                            idleStrategy.Idle(pingSubscription.Poll(dataHandler, FrameCountLimit));
                        }

                        Console.WriteLine("Shutting down...");
                    }
        }
Ejemplo n.º 2
0
        public static async Task Main(string[] args)
        {
            var listenPort  = ushort.Parse(args[0]);
            var servicePort = ushort.Parse(args[1]);

            var seeds  = args.Skip(2).Select(Utils.IPEndPointFromString).ToArray();
            var logger = Utils.CreateLogger <Program>();

            var gossiper = await StartGossiper(listenPort, servicePort, seeds, logger);

            const int fragmentLimitCount = 10;
            string    channel            = "aeron:udp?endpoint=localhost:" + servicePort;

            // A unique identifier for a stream within a channel. Stream ID 0 is reserved
            // for internal use and should not be used by applications.
            const int streamId = 10;

            Console.WriteLine("Subscribing to " + channel + " on stream Id " + streamId);


            // dataHandler method is called for every new datagram received
            var fragmentHandler = HandlerHelper.ToFragmentHandler((buffer, offset, length, header) =>
            {
                var data = new byte[length];
                buffer.GetBytes(offset, data);

                Console.WriteLine($"Received message ({Encoding.UTF8.GetString(data)}) to stream {streamId:D} from session {header.SessionId:x} term id {header.TermId:x} term offset {header.TermOffset:D} ({length:D}@{offset:D})");
            });

            // Create a context, needed for client connection to media driver
            // A separate media driver process need to run prior to running this application
            var ctx = new Aeron.Context();

            // Create an Aeron instance with client-provided context configuration, connect to the
            // media driver, and add a subscription for the given channel and stream using the supplied
            // dataHandler method, which will be called with new messages as they are received.
            // The Aeron and Subscription classes implement AutoCloseable, and will automatically
            // clean up resources when this try block is finished.
            using (var aeron = Aeron.Connect(ctx))
                using (var subscription = aeron.AddSubscription(channel, streamId))
                {
                    IIdleStrategy idleStrategy = new BusySpinIdleStrategy();

                    // Try to read the data from subscriber
                    while (true)
                    {
                        // poll delivers messages to the dataHandler as they arrive
                        // and returns number of fragments read, or 0
                        // if no data is available.
                        var fragmentsRead = subscription.Poll(fragmentHandler, fragmentLimitCount);
                        // Give the IdleStrategy a chance to spin/yield/sleep to reduce CPU
                        // use if no messages were received.
                        idleStrategy.Idle(fragmentsRead);
                    }
                }
        }
Ejemplo n.º 3
0
        public static void Main()
        {
            ComputerSpecifications.Dump();


            var reporter            = new RateReporter(1000, PrintRate);
            var rateReporterHandler = SamplesUtil.RateReporterHandler(reporter);
            var context             = new Aeron.Context();

            var running = new AtomicBoolean(true);

            var reportThread    = new Thread(reporter.Run);
            var subscribeThread = new Thread(subscription => SamplesUtil.SubscriberLoop(rateReporterHandler, FragmentCountLimit, running)((Subscription)subscription));

            using (var aeron = Aeron.Connect(context))
                using (var publication = aeron.AddPublication(Channel, StreamID))
                    using (var subscription = aeron.AddSubscription(Channel, StreamID))
                        using (var byteBuffer = BufferUtil.AllocateDirectAligned(MessageLength, BitUtil.CACHE_LINE_LENGTH))
                            using (var buffer = new UnsafeBuffer(byteBuffer))
                            {
                                reportThread.Start();
                                subscribeThread.Start(subscription);

                                do
                                {
                                    Console.WriteLine("Streaming {0:G} messages of size {1:G} bytes to {2} on stream Id {3}", NumberOfMessages, MessageLength, Channel, StreamID);

                                    _printingActive = true;

                                    long backPressureCount = 0;
                                    for (long i = 0; i < NumberOfMessages; i++)
                                    {
                                        buffer.PutLong(0, i);

                                        OfferIdleStrategy.Reset();
                                        while (publication.Offer(buffer, 0, buffer.Capacity) < 0)
                                        {
                                            OfferIdleStrategy.Idle();
                                            backPressureCount++;
                                        }
                                    }

                                    Console.WriteLine("Done streaming. backPressureRatio=" + (double)backPressureCount / NumberOfMessages);

                                    if (0 < LingerTimeoutMs)
                                    {
                                        Console.WriteLine("Lingering for " + LingerTimeoutMs + " milliseconds...");
                                        Thread.Sleep((int)LingerTimeoutMs);
                                    }

                                    _printingActive = false;
                                } while (Console.ReadLine() != "x");

                                reporter.Halt();
                                running.Set(false);

                                if (!subscribeThread.Join(5000))
                                {
                                    Console.WriteLine("Warning: not all tasks completed promptly");
                                }
                            }
        }
Ejemplo n.º 4
0
        public static void Main()
        {
            // Maximum number of message fragments to receive during a single 'poll' operation
            const int fragmentLimitCount = 10;

            // The channel (an endpoint identifier) to receive messages from
            const string channel = "aeron:udp?endpoint=localhost:40123";

            // A unique identifier for a stream within a channel. Stream ID 0 is reserved
            // for internal use and should not be used by applications.
            const int streamId = 10;

            Console.WriteLine("Subscribing to " + channel + " on stream Id " + streamId);

            var running = new AtomicBoolean(true);

            // Register a SIGINT handler for graceful shutdown.
            Console.CancelKeyPress += (s, e) => running.Set(false);

            // dataHandler method is called for every new datagram received
            FragmentHandler fragmentHandler = (buffer, offset, length, header) =>
            {
                var data = new byte[length];
                buffer.GetBytes(offset, data);

                Console.WriteLine($"Received message ({Encoding.UTF8.GetString(data)}) to stream {streamId:D} from session {header.SessionId:x} term id {header.TermId:x} term offset {header.TermOffset:D} ({length:D}@{offset:D})");

                // Received the intended message, time to exit the program
                running.Set(false);
            };

            // Create a context, needed for client connection to media driver
            // A separate media driver process need to run prior to running this application
            var ctx = new Aeron.Context();

            // Create an Aeron instance with client-provided context configuration, connect to the
            // media driver, and add a subscription for the given channel and stream using the supplied
            // dataHandler method, which will be called with new messages as they are received.
            // The Aeron and Subscription classes implement AutoCloseable, and will automatically
            // clean up resources when this try block is finished.
            using (var aeron = Aeron.Connect(ctx))
                using (var subscription = aeron.AddSubscription(channel, streamId))
                {
                    IIdleStrategy idleStrategy = new BusySpinIdleStrategy();

                    // Try to read the data from subscriber
                    while (running.Get())
                    {
                        // poll delivers messages to the dataHandler as they arrive
                        // and returns number of fragments read, or 0
                        // if no data is available.
                        var fragmentsRead = subscription.Poll(fragmentHandler, fragmentLimitCount);
                        // Give the IdleStrategy a chance to spin/yield/sleep to reduce CPU
                        // use if no messages were received.
                        idleStrategy.Idle(fragmentsRead);
                    }

                    Console.WriteLine("Press any key...");
                    Console.ReadLine();
                }
        }