zoukankan      html  css  js  c++  java
  • 新人补钙系列教程之: 搭建 PureMVC pipe

    首先我们需要建立一个类来声明一些管道常量

    1. package com.hpcc.police.common 
    2. {
    3. public class PipeAwareModuleConstants
    4. {
    5.   // const 
    6.   public static const SHELL_TO_MODULE_PIPE:String = 'shellToModulePipe'; 
    7.   public static const STDOUT:String = 'standardOutput';
    8.   public static const STDIN:String = 'standardInput';  
    9.   public static const MODULE_TO_SHELL_PIPE:String = 'moduleToShellPipe';
    10.   public static const MODULE_TO_SHELL_MESSAGE: String = "moduleToShellMessage";
    11.   public static const SHELL_TO_MODULE_MESSAGE:String = 'shellToModuleMessage';
    12.   public static const MODULE_TO_MODULE_PIPE:String = 'moduleToModulePipe';
    13.   
    14.   public function PipeAwareModuleConstants()
    15.   {
    16.    
    17.   }
    18. }
    19. }
    复制代码

    然后再shell的view层写一个Mediator文件 这个文件继承自JunctionMediator 我的文件名叫ShellJunctionMediator

    我只把有用的代码 粘上来了 至于Mediator类的模板你随便下个就有了

    直接看代码吧
    这里我们需要声明一个object来保存建立的管道信息 这样你在删除管道的时候就可一根据名字来进行删除

    1. private var outMap:Object = {};//这个就是保存管道信息的类
    2. [code]override public function handleNotification(note:INotification):void
    3.   {
    4.   
    5.    var moduleID:String = String(note.getBody())
    6.    switch(note.getName()) 
    7.    {
    8.     case ApplicationFacade.MODULE_ADDED:
    9.      connectModule(note.getBody() as IPipeAwareModule);//在module加载完毕时创建管道连接
    10. break;
    11.       case ApplicationFacade.DISCONNECT_MOUDLE_PIPE:
    12.      var junctionOut:TeeSplit = junction.retrievePipe(PipeAwareModuleConstants.STDOUT) as TeeSplit;
    13.      junctionOut.disconnectFitting(outMap[moduleID])//删除时你只需要删除主应用到module的就可以了
    14.    delete outMap[moduleID];
    15.     break; 
    16.     default:
    17.      super.handleNotification(note);
    18.     break;   
    19.    }
    20.   } 
    21.   
    22.   
    23.   
    24.   public function connectModule(module:IPipeAwareModule):void
    25.   {
    26.        // Register SHELL_TO_MODULE_PIPE from the shell to all modules(注册从主应用到所有Module的管道)
    27.    junction.registerPipe(PipeAwareModuleConstants.STDOUT,Junction.OUTPUT,new TeeSplit());
    28.    
    29.    // Register MODULE_TO_SHELL_PIPE to the shell from all modules (注册从所有Module到主应用的管道)
    30.    junction.registerPipe(PipeAwareModuleConstants.STDIN,Junction.INPUT,new TeeMerge());
    31.    
    32.    // add a pipe listener for messages sended by module(给Module发送的message添加管道监听)  
    33.    junction.addPipeListener(PipeAwareModuleConstants.STDIN,this,handlePipeMessage);
    34.   
    35.    
    36.    // module -> shell(从module到主应用)
    37.    var moduleToShell:Pipe=new Pipe();
    38.    module.acceptOutputPipe(PipeAwareModuleConstants.MODULE_TO_SHELL_PIPE,moduleToShell);
    39.    var shellInFitting:TeeMerge=junction.retrievePipe(PipeAwareModuleConstants.STDIN) as TeeMerge;
    40.    shellInFitting.connectInput(moduleToShell);
    41.    
    42.    //shell -> module(从主应用到module)
    43.    var shellToModule:Pipe=new Pipe();
    44.    module.acceptInputPipe(PipeAwareModuleConstants.STDOUT,shellToModule);
    45.    var shellOutFitting:TeeSplit=junction.retrievePipe(PipeAwareModuleConstants.STDOUT) as TeeSplit;
    46.    shellOutFitting.connect(shellToModule);
    47.    var moduleID:String = String(module)
    48.    outMap[moduleID] = shellToModule;
    49.   }
    50.    
    51.   
    52.   override public function handlePipeMessage(message:IPipeMessage):void
    53.   {  }
    复制代码

    这样shell到module之间的管道已经建立了 下面我们还需要建立module到module之间的管道信息

    还是直接代码吧

    1. override public function handleNotification(note:INotification):void
    2.   {
    3.   
    4.    var moduleID:String = String(note.getBody())
    5.    //var moduleFacade:IPipeAware = note.getBody() as IPipeAware
    6.    
    7.    switch(note.getName()) 
    8.    {
    9.     case ApplicationFacade.MODULE_ADDED:
    10.      connectModule(note.getBody() as IPipeAwareModule);
    11.     
    12.     break;
    13.     case ApplicationFacade.DISCONNECT_MODULE_TO_MODULE:
    14.      var junctionOut:TeeSplit = junction.retrievePipe(PipeAwareModuleConstants.STDIN) as TeeSplit;
    15.      junctionOut.disconnectFitting(outMap[moduleID])
    16.      delete outMap[moduleID];
    17.     break;
    18.     default:
    19.      super.handleNotification(note);
    20.     break;   
    21.    }
    22.   } 
    23.    
    24.   
    25.   
    26.   public function connectModule(module:IPipeAwareModule):void
    27.   {
    28.     // Register SHELL_TO_MODULE_PIPE from the shell to all modules(注册从主应用到所有Module的管道)
    29.    junction.registerPipe(PipeAwareModuleConstants.STDIN,Junction.OUTPUT,new TeeSplit());
    30.    
    31.    // Register MODULE_TO_MODULE_PIPE to the modules from all modules (注册从所有Module到Module的管道)
    32.    junction.registerPipe(PipeAwareModuleConstants.MODULE_TO_MODULE_PIPE,Junction.INPUT,new TeeMerge());
    33.    
    34.    // add a pipe listener for messages sended by module(给Module发送的message添加管道监听)  
    35.    junction.addPipeListener(PipeAwareModuleConstants.MODULE_TO_MODULE_PIPE,this,handlePipeMessage);
    36.    
    37.     
    38.    // module -> shell(从module到主应用)
    39.    var moduleToShellJunction:Pipe=new Pipe();
    40.    module.acceptOutputPipe(PipeAwareModuleConstants.MODULE_TO_MODULE_PIPE,moduleToShellJunction);
    41.    var shellInFitting:TeeMerge=junction.retrievePipe(PipeAwareModuleConstants.MODULE_TO_MODULE_PIPE) as TeeMerge;
    42.    shellInFitting.connectInput(moduleToShellJunction);
    43.    
    44.    //shell -> module(从主应用到module)
    45.    var shellToModuleJunction:Pipe=new Pipe();
    46.    module.acceptInputPipe(PipeAwareModuleConstants.STDIN,shellToModuleJunction);
    47.    var shellOutFitting:TeeSplit=junction.retrievePipe(PipeAwareModuleConstants.STDIN) as TeeSplit;
    48.    shellOutFitting.connect(shellToModuleJunction);
    49.    var moduleID:String = String(module)
    50.    outMap[moduleID] = shellToModuleJunction;
    51.   }
    52.    
    53.   
    54. //注意下这的处理管道信息方式与shell到module的不一样
    55. override public function handlePipeMessage(message:IPipeMessage):void
    56.   {
    57.    var junctionOut:TeeSplit = junction.retrievePipe( PipeAwareModuleConstants.STDIN ) as TeeSplit;
    58.    junctionOut.write( message );
    59.   }   
    复制代码

    以上两个文件的是在shell的view层写的 下面该module的view层做些什么了

    1. override public function onRegister():void
    2.   {
    3.    var teeMerge:TeeMerge = new TeeMerge();
    4.    junction.registerPipe( PipeAwareModuleConstants.STDIN, Junction.INPUT, teeMerge );
    5.    junction.addPipeListener( PipeAwareModuleConstants.STDIN, this, handlePipeMessage );
    6.    var teeSplit:TeeSplit = new TeeSplit();   
    7.    junction.registerPipe( PipeAwareModuleConstants.MODULE_TO_MODULE_PIPE, Junction.OUTPUT, teeSplit );
    8.   } 
    9.    
    10.   override public function onRemove():void
    11.   {
    12.   } 
    13.    
    14.   override public function listNotificationInterests():Array
    15.   {
    16.     var interests:Array = super.listNotificationInterests();
    17.    return interests; 
    18.   }
    19.   override public function handleNotification( note:INotification ):void
    20.   {
    21.       var pipe:IPipeFitting;
    22.    var type:String = note.getType() as String;
    23.    switch( note.getName() )
    24.    {
    25.      case JunctionMediator.ACCEPT_INPUT_PIPE:
    26.     {
    27.      
    28.      if (type ==PipeAwareModuleConstants.STDIN)
    29.      {
    30.       pipe = note.getBody() as IPipeFitting;
    31.       var teeMerge:TeeMerge = junction.retrievePipe(PipeAwareModuleConstants.STDIN) as TeeMerge;
    32.       teeMerge.connectInput(pipe);
    33.        
    34.       
    35.       //It does not need to be handled by super.  
    36.       return;
    37.      }
    38.       
    39.      break;  
    40.     }
    41.     
    42.    
    43.     case JunctionMediator.ACCEPT_OUTPUT_PIPE:
    44.     {
    45.      
    46.      if (type == PipeAwareModuleConstants.MODULE_TO_MODULE_PIPE)
    47.      {
    48.       pipe = note.getBody() as IPipeFitting;
    49.       var teeSplit:TeeSplit = junction.retrievePipe(PipeAwareModuleConstants.MODULE_TO_MODULE_PIPE) as TeeSplit;
    50.       teeSplit.connect( pipe );
    51.       
    52.       //It does not need to be handled by super.  
    53.       return;
    54.      }
    55.      
    56.      break;
    57.     }           
    58.    }  
    59.    super.handleNotification(note);//
    复制代码

    这个地方大家一定要注意下 它是写在switch语句外面的 如果你写在里面了 shell到module的管道通信就没被注册

  • 相关阅读:
    Linux内核调试方法
    linux查看系统的日志------健康检查特性
    检测磁盘驱动的健康程度SMART
    用十条命令在一分钟内检查Linux服务器性能
    Nginx安装及配置
    getopts的使用
    grub rescue 主引导修复
    linux C中调用shell命令和运行shell脚本
    Makefile基础---编译
    OVMF基础
  • 原文地址:https://www.cnblogs.com/keng333/p/2299757.html
Copyright © 2011-2022 走看看