首先我们需要建立一个类来声明一些管道常量
- package com.hpcc.police.common
- {
- public class PipeAwareModuleConstants
- {
- // const
- public static const SHELL_TO_MODULE_PIPE:String = 'shellToModulePipe';
- public static const STDOUT:String = 'standardOutput';
- public static const STDIN:String = 'standardInput';
- public static const MODULE_TO_SHELL_PIPE:String = 'moduleToShellPipe';
- public static const MODULE_TO_SHELL_MESSAGE: String = "moduleToShellMessage";
- public static const SHELL_TO_MODULE_MESSAGE:String = 'shellToModuleMessage';
- public static const MODULE_TO_MODULE_PIPE:String = 'moduleToModulePipe';
- public function PipeAwareModuleConstants()
- {
- }
- }
- }
然后再shell的view层写一个Mediator文件 这个文件继承自JunctionMediator 我的文件名叫ShellJunctionMediator
我只把有用的代码 粘上来了 至于Mediator类的模板你随便下个就有了
直接看代码吧
这里我们需要声明一个object来保存建立的管道信息 这样你在删除管道的时候就可一根据名字来进行删除
- private var outMap:Object = {};//这个就是保存管道信息的类
- [code]override public function handleNotification(note:INotification):void
- {
- var moduleID:String = String(note.getBody())
- switch(note.getName())
- {
- case ApplicationFacade.MODULE_ADDED:
- connectModule(note.getBody() as IPipeAwareModule);//在module加载完毕时创建管道连接
- break;
- case ApplicationFacade.DISCONNECT_MOUDLE_PIPE:
- var junctionOut:TeeSplit = junction.retrievePipe(PipeAwareModuleConstants.STDOUT) as TeeSplit;
- junctionOut.disconnectFitting(outMap[moduleID])//删除时你只需要删除主应用到module的就可以了
- delete outMap[moduleID];
- break;
- default:
- super.handleNotification(note);
- break;
- }
- }
- public function connectModule(module:IPipeAwareModule):void
- {
- // Register SHELL_TO_MODULE_PIPE from the shell to all modules(注册从主应用到所有Module的管道)
- junction.registerPipe(PipeAwareModuleConstants.STDOUT,Junction.OUTPUT,new TeeSplit());
- // Register MODULE_TO_SHELL_PIPE to the shell from all modules (注册从所有Module到主应用的管道)
- junction.registerPipe(PipeAwareModuleConstants.STDIN,Junction.INPUT,new TeeMerge());
- // add a pipe listener for messages sended by module(给Module发送的message添加管道监听)
- junction.addPipeListener(PipeAwareModuleConstants.STDIN,this,handlePipeMessage);
- // module -> shell(从module到主应用)
- var moduleToShell:Pipe=new Pipe();
- module.acceptOutputPipe(PipeAwareModuleConstants.MODULE_TO_SHELL_PIPE,moduleToShell);
- var shellInFitting:TeeMerge=junction.retrievePipe(PipeAwareModuleConstants.STDIN) as TeeMerge;
- shellInFitting.connectInput(moduleToShell);
- //shell -> module(从主应用到module)
- var shellToModule:Pipe=new Pipe();
- module.acceptInputPipe(PipeAwareModuleConstants.STDOUT,shellToModule);
- var shellOutFitting:TeeSplit=junction.retrievePipe(PipeAwareModuleConstants.STDOUT) as TeeSplit;
- shellOutFitting.connect(shellToModule);
- var moduleID:String = String(module)
- outMap[moduleID] = shellToModule;
- }
- override public function handlePipeMessage(message:IPipeMessage):void
- { }
这样shell到module之间的管道已经建立了 下面我们还需要建立module到module之间的管道信息
还是直接代码吧
- override public function handleNotification(note:INotification):void
- {
- var moduleID:String = String(note.getBody())
- //var moduleFacade:IPipeAware = note.getBody() as IPipeAware
- switch(note.getName())
- {
- case ApplicationFacade.MODULE_ADDED:
- connectModule(note.getBody() as IPipeAwareModule);
- break;
- case ApplicationFacade.DISCONNECT_MODULE_TO_MODULE:
- var junctionOut:TeeSplit = junction.retrievePipe(PipeAwareModuleConstants.STDIN) as TeeSplit;
- junctionOut.disconnectFitting(outMap[moduleID])
- delete outMap[moduleID];
- break;
- default:
- super.handleNotification(note);
- break;
- }
- }
- public function connectModule(module:IPipeAwareModule):void
- {
- // Register SHELL_TO_MODULE_PIPE from the shell to all modules(注册从主应用到所有Module的管道)
- junction.registerPipe(PipeAwareModuleConstants.STDIN,Junction.OUTPUT,new TeeSplit());
- // Register MODULE_TO_MODULE_PIPE to the modules from all modules (注册从所有Module到Module的管道)
- junction.registerPipe(PipeAwareModuleConstants.MODULE_TO_MODULE_PIPE,Junction.INPUT,new TeeMerge());
- // add a pipe listener for messages sended by module(给Module发送的message添加管道监听)
- junction.addPipeListener(PipeAwareModuleConstants.MODULE_TO_MODULE_PIPE,this,handlePipeMessage);
- // module -> shell(从module到主应用)
- var moduleToShellJunction:Pipe=new Pipe();
- module.acceptOutputPipe(PipeAwareModuleConstants.MODULE_TO_MODULE_PIPE,moduleToShellJunction);
- var shellInFitting:TeeMerge=junction.retrievePipe(PipeAwareModuleConstants.MODULE_TO_MODULE_PIPE) as TeeMerge;
- shellInFitting.connectInput(moduleToShellJunction);
- //shell -> module(从主应用到module)
- var shellToModuleJunction:Pipe=new Pipe();
- module.acceptInputPipe(PipeAwareModuleConstants.STDIN,shellToModuleJunction);
- var shellOutFitting:TeeSplit=junction.retrievePipe(PipeAwareModuleConstants.STDIN) as TeeSplit;
- shellOutFitting.connect(shellToModuleJunction);
- var moduleID:String = String(module)
- outMap[moduleID] = shellToModuleJunction;
- }
- //注意下这的处理管道信息方式与shell到module的不一样
- override public function handlePipeMessage(message:IPipeMessage):void
- {
- var junctionOut:TeeSplit = junction.retrievePipe( PipeAwareModuleConstants.STDIN ) as TeeSplit;
- junctionOut.write( message );
- }
以上两个文件的是在shell的view层写的 下面该module的view层做些什么了
- override public function onRegister():void
- {
- var teeMerge:TeeMerge = new TeeMerge();
- junction.registerPipe( PipeAwareModuleConstants.STDIN, Junction.INPUT, teeMerge );
- junction.addPipeListener( PipeAwareModuleConstants.STDIN, this, handlePipeMessage );
- var teeSplit:TeeSplit = new TeeSplit();
- junction.registerPipe( PipeAwareModuleConstants.MODULE_TO_MODULE_PIPE, Junction.OUTPUT, teeSplit );
- }
- override public function onRemove():void
- {
- }
- override public function listNotificationInterests():Array
- {
- var interests:Array = super.listNotificationInterests();
- return interests;
- }
- override public function handleNotification( note:INotification ):void
- {
- var pipe:IPipeFitting;
- var type:String = note.getType() as String;
- switch( note.getName() )
- {
- case JunctionMediator.ACCEPT_INPUT_PIPE:
- {
- if (type ==PipeAwareModuleConstants.STDIN)
- {
- pipe = note.getBody() as IPipeFitting;
- var teeMerge:TeeMerge = junction.retrievePipe(PipeAwareModuleConstants.STDIN) as TeeMerge;
- teeMerge.connectInput(pipe);
- //It does not need to be handled by super.
- return;
- }
- break;
- }
- case JunctionMediator.ACCEPT_OUTPUT_PIPE:
- {
- if (type == PipeAwareModuleConstants.MODULE_TO_MODULE_PIPE)
- {
- pipe = note.getBody() as IPipeFitting;
- var teeSplit:TeeSplit = junction.retrievePipe(PipeAwareModuleConstants.MODULE_TO_MODULE_PIPE) as TeeSplit;
- teeSplit.connect( pipe );
- //It does not need to be handled by super.
- return;
- }
- break;
- }
- }
- super.handleNotification(note);//
这个地方大家一定要注意下 它是写在switch语句外面的 如果你写在里面了 shell到module的管道通信就没被注册