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

📄 cncmain.java~1~

📁 cnc网通小灵通网关接口源码
💻 JAVA~1~
字号:
package cncgw;/** * <p>Title: 小灵通网关</p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2004</p> * <p>Company: yy</p> * @author lxf * @version 1.0 */import java.io.*;import java.net.*;import java.util.*;import org.apache.log4j.*;import cncgw.xsms.*;import cncgw.log.*;import cncgw.parser.*;import cncgw.smdb.*;public class cncMain {  public cncMain()  {    init();  }  static cncgw.log.Logger log = null;  static xsms xsms = new xsms();  static conn_desc conn = new conn_desc();  static PrintStream feelog = null;  static PrintStream errorlog = null;  public static Properties props = new Properties();  public static Vector threads = new Vector();  public static Vector submits = new Vector();  public static Vector delivers = new Vector();  public static Vector messages = new Vector();  public static PoolManager poolmanager;  static int timeout = 0;  static int workers = 5;  static String ismghost = "";  static int ismgport = 0;  public static String icpid = "";  public static String icpphone = "";  static String icpauth = "";  static String system_id = "";  static String system_type = "";  static String interface_version = "10";  static byte ton = 0;  static byte npi = 0;  static String address_range = "";  //db配置  static xsmse_deliver_result cdold=new xsmse_deliver_result();    /* print to stdout */  public static void p(String s) {    System.out.println(s);  }  public static void init()  {  }  public static void feelog(String s) {    LoggerManager.getFeeLogger().info(s);  }  public static void errorlog(String s) {    LoggerManager.getErrorLogger().error(s);  }  /* add by simon zhen on 24/07/02 to process telephone number */  /* convert a string to byte array,add a zero indicate end of string */  public static byte[] str2byte(String s,int maxlen) {    byte[] temp = s.getBytes();    int strlen = s.length();    if (strlen >= maxlen) strlen = maxlen-1; //more than maxlen will be cut down    byte[] result = new byte[strlen +1];    for (int i=0;i<strlen;i++) //move source to dest      result[i]=temp[i];    result[strlen] = 0; //add zero to end    return result;  }  /* Convert ucs2 to ascii */  public static String ucs2ascii(byte[] b){    byte[] buffer = new byte[b.length];    int j = 0;    for (int i=0;i<b.length;i++){      if (b[i]!=0){        if ( (b[i]>='0'&&b[i]<='9') ||             (b[i]>='a'&&b[i]<='z') ||             (b[i]>='A'&&b[i]<='Z') ||             (b[i]=='.')){      buffer[j++] = b[i];    }else{      buffer[j++] = ' ';    }      }    }    return new String(buffer).trim();  }  /* Split input string to string array according to cutLenth */  public static String[] split(String msg,int cutLength){    String[] ret = null;    String encoding = "UTF-16";    try {      int msgLength = msg.length();      byte[] b = msg.getBytes(encoding);      int orgLength = b.length;      int num =(int) Math.ceil((double) orgLength / (double) cutLength);      int uniLength = orgLength / num;      int remind = uniLength % 2;      if (remind!=0) uniLength++;      ret = new String[num];      byte[] temp = new byte[uniLength];      for (int i=0;i<num;i++){        for (int j=0;j<uniLength;j++){          if( (i * uniLength +j)< orgLength)            temp[j] = b[i * uniLength + j];        }        ret[i] = new String(temp,encoding);      }    }    catch( Exception e){    }    return ret;  }    /* load www-server.properties from java.home */  static void loadProps() throws IOException {    File f = new File    (System.getProperty("user.dir")+File.separator+    "conf"+File.separator+"ChinaCnc.properties");    if (f.exists()) {      InputStream is =new BufferedInputStream(new          FileInputStream(f));      props.load(is);      is.close();      String r = props.getProperty("port");      if (r != null) {        port = Integer.parseInt(r);      }      r = props.getProperty("timeout");      if (r != null) {        timeout = Integer.parseInt(r);      }      r = props.getProperty("workers");      if (r != null) {        workers = Integer.parseInt(r);      }      r = props.getProperty("ismghost");      if (r != null) {        ismghost = r;      }      r = props.getProperty("ismgport");      if (r != null) {        ismgport = Integer.parseInt(r);      }      r = props.getProperty("icpid");      if (r != null) {        icpid = r;      }      r = props.getProperty("icpphone");      if (r != null) {        icpphone = r;      }      r = props.getProperty("system_id");      if (r != null) {        system_id = r;      }      r = props.getProperty("interface_version");      if (r != null) {        interface_version = r;      }      r = props.getProperty("icpauth");      if (r != null) {        icpauth = r;      }      r = props.getProperty("driver");      if (r != null) {        PoolManager.driver = r;      }      r = props.getProperty("dburl");      if (r != null) {        PoolManager.url = r;      }      r = props.getProperty("dbuser");      if (r != null) {        PoolManager.user = r;      }      r = props.getProperty("dbpassword");      if (r != null) {        PoolManager.password = r;      }      r = props.getProperty("dbinitconn");      if (r != null) {        PoolManager.initconn = Integer.parseInt(r);      }      r = props.getProperty("dbmaxconns");      if (r != null) {              PoolManager.maxconns = Integer.parseInt(r);      }      r = props.getProperty("logintimeout");      if (r != null) {        PoolManager.logintimeout  = Integer.parseInt(r);      }    }        /* if no properties were specified, choose defaults */    if (feelog == null) {      p("记录日志到控制台");      feelog = System.out;    }    if (ismghost==null || ismghost.length()==0){      ismghost = "127.0.0.1";    }    if (icpid==null || icpid.length()==0){      icpid = "?????";    }    if (icpphone==null || icpphone.length()==0){      icpphone = "??????";    }    if (system_id==null || system_id.length()==0){      system_id = "310";    }    if (interface_version==null || interface_version.length()==0){      interface_version = "10";    }    if (icpauth==null || icpauth.length()==0){      icpauth = "?????";    }        poolmanager = PoolManager.getInstance();  }  static void printProps() {    p("网络连接超时设定="+timeout);    p("移动短信网关地址/域名="+ismghost);    p("移动短信网关端口="+ismgport);    p("服务提供商经营许可证编号="+icpid);    p("服务提供商特服号="+icpphone);    p("接口id="+system_id);    p("接口版本号="+interface_version);    p("数据库地址="+PoolManager.url);    p("数据库用户="+PoolManager.user);    p("数据库初始连接数="+PoolManager.initconn);    p("数据库最大连接数="+PoolManager.maxconns);  }  public static void main(String[] a) throws Exception {    loadProps();    printProps();    Connect();  //连接小灵通短信网关    System.out.println("绑定连接启动!");    ActiveTest At = new ActiveTest();    new Thread(At).start();  /* start connetion active test */    System.out.println("与小灵通??网关心跳线程启动!");    Deliver deliverToDb = new Deliver();    new Thread(deliverToDb).start(); /* start process deliver request */    System.out.println("从小灵通??网关接受消息线程启动!");    /* start put submit request from cnc_submit threads */    SubmitFromDb pp = new SubmitFromDb();    new Thread(pp).start();    System.out.println("向小灵通??网关发送消息线程启动!");//        SubmitDb submitdb = new SubmitDb();//        DeliverDb deliverdb = new DeliverDb();//        submitdb.updateHaveSent(2);//        Vector aaa= submitdb.queryResults(4);//        System.out.println("aaa.size:"+aaa.size());////        System.out.println("aaa--id-"+((Vector)aaa.get(0)).get(0)+" flag "+((Vector)aaa.get(0)).get(8));//        System.out.println("aaa--id-"+((Vector)aaa.get(1)).get(0)+" flag "+((Vector)aaa.get(1)).get(8));//        System.out.println("aaa--id-"+((Vector)aaa.get(2)).get(0)+" flag "+((Vector)aaa.get(2)).get(8));//        System.out.println("aaa--id-"+((Vector)aaa.get(3)).get(0)+" flag "+((Vector)aaa.get(3)).get(8));////        deliverdb.insertDBByID("13935180907","accept info","0");//        deliverdb.insertDBByID("13935180907","收到 消息!","0");//        System.out.println("end");  }  public static void Connect()  {    try    {      Date date = new Date();      int timestamp = (int)(date.getTime()/1000);      int bindtype = 2;      int version = 0x12;      xsms.xsms_connect_to_ismg(ismghost, ismgport, conn);      xsms.xsms_login(conn,icpid, icpphone,ismghost,icpauth, bindtype, version, timestamp);    }    catch (Exception e)    {      errorlog("连接小灵通短信网关:"+e);    }  }  public void destroy()  {    try    {      xsms.xsms_logout(conn);    }    catch (Exception e)    {      errorlog("退出系统:"+e);    }  }}/*与移动发送接受测试连接全部完成*/class ActiveTest extends cncMain implements Runnable{  ActiveTest() {}    SubmitDb submitdb = new SubmitDb();  public synchronized void run()  {    Thread thisThread = Thread.currentThread();    xsmse_submit submit = null;    while(true)    {      try      {        thisThread.sleep(500);        Vector pool = cncMain.messages;        synchronized (pool) {//for(15)  int pool.size纪录数          if (!pool.isEmpty()) {//重发判断怎么处理???            submit = (xsmse_submit) pool.elementAt(0);            pool.removeElementAt(0); //remove message            int seq = xsms.xsms_submit(conn, submit);          }          else {            xsms.xsms_active_test(conn);//同时设纪录数为1          }        }//        if(readPa(conn)==0)//while(记录数>0){收,纪录数--}//          feelog("目的手机="+submit.get_dst_addr()//                 +"内容="+submit.get_short_msg());      }      catch (Exception e)      {        errorlog("连接测试:"+e);        Connect();      }    }  }  private int readPa(conn_desc con) throws Exception//返回0是指发送成功  {    xsmse_result cr = null;    Date date = new Date();    int statuscode = 1;    cr = xsms.readResPack(conn);    switch(cr.pack_id)    {      case -2147483648:        p("get nack pack");        break;      case -2147483647:        xsmse_login_result cl = (xsmse_login_result)cr;        p(date.toString()+"登录----login resp----: STAT = " + ((xsmse_result) (cl)).stat);        statuscode=1;        break;      case -2147483646:        p("退出------------logout resp----------: STAT = " + cr.stat);        break;      case -2147483644:        xsmse_submit_result sr = (xsmse_submit_result)cr;        p(date.toString()+"发送------------submit resp----------: STAT = " + ((xsmse_result) (sr)).stat + " SEQ = " + sr.seq);        if(((xsmse_result) (sr)).stat==0){          statuscode = 0;          submitdb.updateHaveSent(sr.seq);        }        break;      case 5: // '\005'        p(date.toString()+"接收------------deliver---------: STAT = 0");        xsmse_deliver_result cd = (xsmse_deliver_result)cr;        Vector pool = cncMain.delivers;        synchronized (pool) {          if(! new String(cd.src_addr).equals(new String(cdold.src_addr))             || ! new String(cd.short_msg) .equals(new String(cdold.short_msg))) {            cdold = cd;            pool.addElement(cd);          }        }        xsms.xsms_send_deliver_resp(conn, cd.seq, ((xsmse_result) (cd)).stat);//返给移动deliver_resp        break;      case -2147483642:        xsmse_query_result qr = (xsmse_query_result)cr;        p("查询------------query-----------::");        //qr.printAllField();        break;      case -2147483641:        p(new Date().toString() + "取消---------cancel-----------: STAT = " + cr.stat);        break;      case -2147483640:        if(cr.stat!=0)          p(new Date().toString() + "连接响应---------active resp-----------: STAT " + cr.stat);        break;    }    return statuscode;  }}/*从移动接收到的消息放入cnc_deliver库*/class Deliver extends cncMain implements Runnable{  Deliver() {}  DeliverDb deliverdb = new DeliverDb();  public synchronized void run()  {    Vector pool = cncMain.delivers;    Thread thisThread = Thread.currentThread();    int deliverlength = 0;    String src_addr = "";    while(true)    {      try      {        thisThread.sleep(500);        synchronized (pool) {          if (!pool.isEmpty()) {            xsmse_deliver_result cd = (xsmse_deliver_result) pool.elementAt(0);            deliverlength = cd.sm_len;            src_addr = new String(cd.src_addr).trim();            byte[] newshort_msg = new byte[cd.sm_len];            System.out.println("smlen:"+deliverlength);            for(int i=0;i<deliverlength;i++)            {              newshort_msg[i]=cd.short_msg[i];            }            String short_msg="";            if (cd.data_coding==0){              short_msg=new String(newshort_msg,"GBK").trim();            }else{              short_msg = new String(newshort_msg,"UTF-16BE").trim();            }            byte encoding = cd.data_coding; //编码方式            feelog("[消息]用户定制 手机号码=" + src_addr                   + "编码=" + encoding );            deliverdb.insertDBByID(src_addr,short_msg,"0");            pool.removeElementAt(0); //remove message when success          }        }      }      catch (Exception e)      {        pool.removeElementAt(0);        errorlog("提交数据库:"+e+deliverlength);      }    }  }}/*将cnc_submit库消息放入发送队列*/class SubmitFromDb extends cncMain implements Runnable{  SubmitFromDb() {}  SubmitDb submitdb = new SubmitDb();  public synchronized void run()  {    Thread thisThread = Thread.currentThread();    Vector submitsVec = new Vector(10);    cncParser parser = new cncParser();    while(true)    {      try      {        thisThread.sleep(500);        Vector pool = cncMain.messages;        System.out.println("messages.size-"+pool.size());        if (5-pool.size()>0) {//重发判断怎么处理???          submitsVec = submitdb.queryResults(5-pool.size());          for(int i = 0;i<submitsVec.size();i++){            if (parser.getSubmit((Vector)submitsVec.elementAt(i))){              System.out.println("from submitdb success"+i);            }            else{              System.out.println("from submitdb fail"+i);            }          }        }      }      catch (Exception e)      {        errorlog("取数据库测试:"+e);      }    }  }}

⌨️ 快捷键说明

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