Ejemplo n.º 1
0
        private EchoServer(Aeron in_aeron, IPEndPoint in_local_address)
        {
            this.aeron         = in_aeron ?? throw new ArgumentNullException(nameof(in_aeron));
            this.local_address = in_local_address ?? throw new ArgumentNullException(nameof(in_local_address));

            this.clients = new Dictionary <int, ServerClient>(32);
        }
Ejemplo n.º 2
0
        public static void Main()
        {
            const string channel  = "aeron:ipc";
            const int    streamId = 42;

            var buffer  = new UnsafeBuffer(new byte[256]);
            var handler = HandlerHelper.ToFragmentHandler(PrintMessage);

            try
            {
                using (var aeron = Aeron.Connect())
                    using (var publisher = aeron.AddPublication(channel, streamId))
                        using (var subscriber = aeron.AddSubscription(channel, streamId))
                        {
                            var message = buffer.PutStringWithoutLengthUtf8(0, "Hello World!");

                            publisher.Offer(buffer, 0, message);
                            Console.WriteLine("Message sent...");

                            while (subscriber.Poll(handler, 1) == 0)
                            {
                                Thread.Sleep(10);
                            }
                        }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                Console.WriteLine("Press any key to continue...");
                Console.ReadKey();
            }
        }
Ejemplo n.º 3
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.º 4
0
        public static void Main()
        {
            Console.WriteLine("Subscribing to " + Channel + " on stream Id " + StreamID);

            var ctx = new Aeron.Context()
                      .AvailableImageHandler(SamplesUtil.PrintUnavailableImage)
                      .UnavailableImageHandler(SamplesUtil.PrintUnavailableImage);

            var reporter          = new RateReporter(1000, SamplesUtil.PrintRate);
            var fragmentAssembler = new FragmentAssembler(SamplesUtil.RateReporterHandler(reporter));
            var running           = new AtomicBoolean(true);

            var t      = new Thread(subscription => SamplesUtil.SubscriberLoop(fragmentAssembler.OnFragment, FragmentCountLimit, running)((Subscription)subscription));
            var report = new Thread(reporter.Run);

            using (var aeron = Aeron.Connect(ctx))
                using (var subscription = aeron.AddSubscription(Channel, StreamID))
                {
                    t.Start(subscription);
                    report.Start();

                    Console.ReadLine();
                    Console.WriteLine("Shutting down...");
                    running.Set(false);
                    reporter.Halt();

                    t.Join();
                    report.Join();
                }
        }
Ejemplo n.º 5
0
 /// <summary>
 ///  Creates a new Aeron source with the specified parameters.
 /// </summary>
 /// <param name="aeron">the Aeron connection to use</param>
 /// <param name="stream">the Aeron stream ID to use</param>
 /// <param name="channel">the Aeron channel to use</param>
 /// <param name="bufferSize">the data buffer size to use (in bytes)</param>
 public Source(Aeron aeron, int stream, string channel, int bufferSize)
 {
     _stream      = stream;
     _channel     = channel;
     _buffer      = new UnsafeBuffer(BufferUtil.AllocateDirectAligned(bufferSize, BitUtil.CACHE_LINE_LENGTH));
     _publication = aeron.AddPublication(_channel, _stream);
 }
Ejemplo n.º 6
0
 public HelloWorldSender(
     Aeron in_aeron,
     IPEndPoint in_remote_address)
 {
     this.aeron          = in_aeron ?? throw new ArgumentNullException(nameof(in_aeron));
     this.remote_address = in_remote_address ?? throw new ArgumentNullException(nameof(in_remote_address));
 }
Ejemplo n.º 7
0
 public ServerClient(int session, Image in_image, Aeron in_aeron)
 {
     this.session = session;
     this.image   = in_image ?? throw new ArgumentNullException(nameof(in_image));
     this.aeron   = in_aeron ?? throw new ArgumentNullException(nameof(in_aeron));
     this.state   = State.INITIAL;
     this.buffer  = new UnsafeBuffer(BufferUtil.AllocateDirectAligned(2048, 16));
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Creates a new Aeron source using UDP with the specified parameters.
 /// </summary>
 /// <param name="aeron">the Aeron connection to use</param>
 /// <param name="stream">the Aeron stream ID to use</param>
 /// <param name="address">the UDP address to use</param>
 /// <param name="port">the UDP port to use</param>
 /// <param name="interface">the local interface to bind to</param>
 /// <param name="bufferSize">the data buffer size to use (in bytes)</param>
 public Source(Aeron aeron, int stream, string address, int port, string @interface, int bufferSize)
     : this(
         aeron,
         stream,
         string.Format("aeron:udp?endpoint={0}:{1}|interface={2}", address, port, @interface),
         bufferSize
         )
 {
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Creates a new Aeron source using UDP with the specified parameters.
 /// </summary>
 /// <param name="aeron">the Aeron connection to use</param>
 /// <param name="stream">the Aeron stream ID to use</param>
 /// <param name="address">the UDP address to use</param>
 /// <param name="port">the UDP port to use</param>
 /// <param name="bufferSize">the data buffer size to use (in bytes)</param>
 public Source(Aeron aeron, int stream, string address, int port, int bufferSize)
     : this(
         aeron,
         stream,
         string.Format("aeron:udp?endpoint={0}:{1}", address, port),
         bufferSize
         )
 {
 }
Ejemplo n.º 10
0
 public EchoClient(
     Aeron in_aeron,
     IPEndPoint in_local_address,
     IPEndPoint in_remote_address)
 {
     this.aeron          = in_aeron ?? throw new ArgumentNullException(nameof(in_aeron));
     this.local_address  = in_local_address ?? throw new ArgumentNullException(nameof(in_local_address));
     this.remote_address = in_remote_address ?? throw new ArgumentNullException(nameof(in_remote_address));
 }
Ejemplo n.º 11
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.º 12
0
        static void Run(Options options)
        {
            var fragmentCount = 0L;

            try
            {
                using var aeron = Aeron.Connect();
                Console.WriteLine($"Connected to Aeron {aeron.ClientId.ToString()}");
                using var subscription = aeron.AddSubscription(options.Channel, options.StreamId);
                using var cts          = new CancellationTokenSource();
                var messageDecoder  = new MessageDecoder();
                var messageReceiver = new MessageReceiver(subscription, messageDecoder, OnDataValue);

                var ct = cts.Token;

                var pollingTask = Task.Run(async() =>
                {
                    while (!ct.IsCancellationRequested)
                    {
                        var fragmentsProcessed = messageReceiver.Poll();
                        if (fragmentsProcessed == 0)
                        {
                            await Task.Delay(TimeSpan.FromMilliseconds(10), ct);
                        }
                        else
                        {
                            fragmentCount += fragmentsProcessed;
                        }
                    }
                }, ct);


                Console.CancelKeyPress += (o, args) =>
                {
                    args.Cancel = true;
                    Console.Out.WriteLine("Cancel key event intercepted");
                    cts.Cancel();
                };

                pollingTask.GetAwaiter().GetResult();
            }
            catch (OperationCanceledException)
            {
            }
            catch (Exception e)
            {
                Console.WriteLine($"Unhandled Exception: {e}");
            }
            finally
            {
                Console.Out.WriteLine($"Received {fragmentCount} fragments");
                Console.WriteLine("Subscriber finished");
            }
        }
        public IServiceClient CreateServiceClient(IPEndPoint serviceEndPoint)
        {
            string channel     = "aeron:udp?endpoint=" + serviceEndPoint.ToString();
            var    ctx         = new Aeron.Context();
            var    aeron       = Aeron.Connect(ctx);
            var    publication = aeron.AddPublication(channel, 10);

            return(new AeronServiceClient
            {
                ServiceEndPoint = serviceEndPoint,
                Publication = publication
            });
        }
Ejemplo n.º 14
0
 /// <summary>
 /// Creates a new Aeron target with the specified parameters.
 /// </summary>
 /// <param name="aeron">the Aeron connection to use</param>
 /// <param name="stream">the Aeron stream ID to use</param>
 /// <param name="channel">the Aeron channel to use</param>
 /// <param name="idleStrategy">the Aeron idle strategy to use</param>
 /// <param name="fragmentLimit">the number of message fragments to process per poll operation</param>
 public Target(
     Aeron aeron,
     int stream,
     string channel,
     IIdleStrategy idleStrategy,
     int fragmentLimit
     )
 {
     _stream        = stream;
     _channel       = channel;
     _subscription  = aeron.AddSubscription(_channel, _stream);
     _idleStrategy  = idleStrategy;
     _fragmentLimit = fragmentLimit;
 }
Ejemplo n.º 15
0
        public static void Main()
        {
            var ctx = new Aeron.Context()
                      .AvailableImageHandler(AvailablePongImageHandler);

            var fragmentAssembler = new FragmentAssembler(HandlerHelper.ToFragmentHandler(PongHandler));

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

            using (var aeron = Aeron.Connect(ctx))
            {
                Console.WriteLine("Warming up... " + WarmupNumberOfIterations + " iterations of " + WarmupNumberOfMessages + " messages");

                using (var publication = aeron.AddPublication(PingChannel, PingStreamID))
                    using (var subscription = aeron.AddSubscription(PongChannel, PongStreamID))
                        using (var byteBuffer = BufferUtil.AllocateDirectAligned(MessageLength, BitUtil.CACHE_LINE_LENGTH))
                            using (var atomicBuffer = new UnsafeBuffer(byteBuffer))
                            {
                                Latch.Wait();

                                for (var i = 0; i < WarmupNumberOfIterations; i++)
                                {
                                    RoundTripMessages(atomicBuffer, fragmentAssembler, publication, subscription, WarmupNumberOfMessages);
                                }

                                Thread.Sleep(100);

                                do
                                {
                                    Histogram.Reset();
                                    Console.WriteLine("Pinging " + NumberOfMessages + " messages");

                                    RoundTripMessages(atomicBuffer, fragmentAssembler, publication, subscription, NumberOfMessages);
                                    Console.WriteLine("Histogram of RTT latencies in microseconds.");

                                    Histogram.OutputPercentileDistribution(Console.Out, outputValueUnitScalingRatio: 1000);
                                } while (Console.Read() == 'y');
                            }
            }
        }
Ejemplo n.º 16
0
        private void Run()
        {
            const int OneKb = 1024;
            const int OneMb = OneKb * 1024;

            var testStreamId = 99;
            var channel      = "aeron:ipc";
            var maxMsgLength = OneMb;

            // Maximum number of message fragments to receive during a single 'poll' operation
            const int fragmentLimitCount = 10;

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



            // Create a context, needed for client connection to media driver
            // A separate media driver process need to run prior to running this application

            // 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(new Aeron.Context()))
                using (var subscription = aeron.AddSubscription(channel, testStreamId))
                {
                    IIdleStrategy idleStrategy = new SpinWaitIdleStrategy();

                    // Try to read the data from subscriber
                    while (_stopping.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(new FragmentAssembler((buffer, offset, length, header) => {}), fragmentLimitCount);
                        // Give the IdleStrategy a chance to spin/yield/sleep to reduce CPU
                        // use if no messages were received.
                        idleStrategy.Idle(fragmentsRead);
                    }
                }
        }
Ejemplo n.º 17
0
        public BasicAeronSpec()
        {
            BasicConfigurator.Configure();
            _testDataSent     = 0;
            _testDataReceived = 0;

            _testDataHandler = (data, length) => { _testDataReceived += length; };

            const string channel = "aeron:ipc";
            const int    stream  = 42;

            _aeron = Aeron.Connect(Defaults.GetNewSystemContext());

            _source = new Source(_aeron, stream, channel, Defaults.BufferSize);
            _target = new Target(_aeron, stream, channel, Defaults.IdleStrategy, Defaults.FragmentLimit);

            _testByteArraySize = 1000;

            _rnd = new Random();
        }
Ejemplo n.º 18
0
        public static EchoServer Create(DirectoryInfo media_directory, IPEndPoint local_address)
        {
            string directory = media_directory is not null && media_directory.Exists ? media_directory.FullName : throw new DirectoryNotFoundException();

            var aeron_context = new Aeron.Context().AeronDirectoryName(directory);

            Aeron aeron = null;

            try
            {
                aeron = Aeron.Connect(aeron_context);
            }
            catch
            {
                aeron?.Dispose();
                throw;
            }

            return(new EchoServer(aeron, local_address));
        }
Ejemplo n.º 19
0
        public static void Main()
        {
            ComputerSpecifications.Dump();

            var running = new AtomicBoolean(true);

            using (var aeron = Aeron.Connect())
                using (var publication = aeron.AddExclusivePublication(Channel, StreamID))
                    using (var subscription = aeron.AddSubscription(Channel, StreamID))
                    {
                        var subscriber       = new Subscriber(running, subscription);
                        var subscriberThread = new Thread(subscriber.Run)
                        {
                            Name = "subscriber"
                        };
                        var publisherThread = new Thread(new Publisher(running, publication).Run)
                        {
                            Name = "publisher"
                        };
                        var rateReporterThread = new Thread(new RateReporter(running, subscriber).Run)
                        {
                            Name = "rate-reporter"
                        };

                        rateReporterThread.Start();
                        subscriberThread.Start();
                        publisherThread.Start();

                        Console.WriteLine("Press any key to stop...");
                        Console.Read();

                        running.Set(false);

                        subscriberThread.Join();
                        publisherThread.Join();
                        rateReporterThread.Join();
                    }
        }
Ejemplo n.º 20
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();
                }
        }
Ejemplo n.º 21
0
        public void SetUp()
        {
            MockClientErrorHandler = A.Fake <ErrorHandler>(options => options.Wrapping(throwable =>
            {
                if (!SuppressPrintError)
                {
                    Console.WriteLine(throwable.ToString());
                    Console.Write(throwable.StackTrace);
                }
            }));

            PublicationReady  = new PublicationBuffersReadyFlyweight();
            SubscriptionReady = new SubscriptionReadyFlyweight();
            OperationSuccess  = new OperationSucceededFlyweight();
            ErrorResponse     = new ErrorResponseFlyweight();
            ClientTimeout     = new ClientTimeoutFlyweight();

            PublicationReadyBuffer  = new UnsafeBuffer(new byte[SEND_BUFFER_CAPACITY]);
            SubscriptionReadyBuffer = new UnsafeBuffer(new byte[SEND_BUFFER_CAPACITY]);
            OperationSuccessBuffer  = new UnsafeBuffer(new byte[SEND_BUFFER_CAPACITY]);
            ErrorMessageBuffer      = new UnsafeBuffer(new byte[SEND_BUFFER_CAPACITY]);
            ClientTimeoutBuffer     = new UnsafeBuffer(new byte[SEND_BUFFER_CAPACITY]);

            CounterValuesBuffer  = new UnsafeBuffer(new byte[COUNTER_BUFFER_LENGTH]);
            MockToClientReceiver = A.Fake <CopyBroadcastReceiver>();

            MockAvailableImageHandler   = A.Fake <AvailableImageHandler>();
            MockUnavailableImageHandler = A.Fake <UnavailableImageHandler>();
            MockCloseHandler            = A.Fake <Action>();

            LogBuffersFactory = A.Fake <ILogBuffersFactory>();

            DriverProxy = A.Fake <DriverProxy>();

            MockAeron = A.Fake <Aeron>();

            A.CallTo(() => mockClientLock.TryLock()).Returns(true);

            A.CallTo(() => DriverProxy.AddPublication(CHANNEL, STREAM_ID_1)).Returns(CORRELATION_ID);
            A.CallTo(() => DriverProxy.AddPublication(CHANNEL, STREAM_ID_2)).Returns(CORRELATION_ID_2);
            A.CallTo(() => DriverProxy.RemovePublication(CORRELATION_ID)).Returns(CLOSE_CORRELATION_ID);
            A.CallTo(() => DriverProxy.AddSubscription(A <string> ._, A <int> ._)).Returns(CORRELATION_ID);
            A.CallTo(() => DriverProxy.RemoveSubscription(CORRELATION_ID)).Returns(CLOSE_CORRELATION_ID);

            Aeron.Context ctx = new Aeron.Context()
                                .ClientLock(mockClientLock)
                                .EpochClock(EpochClock)
                                .NanoClock(NanoClock)
                                .ToClientBuffer(MockToClientReceiver)
                                .DriverProxy(DriverProxy)
                                .LogBuffersFactory(LogBuffersFactory)
                                .ErrorHandler(MockClientErrorHandler)
                                .AvailableImageHandler(MockAvailableImageHandler)
                                .UnavailableImageHandler(MockUnavailableImageHandler)
                                .CloseHandler(MockCloseHandler)
                                .KeepAliveIntervalNs(KEEP_ALIVE_INTERVAL)
                                .DriverTimeoutMs(AWAIT_TIMEOUT)
                                .InterServiceTimeoutNs(INTER_SERVICE_TIMEOUT_MS * 1000000)
                                .CountersValuesBuffer(CounterValuesBuffer);

            Conductor = new ClientConductor(ctx, MockAeron);

            PublicationReady.Wrap(PublicationReadyBuffer, 0);
            SubscriptionReady.Wrap(SubscriptionReadyBuffer, 0);
            OperationSuccess.Wrap(OperationSuccessBuffer, 0);
            ErrorResponse.Wrap(ErrorMessageBuffer, 0);
            ClientTimeout.Wrap(ClientTimeoutBuffer, 0);

            PublicationReady.CorrelationId(CORRELATION_ID);
            PublicationReady.RegistrationId(CORRELATION_ID);
            PublicationReady.SessionId(SESSION_ID_1);
            PublicationReady.StreamId(STREAM_ID_1);
            PublicationReady.LogFileName(SESSION_ID_1 + "-log");

            OperationSuccess.CorrelationId(CLOSE_CORRELATION_ID);

            var termBuffersSession1 = new UnsafeBuffer[LogBufferDescriptor.PARTITION_COUNT];
            var termBuffersSession2 = new UnsafeBuffer[LogBufferDescriptor.PARTITION_COUNT];

            for (var i = 0; i < LogBufferDescriptor.PARTITION_COUNT; i++)
            {
                termBuffersSession1[i] = new UnsafeBuffer(new byte[TERM_BUFFER_LENGTH]);
                termBuffersSession2[i] = new UnsafeBuffer(new byte[TERM_BUFFER_LENGTH]);
            }

            UnsafeBuffer logMetaDataSession1 = new UnsafeBuffer(new byte[TERM_BUFFER_LENGTH]);
            UnsafeBuffer logMetaDataSession2 = new UnsafeBuffer(new byte[TERM_BUFFER_LENGTH]);

            IMutableDirectBuffer header1 = DataHeaderFlyweight.CreateDefaultHeader(SESSION_ID_1, STREAM_ID_1, 0);
            IMutableDirectBuffer header2 = DataHeaderFlyweight.CreateDefaultHeader(SESSION_ID_2, STREAM_ID_2, 0);

            LogBufferDescriptor.StoreDefaultFrameHeader(logMetaDataSession1, header1);
            LogBufferDescriptor.StoreDefaultFrameHeader(logMetaDataSession2, header2);

            var logBuffersSession1 = A.Fake <LogBuffers>();
            var logBuffersSession2 = A.Fake <LogBuffers>();

            A.CallTo(() => LogBuffersFactory.Map(SESSION_ID_1 + "-log")).Returns(logBuffersSession1);
            A.CallTo(() => LogBuffersFactory.Map(SESSION_ID_2 + "-log")).Returns(logBuffersSession2);

            A.CallTo(() => logBuffersSession1.DuplicateTermBuffers()).Returns(termBuffersSession1);
            A.CallTo(() => logBuffersSession2.DuplicateTermBuffers()).Returns(termBuffersSession2);

            A.CallTo(() => logBuffersSession1.MetaDataBuffer()).Returns(logMetaDataSession1);
            A.CallTo(() => logBuffersSession2.MetaDataBuffer()).Returns(logMetaDataSession2);

            A.CallTo(() => logBuffersSession1.TermLength()).Returns(TERM_BUFFER_LENGTH);
            A.CallTo(() => logBuffersSession2.TermLength()).Returns(TERM_BUFFER_LENGTH);
        }
Ejemplo n.º 22
0
 public SubscriptionCollection(Aeron aeron)
 {
     _aeron = aeron;
 }
Ejemplo n.º 23
0
 public AeronSubscription(string channel, int streamId, Aeron aeron)
     : this(aeron.AddSubscription(channel, streamId))
 {
 }
Ejemplo n.º 24
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.º 25
0
        static void Run(Options options)
        {
            var senderId = 1;

            Console.Out.WriteLine($"Sending messages periodically every {options.SenderPeriod}");

            var counter = 0;

            try
            {
                Console.WriteLine("Publisher started");

                using var aeron = Aeron.Connect();
                Console.WriteLine($"Connected to Aeron {aeron.ClientId.ToString()}");

                using var publication = aeron.AddPublication(options.Channel, options.StreamId);
                var encoder = new MessageEncoder(senderId);
                var sender  = new MessageSender(publication, encoder);

                using var cts = new CancellationTokenSource();
                var ct = cts.Token;

                var offerTask = Task.Run(async() =>
                {
                    while (!ct.IsCancellationRequested)
                    {
                        var sendResult = sender.Send();

                        if (sendResult >= 0)
                        {
                            ++counter;
                        }
                        else
                        {
                            switch (sendResult)
                            {
                            case Publication.NOT_CONNECTED:
                                Console.Out.WriteLine("Send failed: no one connected");
                                break;

                            case Publication.BACK_PRESSURED:
                                Console.Out.WriteLine("Send failed: back pressured");
                                break;

                            default:
                                Console.Out.WriteLine($"Send failed: error code -> {sendResult}");
                                break;
                            }
                        }
                        // Wait for the sender period before trying again.
                        await Task.Delay(options.SenderPeriod, ct);
                    }
                }, ct);

                Console.CancelKeyPress += (o, args) =>
                {
                    args.Cancel = true;
                    Console.Out.WriteLine("Cancel key event intercepted");
                    cts.Cancel();
                };
                offerTask.GetAwaiter().GetResult();
            }
            catch (OperationCanceledException)
            {
                Console.Out.WriteLine("Main loop cancelled.");
                // we were terminated - ignore this.
            }
            catch (Exception e)
            {
                Console.WriteLine($"Unhandled Exception: {e}");
            }
            finally
            {
                Console.Out.WriteLine($"Sent {counter} messages");
                Console.WriteLine("Publisher finished");
            }
        }
Ejemplo n.º 26
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();
                }
        }
Ejemplo n.º 27
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();
        }