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

📄 smgplogic.java

📁 用电信smgp网关发送小灵通的短信,有api和简单调用样例
💻 JAVA
字号:
package com.ut.smgp.api.logic;

import com.ut.smgp.api.structure.*;
import com.ut.smgp.api.configure.*;
import com.ut.smgp.api.function.*;
import com.ut.smgp.api.thread.*;
import java.io.*;
import java.net.Socket;
import java.util.*;

public class smgpLogic {
  private function fun = new function();
  private socketLogic send = new socketLogic();
  public smgpLogic() {
  }

  /********************************************************************************************
   *
   * 断开连接
   *
   */
  public int exit(socketStruct con, initStruct init, deliverVector deliverQueue,
                  int type) {
    return send.exit(con, init.getRequestTimeOut(), deliverQueue, type);
  }

  /*********************************************************************************************
   * 发送单条信息
   *
   */
  public int sendSingle(initStruct init, socketStruct con, submitStruct submit,
                        deliverVector deliverQueue, int type) {
    //
    if (submit.getMsgLength() > configure.MSG_MAX_LENGTH) {
      return configure.ERROR_MSG_LENGTH; //信息超长
    }
    SMGPHeadStruct head = new SMGPHeadStruct();
    byte[] body = new byte[submit.getSubmitLength()];
    int i = 0;
    body[i] = submit.getMsgType_byte();
    i += 1;
    body[i] = submit.getNeedReport_byte();
    i = i + 1;
    body[i] = submit.getPrioprity_byte();
    i = i + 1;
    System.arraycopy(submit.getServiceId_byte(), 0, body, i, 10);
    i += 10;
    System.arraycopy(submit.getFeeType_byte(), 0, body, i, 2);
    i += 2;
    System.arraycopy(submit.getFeeCode_byte(), 0, body, i, 6);
    i += 6;
    System.arraycopy(submit.getFixedFee_byte(), 0, body, i, 6);
    i += 6;
    body[i] = submit.getMsgFormat_byte();
    i += 1;
    System.arraycopy(submit.getValidTime_byte(), 0, body, i, 17);
    i += 17;
    System.arraycopy(submit.getAtTime_byte(), 0, body, i, 17);
    i += 17;
    System.arraycopy(submit.getSrcTermId_byte(), 0, body, i, 21);
    i += 21;
    System.arraycopy(submit.getChargeTermId_byte(), 0, body, i, 21);
    i += 21;
    body[i] = submit.getDestTermIdCount_byte();
    i += 1;
    byte[] temp = submit.getDestTermId_byte();
    System.arraycopy(temp, 0, body, i, temp.length);
    i += temp.length;
    body[i] = submit.getMsgLength_byte();
    i += 1;
    temp = submit.getMsgContent_byte();
    System.arraycopy(temp, 0, body, i, temp.length);
    i += temp.length;
    System.arraycopy(submit.getReserve_byte(), 0, body, i, 8);
    head.packageSize = submit.getSubmitLength() + 12;
    head.requestId = 2;
    byte[] msgId = new byte[10];
    int result = send.submit(head, body, con, init.getSubmitRetry(),
                             init.getRequestTimeOut(), deliverQueue, msgId,
                             type);
    submit.setMsgId(new String(msgId));
    return result;
  }

  /*********************************************************************************************
   * 群发信息
   *
   */
  public int[] sendMulti(initStruct init, socketStruct con,
                         submitStruct[] submitMulti, deliverVector deliverQueue,
                         int timeout, int type) {
    SubmitMultiStruct[] multi = new SubmitMultiStruct[submitMulti.length];
    for (int num = 0; num < submitMulti.length; num++) {
      multi[num] = new SubmitMultiStruct();
      submitStruct submit = submitMulti[num];
      if (submit.getMsgLength() > configure.MSG_MAX_LENGTH) {
        multi[num].result = configure.ERROR_MSG_LENGTH; //信息超长
      }
      SMGPHeadStruct head = new SMGPHeadStruct();
      byte[] body = new byte[submit.getSubmitLength()];
      int i = 0;
      body[i] = submit.getMsgType_byte();
      i += 1;
      body[i] = submit.getNeedReport_byte();
      i = i + 1;
      body[i] = submit.getPrioprity_byte();
      i = i + 1;
      System.arraycopy(submit.getServiceId_byte(), 0, body, i, 10);
      i += 10;
      System.arraycopy(submit.getFeeType_byte(), 0, body, i, 2);
      i += 2;
      System.arraycopy(submit.getFeeCode_byte(), 0, body, i, 6);
      i += 6;
      System.arraycopy(submit.getFixedFee_byte(), 0, body, i, 6);
      i += 6;
      body[i] = submit.getMsgFormat_byte();
      i += 1;
      System.arraycopy(submit.getValidTime_byte(), 0, body, i, 17);
      i += 17;
      System.arraycopy(submit.getAtTime_byte(), 0, body, i, 17);
      i += 17;
      System.arraycopy(submit.getSrcTermId_byte(), 0, body, i, 21);
      i += 21;
      System.arraycopy(submit.getChargeTermId_byte(), 0, body, i, 21);
      i += 21;
      body[i] = submit.getDestTermIdCount_byte();
      i += 1;
      byte[] temp = submit.getDestTermId_byte();
      System.arraycopy(temp, 0, body, i, temp.length);
      i += temp.length;
      body[i] = submit.getMsgLength_byte();
      i += 1;
      temp = submit.getMsgContent_byte();
      System.arraycopy(temp, 0, body, i, temp.length);
      i += temp.length;
      System.arraycopy(submit.getReserve_byte(), 0, body, i, 8);
      head.packageSize = submit.getSubmitLength() + 12;
      head.requestId = 2;
      byte[] msgId = new byte[10];
      multi[num].body = body;
      multi[num].head = head;
      multi[num].msgId = msgId;
    }
    send.submitMulti(multi, con, init.getSubmitRetry(), timeout, deliverQueue,
                     type);
    int[] result = new int[multi.length];
    for (int i = 0; i < multi.length; i++) {
      submitMulti[i].setMsgId(new String(multi[i].msgId));
      result[i] = multi[i].result;
    }
    return result;
  }

  /***********************************************************************
   *
   * active test测试
   *
   */
  public int activeTest(socketStruct socket, initStruct init,
                        deliverVector deliverQueue, int type) {
    return send.activeTest(socket, 1, init.getRequestTimeOut(), deliverQueue,
                           type);
  }

  /*********************************************************************
   * 登录短信网关服务器,并建立active_test线程定时保持连接
   * @param init 系统参数
   * @param
   * @param
   */
  public int login(initStruct init, socketStruct socket) {
    SMGPHeadStruct head = new SMGPHeadStruct();
    //定义消息头
    head.requestId = 1;
    head.packageSize = 42; //4+4+4+8+16+1+4+1

    byte authSource[] = new byte[80]; //实际最大长度40
    byte body[] = new byte[30];
    int bodyLen = 0;
    //byte abyte0[] = new byte[100];
    //byte abyte2[] = new byte[100];
    //byte abyte3[] = new byte[20];
    int errorCode = 0;
    String time = fun.getCurrentTimeStamp();
    byte abyte3[] = new byte[20];
    try {
      //add cpId
      System.arraycopy(init.getCpId().getBytes("GB2312"), 0, body, 0,
                       init.getCpId().length());
      bodyLen += 8; //cpId length

      //计算 authSource
      byte abyte4[] = new byte[10];
      byte abyte5[] = new byte[10];
      byte abyte6[] = new byte[200];
      fun.memset(abyte6, 200);
      byte[] time3 = time.getBytes();

      abyte4 = init.getCpId().getBytes("GB2312");
      abyte5 = init.getCpShareKey().getBytes("GB2312");
      fun.strcpy(abyte6, abyte4, 0, abyte4.length);
      fun.strcpy(abyte6, abyte5, abyte4.length + 7, abyte5.length);
      fun.strcpy(abyte6, time3, abyte4.length + 7 + abyte5.length, time3.length);
      authSource = fun.getMD5(abyte6, abyte4.length + 7 + abyte5.length + 10);
    }
    catch (UnsupportedEncodingException e) {
      System.out.println(e);
    }
    //add authSource
    System.arraycopy(authSource, 0, body, bodyLen, authSource.length);
    bodyLen += 16; //authSource length
    body[bodyLen] = (byte) init.getLoginMode();
    bodyLen += 1; //login Mode length
    //add timstamp
    abyte3 = fun.int2Byte(Integer.parseInt(time));
    body[bodyLen++] = abyte3[0];
    body[bodyLen++] = abyte3[1];
    body[bodyLen++] = abyte3[2];
    body[bodyLen++] = abyte3[3];
    //add version
    body[bodyLen] = (byte) configure.intVersion;
    int result = send.login(head, body, socket, init.getSubmitRetry(),
                            init.getRequestTimeOut());
    if (result != 0) {
      return result;
    }
    DataInputStream dataRec = null;
    try {
      socket.socket.setSoTimeout(configure.SOCKET_SO_TIMEOUT);
      dataRec = new DataInputStream(socket.socket.getInputStream());
    }
    catch (IOException ioexception) {
      System.err.println("Failed I/O: " + ioexception);
      return configure.ERROR_SEND_FAILED;
    }
    catch (Exception e) {}
    for (; ; ) {
      Hashtable hash = send.receive(socket);
      if (hash == null) {
        return configure.ERROR_LOGIN_FAILED;
      }
      head = (SMGPHeadStruct) hash.get("head");
      String r = (String) hash.get("result");
      byte[] msg = (byte[]) hash.get("body");
      if (!"ok".equals(r)) {
        return configure.ERROR_RECEIVE_FAILED;
      }
      if (head.requestId != 0x80000001) {
        continue;
      }
      result = fun.byte2Int(msg, 0, 4);
      if (result != 0) { //登录失败
        return configure.ERROR_LOGIN_FAILED;
      }
      break;
    }
    return result;
  }

  /*****************************************************************
   * 获取至短信网关服务器的socket连接
   *
   * @param init 系统配置结构
   * @return sockStruct 返回一个socket连接
   */
  public socketStruct connectToSMGW(initStruct init) {
    socketStruct con = new socketStruct();
    Socket socket = null;
    try {
      socket = new Socket(init.getServerIp(), init.getServerPort());
      socket.setSoTimeout(configure.SOCKET_RECEIVE_TIMEOUT); //120s
    }
    catch (Exception e) {
      System.out.println(e);
      return null; //error
    }
    con.seq = 1;
    con.socket = socket;
    return con;
  }

  /**********************************************************
   * 解析配置文档
   * @param fileName 配置文件的位置
   * @return initStruct  !=null 初始化正确; ==null 初始化失败
   */
  public initStruct initApi(String fileName) {
    fileName = fun.replace(fileName, "\\", File.separator);
    fileName = fun.replace(fileName, "/", File.separator);
    initStruct init = new initStruct();
    try {
      FileInputStream fileinputstream = new FileInputStream(fileName);
      BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(
          fileinputstream));
      boolean flag = false;
      String param = "";
      while ( (param = bufferedreader.readLine()) != null) {
        param = fun.replace(param.trim(), " ", "");
        if (param.length() == 0) {
          continue;
        }
        if (param.charAt(0) != '#') {
          if (param.indexOf("ServerIP=") == 0) {
            init.setServerIp(param.substring(9).trim());
          }
          else if (param.indexOf("ServerPort=") == 0) {
            init.setServerPort(Integer.parseInt(param.substring(11).trim()));
          }
          else if (param.indexOf("CpShareKey=") == 0) {
            init.setCpShareKey(param.substring(11).trim());
          }
          else if (param.indexOf("CpId=") == 0) {
            init.setCpId(param.substring(5).trim());
          }
          else if (param.indexOf("ClientIP=") == 0) {
            init.setClientIp(param.substring(9).trim());
          }
          else if (param.indexOf("SubmitRetry=") == 0) {
            init.setSubmitRetry(Integer.parseInt(param.substring(12).trim()));
          }
          else if (param.indexOf("RequestTimeout=") == 0) {
            init.setRequestTimeOut(Integer.parseInt(param.substring(15).trim()));
          }
          else if (param.indexOf("ActiveTestCycle=") == 0) {
            init.setActiveTestCycle(Integer.parseInt(param.substring(16).trim()));
          }
          else if (param.indexOf("LoginMode=") == 0) {
            init.setLoginMode(Integer.parseInt(param.substring(10).trim()));
          }
          else if (param.indexOf("ServiceMode=") == 0) {
            init.setServiceMode(Integer.parseInt(param.substring(12).trim()));
          }
        }
      }
      if ("".equals(init.getServerIp())) {
        System.err.println("Can not get ServerIP from file: " + fileName);
        return null;
      }
      if (init.getServerPort() == -1) {
        System.err.println("Can not get ServerPort from file: " + fileName);
        return null;
      }
      /*  if("".equals(init.getCpShareKey()))
        {
           System.err.println("Can not get CpShareKey from file: " + fileName);
            return null;
        }*/
      if ("".equals(init.getCpId())) {
        System.err.println("Can not get CpId from file: " + fileName);
        return null;
      }
      if ("".equals(init.getClientIp())) {
        System.err.println("Can not get ClientIP from file: " + fileName);
        return null;
      }
      if (init.getSubmitRetry() < 0) {
        init.setSubmitRetry(2);
      }
      if (init.getRequestTimeOut() < 0) {
        init.setSubmitRetry(30);
      }
      if (init.getLoginMode() == 2) {
        init.setLoginMode(2);
      }
      else if (init.getLoginMode() == 2) {
        init.setLoginMode(2);
      }
      else {
        init.setLoginMode(0);
      }
      if (init.getActiveTestCycle() < 0) {
        init.setActiveTestCycle(1000);
      }
      fileinputstream.close();
    }
    catch (FileNotFoundException filenotfoundexception) {
      System.err.println("Error: file " + fileName + " not found!");
      return null;
    }
    catch (Exception e) {
      System.out.println("Fail to read file: " + fileName);
      System.out.println(e);
      return null;
    }
    return init;
  }
}

⌨️ 快捷键说明

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