Exemplo 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...");
                    }
        }
Exemplo 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);
                    }
                }
        }
Exemplo n.º 3
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();
                }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Return a reusable, parameterised event loop that calls a default idler when no messages are received
        /// </summary>
        /// <param name="fragmentHandler"> to be called back for each message. </param>
        /// <param name="limit">           passed to <seealso cref="Subscription#poll(FragmentHandler, int)"/> </param>
        /// <param name="running">         indication for loop </param>
        /// <returns> loop function </returns>
        public static Action <Subscription> SubscriberLoop(IFragmentHandler fragmentHandler, int limit, AtomicBoolean running)
        {
            IIdleStrategy idleStrategy = new BusySpinIdleStrategy();

            return(SubscriberLoop(fragmentHandler, limit, running, idleStrategy));
        }