/// <summary> /// Add the subscription to the list /// </summary> /// <typeparam name="S">Signal type</typeparam> /// <typeparam name="R">Response type</typeparam> /// <param name="subscription">Subscription to add</param> /// <param name="group">The group to use</param> private void _AddSubscription <S, R>(ISubscription subscription, long?group) { var responseType = typeof(R); var signalKey = new SignalKey(typeof(S), responseType, group); IList subscriptionList; // Set the subscriptions signal key subscription._SignalKey = signalKey; // If the signal type is not found, then create a new signal type in the dictionary if (!_subscriptions.TryGetValue(signalKey, out subscriptionList)) { // Set up the appropriate subscription list based on whether it is a message or request if (responseType == typeof(NoResponse)) { subscriptionList = new LiteList <MessageSubscription <S> >(); } else { subscriptionList = new LiteList <RequestSubscription <S, R> >(); } subscriptionList.Add(subscription); _subscriptions.Add(signalKey, subscriptionList); } else { // The signal type / group was found, so we insert the subscription _InsertSubscription(subscriptionList, subscription); } // Set the subscription's list subscription._SubscriptionList = subscriptionList; }
/// <summary> /// Searches for a subscription with the proper subscriber, request action, and group /// </summary> /// <typeparam name="S">Signal Type</typeparam> /// <typeparam name="R">Response Type</typeparam> /// <param name="group">Group</param> /// <returns>subscription if found</returns> private ISubscription _GetSubscription <S, R>(ISubscriber subscriber, RequestAction <S, R> action, long?group = null) { var signalKey = new SignalKey(typeof(S), typeof(R), group); IList subscriptionList; // If the signal type is not found, then create a new signal type in the dictionary if (_subscriptions.TryGetValue(signalKey, out subscriptionList)) { // cast the subscription list to the appropriate type LiteList <RequestSubscription <S, R> > typeList = (LiteList <RequestSubscription <S, R> >)subscriptionList; // search through the subscriptions for one that has the correct // subscriber and action for (var i = 0; i < typeList.Count; i++) { if (typeList[i]._Subscriber == subscriber && typeList[i].Action == action ) { return(typeList[i]); } } } return(null); }
/// <summary> /// Broadcast a request signal and return responses. /// </summary> /// <typeparam name="S">Signal type</typeparam> /// <typeparam name="R">Response type</typeparam> /// <param name="broadcaster">The broadcaster of the signal</param> /// <param name="responses">A list of responses to return</param> /// <param name="signal">The signal to send. Can be left blank if no data is being passed (like a trigger).</param> /// <param name="group">The group to use. Can be left blank if no group is being used.</param> /// <returns>Returns the count of the subscribers that received the signal</returns> public int Broadcast <S, R>(IBroadcaster broadcaster, out List <SignalResponse <R> > responses, S signal = default(S), long?group = null) { IList subscriptionList; SignalKey signalKey; // Get the signal key and subscription list if (!_PreBroadcast <S, R>(group, out signalKey, out subscriptionList)) { responses = null; return(0); } var signalType = typeof(S); var count = 0; int i; // Cast the subscription list to the signal/response types LiteList <RequestSubscription <S, R> > typeList = (LiteList <RequestSubscription <S, R> >)subscriptionList; responses = new List <SignalResponse <R> >(); R response; // Go through each subscription for (i = 0; i < typeList.Count; i++) { if (_logInfo) { Debug.Log(string.Format("Signaler::Broadcasting request signal {0}: {1} of {2}", signalType, i + 1, subscriptionList.Count)); } // Perform the subscription's action and get the response if (typeList[i].Action(signal, out response)) { if (_logInfo) { Debug.Log(string.Format("Signaler::Request signal {0} received by {1}", signalType, typeList[i]._Subscriber)); } // The response exists, so we add it to the list of responses to return responses.Add(new SignalResponse <R>() { subscriber = typeList[i]._Subscriber, response = response }); count++; } } // clean up broadcast _PostBroadcast(signalKey); return(count); }
/// <summary> /// Broadcast a message signal. /// </summary> /// <typeparam name="S">Signal type</typeparam> /// <param name="broadcaster">The broadcaster of the signal</param> /// <param name="signal">The signal to send. Can be left blank if no data is being passed (like a trigger).</param> /// <param name="group">The group to use. Can be left blank if no group is being used.</param> /// <returns>Returns the count of subscribers that received the signal</returns> public int Broadcast <S>(IBroadcaster broadcaster, S signal = default(S), long?group = null) { IList subscriptionList; SignalKey signalKey; // Get the signal key and subscription list if (!_PreBroadcast <S, NoResponse>(group, out signalKey, out subscriptionList)) { return(0); } var signalType = typeof(S); int count = 0; int i; // Cast the subscription list to the signal/response types LiteList <MessageSubscription <S> > typeList = (LiteList <MessageSubscription <S> >)subscriptionList; // Go through each subscription for (i = 0; i < typeList.Count; i++) { if (_logInfo) { Debug.Log(string.Format("Signaler::Broadcasting message signal {0}: {1} of {2}", signalType, i + 1, subscriptionList.Count)); } // Perform the subscription's action if (typeList[i].Action(signal)) { if (_logInfo) { Debug.Log(string.Format("Signaler::Message signal {0} {1} of {2} received by {3}", signalType, i + 1, subscriptionList.Count, typeList[i]._Subscriber)); } count++; } } // clean up broadcast _PostBroadcast(signalKey); return(count); }
public LiteListEnum(LiteList <T> list) { this.list = list; }