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

📄 gwifmtsender.java

📁 中国联通短信通信协议
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.wireless.sms.gwif.test;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Panel;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneLayout;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.DOM4JConfiguration;

import com.wireless.sms.pub.entity.MT;

public class GwifMtSender extends JFrame {
	static final long serialVersionUID=1L; 
    /**配置文件对象**/
    private static JTextArea txt = null;
    private static String testtype = null;
    private static int cyclecount;
    public static Configuration configuration = null;
    public static MT mt;
    public static int WIDTH = 800;
    public static int HEIGHT = 600;

    public GwifMtSender() {

        this.getContentPane().setLayout(new BorderLayout());

        int maxLabelLength = 8;
        String[] labelNames = new String[] {"网络测试说明:", "1 联通网关", "2 电信网关", "3 移动网关", "4 网通网关"};

        Vector labels = new Vector();
        for (int i = 0; i < labelNames.length; i++) {
            String tempLabelName = labelNames[i];

            int j = 0;
            do {
                String innerTempLabelName =
                    tempLabelName.substring(j * maxLabelLength,
                    tempLabelName.length() > (j + 1) * maxLabelLength ? (j + 1) * maxLabelLength : tempLabelName.length());
                JLabel tempLabel = new JLabel(innerTempLabelName.length() >= 6 ? innerTempLabelName :
                                   "  " + innerTempLabelName);
                tempLabel.setFont(new Font("宋体", Font.PLAIN, 12));
                labels.add(tempLabel);

                tempLabelName = tempLabelName.substring(innerTempLabelName.length());
            } while (tempLabelName.length() > 0);
        }

        JButton b1 = new JButton(" SendMT ");
        JButton b2 = new JButton(" Clear ");
        JButton b3 = new JButton("  Exit  ");
        txt = new JTextArea();
        txt.setLineWrap(true);

        ActionListener a1 = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	boolean sendFlag = false;
                try {
                    File file = new File("./conf/GwifTest.xml");
                    if (file.exists()) {
                        configuration = new DOM4JConfiguration(file);
                    } else {
                        JOptionPane.showMessageDialog(null, "当前目录未找到配置文件 GwifTest.xml!", "错误提示",
                            JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                    testtype =configuration.getString("MT.TestType");
                    int cyclenum = 1;
                    cyclecount = 1;

//                    boolean sendFlag = false;

                    for (int i = 0; i < cyclenum; i++) {
                        if (testtype.equalsIgnoreCase("cm")) {
                            sendFlag = sendMT(getcm_submit());
                        } else if (testtype.equalsIgnoreCase("cn")) {
                            sendFlag = sendMT(getcn_submit());
                        } else if (testtype.equalsIgnoreCase("ct")) {
                            sendFlag = sendMT(getct_submit());
                        } else if (testtype.equalsIgnoreCase("un")) {
                            sendFlag = sendMT(getun_submit());
                        }
                    }

                } catch (Exception ex) {
                    txt.setText(txt.getText() + "\n*******************************************\n" +
                        getStackTrace(ex) + "\n*******************************************\n"+
                        "sendFlag: "+sendFlag);
                }

            }
        };
        ActionListener a2 = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                txt.setText("");
            }
        };

        ActionListener a3 = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                dispose();
                System.exit(0);
            }
        };

        Border border = BorderFactory.createEtchedBorder(EtchedBorder.RAISED);
        b1.addActionListener(a1);
        b1.setBorder(border);
        b1.setBackground(new Color(245, 174, 56));
        b2.addActionListener(a2);
        b2.setBorder(border);
        b2.setBackground(new Color(179, 245, 53));
        b3.addActionListener(a3);
        b3.setBorder(border);
        b3.setBackground(new Color(242, 60, 155));

        txt.setBorder(border);

        JScrollPane textPanel = new JScrollPane(txt);
        textPanel.setLayout(new ScrollPaneLayout());
        textPanel.setAutoscrolls(true);

        Panel buttonPanel = new Panel();
        buttonPanel.setLayout(new FlowLayout());
        buttonPanel.setBackground(new Color(137, 196, 248));
        buttonPanel.add(b1);
        buttonPanel.add(b2);
        buttonPanel.add(b3);

        GridBagConstraints gbc = new GridBagConstraints();
        Panel labelPanel = new Panel();
        labelPanel.setLayout(new GridBagLayout());
        labelPanel.setBackground(new Color(137, 196, 248));
        for (int i = 0; i < labels.size(); i++) {
            gbc.gridx = 0;
            gbc.gridy = i;
            gbc.gridwidth = 1;
            gbc.gridheight = 1;
            gbc.weighty = 1;
            gbc.insets = new Insets(0, 5, 0, 5);
            gbc.anchor = GridBagConstraints.CENTER;
            gbc.fill = GridBagConstraints.HORIZONTAL;

            labelPanel.add((JLabel)labels.elementAt(i), gbc);
        }

        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension screenSize = toolkit.getScreenSize();
        int screenSize_x = (int)screenSize.getWidth();
        int screenSize_y = (int)screenSize.getHeight();

        int location_x = (screenSize_x - WIDTH) / 2;
        int location_y = (screenSize_y - HEIGHT) / 2;

        this.getContentPane().setLocation(location_x, location_y);
        this.getContentPane().setLayout(new BorderLayout());
        this.getContentPane().add(buttonPanel, BorderLayout.NORTH);
        this.getContentPane().add(textPanel, BorderLayout.CENTER);
        this.getContentPane().add(labelPanel, BorderLayout.WEST);

        this.setSize(WIDTH, HEIGHT);
        this.setVisible(true);
        this.setTitle("网络连接测试程序");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public boolean sendMT(MT mt) throws Exception{
        Vector mthost = new Vector();
        Vector mtport = new Vector();
        mthost.add(configuration.getString("MT.MtHost"));
        mtport.add(configuration.getString("MT.MtPort"));
//        mthost.add("192.168.2.85");
//        mtport.add("2094");
        boolean thisflag = false;
        for (int i = 0; i < mthost.size() && !thisflag; i++) {
            try {
                String host = (String)mthost.elementAt(i);
                int port = Integer.parseInt((String)mtport.elementAt(i));

                MTClientSocket mtSender = new MTClientSocket(host, "" + port);
                thisflag = mtSender.sendMT(mt);
            } catch (Exception ex) {
                txt.setText(txt.getText() + "\n*******************************************\n" +
                    getStackTrace(ex) + "\n*******************************************\n");
                thisflag = false;
            }
        }

        String name = "发送 " + testtype.toUpperCase() + " 模拟MT " + (cyclecount++) + "次 " +
                      (thisflag ? "成功" : " 失败") + "!";
        String mtmessage = mt.toString();

        txt.setText(txt.getText() + "------------------------------- " + name + "-------------------------------\n" + mtmessage +
            "\n--------------------------------------------------------------\n\n");

        return thisflag;
    }

    public String getStackTrace(Exception e) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        return sw.toString();
    }

    public static void main(String args[]) {
//        GwifMtSender frame = new GwifMtSender();
    	new GwifMtSender();
    }

    public MT getcm_submit() {
        mt = new MT();
        String msgID;
        msgID = configuration.getString("MT.cmpp.msgID");
        mt.setMsgID(msgID); //1

        String GateWayID;
        GateWayID = configuration.getString("MT.cmpp.GateWayID");
        mt.setGatewayID(GateWayID); //2

        String SrcTermID;
        SrcTermID = configuration.getString("MT.cmpp.SrcTermID");
        mt.setSrcTermID(SrcTermID); //3

        String DestTermID;
        DestTermID = configuration.getString("MT.cmpp.DestTermID");
        mt.setDestTermID(DestTermID); //4

        String FeeTermID;
        FeeTermID = configuration.getString("MT.cmpp.FeeTermID");
        mt.setFeeTermID(FeeTermID); //5

        String FeeCode;
        FeeCode = configuration.getString("MT.cmpp.FeeCode");
        mt.setFeeCode(FeeCode); //6

        String FeeType;
        FeeType = configuration.getString("MT.cmpp.FeeType");
        mt.setFeeType(FeeType); //7

        String FeeUserType;
        FeeUserType = configuration.getString("MT.cmpp.FeeUserType");
        mt.setFeeUserType(FeeUserType); //8

        String MtType;
        MtType = configuration.getString("MT.cmpp.MtType");
        mt.setMtType(MtType); //9

        String Spnumber;
        Spnumber = configuration.getString("MT.cmpp.Spnumber");
        mt.setSpnumber(Spnumber); //10

        int SubType;
        SubType = configuration.getInt("MT.cmpp.SubType");
        mt.setSubType(SubType); //11

        String MsgContent;
        MsgContent = configuration.getString("MT.cmpp.MsgContent");
        mt.setMsgContent(MsgContent); //12
        mt.setMsgLen("" + MsgContent.length()); //13

        String MsgFormat;
        MsgFormat = configuration.getString("MT.cmpp.MsgFormat");
        mt.setMsgFormat(MsgFormat); //14

        String ServiceCode;
        ServiceCode = configuration.getString("MT.cmpp.ServiceCode");
        mt.setServiceCode(ServiceCode); //15

        String NeedReply;
        NeedReply = configuration.getString("MT.cmpp.NeedReply");

⌨️ 快捷键说明

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