/// <summary> /// Intent: Create a new subscriber, using NetMQ as the transport layer. /// </summary> /// <param name="addressZeroMq">ZeroMq address to bind to, e.g. "tcp://localhost:56001".</param> /// <param name="subscriberFilterName">Filter name on receiver. If you do not set this, it will default to the /// type name of T, and everything will just work.</param> /// <param name="whenToCreateNetworkConnection">When to create the network connection.</param> /// <param name="cancellationTokenSource">Allows graceful termination of all internal threads associated with this subject.</param> /// <param name="loggerDelegate">(optional) If we want to look at messages generated within this class, specify a logger here.</param> /// <param name="serializer">(optional) Define the serializer we will use to convert to typed data.</param> public SubscriberNetMq(string addressZeroMq, INetMQSerializer <T> serializer, string subscriberFilterName = null, WhenToCreateNetworkConnection whenToCreateNetworkConnection = WhenToCreateNetworkConnection.SetupSubscriberTransportNow, CancellationTokenSource cancellationTokenSource = default(CancellationTokenSource), Action <string> loggerDelegate = null) { AddressZeroMq = addressZeroMq; _cancellationTokenSource = cancellationTokenSource; _loggerDelegate = loggerDelegate; _serializer = serializer; if (subscriberFilterName == null) { // Unfortunately, the subscriber never scans more than the first 32 characters of the filter, so we must // trim to less than this length, but also ensure that it's unique. This ensures that if we get two // classnames of 50 characters that only differ by the last character, everything will still work and we // won't get crossed subscriptions. SubscriberFilterName = typeof(T).Name; if (SubscriberFilterName.Length > 32) { string uniqueHashCode = string.Format("{0:X8}", SubscriberFilterName.GetHashCode()); SubscriberFilterName = SubscriberFilterName.Substring(0, Math.Min(SubscriberFilterName.Length, 24)); SubscriberFilterName += uniqueHashCode; } if (SubscriberFilterName.Length > 32) { throw new Exception( "Error E38742. Internal error; subscription length can never be longer than 32 characters."); } } #if !UAP if (string.IsNullOrEmpty(Thread.CurrentThread.Name) == true) { // Cannot set the thread name twice. Thread.CurrentThread.Name = subscriberFilterName; } #endif AddressZeroMq = addressZeroMq; if (string.IsNullOrEmpty(AddressZeroMq)) { throw new Exception("Error E76244. Must define the endpoint address for ZeroMQ to connect (or bind) to."); } switch (whenToCreateNetworkConnection) { case WhenToCreateNetworkConnection.LazyConnectOnFirstUse: // (default) Do nothing; will be instantiated on first use. break; case WhenToCreateNetworkConnection.SetupPublisherTransportNow: throw new Exception( "Error E77238. Internal error. Cannot instantate the publisher in the subscriber."); case WhenToCreateNetworkConnection.SetupSubscriberTransportNow: InitializeSubscriberOnFirstUse(addressZeroMq); break; default: throw new Exception( "Error E38745. Internal error. At least one publisher or subscriber must be instantiated."); } }
public SubjectNetMQ(string addressZeroMq, INetMQSerializer <T> serializer, string subscriberFilterName = null, WhenToCreateNetworkConnection whenToCreateNetworkConnection = WhenToCreateNetworkConnection.LazyConnectOnFirstUse, CancellationTokenSource cancellationTokenSource = default(CancellationTokenSource), Action <string> loggerDelegate = null) { _publisher = new PublisherNetMq <T>(addressZeroMq, serializer, subscriberFilterName, WhenToCreateNetworkConnection.LazyConnectOnFirstUse, cancellationTokenSource, loggerDelegate); _subscriber = new SubscriberNetMq <T>(addressZeroMq, serializer, subscriberFilterName, WhenToCreateNetworkConnection.LazyConnectOnFirstUse, cancellationTokenSource, loggerDelegate); }