南通最专业的开锁公司 南通最好的开锁公司 君威遥控器电池 君威专用汽车钥匙电池雷诺科雷傲汽车钥匙遥控器电池更换方法 南通开锁公司电话_南通修锁 换锁芯 开汽车锁 崇川区 港闸区 如皋 如东开锁 南通开锁公司电话 南通金钥匙开锁服务部-专业开修锁 换锁芯(正规公安工商备案) 南通开锁公司电话 南通开锁公司修锁 装锁电话|崇川区开锁|港闸区开锁 led吸顶灯光源 吸顶灯32w 吸顶灯 灯管 吸顶灯 节能灯泡 led吸顶灯灯泡 三头吸顶灯 led吸顶灯灯管 欧普圆形吸顶灯 55w吸顶灯 吸顶灯光源 旅游壶 真空保温 汉方养生壶 美的电热水壶15s08a2 半球牌电饭煲 美的 mg123-d hd2 飞科 三洋洗衣机全自动 新安怡 学饮杯 黑苦荞黄酮软胶囊 苦荞糖安胶囊 五彩凉山苦荞茶 苦荞茶250g 全胚芽黑苦荞茶500 内蒙特产苦荞茶 富贵康苦荞茶 苦荞面包邮 正中苦荞茶包邮 正中苦荞茶 包邮 苦荞香茶西部 苦荞香茶 西部 轩庆苦荞玫瑰鲜花饼 苦荞真空 苦荞500克 苦荞茶120 苦荞 真空 0.02安全套包邮0.2大力马鱼线0.4mm0.4鱼线0.5l氮纤维高压瓶007sh充电器007电子请柬00高达xn-raiser01102400018401背囊0-25微米千分尺03g101系列全套图集0-3个月婴儿连体哈衣05-08本田奥德赛05ffc23套机05版河南动感地带0603贴片电阻包06年款英派斯06斯巴鲁森林人cd机07军衔07军用水壶07款福克斯两厢07数码迷彩07速腾大灯总成 泰好大门锁 泰好豪华大门锁 cei超b级锁芯 玻璃橱门锁 指纹锁耶鲁4109 耶鲁凯特曼指纹锁 防盗挂锁包邮 包挂锁 包梁挂锁 新君威门锁 新君威门锁扣 指纹锁木门 太空铝黑色门锁 泰好浴室铜门锁 浴室门锁不锈钢 1178冷库门锁 南通配新桑塔纳2000 3000 时代超人 志俊汽车钥匙_配帕桑塔纳钥匙遥控器_帕萨特钥匙全丢重配崇川区 南通配宝来汽车钥匙_配宝来遥控器_宝来钥匙全丢如皋 南通配帕萨特汽车钥匙_配帕萨特遥控器_帕萨特钥匙全丢通州 南通配凯越汽车钥匙_配凯越遥控器_凯越钥匙全丢如东

  • 格子衬衣女 长袖 秋
  • t男恤 短袖 韩版包邮
  • 男士职业衬衫
  • 衬衫 女 长袖点点
  • 衬衫 素色 男 宽松 休闲
  • 职场气质衬衫女
  • t恤男短袖加大码不掉色
  • 帅T 小西装
  • 裤子棕色长裤男
  • 衬衣 女 灯芯绒
  • 衬衣 女商务
  • 女士职业衬衫短袖
  • 休闲裤 男 修身
  • jamesearl纯棉休闲长裤
  • 占姆士女式长袖衬衫
  • 男黑色商务休闲棉长裤
  • 女士长袖衬衫
  • 男士绿色休闲长裤
  • 全棉短袖衬衫 男 薄
  • 女士长袖纯棉衬衫
  • 秋裤男直筒
  • 马自达3汽车挂件吊坠饰品 高档琉璃汽车挂饰品蒙迪欧致胜汽车挂件吊坠饰品 高档琉璃汽车挂饰品 启辰D50汽车挂件吊坠饰品 高档琉璃汽车挂饰品起亚K2汽车挂件吊坠饰品 高档琉璃汽车挂饰品索纳塔汽车挂件吊坠饰品 高档琉璃汽车挂饰品天籁汽车挂件吊坠饰品 高档琉璃汽车挂饰品 天语SX4汽车挂件吊坠饰品 高档琉璃汽车挂饰品英朗GT汽车挂件吊坠饰品 高档琉璃汽车挂饰品英伦SC7汽车挂件吊坠饰品 高档琉璃汽车挂饰品自由舰汽车挂件吊坠饰品 高档琉璃汽车挂饰品雨燕汽车挂件吊坠饰品 高档琉璃汽车挂饰品雪佛兰迈锐宝汽车挂件吊坠饰品 高档琉璃汽车挂饰品

    秋冬季现代瑞纳 宝马5系 帕萨特 日产骐达专用汽车毛绒方向盘套 江苏省盐城市地图2014 最新版 江苏省卫星地图2014 最新版 百度地图-谷歌地图-中国地图-北京地图-搜狗地图google地图 广本凌派汽车座垫 凌派秋冬季汽车坐垫 四季通用 长安福特福克斯油耗详解 宝来汽车遥控钥匙电池 更换方法 图解宝来遥控钥匙的电池换法 宝骏630车钥匙遥控器电池更换步骤 更换方法 迈腾车钥匙电池 迈腾更换遥控器电池详解逍客汽车钥匙电池 遥控器逍客智能钥匙的电池更换步骤 新天籁智能遥控钥匙电池 新天籁智能钥匙如何更换纽扣电池 golf6 汽车钥匙遥控器换电池 图解高尔夫6遥控钥匙的电池换法 长安福特福克斯加速时间详解[图] 福克斯汽车钥匙电池 福克斯钥匙电池更换作业 Q5车钥匙更换电池方法 图解新凯越车钥匙更换电池
  • 汽车坐垫 可爱
  • wrc汽车坐垫
  • 汽车坐垫套四季通用
  • 明锐冰丝 汽车坐套
  • 骐达汽车脚垫全包围
  • 三菱蓝瑟汽车坐垫
  • 捷达汽车脚垫 3d
  • 奥迪S7坐垫
  • 别克荣御坐垫
  • Jeep指南者坐垫
  • 力帆320坐垫
  • 路虎卫士坐垫
  • 双龙爱腾坐垫
  • 斯柯达Yeti坐垫
  • 现代途胜坐垫
  • 一汽威乐坐垫
  • 哈飞路宝汽车坐垫
  • 吉利远景汽车坐垫
  • 吉利全球鹰汽车坐垫
  • 房门锁执手锁 防盗挂锁
  • 运动短裤
  • 短裤 男
  • 半裙
  • 手工拖鞋鞋底
  • iphone4皮套 手机套
  • 机油滤清器扳手工具
  • 桑塔纳3000机油
  • 家居摆件现代
  • 玻璃摆件
  • 瓷器摆件
  • 马到成功摆件
  • 高档瓷器摆件
  • 宝马山地自行车包邮
  • 根雕工艺品摆件
  • 家居摆件吊脚娃娃
  • 泰国佛像摆件工艺品
  • 美利达儿童自行车
  • 天鹅摆件结婚
  • 龙龟摆件纯铜
  • 金摆件聚宝盆
  • 亲子自行车 女式
  • 客厅卧室装饰摆件
  • 贵宾狗摆件
  • 木摆件
  • 羚羊头摆件
  • 招财兔摆件
  • 自行车前包
  • 牛仔短裤
  • 短裤 男 休闲
  • 高腰短裤
  • 夏季短裤
  • 蕾丝短裤
  • 连体短裤
  • 裙裤
  • 裤裙
  • 拖鞋 海外
  • 拖鞋 浴室
  • 拖鞋 防滑
  • 拖 鞋 男
  • 拖鞋 女
  • 浴室拖鞋
  • 家居拖鞋
  • 凉拖鞋
  • 夏天拖鞋
  • 香菇丸
  • 韩国饮料
  • 打草绳小松打草绳二手挖掘机小松 小松割灌机配件 钢木书桌办公桌钢木桌玻璃杯子水壶钢化玻璃水壶钢木办公桌钢木电脑桌进口玻璃水壶玻璃水成份防冻玻璃水包邮防冻玻璃水车用 环保果皮箱环保垃圾袋环保垃圾桶小松120-6常州小松割草机小松打草头小松割灌机小松割灌机配件 咖啡厅员工制服_咖啡厅服务员制服_西餐咖啡服务员制服_咖啡馆制服_咖啡店西餐
  • 5号充电电池8节套装
  • 5-7号电池充电器
  • 充电电池5号正品
  • 可充电电池5号
  • 7号充电电池批发
  • 7号电池包邮
  • 1.5v充电电池5号
  • 5号充电器电池免邮
  • 双鹿电池5号正品
  • 双鹿9v电池
  • 双鹿电池批发
  • 双鹿7号电池
  • 双鹿1号电池
  • 双鹿碱性电池
  • 三星s4电池原装正品
  • 三星s4原装电池后盖
  • n7100原装电池
  • 小米2s原装电池
  • galaxy s4大容量电池
  • galaxy s4原装电池
  • galaxy s3电池
  • galaxy nexus电池
  • 三星galaxy s4电池后盖
  • 品胜小米2s电池
  • 品胜手机电池
  • 小米2a电池品胜
  • 背夹电池iphone4s
  • note2背夹电池
  • 三星s4背夹电池皮套
  • 自行车fixedgear 自行车眼镜 近视 婴儿推车小自行车 自行车骑行眼镜风镜 钻石自行车 山地自行车立管 led自行车气嘴专用灯 三鼎自行车坐椅儿童 自行车线管 金镶玉玉器挂件 自行车脚踏双轴承 铬钼钢架自行车 邦德自行车女式 山地公路自行车 自行车骑行手电筒 自行车灯架 灯夹包邮 自行车水壶支架 自行车 组装 mosso 自行车载mp3 26自行车 男生单车 自行车 折叠 复古 giant山地车 自行车 炫彩自行车 七彩马亲子自行车 ucc山地车 自行车 贝嘉琦 女孩自行车 zxc自行车配件码表 自行车前置安全椅 女童自行车 小龙哈比 自行车碳纤维垫圈 自行车骑行用品 装饰木板板材 美式壁挂装饰 家具装饰配件 衣服装饰亮片 婚礼礼品实用 公司开业庆典礼品 婴儿衣柜 儿童欧式公主床 床围栏护栏嵌入式 床靠背套 派克圆珠笔芯 卧室台灯床头灯现代 个性笔袋 派克im签字笔 彩色铅笔包邮 派克宝珠笔芯 派克笔芯正品 百乐摩可擦笔笔芯 斑马油笔 微型钢笔钓鱼竿 电子教鞭遥控笔 镭射灯激光笔 彩色水笔芯 黑色水笔笔芯 公爵宝珠笔芯 touch3代马克笔 韩国文具笔包邮 水溶性彩色铅笔 马可 sgp触控笔 礼品创意笔筒 铂金 项链 pt950 男 老竹笔筒 竹雕笔筒 六福珠宝铂金手镯 8.9寸笔记本电脑 三菱签字笔1.0 华硕游戏笔记本 插座面板

    Mina状态机State Machine

    首先,关于状态机的一个极度确切的描述是它是一个有向图形,由一组节点和一组相应的转移函数组成。状态机通过响应一系列事件而“运行”。每个事件都在属于 “当前” 节点的转移函数的控制范围内,其中函数的范围是节点的一个子集。函数返回“下一个”(也许是同一个)节点。这些节点中至少有一个必须是终态。当到达终态, 状态机停止。

    接下来的问题是,我们为什么要用状态机,什么时候用:

    传统应用程序的控制流程基本是顺序的:遵循事先设定的逻辑,从头到尾地执行。很少有事件能改变标准执行流程;而且这些事件主要涉及异常情况。“命令行实用程序”是这种传统应用程序的典型例子。

    另一类应用程序由外部发生的事件来驱动——换言之,事件在应用程序之外生成,无法由应用程序或程序员来控制。具体需要执行的代码取决于接收到的事件,或者 它 相对于其他事件的抵达时间。所以,控制流程既不能是顺序的,也不能是事先设定好的,因为它要依赖于外部事件。事件驱动的GUI应用程序是这种应用程序的典 型例子,它们由命令和选择(也就是用户造成的事件)来驱动。

    下面我们看个mina-statemachine的简单例子。 

    The picture below shows a state machine for a typical tape deck. The ellipsis is the states while the arrows are the transitions. Each transition is labeled with an event name which triggers that transition.

    状态机可归纳为4个要素,即现态、条件、动作、次态。

    下面用代码来实现这一过程,首先我们定义动作接口:

    01package com.a2.desktop.example9.mina.statemachine;
    02 
    03/**
    04 * 动作接口
    05 *
    06 * @author ChenHui
    07 *
    08 */
    09public interface TapeDeck {
    10     
    11    void load(String nameOfTape);
    12 
    13    void eject();
    14 
    15    void play();
    16 
    17    void pause();
    18 
    19    void stop();
    20}

    Tape Deck就是老式的录音机的意思。而动作接口,就相当于录音机外面的几个按钮。接下来我们定义状态:

    01package com.a2.desktop.example9.mina.statemachine;
    02 
    03import org.apache.mina.statemachine.annotation.State;
    04import org.apache.mina.statemachine.annotation.Transition;
    05import org.apache.mina.statemachine.annotation.Transitions;
    06 
    07/**
    08 * 定义状态
    09 *
    10 * @author ChenHui
    11 *
    12 */
    13public class TapeDeckHandler {
    14    @State
    15    public static final String EMPTY = "Empty";
    16    @State
    17    public static final String LOADED = "Loaded";
    18    @State
    19    public static final String PLAYING = "Playing";
    20    @State
    21    public static final String PAUSED = "Paused";
    22 
    23    @Transition(on = "load", in = EMPTY, next = LOADED)
    24    public void loadTape(String nameOfTape) {
    25        System.out.println("Tape '" + nameOfTape + "' loaded");
    26    }
    27 
    28    @Transitions({
    29        @Transition(on = "play", in = LOADED, next = PLAYING),
    30        @Transition(on = "play", in = PAUSED, next = PLAYING)
    31    })
    32    public void playTape() {
    33        System.out.println("Playing tape");
    34    }
    35 
    36    @Transition(on = "pause", in = PLAYING, next = PAUSED)
    37    public void pauseTape() {
    38        System.out.println("Tape paused");
    39    }
    40 
    41    @Transition(on = "stop", in = PLAYING, next = LOADED)
    42    public void stopTape() {
    43        System.out.println("Tape stopped");
    44    }
    45 
    46    @Transition(on = "eject", in = LOADED, next = EMPTY)
    47    public void ejectTape() {
    48        System.out.println("Tape ejected");
    49    }
    50}

    状态即现态。在Transition Annotation中的on表示动作的ID,对应着动作接口中的方法名,in表示的是动作的起始状态,next表示的是动作的后续状态。

    这里要注意以下几点:

    1More about the @Transition parameters
    2•   If you omit the on parameter it will default to "*" which will match any event.
    3•   If you omit the next parameter it will default to "_self_" which is an alias for the current state. To create a loop transition in your state machine all you have to do is to omit the next parameter.
    4•   The weight parameter can be used to define in what order transitions will be searched. Transitions for a particular state will be ordered in ascending order according to their weight value. weight is 0 by default.

    最后我们看一下测试类:

    01package com.a2.desktop.example9.mina.statemachine;
    02 
    03import org.apache.mina.statemachine.StateMachine;
    04import org.apache.mina.statemachine.StateMachineFactory;
    05import org.apache.mina.statemachine.StateMachineProxyBuilder;
    06import org.apache.mina.statemachine.annotation.Transition;
    07 
    08public class Test {
    09     
    10        public static void main(String[] args) {
    11             
    12            TapeDeckHandler handler = new TapeDeckHandler();
    13            StateMachine sm = StateMachineFactory.getInstance(Transition.class).create(TapeDeckHandler.EMPTY, handler);
    14            TapeDeck deck = new StateMachineProxyBuilder().create(TapeDeck.class, sm);
    15             
    16            deck.load("Kiss Goodbye-Lee Hom");
    17            deck.play();
    18            deck.pause();
    19            deck.play();
    20            deck.stop();
    21            deck.eject();
    22        }
    23    }

    运行结果正确,按着我们放置录音的想法来的。如果,我们调换其中的一个顺序,当然不符合我的逻辑:

    1deck.load("Kiss Goodbye-Lee Hom");
    2            deck.play();
    3            deck.pause();
    4            deck.play();
    5            deck.stop();
    6            deck.pause();/**stop-->pause org.apache.mina.statemachine.event.UnhandledEventException*/
    7            deck.eject();

    这样程序就报错了,所以mina的状态机帮我们很方便的调控了事件发生的状态。

    我们来看一下mina状态机实现的一些细节:

    1.    Lookup a StateContext Object

           The StateContext object is important because it holds the current State. When a method is called on the proxy it will ask aStateContextLookup instance to get the StateContext from the method's arguments. Normally, the StateContextLookup implementation will loop through the method arguments and look for a particular type of object and use it to retrieve a StateContext object. If noStateContext has been assigned yet the StateContextLookup will create one and store it in the object.

    2.    Convert the method invocation into an Event object

    All method invocations on the proxy object will be translated into Event objects by the proxy. An Event has an id and zero or more arguments. The id corresponds to the name of the method and the event arguments correspond to the method arguments. The method call deck.load("The Knife - Silent Shout") corresponds to the event {id = "load", arguments = ["The Knife - Silent Shout"]}. The Event object also contains a reference to the StateContext object looked up previously.

    3.    Invoke the StateMachine

    Once the Event object has been created the proxy will call StateMachine.handle(Event). StateMachine.handle(Event) loops through the Transition objects of the current State in search for a Transition instance which accepts the current Event. This process will stop after a Transition has been found. The Transition objects will be searched in order of weight (typically specified by the@Transition  annotation).

    4.    Execute the Transition

    The final step is to call Transition.execute(Event) on the Transition which matched the Event. After the Transition has been executed the StateMachine will update the current State with the end state defined by the Transition.

    --------------------------------------------------------------------

    上面只是用mina实现了一个最简单的状态机,通过这个例子我们可以大概的了解到了状态机的执行过程,当然基于Annotation这样的方式我们用最基本的代码也能实现出来。接下来我们要把这样的方式用在通信中。用通信的方式来模拟录放机的按钮。

    我们先看状态的定义:

    001package com.a2.desktop.example10.mina.statemachine;
    002 
    003import static org.apache.mina.statemachine.event.IoHandlerEvents.EXCEPTION_CAUGHT;
    004import static org.apache.mina.statemachine.event.IoHandlerEvents.MESSAGE_RECEIVED;
    005import static org.apache.mina.statemachine.event.IoHandlerEvents.SESSION_OPENED;
    006 
    007import org.apache.mina.core.future.IoFutureListener;
    008import org.apache.mina.core.session.IoSession;
    009 
    010import org.apache.mina.statemachine.StateControl;
    011import org.apache.mina.statemachine.annotation.IoHandlerTransition;
    012import org.apache.mina.statemachine.annotation.IoHandlerTransitions;
    013import org.apache.mina.statemachine.annotation.State;
    014import org.apache.mina.statemachine.context.AbstractStateContext;
    015import org.apache.mina.statemachine.context.StateContext;
    016import org.apache.mina.statemachine.event.Event;
    017 
    018public class TapeDeckServer {
    019 
    020    @State
    021    public static final String ROOT = "Root";
    022    @State(ROOT)
    023    // 表示继承关系
    024    public static final String EMPTY = "Empty";
    025    @State(ROOT)
    026    public static final String LOADED = "Loaded";
    027    @State(ROOT)
    028    public static final String PLAYING = "Playing";
    029    @State(ROOT)
    030    public static final String PAUSED = "Paused";
    031 
    032    private final String[] tapes = { "盖世英雄-王力宏", "唯一-王力宏" };
    033 
    034    static class TapeDeckContext extends AbstractStateContext {
    035        public String tapeName;
    036    }
    037     
    038     @IoHandlerTransition(on = SESSION_OPENED, in = EMPTY)
    039        public void connect(IoSession session) {
    040            session.write("+ Greetings from your tape deck!");
    041        }
    042         
    043        @IoHandlerTransition(on = MESSAGE_RECEIVED, in = EMPTY, next = LOADED)
    044        public void loadTape(TapeDeckContext context, IoSession session, LoadCommand cmd) {
    045      
    046            if (cmd.getTapeNumber() < 1 || cmd.getTapeNumber() > tapes.length) {
    047                session.write("- Unknown tape number: " + cmd.getTapeNumber());
    048                StateControl.breakAndGotoNext(EMPTY);
    049            } else {
    050                context.tapeName = tapes[cmd.getTapeNumber() - 1];
    051                session.write("+ \"" + context.tapeName + "\" loaded");
    052            }
    053        }
    054 
    055        @IoHandlerTransitions({
    056            @IoHandlerTransition(on = MESSAGE_RECEIVED, in = LOADED, next = PLAYING),
    057            @IoHandlerTransition(on = MESSAGE_RECEIVED, in = PAUSED, next = PLAYING)
    058        })
    059        public void playTape(TapeDeckContext context, IoSession session, PlayCommand cmd) {
    060            session.write("+ Playing \"" + context.tapeName + "\"");
    061        }
    062         
    063        @IoHandlerTransition(on = MESSAGE_RECEIVED, in = PLAYING, next = PAUSED)
    064        public void pauseTape(TapeDeckContext context, IoSession session, PauseCommand cmd) {
    065            session.write("+ \"" + context.tapeName + "\" paused");
    066        }
    067         
    068        @IoHandlerTransition(on = MESSAGE_RECEIVED, in = PLAYING, next = LOADED)
    069        public void stopTape(TapeDeckContext context, IoSession session, StopCommand cmd) {
    070            session.write("+ \"" + context.tapeName + "\" stopped");
    071        }
    072         
    073        @IoHandlerTransition(on = MESSAGE_RECEIVED, in = LOADED, next = EMPTY)
    074        public void ejectTape(TapeDeckContext context, IoSession session, EjectCommand cmd) {
    075            session.write("+ \"" + context.tapeName + "\" ejected");
    076            context.tapeName = null;
    077        }
    078         
    079        @IoHandlerTransition(on = MESSAGE_RECEIVED, in = ROOT)
    080        public void listTapes(IoSession session, ListCommand cmd) {
    081            StringBuilder response = new StringBuilder("+ (");
    082            for (int i = 0; i < tapes.length; i++) {
    083                response.append(i + 1).append(": ");
    084                response.append('"').append(tapes[i]).append('"');
    085                if (i < tapes.length - 1) {
    086                    response.append(", ");
    087                }
    088            }
    089            response.append(')');
    090            session.write(response);
    091        }
    092         
    093        @IoHandlerTransition(on = MESSAGE_RECEIVED, in = ROOT)
    094        public void info(TapeDeckContext context, IoSession session, InfoCommand cmd) {
    095            String state = context.getCurrentState().getId().toLowerCase();
    096            if (context.tapeName == null) {
    097                session.write("+ Tape deck is " + state + "");
    098            } else {
    099                session.write("+ Tape deck is " + state
    100                        + ". Current tape: \"" + context.tapeName + "\"");
    101            }
    102        }
    103         
    104        @IoHandlerTransition(on = MESSAGE_RECEIVED, in = ROOT)
    105        public void quit(TapeDeckContext context, IoSession session, QuitCommand cmd) {
    106            session.write("+ Bye! Please come back!").addListener(IoFutureListener.CLOSE);
    107        }
    108         
    109        @IoHandlerTransition(on = MESSAGE_RECEIVED, in = ROOT, weight = 10)
    110        public void error(Event event, StateContext context, IoSession session, Command cmd) {
    111            session.write("- Cannot " + cmd.getName()
    112                    + " while " + context.getCurrentState().getId().toLowerCase());
    113        }
    114         
    115        @IoHandlerTransition(on = EXCEPTION_CAUGHT, in = ROOT)
    116        public void commandSyntaxError(IoSession session, CommandSyntaxException e) {
    117            session.write("- " + e.getMessage());
    118        }
    119         
    120        @IoHandlerTransition(on = EXCEPTION_CAUGHT, in = ROOT, weight = 10)
    121        public void exceptionCaught(IoSession session, Exception e) {
    122            e.printStackTrace();
    123            session.close(true);
    124        }
    125         
    126        @IoHandlerTransition(in = ROOT, weight = 100)
    127        public void unhandledEvent() {
    128        }
    129         
    130}

    命令的抽象类:

    1package com.a2.desktop.example10.mina.statemachine;
    2 
    3public abstract class Command {
    4     public abstract String getName();
    5}

    以下是各类命令,实现形式相似:

    01package com.a2.desktop.example10.mina.statemachine;
    02 
    03public class LoadCommand extends Command {
    04     
    05    public static final String NAME = "load";
    06 
    07    private final int tapeNumber;
    08 
    09    public LoadCommand(int tapeNumber) {
    10        this.tapeNumber = tapeNumber;
    11    }
    12 
    13    public int getTapeNumber() {
    14        return tapeNumber;
    15    }
    16 
    17    @Override
    18    public String getName() {
    19        return NAME;
    20    }
    21 
    22}
    23 
    24package com.a2.desktop.example10.mina.statemachine;
    25 
    26public class PlayCommand extends Command {
    27 
    28    public static final String NAME = "play";
    29 
    30    @Override
    31    public String getName() {
    32        return NAME;
    33    }
    34}
    35//以下省略各种命令…

    下面是解码器,继承了文本的解码方式:

    01package com.a2.desktop.example10.mina.statemachine;
    02 
    03import java.nio.charset.Charset;
    04import java.util.LinkedList;
    05 
    06import org.apache.mina.core.buffer.IoBuffer;
    07import org.apache.mina.core.filterchain.IoFilter.NextFilter;
    08import org.apache.mina.core.session.IoSession;
    09import org.apache.mina.filter.codec.ProtocolDecoderOutput;
    10import org.apache.mina.filter.codec.textline.LineDelimiter;
    11import org.apache.mina.filter.codec.textline.TextLineDecoder;
    12 
    13public class CommandDecoder extends TextLineDecoder {
    14 
    15     public CommandDecoder() {
    16            super(Charset.forName("UTF8"), LineDelimiter.WINDOWS);
    17        }
    18         
    19        private Object parseCommand(String line) throws CommandSyntaxException {
    20            String[] temp = line.split(" +", 2);
    21            String cmd = temp[0].toLowerCase();
    22            String arg = temp.length > 1 ? temp[1] : null;
    23             
    24            if (LoadCommand.NAME.equals(cmd)) {
    25                if (arg == null) {
    26                    throw new CommandSyntaxException("No tape number specified");
    27                }
    28                try {
    29                    return new LoadCommand(Integer.parseInt(arg));
    30                } catch (NumberFormatException nfe) {
    31                    throw new CommandSyntaxException("Illegal tape number: " + arg);
    32                }
    33            } else if (PlayCommand.NAME.equals(cmd)) {
    34                return new PlayCommand();
    35            } else if (PauseCommand.NAME.equals(cmd)) {
    36                return new PauseCommand();
    37            } else if (StopCommand.NAME.equals(cmd)) {
    38                return new StopCommand();
    39            } else if (ListCommand.NAME.equals(cmd)) {
    40                return new ListCommand();
    41            } else if (EjectCommand.NAME.equals(cmd)) {
    42                return new EjectCommand();
    43            } else if (QuitCommand.NAME.equals(cmd)) {
    44                return new QuitCommand();
    45            } else if (InfoCommand.NAME.equals(cmd)) {
    46                return new InfoCommand();
    47            }
    48             
    49            throw new CommandSyntaxException("Unknown command: " + cmd);
    50        }
    51 
    52        @Override
    53        public void decode(IoSession session, IoBuffer in, final ProtocolDecoderOutput out)
    54                throws Exception {
    55             
    56            final LinkedList<String> lines = new LinkedList<String>();
    57            super.decode(session, in, new ProtocolDecoderOutput() {
    58                public void write(Object message) {
    59                    lines.add((String) message);
    60                }
    61                public void flush(NextFilter nextFilter, IoSession session) {}
    62            });
    63             
    64            for (String s: lines) {
    65                out.write(parseCommand(s));
    66            }
    67        }
    68}

    处理异常类:

    01package com.a2.desktop.example10.mina.statemachine;
    02 
    03import org.apache.mina.filter.codec.ProtocolDecoderException;
    04 
    05/**
    06 * Exception thrown by CommandDecoder when a line cannot be decoded as a Command
    07 * object.
    08 *
    09 */
    10public class CommandSyntaxException extends ProtocolDecoderException {
    11    private final String message;
    12 
    13    public CommandSyntaxException(String message) {
    14        super(message);
    15        this.message = message;
    16    }
    17 
    18    @Override
    19    public String getMessage() {
    20        return message;
    21    }
    22}
    测试类:
    01package com.a2.desktop.example10.mina.statemachine;
    02 
    03import java.net.InetSocketAddress;
    04 
    05import org.apache.mina.core.service.IoHandler;
    06import org.apache.mina.filter.codec.ProtocolCodecFilter;
    07import org.apache.mina.filter.codec.textline.TextLineEncoder;
    08import org.apache.mina.filter.logging.LoggingFilter;
    09import org.apache.mina.statemachine.StateMachine;
    10import org.apache.mina.statemachine.StateMachineFactory;
    11import org.apache.mina.statemachine.StateMachineProxyBuilder;
    12import org.apache.mina.statemachine.annotation.IoHandlerTransition;
    13import org.apache.mina.statemachine.context.IoSessionStateContextLookup;
    14import org.apache.mina.statemachine.context.StateContext;
    15import org.apache.mina.statemachine.context.StateContextFactory;
    16import org.apache.mina.transport.socket.SocketAcceptor;
    17import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
    18 
    19public class TestMain {
    20     private static final int PORT = 8082;
    21         
    22        private static IoHandler createIoHandler() {
    23            StateMachine sm = StateMachineFactory.getInstance(
    24                    IoHandlerTransition.class).create(TapeDeckServer.EMPTY,
    25                    new TapeDeckServer());
    26 
    27            return new StateMachineProxyBuilder().setStateContextLookup(
    28                    new IoSessionStateContextLookup(new StateContextFactory() {
    29                        public StateContext create() {
    30                            return new TapeDeckServer.TapeDeckContext();
    31                        }
    32                    })).create(IoHandler.class, sm);
    33        }
    34         
    35        public static void main(String[] args) throws Exception {
    36            SocketAcceptor acceptor = new NioSocketAcceptor();
    37            acceptor.setReuseAddress(true);
    38            ProtocolCodecFilter pcf = new ProtocolCodecFilter(
    39                    new TextLineEncoder(), new CommandDecoder());
    40            acceptor.getFilterChain().addLast("log1", new LoggingFilter("log1"));
    41            acceptor.getFilterChain().addLast("codec", pcf);
    42            acceptor.getFilterChain().addLast("log2", new LoggingFilter("log2"));
    43            acceptor.setHandler(createIoHandler());
    44            acceptor.bind(new InetSocketAddress(PORT));
    45        }
    46}

    启动测试类,用telnet去连,然后输入各种命令,效果如下:

    更详细的代码可以参阅: org.apache.mina.example.tapedeck

    代码其实都不难,只是我们需要灵活的将状态机这样的模式运用到自己的项目中去,通过状态之间的有规则的切换来控制更复杂的业务逻辑。
    posted @ 2012-11-10 13:41  ChaunceyHao  阅读(453)  评论(0编辑  收藏  举报
  • 汽车羊毛坐垫长毛
  • 车垫四季通用
  • 御车宝
  • 凯美瑞坐垫四季通用
  • 手编冰丝坐垫
  • 珍珠戒指新款
  • 汽车皮坐垫四季垫
  • 汽车四季坐垫新款
  • 汽车皮坐垫四季通用
  • 汽车羊毛坐垫短毛
  • 包女
  • 汽车亚麻坐垫包邮
  • 汽车布坐垫
  • 汽车布坐垫套
  • 紫风铃亚麻四季坐垫
  • 蒙奇奇汽车
  • 蒙奇奇汽车毛绒坐垫
  • 蒙奇奇坐垫三件套
  • 尼罗河车坐垫
  • 尼罗河手编坐垫
  • 尼罗河四季垫
  • 羊剪绒坐垫
  • 牧宝冬季坐垫
  • 牧宝四季垫正品
  • 牧宝羽绒坐垫
  • 牧宝汽车垫羊毛
  • 高压洗车机
  • 高压洗车器
  • 高压洗车水泵
  • 高压洗车泵
  • 熊猫洗车
  • 洗车器高压220v
  • 洗车器便携高压
  • 高压水枪洗车机
  • 220v高压水枪洗车机
  • 家用洗车水枪高压枪
  • 汽车洗车水枪
  • 虎贝尔洗车水枪
  • 水枪头洗车
  • 洗车水枪水管
  • 洗车水枪套装
  • 洗车水枪接头
  • 佳百丽洗车水枪
  • 洗车工具套装
  • 洗车用品工具
  • 家用洗车工具
  • 汽车洗车工具
  • 洗车设备工具
  • 洗车刷子
  • 现代途胜保险杠
  • 现代新胜达保险杠
  • 保险杠车贴纸
  • 后保险杠车贴纸
  • 前保险杠车贴
  • 雨燕保险杠
  • 后保险杠贴纸
  • 前保险杠贴纸
  • 汽车保险贴纸
  • rav4后保险杠
  • 专业运动文胸htc c715e evo design 4g
  • htc evo 4g a9292
  • htc x515m g17 evo 3d
  • htc one x s720e
  • htc x920e
  • 名牌手表男正品
  • povos 奔腾 pffn3003苏泊尔电热水壶正品
  • tcl tcl电热水壶ta-kb181a
  • 乔邦181j
  • 乔邦不锈钢电热水壶
  • supor 苏泊尔 swf15j3-150
  • 逸动空气滤芯
  • 空气压缩机喷漆
  • 德国车载空气净化器
  • sr927w手表电池
  • cartier手表蓝气球
  • 机械手表男士名表
  • anne klein手表
  • 手表柜台展示柜
  • 女士皮带 头层牛皮
  • 女士皮带 头层纯牛皮
  • jeep皮带 纯牛皮
  • 女皮带 真皮 牛皮
  • 女士皮带 真皮
  • 头层牛皮皮带
  • 女士皮带 时尚
  • 真皮皮带头层牛皮女
  • 女士皮带 包邮
  • 皮带条 头层牛皮
  • 女士皮带 时尚 宽
  • 头层牛皮纯铜扣皮带
  • 女士皮带 纯牛皮
  • 女士皮带头层纯牛皮
  • 女士皮带头层牛皮
  • jeep皮带纯牛皮
  • 女皮带真皮牛皮
  • 女士皮带真皮
  • 女士牛皮皮带包邮
  • 女士皮带时尚
  • 三星微单镜头
  • 三星单反镜头
  • 三星50-200镜头
  • 三星7100镜头保护膜
  • 三星i9210
  • 三星i9210手机套面包蛋糕酸奶机
  • 鸡蛋牛奶面包干饼干
  • 面包片饼干
  • 摩托车机油