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

📄 smgp.java

📁 本人为友邦公司做的模拟网关源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package smgpgw;

//package smgw;
import java.io.*;
import java.util.Calendar;

/**
 * Title: SMGP 数据封装类

 * Description:
 *  该类用途 1、调用getCommand()把要发送的信息通过SMGP_Data类传入,根据SMGP协议打包后,输出到DataOutputStream流中
 *           2、调用parseMessage()把接受的信息通过Byte[]传入,根据SMGP协议解析后,返回SMGP_Data对象
 *  另:如何设置类SMGP_Data请参阅SMGP_Data.java文件
 * Copyright:yongliu wangping   Copyright (c) 2001 /3
 * Company:talkeweb
 * @author
 * @version 1.0
 */

public class Smgp {
  /************************************************************************/
//SMGP协议中各种命令的常量定义 参见SMGPV1.1
  public static final int SMGP_login = 0x00000001; //请求连接
  public static final int SMGP_login_resp = 0x80000001; //请求连接应答
  public static final int SMGP_submit = 0x00000002;
  public static final int SMGP_submit_resp = 0x80000002;
  public static final int SMGP_deliver = 0x00000003;
  public static final int SMGP_deliver_resp = 0x80000003;
  public static final int SMGP_active_test = 0x00000004;
  public static final int SMGP_active_test_resp = 0x80000004;

  /******************************************************************************
   SMGP协议中返回的命令状态信息代码定义
   */

  public static final int CORRECT = 0; //成功
  public static final int SYSTEMBUZY_ERR = 1; //系统忙
  public static final int EXCEEDCONNECT_ERR = 2; //超过最大连接数
  public static final int MISSTRUCTURE_ERR = 10; //消息结构错
  public static final int MISCOMMOND_ERR = 11; //命令字错
  public static final int REPEATSEQ_ERR = 12; //序列号重复
  public static final int MISIP_ERR = 20; //IP地址错
  public static final int MISAUTH_ERR = 21; //认证错
  public static final int HIGHVERSION_ERR = 22; //版本太高
  public static final int MISMSGTYPE_ERR = 30; //非法消息类型(SMType)
  public static final int MISPRIORITY_ERR = 31; //非法优先级(Priority)
  public static final int MISFEETYPE_ERR = 32; //非法资费类型(FeeType)
  public static final int MISFEECODE_ERR = 33; //非法资费代码(FeeCode)
  public static final int MISFORMAT_ERR = 34; //非法短消息格式(MsgFormat)
  public static final int MISTIMESTRUCTURE_ERR = 35; //非法时间格式
  public static final int MISLENGTH_ERR = 36; //非法短消息长度(MsgLength)
  public static final int MISAVAILABILITY_ERR = 37; //有效期已过
  public static final int MISQUERYTYPE_ERR = 38; //非法查询类别(QueryType)
  public static final int MISROUTE_ERR = 39; //路由错误
  public static final int OTHERS_ERR = 9; //其他错误

  public static byte[] Lmsgid = new byte[8];
  private static final String READSTRINGERROR = "readStringError";

//-----------------------------------------------
  /**
   * 接口函数。调用该函数把发送信息按SMGP协议打包,打包结果输入到流中
   */
//public static  boolean getCommand( DataOutputStream   dataOutStream,SMGP_Data SmgpData){
  public synchronized static boolean getCommand(DataOutputStream dataOutStream,
                                                SmgpData SmgpData) {
    switch (SmgpData.headCmdID) {
      case SMGP_active_test: //连接测试
        return getActiveTestCommand(dataOutStream, SmgpData);
      case SMGP_active_test_resp: //连接测试应答
        return getActiveTestRepCommand(dataOutStream, SmgpData);
      case SMGP_login_resp: //连接请求回应
        return getConnectRepCommand(dataOutStream, SmgpData);
      case SMGP_submit_resp: //submit回应
        return getSubmitRepCommand(dataOutStream, SmgpData);
      case SMGP_deliver: //下发短信
        return getDeliverCommand(dataOutStream, SmgpData);
    }
    return true;
  }

  private static byte connectdata1(long num) {
    byte x = (byte) (num / (256 * 256));
    return x;
  }

  private static byte connectdata2(long num) {
    byte x = (byte) ( (num / 256) % 256);
    return x;
  }

  private static byte connectdata3(long num) {
    byte x = (byte) (num % 256);
    return x;
  }

//-------------------------------------------------------------
  private static boolean getActiveTestCommand(DataOutputStream dataOutStream,
                                              SmgpData SmgpData) {

    try {
      int location = 0;
      byte[] data = new byte[12];
      byte[] tmp = toByteArray( (int) 12);
      for (int i = 0; i < tmp.length; i++)
        data[location++] = tmp[i];
      tmp = toByteArray(SmgpData.headCmdID);
      for (int i = 0; i < tmp.length; i++)
        data[location++] = tmp[i];
      tmp = toByteArray(SmgpData.headSeqcNo);
      for (int i = 0; i < tmp.length; i++)
        data[location++] = tmp[i];
      dataOutStream.write(data);
      dataOutStream.flush();
    }
    catch (IOException e) {
      return false;
    }
    return true;
  }

  private static boolean getActiveTestRepCommand(DataOutputStream dataOutStream,
                                                 SmgpData SmgpData) {
    try {
      int location = 0;
      byte[] data = new byte[12];
      byte[] tmp = toByteArray( (int) 12);
      for (int i = 0; i < tmp.length; i++)
        data[location++] = tmp[i];
      tmp = toByteArray(SmgpData.headCmdID);
      for (int i = 0; i < tmp.length; i++)
        data[location++] = tmp[i];
      tmp = toByteArray(SmgpData.headSeqcNo);
      for (int i = 0; i < tmp.length; i++)
        data[location++] = tmp[i];
      dataOutStream.write(data);
      dataOutStream.flush();
    }
    catch (IOException e) {
      return false;
    }
    return true;
  }

  private static boolean getConnectRepCommand(DataOutputStream dataOutStream,
                                              SmgpData SmgpData) {
    try {
      int location = 0;
      byte[] data = new byte[33];
      byte[] tmp = toByteArray( (int) 33);
      for (int i = 0; i < tmp.length; i++)
        data[location++] = tmp[i];
      tmp = toByteArray(SmgpData.headCmdID);
      for (int i = 0; i < tmp.length; i++)
        data[location++] = tmp[i];
      tmp = toByteArray(SmgpData.headSeqcNo);
      for (int i = 0; i < tmp.length; i++)
        data[location++] = tmp[i];
      tmp = toByteArray(SmgpData.status);
      for (int i = 0; i < tmp.length; i++)
        data[location++] = tmp[i];
      writeString(SmgpData.AuthenticatorServer, data, 16, location);
      location = location + 16;
      data[location++] = SmgpData.version;
      dataOutStream.write(data);
      dataOutStream.flush();
    }
    catch (IOException e) {
      return false;
    }
    return true;
  }

//1215
  public synchronized static boolean getSubmitRepCommand(DataOutputStream
      dataOutStream, SmgpData SmgpData) {

    try {
      int location = 0;
      byte[] data = new byte[26];
      byte[] tmp = toByteArray( (int) 26);
      for (int i = 0; i < tmp.length; i++)
        data[location++] = tmp[i];

      tmp = toByteArray(SmgpData.headCmdID);

      for (int i = 0; i < tmp.length; i++)
        data[location++] = tmp[i];

      tmp = toByteArray(SmgpData.headSeqcNo);

      for (int i = 0; i < tmp.length; i++)
        data[location++] = tmp[i];
//      System.err.println("nnnnnnnnnnnnnnnnnnnnnnnnnnnnn:" +SmgpData.MsgID);
      writeString(SmgpData.MsgID, data, 10, location);
      location = location + 10;

      tmp = toByteArray(SmgpData.status);
      for (int i = 0; i < tmp.length; i++)
        data[location++] = tmp[i];

      dataOutStream.write(data);
      dataOutStream.flush();
    }
    catch (IOException e) {
      System.err.println("getSubmitRepCommand:" + e.getMessage());
      return false;
    }
    return true;
  }

  public synchronized static boolean getDeliverCommand(DataOutputStream
      dataOutStream, SmgpData SmgpData) {
    byte[] msgText = null;
    int totalSize;
    int msgLen;
    try {
      if (SmgpData.msgIsReport == 1) {}
      else {
        /**
         * modified by me(yzx) at 2004-03-03:修正原来笼统的转换
         */
        if (SmgpData.msgMsgFormat == 8) { //lgh
          //SmgpData.msgContent=new String(SmgpData.msgContent.getBytes ("UTF-16BE"),0);
          SmgpData.msgMsgContent = new String(SmgpData.msgMsgContent.getBytes(),
                                              "UTF-16BE");
          SmgpData.msgMsgLength = (byte) SmgpData.msgMsgContent.getBytes(
              "UTF-16BE").length;
          msgText = SmgpData.msgMsgContent.getBytes(
              "UTF");
        }
        else if (SmgpData.msgMsgFormat == 15) { //lgh
          SmgpData.msgMsgContent = new String(SmgpData.msgMsgContent.getBytes(
              "gb2312"));
          SmgpData.msgMsgLength = (byte) SmgpData.msgMsgContent.getBytes(
              "gb2312").length;
          msgText = SmgpData.msgMsgContent.getBytes(
              "gb2312");
        }
        else if (SmgpData.msgMsgFormat == 4) {
          msgText = Smgp.convertStringToBytes(SmgpData.msgMsgContent);
          Integer msgTextLen = new Integer(msgText.length);
          SmgpData.msgMsgLength = msgTextLen.byteValue();
        }
        else {
          msgText = SmgpData.msgMsgContent.getBytes();
          Integer msgTextLen = new Integer(msgText.length);
          SmgpData.msgMsgLength = msgTextLen.byteValue();
        }
      }
//      System.err.println("delive msg length:" + SmgpData.msgMsgLength);

      if (SmgpData.msgMsgLength < 0) {
        msgLen = 256 + SmgpData.msgMsgLength;
      }
      else {
        msgLen = SmgpData.msgMsgLength;
      }
      if (msgLen > 160) {
//		  method.error ("send  date has too long!");
        return false;
      }
      if (SmgpData.msgIsReport == 1)
        msgLen = SmgpData.msgMsgLength = 122;

//     System.err.println("delive msg length:" + SmgpData.msgMsgLength);

      totalSize = 89 + msgLen;

      int location = 0;
      byte[] data = new byte[totalSize];
      byte[] tmp = toByteArray(totalSize);
      /**
       * 写入字节长度
       */
      for (int i = 0; i < tmp.length; i++)
        data[location++] = tmp[i];
        /**
         * 写入命令字
         */
      tmp = toByteArray(SmgpData.headCmdID);
      for (int i = 0; i < tmp.length; i++)
        data[location++] = tmp[i];
        /**
         * 写入序列号
         */

      tmp = toByteArray(SmgpData.headSeqcNo);
      for (int i = 0; i < tmp.length; i++)
        data[location++] = tmp[i];
//      System.err.println("dddddddddddddddddddddddddd:"+(location-1));


      writeString(SmgpData.MsgID, data, 10, location);
      location = location + 10;

      data[location++] = SmgpData.msgIsReport;
      data[location++] = SmgpData.msgMsgFormat;
//      System.err.println("qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq:" + SmgpData.msgRecvTime);
      writeString(SmgpData.msgRecvTime, data, 14, location);
      location = location + 14;

      writeString(SmgpData.msgSrcTermID, data, 21, location);
      location = location + 21;

      writeString(SmgpData.msgDestTermID, data, 21, location);
      location = location + 21;
      data[location++] = SmgpData.msgMsgLength;
      if (SmgpData.msgIsReport == 1) { //状态报告
//        System.err.println("report MsgID:" + SmgpServer.submitResp_msg_id);
//        System.err.println("report msgSubmit_date:" + SmgpData.msgSubmit_date);
//        System.err.println("report msgdone_date:" + SmgpData.msgdone_date);
//        System.err.println("report msgStat:" + SmgpData.msgStat);
//        System.err.println("report msgErr:" + SmgpData.msgErr);
        writeString("Id:"+SmgpServer.submitResp_msg_id, data, 13, location);
        location = location + 13;
        writeString(" sub:001", data, 8, location);
        location = location + 8;
        writeString(" DIvrd:001", data, 10, location);
        location = location + 10;

        writeString(" Submit_date:"+SmgpData.msgSubmit_date, data, 23, location);
        location = location + 23;
        writeString(" done_date:"+SmgpData.msgdone_date, data, 21, location);
        location = location + 21;

        writeString(" stat:"+SmgpData.msgStat, data, 13, location);
        location = location + 13;

        writeString(" err:"+SmgpData.msgErr, data, 8, location);
        location = location + 8;

        writeString(" text:00000000000000000000", data, 26, location);
        location = location + 26;


      }
      else {
        for (int j = 0; j < msgText.length; j++) {
          data[location++] = msgText[j];
        }
      }

      SmgpData.msgReserve = "        ";

      if (writeString(SmgpData.msgReserve, data, 8, location) == false)
        return false;

      dataOutStream.write(data);
      dataOutStream.flush();

    }
    catch (Exception e) {
      System.out.println("deliver error=" + e);
      e.printStackTrace();
      return false;
    }
    return true;
  }

//-----------------------------------------------
  /**
   * 解析头内容
   */
  private static void parseHead(DataInputStream dataInStream, SmgpData recvdMsg) {
    try {
      //note:流中不包含消息包长度的四个字节
      recvdMsg.headCmdID = dataInStream.readInt();
      recvdMsg.headSeqcNo = dataInStream.readInt();
    }
    catch (IOException e) {
      recvdMsg.headCmdID = 0;
    }
    return;
  }

//-------------------------------------------------------------
  /**
   * 接口函数。根据SMGP协议解析传入的数据,解析后内容通过SMGP_Data返回
   * 注:如果结果中cmdID == 0 则解析错误
   */
  public static SmgpData parseMessage(byte[] receivedData) {

    ByteArrayInputStream byteStream = new ByteArrayInputStream(receivedData);
    DataInputStream dataInStream = new DataInputStream(byteStream);

    SmgpData recvdMsg = new SmgpData();
    /*    recvdMsg.headCmdID=toInteger(receivedData,0);
     recvdMsg.headSeqcNo=toInteger(receivedData,4);*/
    parseHead(dataInStream, recvdMsg);
    if (recvdMsg.headCmdID == 0)
      return recvdMsg;
    if (receivedData.length < 8) //只有头信息8个字节
      return recvdMsg;

    switch (recvdMsg.headCmdID) {
      case SMGP_login: //请求连接
        parseBodyConnect(dataInStream, recvdMsg);
        break;
      case SMGP_deliver_resp: //下发短信应答
        parseBodyDeliverRep(dataInStream, recvdMsg);
        break;
      case SMGP_submit: //提交短信
        parseBodySubmit(dataInStream, recvdMsg);
        break;
    }
    return recvdMsg;
  }

  private static void parseBodyConnect(DataInputStream dataInStream,
                                       SmgpData SmgpData) {
    try {
      SmgpData.ClientID = readString(dataInStream, 8);
      if (SmgpData.ClientID == READSTRINGERROR) {
        SmgpData.status = MISAUTH_ERR;
        return;

⌨️ 快捷键说明

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