Java(CS)请求分流

接收信息,调用入口

    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        System.out.println(msg);
        curCtx = ctx ;
        //ExplainString(msg); 
        RequetMap.instance().map(msg , this);
    }
RequetMap:
1.单例
2.init初始化
3.map
  1 import java.io.File;
  2 import java.util.List;
  3 
  4 import org.dom4j.Document;
  5 import org.dom4j.DocumentException;
  6 import org.dom4j.Element;
  7 import org.dom4j.io.SAXReader;
  8 
  9 import net.sf.json.JSONObject;
 10 import server.MyServerHanlder;
 11 
 12 /**
 13  * @author kimi
 14  *
 15  */ 
 16 public class RequetMap {
 17 
 18      List<Element> infos ;
 19     
 20     /**
 21      * 将请求的json 解析  将rid根据 xml分流到具体的方法  
 22      * exp: RequetMap.instance().map("{id:'8000',content:{name:'AA'}}");
 23      * @param info
 24      */
 25     public  void  map( String  info, MyServerHanlder handler)
 26     {
 30         
 31         JSONObject  r = JSONObject.fromObject(info) ;
 32         
 33         String  id = r.getString("id") ;
 34         String  content = r.getString("content") ;
 38         for (Element el : infos) {
 40             if( el.attributeValue("id").equals(id))
 41             {
 45                 // 映射方法
 46                 try {
 47                     Class<?> class1 = Class.forName( el.attributeValue("class"));
 48                     
 49                     IMapDemo  demo = ( IMapDemo) class1.newInstance() ;
 50                     demo.Init(content , handler);
 51                     
 52                 } catch ( Exception e) {
 54                     e.printStackTrace();
 55                 } 
 56             }
 57         }
 58         
 59         
 60 
 61     }
 62     
 63     
 64     /**
 65      * 初始化请求映射器
 66      * @throws DocumentException 
 67      */
 68     public void Init()  
 69     {
 70         // 加载xml文件并缓存在变量里面
 71         
 72            SAXReader saxReader = new SAXReader();
 73 
 74             Document document;
 75             try {
 76                 document = saxReader.read(new File("conf/responseMapping.xml"));
 77     
 78             // 获取根元素
 79             Element root = document.getRootElement(); 81 
 82             // 获取所有子元素
 83             infos = root.elements();
 86             
 87             } catch (DocumentException e) {
 89                 e.printStackTrace();
 90             }
 91             
 92     }
 93     
 94     static RequetMap _instance ;
 95     public static  RequetMap instance()
 96     {
 97         if( _instance == null) _instance = new RequetMap() ;
 98         return  _instance ;
 99     }
100 }

IMapDemo

1 public interface IMapDemo {
2  
3     /**
4      * 
5      * @param info
6      * 传参数进来的信息  需要进行再处理
7      */
8     public  void Init( String  info, Object handler) ;
9 }

GameLogin

 1 import model.GameCenter;
 2 import server.MyServerHanlder;
 3 
 4 public class GameLogin implements IMapDemo {
 5 
 6     @Override
 7     public void Init(String info , Object handler)  {
 8         // TODO Auto-generated method stub
 9         MyServerHanlder myhandler = (MyServerHanlder) handler ; 
10         // TODO Auto-generated method stub
11         myhandler.curPlayer = GameCenter.instance().AddPlayer(info) ;
12         GameCenter.instance().RefreshState();
13     }
14 
15 }

responseMapping.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <include>
 3     <item id="8000"  desc="请求描述8000"  class="MapFunc.MapLoginLogin"/>
 4     <item id="8001"  desc="请求描述8001"  class="MapFunc.MapLoginLogout"/>
 5     
 6     <item id="0001"  desc="添加用户注册名字"       class="MapFunc.GameLogin"/>
 7     <item id="0002"  desc="用户准备"            class="MapFunc.GamePrepare"/>
 8     <item id="0003"  desc="用户报数"            class="MapFunc.GameCount"/>
 9     <item id="0004"  desc="用户下一局"           class="MapFunc.GameNext"/>
10 </include>

 

posted @ 2017-03-07 09:33  尚菜  阅读(1604)  评论(0编辑  收藏  举报