예제 #1
0
    public static void ExecuteSubscriptions(string reportName, DateTime routeDate, string routeClass)
    {
        //
        SubscriptionEventHandler publisher = new SubscriptionEventHandler(OnPublish);

        publisher.BeginInvoke(reportName, routeDate, routeClass, null, null);
    }
        /// <summary>
        /// Subscribes to a typed member info presence channel.
        /// </summary>
        /// <typeparam name="MemberT">The type used to deserialize channel member info.</typeparam>
        /// <param name="channelName">The name of the channel to subsribe to.</param>
        /// <param name="subscribedEventHandler">An optional <see cref="SubscriptionEventHandler"/>. Alternatively, use <c>Pusher.Subscribed</c>.</param>
        /// <exception cref="ArgumentNullException">If <paramref name="channelName"/> is <c>null</c> or whitespace.</exception>
        /// <exception cref="ChannelUnauthorizedException">If authorization fails.</exception>
        /// <exception cref="ChannelAuthorizationFailureException">f an HTTP call to the authorization URL fails; that is, the HTTP status code is outside of the range 200-299.</exception>
        /// <exception cref="ChannelException">If the client receives an error (pusher:error) from the Pusher cluster while trying to subscribe.</exception>
        /// <exception cref="OperationTimeoutException">
        /// If the client times out waiting for the Pusher server to confirm the subscription. The timeout is defind in the <see cref="PusherOptions"/>.
        /// </exception>
        /// <returns>A GenericPresenceChannel<MemberT> channel identified by <paramref name="channelName"/>.</returns>
        /// <remarks>
        /// If Pusher is connected when calling this method, the channel will be subscribed when this method returns;
        /// that is <c>channel.IsSubscribed == true</c>.
        /// If Pusher is not connected when calling this method, <c>channel.IsSubscribed == false</c> and
        /// the channel will only be subscribed after calling <c>Pusher.ConnectAsync</c>.
        /// </remarks>
        public async Task <GenericPresenceChannel <MemberT> > SubscribePresenceAsync <MemberT>(
            string channelName,
            SubscriptionEventHandler subscribedEventHandler = null)
        {
            Guard.ChannelName(channelName);

            var channelType = Channel.GetChannelType(channelName);

            if (channelType != ChannelTypes.Presence)
            {
                throw new ArgumentException($"The channel name '{channelName}' is not that of a presence channel. Expecting the name to start with '{Constants.PRESENCE_CHANNEL}'.", nameof(channelName));
            }

            GenericPresenceChannel <MemberT> result;

            if (Channels.TryGetValue(channelName, out Channel channel))
            {
                if (!(channel is GenericPresenceChannel <MemberT> presenceChannel))
                {
                    string errorMsg;
                    if (channel is PresenceChannel)
                    {
                        errorMsg = $"The presence channel '{channelName}' has already been created as a {nameof(PresenceChannel)} : {nameof(PresenceChannel)}<dynamic>.";
                    }
                    else
                    {
                        errorMsg = $"The presence channel '{channelName}' has already been created but with a different type: {channel.GetType()}";
                    }

                    throw new ChannelException(errorMsg, ErrorCodes.PresenceChannelAlreadyDefined, channelName, SocketID);
                }

                if (presenceChannel.IsSubscribed)
                {
                    result = presenceChannel;
                }
                else
                {
                    result = (await SubscribeAsync(channelName, presenceChannel, subscribedEventHandler).ConfigureAwait(false)) as GenericPresenceChannel <MemberT>;
                }
            }
            else
            {
                AuthEndpointCheck(channelName);
                result = new GenericPresenceChannel <MemberT>(channelName, this);
                if (Channels.TryAdd(channelName, result))
                {
                    result = (await SubscribeAsync(channelName, result, subscribedEventHandler).ConfigureAwait(false)) as GenericPresenceChannel <MemberT>;
                }
            }

            return(result);
        }
        /// <summary>
        /// Subscribes to a channel.
        /// </summary>
        /// <param name="channelName">The name of the channel to subsribe to.</param>
        /// <param name="subscribedEventHandler">An optional <see cref="SubscriptionEventHandler"/>. Alternatively, use <c>Pusher.Subscribed</c>.</param>
        /// <exception cref="ArgumentNullException">If <paramref name="channelName"/> is <c>null</c> or whitespace.</exception>
        /// <exception cref="ChannelUnauthorizedException">Only for private or presence channels if authorization fails.</exception>
        /// <exception cref="ChannelAuthorizationFailureException">Only for private or presence channels if an HTTP call to the authorization URL fails; that is, the HTTP status code is outside of the range 200-299.</exception>
        /// <exception cref="ChannelException">If the client receives an error (pusher:error) from the Pusher cluster while trying to subscribe.</exception>
        /// <exception cref="OperationTimeoutException">
        /// If the client times out waiting for the Pusher server to confirm the subscription. The timeout is defind in <see cref="PusherOptions"/>.
        /// </exception>
        /// <returns>The channel identified by <paramref name="channelName"/>.</returns>
        /// <remarks>
        /// If Pusher is connected when calling this method, the channel will be subscribed when this method returns;
        /// that is <c>channel.IsSubscribed == true</c>.
        /// If Pusher is not connected when calling this method, <c>channel.IsSubscribed == false</c> and
        /// the channel will only be subscribed after calling <c>Pusher.ConnectAsync</c>.
        /// </remarks>
        public async Task <Channel> SubscribeAsync(string channelName, SubscriptionEventHandler subscribedEventHandler = null)
        {
            Guard.ChannelName(channelName);

            if (Channels.TryGetValue(channelName, out Channel channel))
            {
                if (channel.IsSubscribed)
                {
                    return(channel);
                }
            }

            return(await SubscribeAsync(channelName, channel, subscribedEventHandler).ConfigureAwait(false));
        }
        private async Task <Channel> SubscribeAsync(string channelName, Channel channel, SubscriptionEventHandler subscribedEventHandler = null)
        {
            if (channel == null)
            {
                channel = CreateChannel(channelName);
            }

            if (subscribedEventHandler != null)
            {
                channel.Subscribed -= subscribedEventHandler;
                channel.Subscribed += subscribedEventHandler;
            }

            if (State == ConnectionState.Connected)
            {
                try
                {
                    TimeSpan timeoutPeriod = Options.ClientTimeout;
                    if (!await channel._subscribeLock.WaitAsync(timeoutPeriod).ConfigureAwait(false))
                    {
                        throw new OperationTimeoutException(timeoutPeriod, $"{Constants.CHANNEL_SUBSCRIPTION_SUCCEEDED} on {channelName}");
                    }

                    if (!channel.IsSubscribed)
                    {
                        if (Channels.ContainsKey(channelName))
                        {
                            await SubscribeChannelAsync(channel).ConfigureAwait(false);
                        }
                    }
                }
                catch (Exception error)
                {
                    HandleSubscribeChannelError(channel, error);
                    throw;
                }
                finally
                {
                    channel._subscribeLock.Release();
                }
            }

            return(channel);
        }
예제 #5
0
        static void Main(string[] args)
        {
            // Parse the command line options to override defaults
            String middlewareName     = "qpid";
            String transportName      = "sub";
            String sourceName         = "OM";
            String symbolName         = null;
            String dictionaryFile     = "/opt/openmama/data/dictionaries/data.dict";
            bool   requiresDictionary = true;
            bool   requiresInitial    = true;

            if (args.Length == 0)
            {
                usageAndExit();
            }

            for (int i = 0; i < args.Length; i++)
            {
                switch (args[i])
                {
                case "-B":
                    requiresDictionary = false;
                    break;

                case "-d":
                    dictionaryFile = args[++i];
                    break;

                case "-I":
                    requiresInitial = false;
                    break;

                case "-m":
                    middlewareName = args[++i];
                    break;

                case "-s":
                    symbolName = args[++i];
                    break;

                case "-S":
                    sourceName = args[++i];
                    break;

                case "-t":
                    transportName = args[++i];
                    break;

                case "-v":
                    Mama.enableLogging(MamaLogLevel.MAMA_LOG_LEVEL_FINEST);
                    break;

                default:
                    usageAndExit();
                    break;
                }
            }

            // Symbol name is mandatory here, so error if it's null
            if (symbolName == null)
            {
                usageAndExit();
            }

            // Load the requested OpenMAMA middleware bridge (and default payload too)
            MamaBridge bridge = Mama.loadBridge(middlewareName);

            // Time to initialize OpenMAMA's underlying bridges with an "open"
            Mama.open();

            // Get default event queue from the bridge for testing
            MamaQueue queue = Mama.getDefaultEventQueue(bridge);

            // Set up the required transport on the specified bridge
            MamaTransport transport = new MamaTransport();

            transport.create(transportName, bridge);

            // Set up the data dictionary
            MamaDictionary dictionary = null;

            if (requiresDictionary)
            {
                dictionary = new MamaDictionary();
                dictionary.create(dictionaryFile);
            }

            // Set up the OpenMAMA source (symbol namespace)
            MamaSource source = new MamaSource();

            source.symbolNamespace = sourceName;
            source.transport       = transport;

            // Set up the event handlers for OpenMAMA
            SubscriptionEventHandler eventHandler = new SubscriptionEventHandler();

            eventHandler.mDictionary = dictionary;

            // Set up the OpenMAMA Subscription (interest in a topic)
            MamaSubscription subscription = new MamaSubscription();

            subscription.setRequiresInitial(requiresInitial);
            subscription.create(queue, eventHandler, source, symbolName);

            // Kick off OpenMAMA now (blocking call, non-blocking call also available)
            Mama.start(bridge);

            // Clean up connection on termination
            Mama.close();
        }