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

📄 smgpserver.java

📁 本人为友邦公司做的模拟网关源代码
💻 JAVA
字号:
package smgpgw;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.*;
import java.beans.*;
import java.io.*;
import java.net.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: </p>
 * @author unascribed
 * @version 1.0
 */

public class SmgpServer
    implements Runnable {
  private static SmgpServer server = null;
  private DataOutputStream out = null;
  private DataInputStream in = null;
  public int loginMode = 0;

  private static DataOutputStream m_Deliverout = null;

  private static short msgId = 1;
  public boolean stopServer = false;
  public static Hashtable conHT = new Hashtable(10,10);
  public static Vector conVec = new Vector(10);

  private static int m_reportCount = 0;

  public static String submitResp_msg_id = "";

  private SmgpServer(Socket socket) {
    try {
      in = new DataInputStream(socket.getInputStream());
      out = new DataOutputStream(socket.getOutputStream());
      m_Deliverout = out;
    }
    catch (IOException e) {
      e.printStackTrace();
    }
  }

  public static void startSmgpServer(Socket socket) {



      server = new SmgpServer(socket);
      Thread MOServer = new Thread(server);
      MOServer.start();
      conVec.add(server);

  }

  public static SmgpServer instance() {
    Vector tmpVec = null;
    if((tmpVec = (Vector)conHT.get((new Integer(0))))!=null){
      return (SmgpServer)tmpVec.get(0);
    }else{
      tmpVec = (Vector)conHT.get((new Integer(2)));
      return (SmgpServer)tmpVec.get(0);
    }

  }

  public static void sendDeliverReport(SmgpData smgp) {
    if (Constants.DISPLAY_BINARY) {
      SMSCFrame.SendArea.append(Constants.byteToHexString(smgp.getPackage()));
      SMSCFrame.SendArea.append("\n");
    }
    Smgp.getDeliverCommand(m_Deliverout, smgp);
    if (Constants.DISPLAY_MSG) {
      /**
       * modified by me(yzx) at 2004-03-03:修正原来笼统的转换
       */
      String temp = null;
      try {
        if (smgp.msgMsgFormat == 15) {
          temp = new String(smgp.msgMsgContent.getBytes("gb2312"));
        }
        else if (smgp.msgMsgFormat == 8) {
          temp = new String(smgp.msgMsgContent.getBytes("UTF-16BE"));
          temp = new String(smgp.msgMsgContent.getBytes("UTF-16BE"), "gb2312");
        }
        else {
          temp = smgp.msgMsgContent;
        }
      }
      catch (java.io.UnsupportedEncodingException e) {
        e.printStackTrace();
      }

      SMSCFrame.SendArea.append("发送消息 to " + smgp.msgDestTermIDArr[0] + " :"
                                + temp + "\n");
    }

  }

  public void sendDeliver(SmgpData smgp) {
    //System.out.println("come in sendDeliver");
    if (Constants.DISPLAY_BINARY) {
      SMSCFrame.SendArea.append(Constants.byteToHexString(smgp.getPackage()));
      SMSCFrame.SendArea.append("\n");
    }

    Smgp.getDeliverCommand(out, smgp);

    if (Constants.DISPLAY_MSG) {
      SMSCFrame.SendArea.append("发送消息 to " + smgp.msgDestTermIDArr[0] + " :"
                                + smgp.msgMsgContent + "\n");
    }
    //System.out.println("go out sendDeliver");
  }

  public void run() {
    while (!stopServer) {
      final byte[] receivedData = receiver();

      if (1 == receivedData[0]) {
        stopServer = true;
        server = null;
        return;
      }

      SmgpData smgp = Smgp.parseMessage(receivedData);

      switch (smgp.headCmdID) {
        case Smgp.SMGP_submit:

          SmgpData submitResp = new SmgpData();

          if (Constants.DISPLAY_BINARY) {
            SMSCFrame.RecvArea.append(Constants.byteToHexString(submitResp.
                getPackage()));
            SMSCFrame.RecvArea.append("\n");
          }

          submitResp.headCmdID = Smgp.SMGP_submit_resp;
          submitResp.headSeqcNo = smgp.headSeqcNo;
          submitResp.MsgID = getMsgID();
          submitResp_msg_id = submitResp.MsgID;
          submitResp.status = smgp.status;

          Smgp.getCommand(out, submitResp); //发送命令

          if (Constants.DISPLAY_MSG) {
            SMSCFrame.RecvArea.append("\n接收消息 from " + smgp.msgSrcTermID + " :" +
                                      smgp.msgMsgContent + "\n" + "目的手机=" +
                                      smgp.msgDestTermID + "\n" +
                                      "Src_terminal_id=" +
                                      smgp.msgSrcTermID);
          }
//          System.out.println(smgp.msgNeedReport);
          if (smgp.msgNeedReport == 1) {
            SendDataThread.SendReport(smgp.msgDestTermID,
                                      submitResp.MsgID);
            m_reportCount++;
            System.err.println("发送状态报告条数=" + m_reportCount);
          }

          break;

        case Smgp.SMGP_login:


            Integer tmpInt = new Integer((int)smgp.LoginMode);
            Vector tmpVec = (Vector)conHT.get(tmpInt);
            if(tmpVec!=null){
              tmpVec.add(this);
            }else{
              tmpVec = new Vector(10);
              tmpVec.add(this);
              conHT.put(tmpInt,tmpVec);
            }


          SmgpData connectResp = new SmgpData();
          connectResp.headCmdID = Smgp.SMGP_login_resp;
          connectResp.headSeqcNo = smgp.headSeqcNo;
//          System.out.println("login status : " + smgp.status);
          connectResp.AuthenticatorServer = new String(Constants.
              generateRespAuthCode( (byte) smgp.status, Constants.publicKey,
                                   smgp.AuthenticatorClient.getBytes()));
//          System.out.println("login AuthenticatorServer : " + connectResp.AuthenticatorServer.length());
          Smgp.getCommand(out, connectResp);
          break;

        case Smgp.SMGP_active_test:
          smgp.headCmdID = Smgp.SMGP_active_test_resp;
          Smgp.getCommand(out, smgp);
          break;
        default:
//          System.out.println("command error");

      }

//			try {
//				Thread.sleep(100);
//			}
//			catch (InterruptedException ex) {
//				}
    }

  }

  private synchronized byte[] receiver() {
    byte[] receivedData;
    try {
      int dataSize = in.readInt();
      receivedData = new byte[dataSize - 4]; //note:byte 不包含消息包长度这四个字节
      int dataTotalSizeToRead = dataSize - 4;
      int dataToReadLeft = dataSize - 4;
      int dataThisTimeRead;
      while (dataToReadLeft > 0) {
        dataThisTimeRead = in.read(receivedData,
                                   dataTotalSizeToRead - dataToReadLeft,
                                   dataToReadLeft);
        dataToReadLeft -= dataThisTimeRead;
      }

    }
    catch (InterruptedIOException e) {
      System.out.println("InterruptedIOException exception");
      e.printStackTrace();
      Integer tmpInt = new Integer( (int)this.loginMode);
      Vector tmpVec = (Vector) conHT.get(tmpInt);
      if (tmpVec != null && tmpVec.size() > 0) {
        tmpVec.remove(this);
      }

      receivedData = new byte[1];
      receivedData[0] = 0;
    }
    catch (IOException e) {
      System.out.println("IOException exception");
      e.printStackTrace();
      Integer tmpInt = new Integer( (int)this.loginMode);
      Vector tmpVec = (Vector) conHT.get(tmpInt);
      if (tmpVec != null && tmpVec.size() > 0) {
        tmpVec.remove(this);
      }

      receivedData = new byte[1];
      receivedData[0] = 1;
    }
    catch (Throwable ex) {
      System.out.println("Throwable exception");
      ex.printStackTrace();
      Integer tmpInt = new Integer( (int)this.loginMode);
      Vector tmpVec = (Vector) conHT.get(tmpInt);
      if (tmpVec != null && tmpVec.size() > 0) {
        tmpVec.remove(this);
      }

      receivedData = new byte[1];
      receivedData[0] = 2;
    }

    return receivedData;
  }

  public static synchronized String getSeq() {
    if (msgId == 9999) {
      msgId = 1;
    }else
      msgId ++;
    String temp = String.valueOf(msgId);
    int count = 4-temp.length();
    for(int i=0;i<count;i++){
      temp = "0" + temp;
    }
    return temp;
  }

  public static String GetTime() {
    StringBuffer buf;
    Date date;
    int temp;
    String s;
    date = new Date();
    buf = new StringBuffer();
    temp = date.getHours();
    if (temp < 10)
      buf.append("0").append(temp);
    else
      buf.append(temp);
    temp = date.getMinutes();
    if (temp < 10)
      buf.append("0").append(temp);
    else
      buf.append(temp);
    temp = date.getSeconds();
    if (temp < 10)
      buf.append("0").append(temp);
    else
      buf.append(temp);
    return buf.toString();
  }

  public static String getMsgID() {
    byte[] megid = new byte[10];
    byte[] temp;
    String strTime = GetTime();
    temp = strTime.getBytes();
    int i = 0;
    for (i = 0; i < temp.length; i++) {
      megid[i] = temp[i];
    }
    String tempStr = getSeq();
    temp = tempStr.getBytes();
    for (int j = 0; j < temp.length; j++) {
      megid[i + j] = temp[j];
    }
    return new String(megid);
  }

  protected static byte[] intToBytes(int i) {
    try {
      ByteArrayOutputStream byteArrayos = new ByteArrayOutputStream();
      DataOutputStream dataOutStream = new DataOutputStream(byteArrayos);
      dataOutStream.writeInt(i);
      return byteArrayos.toByteArray();
    }
    catch (Exception e) {
      return null;
    }
  }

  public boolean isStopServer() {
    return stopServer;
  }

  public void stopServer() {
    this.stopServer = true;
    try {
      Thread.sleep(100);
    }
    catch (InterruptedException ex) {
    }
    server = null;
  }

}

⌨️ 快捷键说明

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