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

📄 simulaterouter.java

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

import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.util.Vector;

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

import com.wireless.sms.pub.entity.MT;
import com.wireless.sms.pub.mq.MTQueue;
//import com.wireless.sms.gwif.test.*;
import com.wireless.gwif.socketconn.*;
import com.wireless.sms.gwif.smsagent.global.GetMoMtGlobalForUsual;

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

    private Client client = null;
    private MTQueue mtqueue = GetMoMtGlobalForUsual.mtqueue;

    public SimulateRouter() {

        initClient();

        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) {
                try {
                    reconfig();

                    testtype = configuration.getString("MT.TestType");
                    int cyclenum = configuration.getInt("MT.CycleNum");
                    cyclecount = 1;

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

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

            }
        };
        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 void reconfig(){
        try{
            File file = new File("./GwifTest.xml");
            if (file.exists()) {
                configuration = new DOM4JConfiguration(file);
            } else {
                JOptionPane.showMessageDialog(null, "当前目录未找到配置文件 GwifTest.xml!", "错误提示",
                    JOptionPane.ERROR_MESSAGE);
                return;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public void initClient(){
        reconfig();

        client = new Client(configuration.getString("MT.MtHost"), Integer.parseInt(configuration.getString("MT.MtPort")));
        client.setIDLE_TIME(15);
        Sender templet = new Sender();
//          templet.setPeriod(500);
        templet.setPeriod(1);
        client.registerConnTemplet(templet);
        client.registerMessageRecognizer( new FixedByteMessageRecognizer() );
        client.start();
    }

    public void sendMT(MT mt){
        mtqueue.add(mt);
    }


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

    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");
        mt.setNeedReply(NeedReply); //16

        String sendTime;
        sendTime = configuration.getString("MT.cmpp.sendTime");
        mt.setMsgLevel(sendTime); //17

        String MsgLevel;
        MsgLevel = configuration.getString("MT.cmpp.MsgLevel");
        mt.setMsgLevel(MsgLevel); //18

        String IsImBalance;
        IsImBalance = configuration.getString("MT.cmpp.IsImBalance");
        mt.setIsImBalance(IsImBalance); //19

        String LinkId;
        LinkId = configuration.getString("MT.cmpp.LinkId");
        mt.setLinkID(LinkId); //20

        int sendNum;
        sendNum = configuration.getInt("MT.cmpp.sendNum");
        mt.setSendNum(sendNum); //21

//        System.out.println("" + mt.toString());
        return mt;
    }

    public MT getun_submit() {
        mt = new MT();

        String msgID;
        msgID = configuration.getString("MT.sgip.msgID");
        mt.setMsgID(msgID); //1

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

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

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

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

⌨️ 快捷键说明

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