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

📄 send2dongruan.java

📁 东软短信网关接入程序
💻 JAVA
字号:
package com.zznode.dp.ismg.sender;

import com.commerceware.cmpp.CMPP;
import com.commerceware.cmpp.cmppe_result;
import com.commerceware.cmpp.cmppe_submit;
import com.commerceware.cmpp.conn_desc;

import com.zznode.dp.ismg.util.Log;
import com.zznode.dp.ismg.util.StringHandler;
import com.zznode.dp.ismg.data.MessageModel;
import com.zznode.dp.ismg.data.ConnectionModel;
import com.zznode.dp.ismg.data.SubmitMessageModel;
import com.commerceware.cmpp.cmppe_submit_result;
import com.commerceware.cmpp.cmppe_login_result;

public class Send2DongRuan {
  private static ConnectionModel connectionModel = null;
  private static SubmitMessageModel submitmMessageModel = null;
  private static cmppe_result result;
  private static CMPP cmpp = new CMPP();
  private static conn_desc connDesc = new conn_desc();
  private static int sendBackState = -1;
  private static final int CMPPE_LOGIN_RESP = 0x80000001;
  private static final int CMPPE_SUBMIT_RESP = 0x80000004;
  private static final int CMPPE_LOGOUT_RESP = 0x80000002;
  /**日志文件*/
  private static Log log;

  StringHandler stringhandler = null;
  private SMArgument pArgs = new SMArgument();

  public Send2DongRuan(ConnectionModel connectionModel,
                       SubmitMessageModel submitmMessageModel) {
    this.connectionModel = connectionModel;
    this.submitmMessageModel = submitmMessageModel;
    log = new Log().getLog();
    stringhandler = new StringHandler().getStringHandler();
  }

  /**
   * 短信发送程序入口
   * @param st1 StringTokenizer
   * @param sb1 StringBuffer
   * @param Msg String
   * @param alarmid String
   */
  public String sendSms(MessageModel[] messageModelArry) {
    StringBuffer reponse = new StringBuffer(); ; //返回消息
    boolean isSuccess = true; //是否发送成功
    /*---------------------登录----------------------------------*/
    log.writeLog("start... ");
    System.out.println("start... ");
    if (isLogin()) { //登录
    /**----------------------提交信息------------------------------*/
    for (int i = 0; i < messageModelArry.length; i++) {
      MessageModel messageModel = messageModelArry[i];
      String[] dstArray = messageModel.getPhone();
      String[] messArray = messageModel.getMessageContent();
      log.writeLog("start send message... ");
      System.out.println("start send message... ");
      try {
        for (int m = 0; m < dstArray.length; m++) { //逐个联系人
          dstArray[m] += "\0"; //每个号码必须以byte 0结尾
          for (int n = 0; n < messArray.length; n++) { //逐条消息
            isSuccess = isSubmit(dstArray[m], messArray[n].getBytes()); //发送信息
          }
        }
        if (isSuccess) {
          reponse.append(messageModel.getMessageId() + "@@success@@,");
        }
        else {
          reponse.append(messageModel.getMessageId() + "@@false@@,");
        }
      }
      catch (Exception e) {
        log.writeLog("send message exception  :" + e);
        System.out.println("send message exception  : " + e);
        e.printStackTrace();
      }
    }
    log.writeLog("send accomplish!");
    System.out.println("send accomplish!");
    isLogout(); //登出
    }
    return reponse.toString();
  }

  /**
   * 登录
   * @return boolean
   */
  private boolean isLogin() {
    boolean isLogin = true;
    try {
      cmpp.cmpp_connect_to_ismg(connectionModel.getHost(),
                                Integer.parseInt(connectionModel.getPort()),
                                connDesc);
      log.writeLog("connection success...");
      System.out.println("connection success...");
      cmpp.cmpp_login(connDesc, connectionModel.getSource_addr(),
                      connectionModel.getShared_secret(), (byte) 2, 0x12,
                      (int) System.currentTimeMillis());

      result = cmpp.readResPack(connDesc); //back cmppe_result
      if (result.pack_id == CMPPE_LOGIN_RESP) {
        cmppe_login_result cl;
        cl = (cmppe_login_result) result;
        sendBackState = cl.stat;
        log.writeLog("------------login resp----------: STAT = " +
                     sendBackState);
      }
      if (sendBackState == 0) {
        System.out.println("Login  Success...");
        log.writeLog("Login  Success...");
        return true;
      }
      else {
        System.out.println("Login  False...");
        log.writeLog("Login  False...");
        return false;
      }
    }
    catch (Exception e) {
      log.writeLog("Login  False... " + e);
      System.out.println("Login  False... " + e);
      isLogin = false;
    }
    return isLogin;
  }

  /**
   *  提交信息
   * @param phoneno String
   * @param content byte[]
   * @return boolean
   */
  private boolean isSubmit(String phoneno, byte[] content) {
    boolean isSuccess = true;
    cmppe_submit sub = new cmppe_submit();
    int sm_len = content.length;
    sm_len = content.length;

    //read arguments from file
    setArguments(pArgs, phoneno);

    pArgs.short_msg = new byte[sm_len];
    System.arraycopy(content, 0, pArgs.short_msg, 0, sm_len);
    try {
      sub.set_icpid(pArgs.icp_id);
      sub.set_svctype(pArgs.svc_type);
      sub.set_feetype(pArgs.fee_type);
      sub.set_infofee(pArgs.info_fee);
      sub.set_protoid(pArgs.proto_id);
      sub.set_msgmode(pArgs.msg_mode);
      sub.set_priority(pArgs.priority);
      sub.set_validate(pArgs.validate);
      sub.set_schedule(pArgs.schedule);
      sub.set_feeutype(pArgs.fee_utype);
      sub.set_feeuser(pArgs.fee_user);
      sub.set_srcaddr(pArgs.src_addr);
      sub.set_ducount(pArgs.du_count);
      sub.set_dstaddr(pArgs.dst_addr);
      sub.set_msg(pArgs.data_coding, sm_len, pArgs.short_msg);
      cmpp.cmpp_submit(connDesc, sub); //Submit
      log.writeLog("Messages have submited! ");
      if (result.pack_id == CMPPE_SUBMIT_RESP) {
        cmppe_submit_result sr;
        sr = (cmppe_submit_result) result;
        sendBackState = sr.stat;
        log.writeLog("------------submit resp----------: STAT = " + sr.stat +
                     " SEQ = " + sr.seq);
      }
      if (sendBackState == 0) {
        System.out.println("Send Success: send message [" +
                           new String(pArgs.short_msg) + "] to phone [" +
                           phoneno + "]");
        log.writeLog("Send Success: send message [" +
                     new String(pArgs.short_msg) + "] to phone [" +
                     phoneno + "]");
      }
      else {
        System.out.println("Send Failure: send message [" +
                           new String(pArgs.short_msg) + "] to phone [" +
                           phoneno + "]");
        log.writeLog("Send Failure: send message [" +
                     new String(pArgs.short_msg) + "] to phone [" +
                     phoneno + "]");
        isSuccess = false;
      }
    }
    catch (Exception e) {
      System.out.println(e.getMessage());
      e.printStackTrace();
      System.out.println("have a exception");
      isSuccess = false;
    }
    return isSuccess;
  }

  /**
   * 登出
   * @return boolean
   */
  private boolean isLogout() {
    try {
      cmpp.cmpp_logout(connDesc);
      result = cmpp.readResPack(connDesc); //back cmppe_result
      if (result.pack_id == CMPPE_LOGOUT_RESP) {
        sendBackState = result.stat;
        System.out.println("------------logout resp----------: STAT = " +
                           result.stat);
        log.writeLog("------------logout resp----------: STAT = " + result.stat);
      }
      if (sendBackState == 0) {
        log.writeLog("logout success!");
        System.out.println("logout success!");
        return true;
      }
      else {
        log.writeLog("logout Failure!");
        System.out.println("logout Failure!");
        return false;
      }

    }
    catch (Exception e) {
      log.writeLog("have a exception :" + e);
      e.printStackTrace();
      System.out.println("have a exception");
      return false;
    }
  }

  /**
   * 设置submit参数
   * @param pArgs SMArgument
   * @param phoneno String
   * @return int
   */
  private void setArguments(SMArgument pArgs, String phoneno) {
    String icp_id = connectionModel.getSource_addr() + "\0"; //必须以byte 0结尾
    pArgs.icp_id = icp_id.getBytes();

    String svc_type = submitmMessageModel.getSvcType() + "\0"; //必须以byte 0结尾
    pArgs.svc_type = svc_type.getBytes();

    String fee_user = submitmMessageModel.getSrc_addr() + "\0"; //必须以byte 0结尾
    pArgs.fee_user = fee_user.getBytes();

    System.arraycopy(phoneno.getBytes(), 0, pArgs.dst_addr[0], 0,
                     phoneno.length());

    pArgs.data_coding = (byte) submitmMessageModel.getMsg_Fmt();

    pArgs.fee_type = (byte) Integer.parseInt(submitmMessageModel.getFee_Type());
    pArgs.info_fee = (byte) Integer.parseInt(submitmMessageModel.getFee_Code());
    pArgs.proto_id = 1;
    pArgs.msg_mode = 0;
    pArgs.priority = 0;
    pArgs.fee_utype = 1;
    pArgs.validate[0] = 0;
    pArgs.schedule[0] = 0;
    pArgs.du_count = 1;
  }

  /**
   * submit参数类
   * <p>Title: </p>
   * <p>Description: </p>
   * <p>Copyright: Copyright (c) 2005</p>
   * <p>Company: </p>
   * @author not attributable
   * @version 1.0
   */
  class SMArgument {
    public byte icp_id[];
    public byte svc_type[];
    public byte fee_type;
    public byte fee_user[];
    public byte info_fee;
    public byte proto_id;
    public byte msg_mode;
    public byte priority;
    public byte fee_utype;
    public byte validate[] = new byte[10];
    public byte schedule[] = new byte[2];
    public byte src_addr[] = new byte[12];
    public byte du_count;
    public byte dst_addr[][] = new byte[10][15];
    public byte data_coding;
    public byte short_msg[];
    public String IcpId;
    public String IcpShareKey;
    public String ClientIP;
    public String ServerIP;
    public int ServerPort;
  }
}

⌨️ 快捷键说明

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