/// <summary> /// Receives actor subscriptions and informs what parent they should connect to. /// </summary> /// <param name="context"></param> /// <param name="request"></param> private void HandleSubscribe(HttpListenerContext context, HttpListenerRequest request) { StreamReader reader = new StreamReader(request.InputStream); XmlSerializer deserializer = new XmlSerializer(typeof(XMLQuarkSubscription)); XMLQuarkSubscription quarkSub = (XMLQuarkSubscription)deserializer.Deserialize(reader); HttpListenerResponse response = context.Response; response.StatusCode = (int)HttpStatusCode.OK; Stream output = response.OutputStream; // Default algorithm is set for star topology. if (request.RawUrl.Contains("/quark/subscribe/actor")) { RegisterActor(quarkSub, false); // Construct a response. // Currently sending all roots. Ideally, a policy/algorithm should decide to what List <string> allRoots = new List <string>(); foreach (RootInfo ri in m_rootActors) { allRoots.Add(ri.syncAddress); } System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(allRoots.GetType()); x.Serialize(output, allRoots); } else if (request.RawUrl.Contains("/quark/subscribe/root")) { RegisterActor(quarkSub, true); } // TODO: Should root actors know about other root actors? Currently, we reply nothing to root actors. output.Close(); response.Close(); }
private void RegisterActor(XMLQuarkSubscription sub, bool isRoot) { HashSet <string> activeQuarks = SyncQuark.DecodeSyncQuarks(sub.activeQuarks); HashSet <string> passiveQuarks = SyncQuark.DecodeSyncQuarks(sub.passiveQuarks); QuarkPublisher qp; // Save SyncID to XMLQuarkSubscription, if we need the info later if (m_actor.ContainsKey(sub.actorID)) { UnRegisterActor(sub.actorID); } if (isRoot) { RootInfo root = new RootInfo(); root.quarkAddress = sub.rootAddress; root.syncAddress = sub.syncListenerAddress; root.Roots.Add(root); m_actor[sub.actorID] = root; m_rootActors.Add((RootInfo)m_actor[sub.actorID]); } else { if (sub.syncListenerAddress.Length > 0) { RelayActor relActor = new RelayActor(); relActor.syncAddress = sub.syncListenerAddress; m_actor[sub.actorID] = relActor; } else { Actor actor = new Actor(); m_actor[sub.actorID] = actor; } // Note: Adding all the roots as this actor's root for now. foreach (RootInfo root in m_rootActors) { m_actor[sub.actorID].Roots.Add(root); } } m_actor[sub.actorID].ActiveQuarks = activeQuarks; m_actor[sub.actorID].PassiveQuarks = passiveQuarks; m_actor[sub.actorID].XmlSubscription = sub; foreach (string quark in activeQuarks) { if (!QuarkSubscriptions.TryGetValue(quark, out qp)) { qp = new QuarkPublisher(new SyncQuark(quark)); QuarkSubscriptions.Add(quark, qp); } qp.AddActiveSubscriber(sub.actorID); if (isRoot) { qp.SetRootActor(sub.actorID); } } foreach (string quark in passiveQuarks) { if (!QuarkSubscriptions.TryGetValue(quark, out qp)) { qp = new QuarkPublisher(new SyncQuark(quark)); QuarkSubscriptions.Add(quark, qp); } qp.AddPassiveSubscriber(sub.actorID); } }