专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »编程综合 » thunderbird:基于Mozilla Thunderbird的扩展开发( 6)---进程间通信的Socket篇(下) »正文

thunderbird:基于Mozilla Thunderbird的扩展开发( 6)---进程间通信的Socket篇(下)

来源: 发布时间:星期四, 2009年1月15日 浏览:32次 评论:0
  在上基于Mozilla Thunderbird扩展开发( 5)---进程间通信的Socket篇(上)中开发了个简单TCP服务器本文将介绍其对应客户端

  客户端代码:

   const tBirdBfClientUi =
 {
  tBirdBfClientOnLoad: function
  {
   // remove to avoid duplicate initialization
   removeEventListener("load", tBirdBfClientUi.tBirdBfClientOnLoad, true);
   //化客户端
   var client = Components.es["@phinecos.cnblogs.com/TBbf/client;1"].getService(Components.erfaces.nsISupports).wrappedJSObject;
   client.initialize;
   var windowCount = client.getWindowCollection.Count;
   client.addWindow(window);
   client = null;
   tBirdBfClientObserver.updateUi;//更新UI
   // register for notications
   var service = Components.es["@mozilla.org/observer-service;1"].getService(Components.erfaces.nsIObserverService);
   service.addObserver(tBirdBfClientObserver, "thunderbirdBf.uiUpdate", false);//加入监控者
   service = null;
  },
  
  tBirdBfClientOnClose: function
  {
   // remove to avoid duplicate initialization
   removeEventListener("close", tBirdBfClientUi.tBirdBfClientOnClose, true);
   var service = Components.es["@mozilla.org/observer-service;1"].getService(Components.erfaces.nsIObserverService);
   service.removeObserver(tBirdBfClientObserver, "thunderbirdBf.uiUpdate");//移除监控者
   service = null;
  
   var client = Components.es["@phinecos.cnblogs.com/TBbf/client;1"].getService(Components.erfaces.nsISupports).wrappedJSObject;
   client.removeWindow(window);//移除窗口
   client = null;
  }
 }
 addEventListener("load", tBirdBfClientUi.tBirdBfClientOnLoad, true);
 addEventListener("close", tBirdBfClientUi.tBirdBfClientOnClose, true);


  客户类:

const CI = Components.erfaces, CC = Components.es, CR = Components.results;
tBirdBfClient.ID = Components.ID("{5b0bccd0-83b9-11db-9fe1-0800200c9a66}");
tBirdBfClient.contractID = "@phinecos.cnblogs.com/TBbf/client;1";
tBirdBfClient.Description = "Bf Client Service";
  
function tBirdBfClient
{
 this.timer = CC["@mozilla.org/timer;1"].getService(CI.nsITimer);//定时器
 this.erval = null;//定时器时间间隔
 this.Transport = null;
 this.bfState = null;//邮箱状态
 this. = null;//对象
 this.input = null;//输入流
 this.port = null;//服务器端口
 this.hostname = null;//服务器主机名称
 this.windowCollection = CC["@mozilla.org/supports-.gif' />;1"].createInstance(CI.nsICollection);//客户端窗口集合
 this.initialized = false;//是否已经
}
  
tBirdBfClient.prototype =
{
 getConnection: function
 {
  this.closeSocket;//关闭先前连接
  this. = this.Transport.createTransport(null, 0, this.hostname, this.port, null);//创建连接
  (!this.)
  {
   ;
  }
  var stream = this..openInputStream(CI.nsITransport.OPEN_BLOCKING | CI.nsITransport.OPEN_UNBUFFERED, 0, 0);//打开输入流,类型为阻塞式无缓冲
  (!stream)
  {
   this.closeSocket;
   ;
  }
  this.input = CC["@mozilla.org/scriptableinputstream;1"].createInstance(CI.nsIScriptableInputStream);
  (!this.input)
  {
   this.closeSocket;
   ;
  }
  this.input.init(stream);//化输入流
 },
 closeSocket: function
 {//关闭连接
  (this.input)
  {
   this.input.close(null);
   this.input = null;
  }
  
  (this.)
  {
   this..close(null);
   this. = null;
  }
 },
 currentEventQueue: function
 {//当前事件队列
  var EQS = CC["@mozilla.org/event-queue-service;1"].getService(CI.nsIEventQueueService);
   EQS.getSpecialEventQueue(EQS.CURRENT_THREAD_EVENT_QUEUE);
 },
 initialize: function
 {//化客户端
  (this.initialized)
  {
   ;
  }
  
  this.getIntervalPref;//获取时间间隔
  this.hostname = this.utility.getHostnamePref;//获取主机名称
  this.port = this.utility.getPortPref;//获取端口
  this.Transport = CC["@mozilla.org/network/-transport-service;1"].getService(CI.nsISocketTransportService);
  this.getConnection;//创建连接
  this.timer.initWithCallback(tBirdBfCheckMailCallback, 1000, this.timer.TYPE_ONE_SHOT);//启动定时器监听来自服务器响应
  this.initialized = true;
 },
 updateUi: function(result)
 {//更新客户端UI
  var state;
  switch(result)
  {
    "":
   {
    state = "offline";
    ;
   }
    "0":
   {
     state = "noMail";
    ;
   }
    "1":
   {
    state = "Mail";
    ;
   }
    "-1":
   {
    state = "error";
    ;
   }
   default:
   {
    state = "weirdness";
    ;
   }
  }
  this.bfState = state;
  state = null;
  var service = CC["@mozilla.org/observer-service;1"].getService(CI.nsIObserverService);
  service.notyObservers(null, "thunderbirdBf.uiUpdate", null);
  service = null;
 },
 checkForMail: function
 {//检查是否有数据到来
  this.timer.initWithCallback(tBirdBfReadFromServerCallback, 1000, this.timer.TYPE_ONE_SHOT);
 },
  
 readFromServer: function
 {//从服务器读取数据
  (!this.input)
  {//还未化输入流
   this.getConnection;
  }
  try
  {
   var result = this.input.read(1);//读取数据
   (result.length 1)
   {
    this.updateUi(result);//更新状态
   }
   result = null;
  }
  catch (e)
  {
   this.getConnection;
   this.updateUi("");
  }
  this.timer.initWithCallback(tBirdBfCheckMailCallback, this.erval, this.timer.TYPE_ONE_SHOT);//设置定时器
 },
}




  客户端监听者负责监视邮箱状态变化和读取来自服务器端数据:

const tBirdBfReadFromServerCallback =
{
 noty: function(timer)
 {//从服务器读取数据
  var client = CC[tBirdBfClient.contractID].getService(CI.nsISupports).wrappedJSObject;
  client.readFromServer;
  client = null;
 }
}
  
const tBirdBfCheckMailCallback =
{
 noty: function(timer)
 {//检查邮箱状态
  var client = CC[tBirdBfClient.contractID].getService(CI.nsISupports).wrappedJSObject;
  client.checkForMail;
  client = null;
 }
}


  为了测试服务器和客户端我们使用firefox作为客户端载体thunderbird作为服务器端载体可以看到thunderbird邮箱状态会定时传给firefox从而使得后者能随的更新其状态



0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: