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(); } }
public void ShouldNotAllowConcludeMoreThanOnce() { var ctx = new Aeron.Context(); ctx.Conclude(); Assert.Throws(typeof(ConcurrentConcludeException), () => ctx.Conclude()); }
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(); } }
public static void Main() { var ctx = new Aeron.Context() .AvailableImageHandler(SamplesUtil.PrintUnavailableImage) .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..."); } }
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(); } }
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(); } }
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..."); } }
public void ShouldNotAllowConcludeMoreThanOnce() { var ctx = new Aeron.Context(); ctx.Conclude(); ctx.Conclude(); }
public void ShouldAllowConcludeOfClonedContext() { var ctx = new Aeron.Context(); var ctx2 = ctx.Clone(); ctx.Conclude(); ctx2.Conclude(); }
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); } } }
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 }); }
public void Run() { const int OneKb = 1024; const int OneMb = OneKb * 1024; var testStreamId = 99; var channel = "aeron:ipc"; var maxMsgLength = OneMb; var context = new Aeron.Context(); IIdleStrategy offerIdleStrategy = new SpinWaitIdleStrategy(); // 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 = Adaptive.Aeron.Aeron.Connect(context)) using (var publication = aeron.AddPublication(channel, testStreamId)) using (var byteBuffer = BufferUtil.AllocateDirectAligned(maxMsgLength, BitUtil.CACHE_LINE_LENGTH)) using (var buffer = new UnsafeBuffer(byteBuffer)) { do { TestMessage tm = new TestMessage() { EventId = 123, Bleb = string.Join(",", Enumerable.Repeat(Guid.NewGuid().ToString(), 100)) }; //Console.WriteLine($"Streaming {NumberOfMessages} messages of {(RandomMessageLength ? " random" : "")} size {MessageLength} bytes to {Channel} on stream Id {StreamID}"); Publish(tm, publication, offerIdleStrategy, buffer); //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"); } }
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'); } } }
public static void Main() { var ctx = new Aeron.Context() .AvailableImageHandler(AvailablePongImageHandler); var fragmentAssembler = new FragmentAssembler(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.OnFragment, publication, subscription, WarmupNumberOfMessages); } Thread.Sleep(100); do { Histogram.Reset(); Console.WriteLine("Pinging " + NumberOfMessages + " messages"); RoundTripMessages(atomicBuffer, fragmentAssembler.OnFragment, publication, subscription, NumberOfMessages); Console.WriteLine("Histogram of RTT latencies in microseconds."); Histogram.OutputPercentileDistribution(Console.Out, outputValueUnitScalingRatio: 1000); } while (Console.Read() == 'y'); } } }
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)); }
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"); } } }
public void SetUp() { MockClientErrorHandler = A.Fake <ErrorHandler>(options => options.Wrapping(throwable => { if (!SuppressPrintError) { Console.WriteLine(throwable.ToString()); Console.Write(throwable.StackTrace); } })); PublicationReady = new PublicationBuffersReadyFlyweight(); CorrelatedMessage = new CorrelatedMessageFlyweight(); ErrorResponse = new ErrorResponseFlyweight(); PublicationReadyBuffer = new UnsafeBuffer(new byte[SEND_BUFFER_CAPACITY]); CorrelatedMessageBuffer = new UnsafeBuffer(new byte[SEND_BUFFER_CAPACITY]); ErrorMessageBuffer = 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>(); LogBuffersFactory = A.Fake <ILogBuffersFactory>(); SubscriberPositionMap = new Dictionary <long, long>(); // should return -1 when element does not exist DriverProxy = A.Fake <DriverProxy>(); 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) .ImageMapMode(MapMode.ReadOnly) .KeepAliveInterval(KEEP_ALIVE_INTERVAL) .DriverTimeoutMs(AWAIT_TIMEOUT) .InterServiceTimeout(INTER_SERVICE_TIMEOUT_MS * 1000000) .PublicationConnectionTimeout(PUBLICATION_CONNECTION_TIMEOUT_MS) .CountersValuesBuffer(CounterValuesBuffer); Conductor = new ClientConductor(ctx); PublicationReady.Wrap(PublicationReadyBuffer, 0); CorrelatedMessage.Wrap(CorrelatedMessageBuffer, 0); ErrorResponse.Wrap(ErrorMessageBuffer, 0); PublicationReady.CorrelationId(CORRELATION_ID); PublicationReady.SessionId(SESSION_ID_1); PublicationReady.StreamId(STREAM_ID_1); PublicationReady.LogFileName(SESSION_ID_1 + "-log"); SubscriberPositionMap.Add(CORRELATION_ID, 0); CorrelatedMessage.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", A <MapMode> ._)).Returns(logBuffersSession1); A.CallTo(() => LogBuffersFactory.Map(SESSION_ID_2 + "-log", A <MapMode> ._)).Returns(logBuffersSession2); A.CallTo(() => logBuffersSession1.TermBuffers()).Returns(termBuffersSession1); A.CallTo(() => logBuffersSession2.TermBuffers()).Returns(termBuffersSession2); A.CallTo(() => logBuffersSession1.MetaDataBuffer()).Returns(logMetaDataSession1); A.CallTo(() => logBuffersSession2.MetaDataBuffer()).Returns(logMetaDataSession2); }
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(); }
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) .AwaitingIdleStrategy(new NoOpIdleStrategy()) .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); }
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(); }