/// <summary> /// Performs a subscription to a remote publisher for a local subscriber<br /># /// In this method, the remote subscription is completed /// </summary> internal static void SubscribeToRemotePublisher <T>(RemoteSubscriptionHandle handle, object subscriberInstance, Action <T, SubscriptionHandle> newDataCallBack, DataModifyPolicy policy, Func <T, bool> evaluateTemplateObject, Action <Type, SubscriptionHandle> subscriptionCallback) { _log.DebugFormat("Completing subscription to remote publisher {0} on node {1},handle: {2}, type: {3}", handle.PublisherId, handle.PublisherNodeID, handle, typeof(T).Name); //TODO template object //Create a stub Stub <T> s = new Stub <T> { DataType = typeof(T), Handle = handle }; if ( (EllaModel.Instance.GetActivePublishers().Where(p => p.Instance is Stub && (p.Instance as Stub).Handle == handle)).Any()) { _log.DebugFormat("Avoiding double subscription for handle {0}. Anready an active stub available.", handle); return; } var publishesAttribute = (PublishesAttribute)s.GetType().GetCustomAttributes(typeof(PublishesAttribute), false).First(); if (handle is MulticastRemoteSubscriptionhandle) { _log.DebugFormat("{0} is potential multicast subscription, opening multicast port", handle); MulticastRemoteSubscriptionhandle mh = handle as MulticastRemoteSubscriptionhandle; Networking.ConnectToMulticast(mh.IpAddress, mh.Port); RemoteSubscriptionHandle h = new RemoteSubscriptionHandle() { PublisherNodeID = handle.PublisherNodeID, PublisherId = handle.PublisherId, SubscriptionReference = handle.SubscriptionReference, EventID = handle.EventID, SubscriberNodeID = handle.SubscriberNodeID }; s.Handle = h; } Start.Publisher(s); Event ev = new Event { Publisher = s, EventDetail = publishesAttribute }; handle.SubscriberId = EllaModel.Instance.GetSubscriberId(subscriberInstance); Subscription sub = new Subscription(subscriberInstance, ev, newDataCallBack.Method, newDataCallBack.Target) { Handle = s.Handle, DataType = typeof(T) }; EllaModel.Instance.AddSubscription(sub); var sanity = EllaModel.Instance.CheckSubscriptionSanity(); if (sanity > 0) { _log.WarnFormat("Subscription sanity restored. removed {0} subscriptions", sanity); } if (subscriptionCallback != null) { _log.DebugFormat("Found subscription callback for {0}, notifying subscriber", handle); subscriptionCallback(typeof(T), sub.Handle); } else { _log.DebugFormat("Subscription callback for {0} was null, not notifying subscriber", handle); } }