コード例 #1
0
        public static void Main()
        {
            // Allocate enough buffer size to hold maximum message length
            // The UnsafeBuffer class is part of the Agrona library and is used for efficient buffer management
            var buffer = new UnsafeBuffer(BufferUtil.AllocateDirectAligned(512, BitUtil.CACHE_LINE_LENGTH));

            // The channel (an endpoint identifier) to send the message to
            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("Publishing to " + channel + " on stream Id " + streamId);

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

            // Create an Aeron instance with client-provided context configuration and connect to the
            // media driver, and create a Publication.  The Aeron and Publication classes implement
            // AutoCloseable, and will automatically clean up resources when this try block is finished.
            using (var aeron = Aeron.Connect(ctx))
                using (var publication = aeron.AddPublication(channel, streamId))
                {
                    Thread.Sleep(100);

                    const string message      = "Hello World! ";
                    var          messageBytes = Encoding.UTF8.GetBytes(message);
                    buffer.PutBytes(0, messageBytes);

                    // Try to publish the buffer. 'offer' is a non-blocking call.
                    // If it returns less than 0, the message was not sent, and the offer should be retried.
                    var result = publication.Offer(buffer, 0, messageBytes.Length);

                    if (result < 0L)
                    {
                        switch (result)
                        {
                        case Publication.BACK_PRESSURED:
                            Console.WriteLine(" Offer failed due to back pressure");
                            break;

                        case Publication.NOT_CONNECTED:
                            Console.WriteLine(" Offer failed because publisher is not connected to subscriber");
                            break;

                        case Publication.ADMIN_ACTION:
                            Console.WriteLine("Offer failed because of an administration action in the system");
                            break;

                        case Publication.CLOSED:
                            Console.WriteLine("Offer failed publication is closed");
                            break;

                        default:
                            Console.WriteLine(" Offer failed due to unknown reason");
                            break;
                        }
                    }
                    else
                    {
                        Console.WriteLine(" yay !!");
                    }

                    Console.WriteLine("Done sending.");
                    Console.WriteLine("Press any key...");
                    Console.ReadLine();
                }
        }
コード例 #2
0
        static async Task Main(string[] args)
        {
            var listenPort = ushort.Parse(args[0]);

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

            var loadbalancer = CreateLoadBalancer();
            var gossiper     = await StartGossiper(listenPort, seeds, new IMemberListener[] { loadbalancer }, logger);

            // Allocate enough buffer size to hold maximum message length
            // The UnsafeBuffer class is part of the Agrona library and is used for efficient buffer management
            var buffer = new UnsafeBuffer(BufferUtil.AllocateDirectAligned(512, BitUtil.CACHE_LINE_LENGTH));

            var stopwatch = new Stopwatch();

            while (true)
            {
                try
                {
                    Console.Write("Please enter your name: ");
                    string message = Console.ReadLine();

                    stopwatch.Restart();
                    var messageBytes = Encoding.UTF8.GetBytes(message);
                    buffer.PutBytes(0, messageBytes);

                    var client = loadbalancer.GetServiceClient <AeronServiceClient>(4);

                    // Try to publish the buffer. 'offer' is a non-blocking call.
                    // If it returns less than 0, the message was not sent, and the offer should be retried.
                    var result = client.Publication.Offer(buffer, 0, messageBytes.Length);

                    if (result < 0L)
                    {
                        switch (result)
                        {
                        case Publication.BACK_PRESSURED:
                            Console.WriteLine(" Offer failed due to back pressure");
                            break;

                        case Publication.NOT_CONNECTED:
                            Console.WriteLine(" Offer failed because publisher is not connected to subscriber");
                            break;

                        case Publication.ADMIN_ACTION:
                            Console.WriteLine("Offer failed because of an administration action in the system");
                            break;

                        case Publication.CLOSED:
                            Console.WriteLine("Offer failed publication is closed");
                            break;

                        default:
                            Console.WriteLine(" Offer failed due to unknown reason");
                            break;
                        }
                    }
                    else
                    {
                        stopwatch.Stop();
                        Console.WriteLine($"Message Sent TimeTaken: {stopwatch.Elapsed.TotalMilliseconds}ms");
                    }
                }

                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
コード例 #3
0
 private void InitializeInstanceFields()
 {
     _unsafeBuffer = new UnsafeBuffer(BufferUtil.AllocateDirectAligned(16 * 1024 + RingBufferDescriptor.TrailerLength, BitUtil.CACHE_LINE_LENGTH));
     _ringBuffer   = new ManyToOneRingBuffer(_unsafeBuffer);
 }
コード例 #4
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");
                                }
                            }
        }
コード例 #5
0
        public static void Main()
        {
            if (MessageLength < BitUtil.SIZE_OF_LONG)
            {
                throw new ArgumentException($"Message length must be at least {BitUtil.SIZE_OF_LONG:D} bytes");
            }

            ComputerSpecifications.Dump();

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

            _reporterThread = new Thread(_ => reporter.Run());
            _reporterThread.Start();

            // Connect to media driver and add publication to send messages on the configured channel and stream ID.
            // The Aeron and Publication classes implement AutoCloseable, and will automatically
            // clean up resources when this try block is finished.
            using (var aeron = Aeron.Connect(context))
                using (var publication = aeron.AddPublication(Channel, StreamID))
                    using (var byteBuffer = BufferUtil.AllocateDirectAligned(MessageLength, BitUtil.CACHE_LINE_LENGTH))
                        using (var buffer = new UnsafeBuffer(byteBuffer))
                        {
                            do
                            {
                                _printingActive = true;

                                Console.WriteLine($"Streaming {NumberOfMessages} messages of {(RandomMessageLength ? " random" : "")} size {MessageLength} bytes to {Channel} on stream Id {StreamID}");

                                long backPressureCount = 0;

                                for (long i = 0; i < NumberOfMessages; i++)
                                {
                                    var length = LengthGenerator.AsInt;

                                    buffer.PutLong(0, i);
                                    OfferIdleStrategy.Reset();
                                    while (publication.Offer(buffer, 0, length) < 0L)
                                    {
                                        // The offer failed, which is usually due to the publication
                                        // being temporarily blocked.  Retry the offer after a short
                                        // spin/yield/sleep, depending on the chosen IdleStrategy.
                                        backPressureCount++;
                                        OfferIdleStrategy.Idle();
                                    }

                                    reporter.OnMessage(1, length);
                                }

                                Console.WriteLine("Done streaming. Back pressure ratio " + (double)backPressureCount / NumberOfMessages);

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

                                _printingActive = false;

                                Console.WriteLine("Execute again?");
                            } while (Console.ReadLine() == "y");
                        }

            reporter.Halt();
            _reporterThread.Join();
        }