开发学院软件开发C语言 适合C# Actor的消息执行方式(6):协变与逆变 阅读

适合C# Actor的消息执行方式(6):协变与逆变

 2010-09-30 20:50:09 来源:WEB开发网   
核心提示: 为了让Monior支持查询,我们还需要为它添加这样的代码:publicclassMonitor:Actor<Action<Monitor>>,IPort<Monitor>,ICrawlResponseHandler,IStatisticRequestHande

为了让Monior支持查询,我们还需要为它添加这样的代码:

public class Monitor : Actor<Action<Monitor>>, IPort<Monitor>, 
  ICrawlResponseHandler, 
  IStatisticRequestHandelr 
{ 
  ... 
 
  #region IStatisticRequestHandelr Members 
 
  void IStatisticRequestHandelr.GetCrawledCount(IPort<IStatisticResponseHandler> requester) 
  { 
    requester.Post(r => r.ReplyCrawledCount(this.m_urlContent.Count)); 
  } 
 
  void IStatisticRequestHandelr.GetContent(IPort<IStatisticResponseHandler> requester, string url) 
  { 
    string content; 
    if (!this.m_urlContent.TryGetValue(url, out content)) 
    { 
      content = null; 
    } 
 
    requester.Post(r => r.ReplyContent(url, content)); 
  } 
 
  #endregion 
}

最后,我们来尝试着使用这个“查询”功能。首先,我们编写一个测试用的TestStatisticPort类:

public class TestStatisticPort : IPort<IStatisticResponseHandler>, IStatisticResponseHandler 
{ 
  private IPort<IStatisticRequestHandelr> m_statisticPort; 
 
  public TestStatisticPort(IPort<IStatisticRequestHandelr> statisticPort) 
  { 
    this.m_statisticPort = statisticPort; 
  } 
 
  public void Start() 
  { 
    while (true) 
    { 
      Console.ReadLine(); 
      this.m_statisticPort.Post(s => s.GetCrawledCount(this)); 
    } 
  } 
 
  #region IPort<IStatisticResponseHandler> Members 
 
  void IPort<IStatisticResponseHandler>.Post(Action<IStatisticResponseHandler> message) 
  { 
    message(this); 
  } 
 
  #endregion 
 
  #region IStatisticResponseHandler Members 
 
  void IStatisticResponseHandler.ReplyCrawledCount(int count) 
  { 
    Console.WriteLine("Crawled: {0}", count); 
  } 
 
  void IStatisticResponseHandler.ReplyContent(string url, string content) { ... } 
 
  #endregion 
}

上一页  1 2 3 4 5 6 7  下一页

Tags:适合 Actor 消息

编辑录入:爽爽 [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接