⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shortmsgcmpp.java

📁 sp最新短信API(综合网关)
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
     *
     * <p><b>处理流程.</b><p>
     * 1 建立字节流输入管道  <br>
     * 2 解析消息头
     * 3 根据命令类型 headCmdID 调用相应的方法解析消息体  <br>
     * @param  receivedPackage
     * @throws Exception 根据各个子方法<br>
     */
    public void parsePackage(byte[] receivedPackage) {
        ByteArrayInputStream byteStream = new ByteArrayInputStream(receivedPackage);
        DataInputStream dataInStream = new DataInputStream(byteStream);

        parseHead(dataInStream);
        switch(headCmdID) {
          case CmppConstants.Cmpp_Submit_Resp:        //下发短信应答
              try {
                  activeResult=dataInStream.readByte();
                  //msgReserve=readString(dataInStream,8);
                  msgLinkId = readString(dataInStream, 8);
              }
              catch (Exception ex) {
                PublicConstants.writeLog.info("CmppConstants.Cmpp_Submit_Resp"+ex.toString(),0);
              }
              break;
          case CmppConstants.Cmpp_Deliver:             //提交短信
              parseBodyDeliver(dataInStream);
            break;
          case CmppConstants.Cmpp_Report:             //提交短信
            parseBodyReport(dataInStream);
            break;
          case CmppConstants.Cmpp_Login:     //Cmpp_MPCU Login请求
            parseBodyLogin(dataInStream);
            break;
          case CmppConstants.Cmpp_Login_Resp:     //Cmpp_MPCU Login请求
            parseBodyLoginRep(dataInStream);
            break;
          case CmppConstants.Cmpp_Logout:   //Cmpp_MPCU Logout请求
            break;
          case CmppConstants.Cmpp_Logout_Resp:   //Cmpp_MPCU Logout请求
            break;
        case CmppConstants.Cmpp_Nack_Resp:   //未知错误应答
            break;
        default:
            //System.out.println("mpcu.ShortMsgCmpp Pause Package can not find the CommandId:"+headCmdID);
        }
    }

    /**
    * 共用方法
    * 打包消息头
    */
    private void getHead(DataOutputStream dataOutStream,int bodySize) {
        packetLength = bodySize;
        respResult=0;
        try {
            dataOutStream.writeInt(bodySize);
            dataOutStream.writeInt(headCmdID);
            dataOutStream.writeByte(0xb2);
            dataOutStream.writeByte(0xd1);
            dataOutStream.writeByte(0xbe);
            dataOutStream.writeByte(0x26);
//            dataOutStream.writeInt(Integer.parseInt(spId));
//            dataOutStream.writeByte(convertStringToBytes("82109"));
            dataOutStream.writeInt(recvTimeStamp);
            //dataOutStream.writeInt(PublicFuction.getCurrentTimeForUnionSeq());
            dataOutStream.writeInt(headSeqcNo);
        } catch(IOException e) {
            System.out.println("ShortMsgCmpp getHead thrown IOException"+e);
            respResult=1;
        }
    }
    /**
     * 取得下发本地一条消息(deliver)的消息体
     *
     * <p><b>处理流程.</b><p>
     * 1 根据短消息内容的编码方式对消息内容进行编码转换  <br>
     * 2 判断消息总长度 <br>
     * 3 写入消息头 <br>
     * 4 依照协议依次将各字段值写入字节流  <br>
     * @param  DataOutputStream dataOutStream 字节输出管道
     * @return boolean
     * @throws IOException 输入输出异常
     * @throws NullPointerException 空指针异常
     * @throws Exception 其他异常<br>
     */

    /**
     * 取得下发全网一条消息(Fwd_Mo)的消息体
     *
     * <p><b>处理流程.</b><p>
     * 1 根据短消息内容的编码方式对消息内容进行编码转换  <br>
     * 2 判断消息总长度 <br>
     * 3 写入消息头 <br>
     * 4 依照协议依次将各字段值写入字节流  <br>
     * @param  dataOutStream 字节输出管道
     * @return boolean
     * @throws IOException 输入输出异常
     * @throws NullPointerException 空指针异常
     * @throws Exception 其他异常<br>
     */
    private  boolean getSubmit(DataOutputStream dataOutStream) {
        byte[] contentBytes;

        ///      contentBytes = ProtocolConverter.en_Code(msgContent,msgDataCoding) ;
        ///      if( contentBytes == null )
        ///        contentBytes = "".getBytes() ;
        try {
            //if(msgDataCoding==4 )
            //    contentBytes=convertStringToBytes(msgContent);
            //else
            if (msgDataCoding==8)
                contentBytes=msgContent.getBytes("UTF-16BE") ;
            else if (msgDataCoding==15)
                contentBytes=msgContent.getBytes("gb2312") ;
            else
                contentBytes=msgContent.getBytes();
        } catch(Exception e) {
            contentBytes=msgContent.getBytes();
            System.out.println("unsupportedEncoding Exception :"+e) ;
        }
        try {
            /**
             * test group send
             */

            totalSize=20+21+21+1+21*submit_destusr_tl+5+10+1+6+6+1+1+1+16+16+1+1+1+1+1+4+contentBytes.length+8;
            getHead(dataOutStream,totalSize);
            writeString(dataOutStream,msgSrcAddr,21); //
            writeString(dataOutStream,feeTerminalId,21);
            dataOutStream.writeByte (submit_destusr_tl);
            for(int i=0;i<submit_destusr_tl;i++)
            {
              writeString(dataOutStream,msgSubmitDest[i],21);
            }
            writeString(dataOutStream,Integer.toString(ComConstants.o_IsmgInfo.spId),5);     //这里用sp的特服号,不知道对不对?
            writeString(dataOutStream,msgServiceType,10);
            dataOutStream.writeByte (msgFeeType);
            writeString(dataOutStream,Integer.toString(msgInfoFee),6);
            writeString(dataOutStream,Integer.toString(msgGivenValue),6);
            dataOutStream.writeByte (msgAgentFlag);
//            if(msgMode==3||msgServiceType.startsWith("Test")||msgServiceType.startsWith("test")||msgServiceType.startsWith("TEST")){
//              msgMorelatetoMTFlag=3;
//            }
            if(msgMode==3) {
              msgMorelatetoMTFlag=2;
            }

            dataOutStream.writeByte (msgMorelatetoMTFlag);
            dataOutStream.writeByte (msgPriority);
            writeString(dataOutStream,msgValidityPeriod,16);
            writeString(dataOutStream,msgSchedule,16);
            dataOutStream.writeByte (msgMode);
            dataOutStream.writeByte (tpPId);
            dataOutStream.writeByte (tpUdhi);
            dataOutStream.writeByte (msgDataCoding);
            dataOutStream.writeByte (0);
            dataOutStream.writeInt(contentBytes.length );
            dataOutStream.write(contentBytes);
            //writeString(dataOutStream,"",8);
            writeString(dataOutStream,msgLinkId, 8);


        } catch(Exception e) {
            System.out.println("write data to pipe has CmppConstants.error"+e.toString());
            return false;
        }
        return true;
    }

    /**
     * 取得对本地提交消息的回应(Submit_resp)的消息体
     *
     * <p><b>处理流程.</b><p>
     * 0 此处需要回应SP一个自己生成的MsgId,在解包时已经生成
     * 1 判断消息总长度  <br>
     * 2 写入消息头  <br>
     * 3 依照协议写入字节流  <br>
     * @param  dataOutStream 字节输出管道
     * @throws IOException 输入输出异常
     * @throws NullPointerException 空指针异常
     * @throws Exception 其他异常<br>
     */
    private  void getDeliverRep(DataOutputStream dataOutStream) {
      int bodySize;
      try {
        bodySize =20 + 1 + 8;
        respResult=0;
        getHead(dataOutStream,bodySize);
        dataOutStream.writeByte(respResult);
        writeString(dataOutStream,"",8);
      }
      catch (Exception ex) {
        PublicConstants.writeLog.info("getDeliverRep"+ex.toString());
      }
    }
    private  void getReportRep(DataOutputStream dataOutStream) {
      int bodySize;
      try {
        bodySize =20 + 1 + 8;
        respResult=0;
        getHead(dataOutStream,bodySize);
        dataOutStream.writeByte(respResult);
        writeString(dataOutStream,"",8);
      }
      catch (Exception ex) {
        PublicConstants.writeLog.info("getReportRep"+ex.toString());
      }
    }
    /**
     * 取得向MPCA发出登录消息(Login)的消息体
     *
     * <p><b>处理流程.</b><p>
     * 0 读取登录所必需的信息 在ShortMsgCmpp.setConSourceAddr(String s) 方法已实现 <br>
     * 1 判断消息总长度  <br>
     * 2 写入消息头  <br>
     * 3 依照协议写入字节流  <br>
     * @param  dataOutStream 字节输出管道
     * @throws IOException 输入输出异常
     * @throws NullPointerException 空指针异常
     * @throws Exception 其他异常<br>
     */
    private  void getSpLogin(DataOutputStream dataOutStream) {
        int bodySize;
        try {
            bodySize = 20 + 1 + 16 + 16 + 8;
            respResult=0;
            getHead(dataOutStream,bodySize);
            dataOutStream.writeByte(conBindType) ;
            writeString(dataOutStream,spUnionUserName,16);
            writeString(dataOutStream,spUnionPasswd,16);
            writeString(dataOutStream,"",8);

        } catch(IOException e) {
            respResult=1;
            System.out.println("get getMpcuLogin  erro"+e);
        }
    }

    /**
     * 共用方法
     * 向流中写入一个字符串,以'\0'结尾
     */
    private  static boolean writeString(DataOutputStream dataOutStream,String str) {
        try {
            dataOutStream.writeBytes(str) ;
            dataOutStream.writeByte('\0');
        } catch(IOException e) {
            return false;
        }
        return true;
    }
//---------------------------------------------------------------
/**
 * 向流中写入一个字符串,以'\0'结尾
 */
    private static boolean writeString(DataOutputStream outStream,String str,int StrLen){
        int i;
        try{
            if(str!=null){
            outStream.writeBytes(str) ;
            for (i=1;i<=StrLen-str.length ();i++){
                outStream.writeByte('\0');
            }
            }
            else{
              for (i=1;i<=StrLen;i++){
                outStream.writeByte('\0');
              }
            }
        }
        catch(IOException e){
            return false;
        }
        return true;
    }

    /**
     * 解析消息头
     *
     * <p><b>处理流程.</b><p>
     * 1 读入 消息指令
     * 2 读入 消息状态
     * 3 读入 消息序列号
     * 其中消息头还应该包括消息总长度 ,但这里不包括,Connnection接收短消息时已经解析了
     * @param  dataInStream 输入字节流
     * @throws IOException 输入输出异常
     * @throws NullPointerException 空指针异常
     * @throws Exception 其他异常<br>
     */

    private  void parseHead(DataInputStream dataInStream) {
        try {
          headCmdID =     dataInStream.readInt();             //消息类型
          srcSectionId =  dataInStream.readInt();
          recvTimeStamp = dataInStream.readInt();
          headSeqcNo=     dataInStream.readInt();             //消息流水号(可以用来完成消息的确认)                         //流中包含消息包长度的四个字节
        }
        catch(IOException e) {
            System.out.println("parseHead  erro  "+e);
            parseResult = CmppConstants.error_msg_structure;
        }
    }
    /**
     * 解析Login消息体或MPCU--MPCA Login消息体
     *
     * <p><b>处理流程.</b><p>
     * 1 依照协议从字节输入管道中读出相应字段<br>
     * 2 判断字段内容的合法性,并将验证结果写入parseResult值 <br>
     * @param  DataInputStream dataInStream 字节输入流
     * @return void (parseResult 解包结果)
     * @throws IOException 输入输出异常
     * @throws NullPointerException 空指针异常
     * @throws Exception 其他异常<br>
     */

    /**
     * 解析MPCU--MPCA Login REP消息体
     *
     * <p><b>处理流程.</b><p>
     * 1 依照协议从字节输入管道中读出相应字段<br>
     * 2 判断字段内容的合法性,并将验证结果写入parseResult值 <br>
     * @param   dataInStream 字节输入流
     * (parseResult 解包结果)
     * @throws IOException 输入输出异常
     * @throws NullPointerException 空指针异常
     * @throws Exception 其他异常<br>
     */
    private void parseBodyLoginRep(DataInputStream dataInStream) {
        try {
             activeResult=dataInStream.readByte();
             headCmdStatus=activeResult;
             //msgReserve=readString(dataInStream,8);
             msgLinkId = readString(dataInStream, 8);
        } catch(Exception e) {
            parseResult =CmppConstants.error_msg_structure;
            System.out.println(" Cmpp sp  loginRep   erro"+e);
            return ;
        }
    }
    private void parseBodyLogin(DataInputStream dataInStream) {
    try {
      conBindType=dataInStream.readByte() ;
      conSourceAddr=readString(dataInStream,16);
      conSourcePasswd=readString(dataInStream,16);
      //msgReserve=readString(dataInStream,8);
      msgLinkId = readString(dataInStream, 8);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -