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

      <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" />

20080506.bmp

Mozilla扩展系列链接:

1浅谈基于Mozilla Thunderbird的扩展开发

2基于Mozilla平台的扩展开发(续)----XPCOM组件篇

3基于Mozilla Thunderbird的扩展开发(三)---如何获取邮件的完整信息

4基于Mozilla Thunderbird的扩展开发(四)---修改源代码实现自动保存附件

5基于Mozilla Thunderbird的扩展开发(五)---进程间通信之Socket篇(上)

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

      在上一篇《基于Mozilla Thunderbird的扩展开发(五)---进程间通信之Socket篇(上)》中开发了一个简单的TCP服务器,本文将介绍其对应的客户端。

   客户端代码:

   

None.gif  const tBirdBiffClientUi  =
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif {
InBlock.gif    tBirdBiffClientOnLoad: 
function()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
// remove to avoid duplicate initialization
InBlock.gif
      removeEventListener("load", tBirdBiffClientUi.tBirdBiffClientOnLoad, true);
InBlock.gif      
//初始化客户端
InBlock.gif
      var client = Components.classes["@phinecos.cnblogs.com/TBbiff/client;1"].getService(Components.interfaces.nsISupports).wrappedJSObject;
InBlock.gif      client.initialize();
InBlock.gif      
var windowCount = client.getWindowCollection().Count();
InBlock.gif      client.addWindow(window);
InBlock.gif      client 
= null;
InBlock.gif      tBirdBiffClientObserver.updateUi();
//更新UI
InBlock.gif
      // register for notifications
InBlock.gif
      var service = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
InBlock.gif      service.addObserver(tBirdBiffClientObserver, 
"thunderbirdBiff.uiUpdate"false);//加入监控者 
InBlock.gif
      service = null;
ExpandedSubBlockEnd.gif    }
,
InBlock.gif
InBlock.gif    tBirdBiffClientOnClose: 
function()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
// remove to avoid duplicate initialization
InBlock.gif
      removeEventListener("close", tBirdBiffClientUi.tBirdBiffClientOnClose, true);
InBlock.gif      
var service = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
InBlock.gif      service.removeObserver(tBirdBiffClientObserver, 
"thunderbirdBiff.uiUpdate");//移除监控者
InBlock.gif
      service = null;
InBlock.gif
InBlock.gif      
var client = Components.classes["@phinecos.cnblogs.com/TBbiff/client;1"].getService(Components.interfaces.nsISupports).wrappedJSObject;
InBlock.gif      client.removeWindow(window);
//移除窗口
InBlock.gif
      client = null;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif  }

None.gif  addEventListener(
" load " , tBirdBiffClientUi.tBirdBiffClientOnLoad,  true );
None.gif  addEventListener(
" close " , tBirdBiffClientUi.tBirdBiffClientOnClose,  true );
None.gif

客户类:

None.gif const CI  =  Components.interfaces, CC  =  Components.classes, CR  =  Components.results;
None.giftBirdBiffClient.classID 
=  Components.ID( " {5b0bccd0-83b9-11db-9fe1-0800200c9a66} " );
None.giftBirdBiffClient.contractID 
=   " @phinecos.cnblogs.com/TBbiff/client;1 " ;
None.giftBirdBiffClient.classDescription 
=   " Biff Client Service " ;
None.gif
None.gif
function  tBirdBiffClient()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif  
this.timer = CC["@mozilla.org/timer;1"].getService(CI.nsITimer);//定时器
InBlock.gif
  this.interval = null;//定时器时间间隔
InBlock.gif
  this.socketTransport = null;
InBlock.gif  
this.biffState = null;//邮箱状态
InBlock.gif
  this.socket = null;//socket对象
InBlock.gif
  this.input = null;//输入流
InBlock.gif
  this.port = null;//服务器端口
InBlock.gif
  this.hostname = null;//服务器主机名称
InBlock.gif
  this.windowCollection = CC["@mozilla.org/supports-array;1"].createInstance(CI.nsICollection);//客户端窗口集合
InBlock.gif
  this.initialized = false;//是否已经初始化
ExpandedBlockEnd.gif
}

None.gif
None.giftBirdBiffClient.prototype 
=
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif  getConnection: 
function()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif    
this.closeSocket();//关闭先前的连接
InBlock.gif
    this.socket = this.socketTransport.createTransport(null0this.hostname, this.port, null);//创建socket连接
InBlock.gif
    if(!this.socket)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
return;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
var stream = this.socket.openInputStream(CI.nsITransport.OPEN_BLOCKING | CI.nsITransport.OPEN_UNBUFFERED, 00);//打开输入流,类型为阻塞式,无缓冲
InBlock.gif
    if(!stream)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
this.closeSocket();
InBlock.gif      
return;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
this.input = CC["@mozilla.org/scriptableinputstream;1"].createInstance(CI.nsIScriptableInputStream);
InBlock.gif    
if(!this.input)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
this.closeSocket();
InBlock.gif      
return;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
this.input.init(stream);//初始化输入流
ExpandedSubBlockEnd.gif
  }
,
InBlock.gif  closeSocket: 
function()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{//关闭socket连接
InBlock.gif
    if(this.input)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
this.input.close(null);
InBlock.gif      
this.input = null;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
if(this.socket)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
this.socket.close(null);
InBlock.gif      
this.socket = null;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif  }
,
InBlock.gif  currentEventQueue: 
function()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{//当前事件队列
InBlock.gif
    var EQS = CC["@mozilla.org/event-queue-service;1"].getService(CI.nsIEventQueueService);
InBlock.gif    
return EQS.getSpecialEventQueue(EQS.CURRENT_THREAD_EVENT_QUEUE);
ExpandedSubBlockEnd.gif  }
,
InBlock.gif  initialize: 
function()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{//初始化客户端
InBlock.gif
    if(this.initialized)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
return;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
this.getIntervalPref();//获取时间间隔
InBlock.gif
    this.hostname = this.utility.getHostnamePref();//获取主机名称
InBlock.gif
    this.port = this.utility.getPortPref();//获取端口
InBlock.gif
    this.socketTransport = CC["@mozilla.org/network/socket-transport-service;1"].getService(CI.nsISocketTransportService);
InBlock.gif    
this.getConnection();//创建socket连接
InBlock.gif
    this.timer.initWithCallback(tBirdBiffCheckMailCallback, 1000this.timer.TYPE_ONE_SHOT);//启动定时器监听来自服务器的响应
InBlock.gif
    this.initialized = true;
ExpandedSubBlockEnd.gif  }
,
InBlock.gif  updateUi: 
function(result)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{//更新客户端UI
InBlock.gif
    var state;
InBlock.gif    
switch(result)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
case "":
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        state 
= "offline";
InBlock.gif        
break;
ExpandedSubBlockEnd.gif      }

InBlock.gif      
case "0":
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif         state 
= "noMail";
InBlock.gif        
break;
ExpandedSubBlockEnd.gif      }

InBlock.gif      
case "1":
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        state 
= "newMail";
InBlock.gif        
break;
ExpandedSubBlockEnd.gif      }

InBlock.gif      
case "-1":
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        state 
= "error";
InBlock.gif        
break;
ExpandedSubBlockEnd.gif      }

InBlock.gif      
default:
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        state 
= "weirdness";
InBlock.gif        
break;
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
this.biffState = state;
InBlock.gif    state 
= null;
InBlock.gif    
var service = CC["@mozilla.org/observer-service;1"].getService(CI.nsIObserverService);
InBlock.gif    service.notifyObservers(
null"thunderbirdBiff.uiUpdate"null);
InBlock.gif    service 
= null;
ExpandedSubBlockEnd.gif  }
,
InBlock.gif  checkForMail: 
function()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{//检查是否有数据到来
InBlock.gif
    this.timer.initWithCallback(tBirdBiffReadFromServerCallback, 1000this.timer.TYPE_ONE_SHOT);
ExpandedSubBlockEnd.gif  }
,
InBlock.gif
InBlock.gif  readFromServer: 
function()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{//从服务器读取数据
InBlock.gif
    if(!this.input)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{//还未初始化输入流
InBlock.gif
      this.getConnection();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
var result = this.input.read(1);//读取数据
InBlock.gif
      if(result.length == 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        
this.updateUi(result);//更新状态
ExpandedSubBlockEnd.gif
      }

InBlock.gif      result 
= null;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
catch (e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
this.getConnection();
InBlock.gif      
this.updateUi("");
ExpandedSubBlockEnd.gif    }

InBlock.gif    
this.timer.initWithCallback(tBirdBiffCheckMailCallback, this.interval, this.timer.TYPE_ONE_SHOT);//设置定时器
ExpandedSubBlockEnd.gif
  }
,
ExpandedBlockEnd.gif}

None.gif



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

None.gif const tBirdBiffReadFromServerCallback  =
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif  notify: 
function(timer)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{//从服务器读取数据
InBlock.gif
    var client = CC[tBirdBiffClient.contractID].getService(CI.nsISupports).wrappedJSObject;
InBlock.gif    client.readFromServer();
InBlock.gif    client 
= null;
ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif}

None.gif
None.gifconst tBirdBiffCheckMailCallback 
=
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif  notify: 
function(timer)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{//检查邮箱状态
InBlock.gif
    var client = CC[tBirdBiffClient.contractID].getService(CI.nsISupports).wrappedJSObject;
InBlock.gif    client.checkForMail();
InBlock.gif    client 
= null;
ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif}

None.gif

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

 Reference:

1 https://addons.mozilla.org/en-US/thunderbird/addon/3788

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值