示例#1
0
文件: Node.cs 项目: modulexcite/WcfEx
 /// <summary>
 /// Initializes a new node instance
 /// </summary>
 /// <param name="nodeID">
 /// This node's ID
 /// </param>
 /// <param name="parentID">
 /// An optional initial peer ID
 /// </param>
 /// <param name="db">
 /// This node's database
 /// </param>
 public Node(
     Uri nodeID,
     Uri parentID,
     Database db)
 {
     this.peers = new PeerList(Config.Instance.MaxPeersPerNode);
      this.peers.OnPeerException += this.HandlePeerException;
      this.wakeupTimer = new System.Timers.Timer();
      this.nodeID = nodeID;
      this.parentID = parentID;
      if (parentID != null)
     this.peers.GetOrAdd(parentID);
      this.db = db;
      this.wakeupTimer.Elapsed += Wakeup;
      this.wakeupTimer.AutoReset = false;
      this.wakeupTimer.Interval = StaticRandom.NextCentered(Config.Instance.WakeupInterval, 100);
      this.wakeupTimer.Start();
 }
示例#2
0
 /// <summary>
 /// Starts up a single gossip node instance
 /// </summary>
 /// <param name="nodeIdx">
 /// The zero-based node index
 /// </param>
 /// <returns>
 /// The WCF service host for the instance
 /// </returns>
 static ServiceHost StartNode(Int32 nodeIdx)
 {
     // construct the node/parent address URIs
      var parentIdx = (nodeIdx - 1) / Config.Instance.MaxPeersPerNode;
      var nodeAddress = new UriBuilder(
     "udp",
     System.Net.Dns.GetHostName(),
     Config.Instance.BasePort + nodeIdx
      ).Uri;
      var parentAddress = (nodeIdx == 0) ?
     (PeerHost != null) ?
        new UriBuilder("udp", PeerHost, Config.Instance.BasePort).Uri :
        null :
     new UriBuilder(
        "udp",
        System.Net.Dns.GetHostName(),
        Config.Instance.BasePort + parentIdx
     ).Uri;
      // create and configure the current node's database, and
      // create and register the configured item combinators
      var db = new Database();
      db.OnCombined += item => HandleCombined(nodeIdx, item);
      foreach (var config in Config.Instance.Combinators)
     db.Register(config.Namespace, CreateCombinator(config));
      // create the node and the WCF service host,
      // and listen on the node's address
      var node = new Node(nodeAddress, parentAddress, db);
      var wcfHost = new ServiceHost(node);
      wcfHost.AddServiceEndpoint(
     typeof(INode),
     new Udp.Binding(),
     nodeAddress
      );
      wcfHost.Open();
      return wcfHost;
 }