/* * Constructor a node and adds it to the provided cluster. */ public Node(int id, int port, bool ensureSecurity, string clusterURL) : this(id, port, ensureSecurity) { INode cluster = (INode)Activator.GetObject(typeof(INode), clusterURL); Console.WriteLine("Joining cluster @ " + clusterURL); ClusterReport report = null; //Attempt to join the cluster int attempts = 3; while (attempts != 0) { attempts--; report = cluster.join(this.URL); if (report != null) { break; } Console.WriteLine("Retrying..."); } //Check for valid cluster's answer if (report == null) { Console.WriteLine("Failed to joining cluster @ " + clusterURL); return; } string trackerURL = report.Tracker; //Check if we started the connection with the tracker or a random node if (trackerURL != clusterURL) { this.tracker = (INode)Activator.GetObject(typeof(INode), trackerURL); this.trkUrl = trackerURL; } else { this.tracker = cluster; this.trkUrl = clusterURL; } Console.WriteLine("Joined cluster @ " + this.trkUrl); //Updates the current view with the cluster's one List <string> clus = new List <string>(report.Cluster); foreach (string s in clus) { if (s != this.URL) { onClusterIncrease(s); } } }
/* * Receives a Node's URL and adds it to the cluster. * It also returns information about all nodes on this cluster and the active tracker */ public ClusterReport join(string nodeUrl) { if (this.IsTracker) { //Creates node's proxy INode newPeer = (INode)Activator.GetObject(typeof(INode), nodeUrl); //Tell cluster to add this new node clusterAction((node) => { node.onClusterIncrease(nodeUrl); return(null); }); //Prepares cluster's view ClusterReport report = new ClusterReport(); report.View = 1;//No use as for now report.Tracker = this.URL; report.Cluster = new List <string>(this.cluster.Keys); return(report); } else { return((ClusterReport)nodeAction((trk) => { return trk.join(nodeUrl); }, this.trkUrl)); } }