Package pushy.modules

Examples of pushy.modules.SocketModule$Protocol


public class ProtocolHelperTest {

  @Test
  public void testFromBytes() throws Exception {
    RequestProtocol rp = new RequestProtocol("lookup", "methodname", null);
    Protocol p = new Protocol(101001, (byte)1, SDPType.Request, rp);
   
    byte[] buffer = p.toBytes();
   
    Protocol p2 = (Protocol)ProtocolHelper.fromBytes(buffer);
    Assert.assertEquals(rp.getLookup(), ((RequestProtocol)p2.getSdpEntity()).getLookup());
    Assert.assertEquals(rp.getMethodName(), ((RequestProtocol)p2.getSdpEntity()).getMethodName());
  }
View Full Code Here


  }
 
  @Test
  public void testProtocol() throws Exception {
    byte[] buffer = new byte[]{1,-79,0,0,0,1,0,0,0,101,2,0,4,1,-5,107,-6,25,0,-23,3,0,0,19,0,0,0,19,0,0,0,0,-22,3,0,0,1,0,0,0,39,5,-10,-65,39,5,-10,-65,0,-21,3,0,0,18,0,0,0,0,-20,3,0,0,4,0,0,0,85,115,101,114,17,-116,74,-65,17,-116,74,-65,0,-19,3,0,0,24,0,0,0,24,0,0,0,0,-18,3,0,0,1,0,0,0,5,0,0,0,29,18,0,0,0,0,-17,3,0,0,19,0,0,0,49,107,99,54,54,52,53,56,56,64,107,117,99,104,101,46,99,111,109,11,0,0,0,116,72,-59,0,0,0,0,0,18,0,0,0,1,-20,3,0,0,18,0,0,0,0,-16,3,0,0,6,0,0,0,117,112,100,97,116,101};
    Protocol p = (Protocol)ProtocolHelper.fromBytes(buffer);
    System.out.println(((RequestProtocol)p.getSdpEntity()).getLookup());
    System.out.println(((RequestProtocol)p.getSdpEntity()).getMethodName());
   
//    Assert.assertEquals(rp.getLookup(), ((RequestProtocol)p2.getSdpEntity()).getLookup());
//    Assert.assertEquals(rp.getMethodName(), ((RequestProtocol)p2.getSdpEntity()).getMethodName());
  }
View Full Code Here

 
 
  @Test
  public void testToBytes() throws Exception {
    RequestProtocol rp = new RequestProtocol("lookup", "methodname", null);
    Protocol p = new Protocol(101001, (byte)1, SDPType.Request, rp);
   
    byte[] buffer = p.toBytes();
    StringBuilder sbBuf = new StringBuilder();
    for(byte b : buffer) {
      sbBuf.append(b);
      sbBuf.append(" ");
    }
View Full Code Here

   * 权限认证filter
   */
  @Override
  public void filter(GaeaContext context) throws Exception {
   
    Protocol protocol = context.getGaeaRequest().getProtocol();   
    if(protocol.getPlatformType() == PlatformType.Java && context.getServerType() == ServerType.TCP){//java 客户端支持权限认证
      GaeaResponse response = new GaeaResponse();
      Global global = Global.getSingleton();
      //是否启用权限认证
      if(Global.getSingleton().getGlobalSecureIsRights()){
        SecureContext sc = global.getGlobalSecureContext(context.getChannel().getNettyChannel());
        //判断当前channel是否通过认证
        if(!sc.isRights()){
          //没有通过认证
          if(protocol != null && protocol.getSdpEntity() instanceof HandclaspProtocol){
            SecureKey sk = new SecureKey();
            HandclaspProtocol handclaspProtocol = (HandclaspProtocol)protocol.getSdpEntity();
            /**
             * 接收 客户端公钥
             */
            if("1".equals(handclaspProtocol.getType())){
              sk.initRSAkey();
              //客户端发送公钥数据
              String clientPublicKey = handclaspProtocol.getData();
              if(null == clientPublicKey || "".equals(clientPublicKey)){
                logger.warn("get client publicKey warn!");
              }
              //java 客户端
              if(protocol.getPlatformType() == PlatformType.Java){
                //服务器生成公/私钥,公钥传送给客户端
                sc.setServerPublicKey(sk.getStringPublicKey());
                sc.setServerPrivateKey(sk.getStringPrivateKey());
                sc.setClientPublicKey(clientPublicKey);
                handclaspProtocol.setData(sk.getStringPublicKey());//服务器端公钥
              }
             
              protocol.setSdpEntity(handclaspProtocol);
              response.setResponseBuffer(protocol.toBytes());
              context.setGaeaResponse(response);
              this.setInvokeAndFilter(context);
              logger.info("send server publieKey sucess!");
            }
            /**
             * 接收权限文件
             */
            else if("2".equals(handclaspProtocol.getType())){
              //客户端加密授权文件
              String clientSecureInfo = handclaspProtocol.getData();
              if(null == clientSecureInfo || "".equals(clientSecureInfo)){
                logger.warn("get client secureKey warn!");
              }
              //授权文件客户端原文(服务器私钥解密)
              String sourceInfo = sk.decryptByPrivateKey(clientSecureInfo, sc.getServerPrivateKey());
              //校验授权文件是否相同
              //判断是否合法,如果合法服务器端生成DES密钥,通过客户端提供的公钥进行加密传送给客户端
              if(global.containsSecureMap(sourceInfo)){
                logger.info("secureKey is ok!");
                String desKey = StringUtils.getRandomNumAndStr(8);
                //设置当前channel属性
                sc.setDesKey(desKey);
                sc.setRights(true);
                handclaspProtocol.setData(sk.encryptByPublicKey(desKey, sc.getClientPublicKey()));
                protocol.setSdpEntity(handclaspProtocol);
                response.setResponseBuffer(protocol.toBytes());
                context.setGaeaResponse(response);
              }else{
                logger.error("It's bad secureKey!");
                this.ContextException(context, protocol, response, "授权文件错误!");
              }
              this.setInvokeAndFilter(context);
            }else{
              //权限认证 异常情况
              logger.error("权限认证异常!");
              this.ContextException(context, protocol, response, "权限认证 异常!");
            }
            response = null;
            sk = null;
            handclaspProtocol = null;
          }else{
            //客户端没有启动权限认证
            logger.error("客户端没有启用权限认证!");
            this.ContextException(context, protocol, response, "客户端没有启用权限认证!");
          }
        }
      }else{
        if(protocol != null && protocol.getSdpEntity() instanceof HandclaspProtocol){
          //异常--当前服务器没有启动权限认证
          logger.error("当前服务没有启用权限认证!");
          this.ContextException(context, protocol, response, "当前服务没有启用权限认证!");
        }
      }
View Full Code Here

 
  @Override
  public void filter(GaeaContext context) throws Exception {
    try{
      if(context.getServerType() == ServerType.TCP) {
        Protocol protocol = context.getGaeaRequest().getProtocol();
        byte[] desKeyByte = null;
        String desKeyStr = null;
        boolean bool = false;
       
        Global global = Global.getSingleton();
        if(global != null){
          //判断当前服务启用权限认证
          if(global.getGlobalSecureIsRights()){
            SecureContext securecontext = global.getGlobalSecureContext(context.getChannel().getNettyChannel());
            bool = securecontext.isRights();
            if(bool){
              desKeyStr = securecontext.getDesKey();
            }
          }
        }
       
        if(desKeyStr != null){
          desKeyByte = desKeyStr.getBytes("utf-8");
        }
       
        if(context.getGaeaResponse() == null){
          GaeaResponse respone = new GaeaResponse();
          context.setGaeaResponse(respone);
        }
   
        context.getGaeaResponse().setResponseBuffer(protocol.toBytes(Global.getSingleton().getGlobalSecureIsRights(),desKeyByte));
      }
    }catch(Exception ex){
      logger.error("Server ProtocolCreateFilter error!");
    }
  }
View Full Code Here

    logger.debug("------------------------------ begin request-----------------------------");

    StringBuffer sbInvokerMsg = new StringBuffer();
    StopWatch sw = context.getStopWatch();
    Object response = null;
    Protocol protocol = null;
   
    try {
        protocol = context.getGaeaRequest().getProtocol();
      RequestProtocol request = (RequestProtocol)protocol.getSdpEntity();
     
      sbInvokerMsg.append("protocol version:");
      sbInvokerMsg.append(protocol.getVersion());
      sbInvokerMsg.append("\nfromIP:");
      sbInvokerMsg.append(context.getChannel().getRemoteIP());
      sbInvokerMsg.append("\nlookUP:");
      sbInvokerMsg.append(request.getLookup());
      sbInvokerMsg.append("\nmethodName:");
      sbInvokerMsg.append(request.getMethodName());
      sbInvokerMsg.append("\nparams:");
     
      if(request.getParaKVList() != null){
        for (KeyValuePair kv : request.getParaKVList()) {
          if(kv != null) {
            sbInvokerMsg.append("\n--key:");
            sbInvokerMsg.append(kv.getKey());
            sbInvokerMsg.append("\n--value:");
            sbInvokerMsg.append(kv.getValue());
          } else {
            logger.error("KeyValuePair is null  Lookup:" + request.getLookup() + "--MethodName:" + request.getMethodName());
          }
        }
      }
     
      logger.debug(sbInvokerMsg.toString());
      logger.debug("begin get proxy factory");
     
      // get local proxy
      IProxyStub localProxy = Global.getSingleton().getProxyFactory().getProxy(request.getLookup());
      logger.debug("proxyFactory.getProxy finish");

      if (localProxy == null) {
        ServiceFrameException sfe = new ServiceFrameException(
            "method:ProxyHandle.invoke--msg:" + request.getLookup() + "." + request.getMethodName() + " not fond",
            context.getChannel().getRemoteIP(),
            context.getChannel().getLocalIP(),
            request,
            ErrorState.NotFoundServiceException,
            null);
        response = ExceptionHelper.createError(sfe);
        logger.error("localProxy is null", sfe);
      } else {
        logger.debug("begin localProxy.invoke");
        String swInvoderKey = "InvokeRealService_" + request.getLookup() + "." + request.getMethodName();
        sw.startNew(swInvoderKey, sbInvokerMsg.toString());
        sw.setFromIP(context.getChannel().getRemoteIP());
        sw.setLocalIP(context.getChannel().getLocalIP());
       
        //invoker real service
        GaeaResponse gaeaResponse = localProxy.invoke(context);
       
        sw.stop(swInvoderKey);
       
        logger.debug("end localProxy.invoke");
        context.setGaeaResponse(gaeaResponse);
        response = createResponse(gaeaResponse);
        logger.debug("localProxy.invoke finish");
      }
    } catch (ServiceFrameException sfe) {
      logger.error("ServiceFrameException when invoke service fromIP:" + context.getChannel().getRemoteIP() + "  toIP:" + context.getChannel().getLocalIP(), sfe);
      response = ExceptionHelper.createError(sfe);
      context.setError(sfe);
    } catch (Throwable e) {
      logger.error("Exception when invoke service fromIP:" + context.getChannel().getRemoteIP() + "  toIP:" + context.getChannel().getLocalIP(), e);
      response = ExceptionHelper.createError(e);
      context.setError(e);
    }
   
    protocol.setSdpEntity(response);
    logger.debug("---------------------------------- end --------------------------------");
  }
View Full Code Here

         
          if(desKeyStr != null){
            desKeyByte = desKeyStr.getBytes("utf-8");
          }
         
          Protocol protocol = context.getGaeaRequest().getProtocol();
          if(protocol == null){
            protocol = Protocol.fromBytes(context.getGaeaRequest().getRequestBuffer(),global.getGlobalSecureIsRights(),desKeyByte);
            context.getGaeaRequest().setProtocol(protocol);
          }
          protocol.setSdpEntity(ExceptionHelper.createError(e));
          context.getGaeaResponse().setResponseBuffer(protocol.toBytes(Global.getSingleton().getGlobalSecureIsRights(),desKeyByte));
        } catch (Exception ex) {
          context.getGaeaResponse().setResponseBuffer(new byte[]{0});
          logger.error("AsyncInvokerHandle invoke-exceptionCaught error", ex);
        }
       
View Full Code Here

  @Override
  public void filter(GaeaContext context) throws Exception {
   
    Global global = Global.getSingleton();
    Protocol p = context.getGaeaRequest().getProtocol();
    GaeaResponse response = new GaeaResponse();
   
    if(p.getPlatformType() == PlatformType.Java && context.getServerType() == ServerType.TCP){
      //当前服务启动权限认证,并且当前channel通过校验,则进行方法校验
      SecureContext securecontext = global.getGlobalSecureContext(context.getChannel().getNettyChannel());
      if(global.getGlobalSecureIsRights()){
        //当前服务启用权限认证,判断当前channel是否通过授权
        if(securecontext.isRights()){
          RequestProtocol request = (RequestProtocol)p.getSdpEntity();
          if(request != null){
            StringBuffer buff = new StringBuffer(request.getLookup() + "." +request.getMethodName());//接口实现类.方法名(参数序列)
            buff.append("(");
            List<KeyValuePair> list = request.getParaKVList();
            if(list != null){
View Full Code Here

        List<KeyValuePair> listPara = new ArrayList<KeyValuePair>();
        for (Parameter p : paras) {
            listPara.add(new KeyValuePair(p.getSimpleName(), p.getValue()));
        }
        RequestProtocol requestProtocol = new RequestProtocol(typeName, methodName, listPara);
        Protocol sendP = new Protocol(createSessionId(),
            (byte) config.getServiceid(),
            SDPType.Request,
            CompressType.UnCompress,
            config.getProtocol().getSerializerType(),
            PlatformType.Java,
            requestProtocol);
       
        Protocol receiveP = null;
        Server server = null;
       
        for(int i = 0; i <= count; i++){
          server = dispatcher.GetServer();
            if (server == null) {
                logger.error("cannot get server");
                throw new Exception("cannot get server");
            }
            try{
              receiveP = server.request(sendP);
              break;
            } catch(IOException io){
              if(count == 0 || i == ioreconnect){
                throw io;
              }
              if(i < count && i < ioreconnect) {
                logger.error(server.getName()+" server has IOException,system will change normal server!");
                continue;
              }
            } catch(RebootException rb){
              this.createReboot(server);
              if(count == 0 || i == ioreconnect){
                throw new IOException("connect fail!");
              }
              if(i < count && i < ioreconnect) {
                logger.error(server.getName()+" server has reboot,system will change normal server!");
                continue;
              }
            }catch(TimeoutException te){
              if(count == 0 || i == requestTime){
                throw new TimeoutException("Receive data timeout or error!");
              }
              if(i < count && i < requestTime) {
                logger.error(server.getName()+" server has TimeoutException,system will change normal server!");
                continue;
              }
            } catch (Throwable ex){
              logger.error("invoke other Exception", ex);
              throw ex;
            }
      }
       
        if(receiveP == null){
          throw new Exception("userdatatype error!");
        }
       
        if (receiveP.getSDPType() == SDPType.Response) {
            ResponseProtocol rp = (ResponseProtocol)receiveP.getSdpEntity();
            logger.debug("invoke time:" + (System.currentTimeMillis() - watcher) + "ms");
            return new InvokeResult(rp.getResult(), rp.getOutpara());
        } else if(receiveP.getSDPType() == SDPType.Reset){ //服务重启
          logger.info(server.getName()+" server is reboot,system will change normal server!");
          this.createReboot(server);
          return invoke(returnType, typeName, methodName, paras);
        }else if (receiveP.getSDPType() == SDPType.Exception) {
            ExceptionProtocol ep = (ExceptionProtocol)receiveP.getSdpEntity();
            throw ThrowErrorHelper.throwServiceError(ep.getErrorCode(), ep.getErrorMsg());
        } else {
            throw new Exception("userdatatype error!");
        }
    }
View Full Code Here

     * @param data
     * @return Protocol
     * @throws Exception
     */
    public Protocol createProtocol(HandclaspProtocol hp) throws Exception{
      Protocol sendRightsProtocol = new Protocol(createSessionId(),
            (byte) config.getServiceid(),
            SDPType.Request,
            CompressType.UnCompress,
            config.getProtocol().getSerializerType(),
            PlatformType.Java,
View Full Code Here

TOP

Related Classes of pushy.modules.SocketModule$Protocol

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.