-2

実際には、sourceno、destinationno、ttl などのユーザーからの入力を取得するスイング パネルを作成し、そのデータに基づいて、純粋にランダムな伝播アルゴリズム (PRP) を適用して、ネットワーク内を通過するルーティング ノードを見つけます。以下のコード (PRP.java) は、swing パネルで送信ボタンを押すと、コマンド プロンプトでトラバースされたルーティング ノードを出力します。

送信ボタンを押すと、結果の通過ルートがコマンドプロンプトではなく、送信ボタンの下の同じパネルのテキストボックスに出力されるように、コードを変更する必要があります

変更されたコードを提案してください.....

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;

public class PRP extends JFrame
{


JTextField srcno;
JTextField destno;
JTextField ttl;
JTextField datapayload;
JLabel srcnode;
JLabel destnode;
JLabel TTL;
JLabel Datapayload;
JButton submit;
JTextArea textFieldName;


  public PRP()
  {

    PRPLayout customLayout = new PRPLayout();

    getContentPane().setFont(new Font("Helvetica", Font.PLAIN, 12));
    getContentPane().setLayout(customLayout);

    srcnode = new JLabel("Enter source node:");
    getContentPane().add(srcnode);

    srcno = new JTextField("number");
    getContentPane().add(srcno);

    destnode = new JLabel("Enter destination node:");
    getContentPane().add(destnode);

    destno = new JTextField("number");
    getContentPane().add(destno);

    TTL = new JLabel("Enter TTL:");
    getContentPane().add(TTL);

    ttl = new JTextField("number");
    getContentPane().add(ttl);

     Datapayload = new JLabel("Enter DataPayload:");
    getContentPane().add(Datapayload);

    datapayload = new JTextField("abcd...");
    getContentPane().add(datapayload);

    submit = new JButton("submit");
    getContentPane().add(submit);

    textFieldName = new JTextArea("PRP ROUTING:",80,10);
    textFieldName.setEditable( false );
    JScrollPane scroll = new JScrollPane(textFieldName) ;
    scroll.setBounds( 10, 60, 225, 150 );
    getContentPane().add( scroll );

    submit.addActionListener(new ActionListener()
                                         {
                                            public void actionPerformed(ActionEvent e) 
                                            {

                                              String[]  prpdata = new String[3];
                                              prpdata[0]=srcno.getText();
                                              prpdata[1]=destno.getText();
                                              prpdata[2]=ttl.getText();
                                              try{ 
                                              PRP pr = new PRP();
                                              pr.PRPRoute(prpdata); }
                                              catch(Exception ez){ textFieldName.append("exception in caller method"); }


                                             }
                                           });

    setSize(getPreferredSize());

    }

    public void PRPRoute(String argus[]) throws ClassNotFoundException,SQLException
    {

     String url="jdbc:oracle:thin:@localhost:1521:xe";
     String u="proj";
     String p="bade";
     String src=argus[0];
     String dest=argus[1];
     int ttl=Integer.parseInt(argus[2]);
     Class.forName("oracle.jdbc.driver.OracleDriver");
     Connection con=DriverManager.getConnection(url,u,p);
     if(con==null)
     {
       textFieldName.append("not connected to oracle");
       return;
      }

     textFieldName.append("connected to oracle");

     String query= "select distinct RFrom,RTo from router where RFrom='"+src+"'"+"or "+ "RTo='"+src+"'";

     Statement st=con.createStatement();
     ResultSet rs=st.executeQuery(query);

     ResultSetMetaData rsmd = rs.getMetaData();

     int j=-1;

     String[]  randata = new String[30];
     textFieldName.append( "\n\t"+"Neighbors of " + src+"::");  

     while( rs.next() )
     {  
       j++;
       for (int i = 1; i <= rsmd.getColumnCount(); i++)
      {

        String ss=rs.getString( i );
        if(!(ss.equals(src)))
        {
           textFieldName.append( "\t" + ss );
           randata[j]=ss ;  
        }
      }

      textFieldName.append( "\n\n" );   
     }
     int k=0;
     textFieldName.append("\n"+"Checking for 1-hop neighbours. . . .");

     for(int i=0;i<=j;i++)
     {
        if(randata[i].equals(dest))
        textFieldName.append("1-hop neighbour found");
        textFieldName.append( "\t" + "Routing followed:"+src+"--->" +randata[i]);
        textFieldName.append("\n\t successfully routed!");
        k=i;
        break;
      }
   }

   if(!(randata[k].equals(dest)))
   {
    textFieldName.append("Choosen dispersive PRP strategy!!!");
    textFieldName.append( "\t" + "Routing followed:"+src+"--->" );
    Random r=new Random();
    if(ttl<=0) { 
              textFieldName.append("MINHOP ROUTING STARTED");

           }
     else {
          ttl--;
          String next=randata[r.nextInt(j)];
          textFieldName.append(next);
          if(next.equals(dest)) { textFieldName.append("\n\t successfully routed!"); }
          else{
               String[] indata = new String[3];
               indata[0]=next;
               indata[1]=dest;
               indata[2]=Integer.toString(ttl);
               try{ PRPRouteout pr=new PRPRouteout(indata); }
               catch(Exception ezl){ textFieldName.append("exception in PRPRouteut caller method"); }


             }
         }
     }


    }




 public static void main(String args[])
 {
    PRP window = new PRP();

    window.setTitle("PRP");
    window.pack();
    window.show();
 }
}

class PRPLayout implements LayoutManager
{
  public PRPLayout() {  }
  public void addLayoutComponent(String name, Component comp) {   }
  public void removeLayoutComponent(Component comp) {  }
  public Dimension preferredLayoutSize(Container parent)
 {
    Dimension dim = new Dimension(0, 0);
    Insets insets = parent.getInsets();
    dim.width = 320 + insets.left + insets.right;
    dim.height = 240 + insets.top + insets.bottom;
     return dim;
 }

 public Dimension minimumLayoutSize(Container parent)
 {
    Dimension dim = new Dimension(0, 0);
    return dim;
 }
 public void layoutContainer(Container parent) 
 {
    Insets insets = parent.getInsets();
    Component c;
    c = parent.getComponent(0);
    if (c.isVisible()) {c.setBounds(insets.left+90,insets.top+8,172,26);}
    c = parent.getComponent(1);
    if (c.isVisible()) {c.setBounds(insets.left+230,insets.top+8,172,26);}
    c = parent.getComponent(2);
    if (c.isVisible()) {c.setBounds(insets.left+90,insets.top+40,172,26);}
    c = parent.getComponent(3);
    if (c.isVisible()) {c.setBounds(insets.left+230,insets.top+40,172,26);}
    c = parent.getComponent(4);
    if (c.isVisible()) {c.setBounds(insets.left+90,insets.top+72,172,26);}
    c = parent.getComponent(5);
    if (c.isVisible()) {c.setBounds(insets.left+230,insets.top+72,172,26);}
    c = parent.getComponent(6);
    if (c.isVisible()) {c.setBounds(insets.left+90,insets.top+112,172,26);}
    c = parent.getComponent(7);
    if (c.isVisible()) {c.setBounds(insets.left+230,insets.top+112,172,26);}
    c = parent.getComponent(8);
    if (c.isVisible()) {c.setBounds(insets.left+230,insets.top+152,142,26);}
    c = parent.getComponent(9);
    if (c.isVisible()) {c.setBounds(insets.left+90,insets.top+200,472,268);}
 }
}
4

2 に答える 2

2

あなたの質問を正しく理解できたら、これを試してください:

textFieldName.setText();の代わりに使用System.out.println();

于 2013-03-30T11:11:43.730 に答える
0

私はあなたのコードについて知りません。しかし、Ping または trace route を使用して結果を .txt ファイルに取り込む方法は知っています。ping 192.168.1.1 -t > filename.txt のようにコマンドを記述します。

これも使用するかどうかに注意してください。ping の間を遅らせ、それぞれの時間を記録するには、スクリプトを実行できます。

@ECHO OFF :LOOPSTART time /T ping xxx.xxx.xxx.xxx -n 4 >> filename.txt ping yyy.yyy.yyy.yyy -n 4 >> filename.txt sleep -m 3000 GOTO LOOPSTART

ルートディレクトリにファイルを作成します。コマンドを終了するまで、すべての結果が出力されます。

于 2013-03-30T12:41:05.873 に答える