基于Mozilla Thunderbird的扩展开发(六)---进程间通信之Socket篇(下)
      
| 
 | Mozilla扩展系列链接: 1,浅谈基于Mozilla Thunderbird的扩展开发 2,基于Mozilla平台的扩展开发(续)----XPCOM组件篇 3,基于Mozilla Thunderbird的扩展开发(三)---如何获取邮件的完整信息 4,基于Mozilla Thunderbird的扩展开发(四)---修改源代码实现自动保存附件 | 
      在上一篇《基于Mozilla Thunderbird的扩展开发(五)---进程间通信之Socket篇(上)》中开发了一个简单的TCP服务器,本文将介绍其对应的客户端。
   客户端代码:
 const tBirdBiffClientUi =
 const tBirdBiffClientUi = {
  { tBirdBiffClientOnLoad: function()
    tBirdBiffClientOnLoad: function() {
    { // remove to avoid duplicate initialization
      // remove to avoid duplicate initialization removeEventListener("load", tBirdBiffClientUi.tBirdBiffClientOnLoad, true);
      removeEventListener("load", tBirdBiffClientUi.tBirdBiffClientOnLoad, true); //初始化客户端
      //初始化客户端 var client = Components.classes["@phinecos.cnblogs.com/TBbiff/client;1"].getService(Components.interfaces.nsISupports).wrappedJSObject;
      var client = Components.classes["@phinecos.cnblogs.com/TBbiff/client;1"].getService(Components.interfaces.nsISupports).wrappedJSObject; client.initialize();
      client.initialize(); var windowCount = client.getWindowCollection().Count();
      var windowCount = client.getWindowCollection().Count(); client.addWindow(window);
      client.addWindow(window); client = null;
      client = null; tBirdBiffClientObserver.updateUi();//更新UI
      tBirdBiffClientObserver.updateUi();//更新UI // register for notifications
      // register for notifications var service = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
      var service = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService); service.addObserver(tBirdBiffClientObserver, "thunderbirdBiff.uiUpdate", false);//加入监控者
      service.addObserver(tBirdBiffClientObserver, "thunderbirdBiff.uiUpdate", false);//加入监控者  service = null;
      service = null; },
    },
 tBirdBiffClientOnClose: function()
    tBirdBiffClientOnClose: function() {
    { // remove to avoid duplicate initialization
      // remove to avoid duplicate initialization removeEventListener("close", tBirdBiffClientUi.tBirdBiffClientOnClose, true);
      removeEventListener("close", tBirdBiffClientUi.tBirdBiffClientOnClose, true); var service = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
      var service = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService); service.removeObserver(tBirdBiffClientObserver, "thunderbirdBiff.uiUpdate");//移除监控者
      service.removeObserver(tBirdBiffClientObserver, "thunderbirdBiff.uiUpdate");//移除监控者 service = null;
      service = null;
 var client = Components.classes["@phinecos.cnblogs.com/TBbiff/client;1"].getService(Components.interfaces.nsISupports).wrappedJSObject;
      var client = Components.classes["@phinecos.cnblogs.com/TBbiff/client;1"].getService(Components.interfaces.nsISupports).wrappedJSObject; client.removeWindow(window);//移除窗口
      client.removeWindow(window);//移除窗口 client = null;
      client = null; }
    } }
  } addEventListener("load", tBirdBiffClientUi.tBirdBiffClientOnLoad, true);
  addEventListener("load", tBirdBiffClientUi.tBirdBiffClientOnLoad, true); addEventListener("close", tBirdBiffClientUi.tBirdBiffClientOnClose, true);
  addEventListener("close", tBirdBiffClientUi.tBirdBiffClientOnClose, true);
客户类:
 const CI = Components.interfaces, CC = Components.classes, CR = Components.results;
const CI = Components.interfaces, CC = Components.classes, CR = Components.results; tBirdBiffClient.classID = Components.ID("{5b0bccd0-83b9-11db-9fe1-0800200c9a66}");
tBirdBiffClient.classID = Components.ID("{5b0bccd0-83b9-11db-9fe1-0800200c9a66}"); tBirdBiffClient.contractID = "@phinecos.cnblogs.com/TBbiff/client;1";
tBirdBiffClient.contractID = "@phinecos.cnblogs.com/TBbiff/client;1"; tBirdBiffClient.classDescription = "Biff Client Service";
tBirdBiffClient.classDescription = "Biff Client Service";
 function tBirdBiffClient()
function tBirdBiffClient() {
{ this.timer = CC["@mozilla.org/timer;1"].getService(CI.nsITimer);//定时器
  this.timer = CC["@mozilla.org/timer;1"].getService(CI.nsITimer);//定时器 this.interval = null;//定时器时间间隔
  this.interval = null;//定时器时间间隔 this.socketTransport = null;
  this.socketTransport = null; this.biffState = null;//邮箱状态
  this.biffState = null;//邮箱状态 this.socket = null;//socket对象
  this.socket = null;//socket对象 this.input = null;//输入流
  this.input = null;//输入流 this.port = null;//服务器端口
  this.port = null;//服务器端口 this.hostname = null;//服务器主机名称
  this.hostname = null;//服务器主机名称 this.windowCollection = CC["@mozilla.org/supports-array;1"].createInstance(CI.nsICollection);//客户端窗口集合
  this.windowCollection = CC["@mozilla.org/supports-array;1"].createInstance(CI.nsICollection);//客户端窗口集合 this.initialized = false;//是否已经初始化
  this.initialized = false;//是否已经初始化 }
}
 tBirdBiffClient.prototype =
tBirdBiffClient.prototype = {
{ getConnection: function()
  getConnection: function() {
  { this.closeSocket();//关闭先前的连接
    this.closeSocket();//关闭先前的连接 this.socket = this.socketTransport.createTransport(null, 0, this.hostname, this.port, null);//创建socket连接
    this.socket = this.socketTransport.createTransport(null, 0, this.hostname, this.port, null);//创建socket连接 if(!this.socket)
    if(!this.socket) {
    { return;
      return; }
    } var stream = this.socket.openInputStream(CI.nsITransport.OPEN_BLOCKING | CI.nsITransport.OPEN_UNBUFFERED, 0, 0);//打开输入流,类型为阻塞式,无缓冲
    var stream = this.socket.openInputStream(CI.nsITransport.OPEN_BLOCKING | CI.nsITransport.OPEN_UNBUFFERED, 0, 0);//打开输入流,类型为阻塞式,无缓冲 if(!stream)
    if(!stream) {
    { this.closeSocket();
      this.closeSocket(); return;
      return; }
    } this.input = CC["@mozilla.org/scriptableinputstream;1"].createInstance(CI.nsIScriptableInputStream);
    this.input = CC["@mozilla.org/scriptableinputstream;1"].createInstance(CI.nsIScriptableInputStream); if(!this.input)
    if(!this.input) {
    { this.closeSocket();
      this.closeSocket(); return;
      return; }
    } this.input.init(stream);//初始化输入流
    this.input.init(stream);//初始化输入流 },
  }, closeSocket: function()
  closeSocket: function() {//关闭socket连接
  {//关闭socket连接 if(this.input)
    if(this.input) {
    { this.input.close(null);
      this.input.close(null); this.input = null;
      this.input = null; }
    }
 if(this.socket)
    if(this.socket) {
    { this.socket.close(null);
      this.socket.close(null); this.socket = null;
      this.socket = null; }
    } },
  }, currentEventQueue: function()
  currentEventQueue: function() {//当前事件队列
  {//当前事件队列 var EQS = CC["@mozilla.org/event-queue-service;1"].getService(CI.nsIEventQueueService);
    var EQS = CC["@mozilla.org/event-queue-service;1"].getService(CI.nsIEventQueueService); return EQS.getSpecialEventQueue(EQS.CURRENT_THREAD_EVENT_QUEUE);
    return EQS.getSpecialEventQueue(EQS.CURRENT_THREAD_EVENT_QUEUE); },
  }, initialize: function()
  initialize: function() {//初始化客户端
  {//初始化客户端 if(this.initialized)
    if(this.initialized) {
    { return;
      return; }
    }
 this.getIntervalPref();//获取时间间隔
    this.getIntervalPref();//获取时间间隔 this.hostname = this.utility.getHostnamePref();//获取主机名称
    this.hostname = this.utility.getHostnamePref();//获取主机名称 this.port = this.utility.getPortPref();//获取端口
    this.port = this.utility.getPortPref();//获取端口 this.socketTransport = CC["@mozilla.org/network/socket-transport-service;1"].getService(CI.nsISocketTransportService);
    this.socketTransport = CC["@mozilla.org/network/socket-transport-service;1"].getService(CI.nsISocketTransportService); this.getConnection();//创建socket连接
    this.getConnection();//创建socket连接 this.timer.initWithCallback(tBirdBiffCheckMailCallback, 1000, this.timer.TYPE_ONE_SHOT);//启动定时器监听来自服务器的响应
    this.timer.initWithCallback(tBirdBiffCheckMailCallback, 1000, this.timer.TYPE_ONE_SHOT);//启动定时器监听来自服务器的响应 this.initialized = true;
    this.initialized = true; },
  }, updateUi: function(result)
  updateUi: function(result) {//更新客户端UI
  {//更新客户端UI var state;
    var state; switch(result)
    switch(result) {
    { case "":
      case "": {
      { state = "offline";
        state = "offline"; break;
        break; }
      } case "0":
      case "0": {
      { state = "noMail";
         state = "noMail"; break;
        break; }
      } case "1":
      case "1": {
      { state = "newMail";
        state = "newMail"; break;
        break; }
      } case "-1":
      case "-1": {
      { state = "error";
        state = "error"; break;
        break; }
      } default:
      default: {
      { state = "weirdness";
        state = "weirdness"; break;
        break; }
      } }
    } this.biffState = state;
    this.biffState = state; state = null;
    state = null; var service = CC["@mozilla.org/observer-service;1"].getService(CI.nsIObserverService);
    var service = CC["@mozilla.org/observer-service;1"].getService(CI.nsIObserverService); service.notifyObservers(null, "thunderbirdBiff.uiUpdate", null);
    service.notifyObservers(null, "thunderbirdBiff.uiUpdate", null); service = null;
    service = null; },
  }, checkForMail: function()
  checkForMail: function() {//检查是否有数据到来
  {//检查是否有数据到来 this.timer.initWithCallback(tBirdBiffReadFromServerCallback, 1000, this.timer.TYPE_ONE_SHOT);
    this.timer.initWithCallback(tBirdBiffReadFromServerCallback, 1000, this.timer.TYPE_ONE_SHOT); },
  },
 readFromServer: function()
  readFromServer: function() {//从服务器读取数据
  {//从服务器读取数据 if(!this.input)
    if(!this.input) {//还未初始化输入流
    {//还未初始化输入流 this.getConnection();
      this.getConnection(); }
    } try
    try {
    { var result = this.input.read(1);//读取数据
      var result = this.input.read(1);//读取数据 if(result.length == 1)
      if(result.length == 1) {
      { this.updateUi(result);//更新状态
        this.updateUi(result);//更新状态 }
      } result = null;
      result = null; }
    } catch (e)
    catch (e) {
    { this.getConnection();
      this.getConnection(); this.updateUi("");
      this.updateUi(""); }
    } this.timer.initWithCallback(tBirdBiffCheckMailCallback, this.interval, this.timer.TYPE_ONE_SHOT);//设置定时器
    this.timer.initWithCallback(tBirdBiffCheckMailCallback, this.interval, this.timer.TYPE_ONE_SHOT);//设置定时器 },
  }, }
}
客户端监听者,负责监视邮箱的状态变化和读取来自服务器端的数据:
 const tBirdBiffReadFromServerCallback =
const tBirdBiffReadFromServerCallback = {
{ notify: function(timer)
  notify: function(timer) {//从服务器读取数据
  {//从服务器读取数据 var client = CC[tBirdBiffClient.contractID].getService(CI.nsISupports).wrappedJSObject;
    var client = CC[tBirdBiffClient.contractID].getService(CI.nsISupports).wrappedJSObject; client.readFromServer();
    client.readFromServer(); client = null;
    client = null; }
  } }
}
 const tBirdBiffCheckMailCallback =
const tBirdBiffCheckMailCallback = {
{ notify: function(timer)
  notify: function(timer) {//检查邮箱状态
  {//检查邮箱状态 var client = CC[tBirdBiffClient.contractID].getService(CI.nsISupports).wrappedJSObject;
    var client = CC[tBirdBiffClient.contractID].getService(CI.nsISupports).wrappedJSObject; client.checkForMail();
    client.checkForMail(); client = null;
    client = null; }
  } }
}
      为了测试服务器和客户端,我们使用firefox作为客户端的载体,thunderbird作为服务器端载体,可以看到thunderbird的邮箱状态会定时传给firefox,从而使得后者能随之更新其状态。
 Reference:
作者:洞庭散人
出处:http://phinecos.cnblogs.com/
本博客遵从Creative Commons Attribution 3.0 License,若用于非商业目的,您可以自由转载,但请保留原作者信息和文章链接URL。
posted on 2008-05-20 22:09 Phinecos(洞庭散人) 阅读(2222) 评论(0) 收藏 举报
 
                    
                
 
  
 
     
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号