开发者

java Swing Application Freezes when call to jpcapCaptor.openDevice method

I am developing some bandwidth utilization monitor tool using jpcap. what am i doing:-

1. i created a radio buttons panel containing the list of all the network interfaces that is present on system, and user has to choose one of them.

2. a jfreechart panel (dynamic) that will show the real time graph of bandwidth utilization when user clicks GO! button.

problem:-

i have added following in ActionListener in GO! button

try{captor = JpcapCaptor.openDevice(devices[selecteddevice], 65535,true, 20);}catch(Exception e){}
timer.start();
    captor.loopPacket(-1,new PacketPrinter());

so when i run the program GUI comes with radiobutton panel and jfreechart panel but when i select an option and press GO! application freezes and chart panel does not show any dynamic updation.

when i commented out the JpcapCaptor.openDevice(devices[selecteddevice], 65535,true, 20); then when i press GO! button, everything works,for example timer starts and chart panel is being updated.(but as captor is null so it is not capturing any data)

Please Help me!!!

my system is ubuntu 10.04, jpcap 0.7

my code is as follows:-

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.DynamicTimeSeriesCollection;
import org.jfree.data.time.Second;
import org.jfree.data.xy.XYDataset;
import java.util.List;
import java.util.ArrayList;
import jpcap.*;
import jpcap.packet.*;
import java.util.*;
import java.awt.event.*;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.*;

public class Sniffer extends JPanel
{
    private static jpcap.NetworkInterfac开发者_StackOverflow中文版e[] devices;
    private static int selecteddevice=-1;
    JFrame frame;
    public static JpcapCaptor captor;
    private static final String TITLE="Bandwidth Utilization Meter";;
public static List<Packet> packets;
public static Timer timer;
   public static ArrayList<JRadioButton> radioButtonArray = new ArrayList<JRadioButton>();
private ButtonGroup group= new ButtonGroup();
    public static JButton  go;
    JFreeChart chart;
    static DynamicTimeSeriesCollection dataset;
    public Sniffer()
    {

         packets = new ArrayList<Packet>();
         dataset =new DynamicTimeSeriesCollection(1,120, new Second());
         dataset.setTimeBase(new Second(0, 0, 0, 2, 1, 2011));
         dataset.addSeries(new float[0], 0, "PPP0 Bandwidth Utilization Meter");
         chart = createChart(dataset); 
         getDevices();

        timer = new Timer(1000,new ActionListener(){
            public void actionPerformed(ActionEvent e)

            {    long tlen=0;
                List<Packet> temp = new ArrayList<Packet>(packets);
                packets.clear();
                for(Packet i : temp)
                {
                    tlen+=i.len;
                }
                float[] newData = new float[1];
                newData[0]=(float)tlen/1024;
                dataset.advanceTime();
                dataset.appendData(newData);
                }});
         setGUI();



    }


    void setGUI()
    {
        setLayout(new BorderLayout());
        frame = new JFrame();
        JPanel panel = new JPanel(new GridLayout(devices.length, 1));
        for (JRadioButton combo : radioButtonArray) 
        {
            panel.add(combo);
        }
        JScrollPane scrollPane = new JScrollPane(panel);
        go= new JButton("GO!");
            go.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent ee)
                    {
   //problem starts here.............     
                        try{captor = JpcapCaptor.openDevice(devices[selecteddevice], 65535,true, 20);}catch(Exception e){}
                        timer.start();
                        captor.loopPacket(-1,new PacketPrinter());
     //.....................................


                    }
            }
            );

            go.setEnabled(false);    

            panel.add(go);
            add(scrollPane, BorderLayout.CENTER);
            scrollPane.setSize(300,300);
            JFrame.setDefaultLookAndFeelDecorated(true);
            frame.setLayout(new GridLayout(2, 0));
            frame.add(scrollPane);
            frame.add(new ChartPanel(chart));
            frame.setSize(1024, 768);
          frame.setTitle("BW");
          frame.setVisible(true);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void getDevices()
    {
        devices = JpcapCaptor.getDeviceList();
        for(int i=0;i<devices.length;i++)
        {
            String device=null;
            radioButtonArray.add(new JRadioButton());
            group.add(radioButtonArray.get(i));
            radioButtonArray.get(i).addActionListener(new RadioButtonListener());
            device= devices[i].name+" "+"("+devices[i].description+")";
            radioButtonArray.get(i).setText(device);
            }    
    }
    public static void startSniffing() throws Exception
    {
        captor = JpcapCaptor.openDevice(devices[selecteddevice], 65535,true, 20);
    }
    public static void setSelectedDevice(int device) 
    {
        selecteddevice = device;
    }

    public NetworkInterface getSelectedDevice()
    {
        if (selecteddevice == -1) 
        {
            return null;
        } 
        else 
        {
            return devices[selecteddevice];
        }
    }
    public static void main(String args[])
    {
         Sniffer sniffer = new Sniffer();
         //sniffer.start();

    }
     private JFreeChart createChart(final XYDataset dataset) {
    final JFreeChart xyz = ChartFactory.createTimeSeriesChart(
        TITLE, "Time(Seconds)", "Bandwidth KB/s", dataset, true, true, false);
    final XYPlot plot = xyz.getXYPlot();
    ValueAxis domain = plot.getDomainAxis();
    domain.setAutoRange(true);
     ValueAxis range = plot.getRangeAxis();
    range.setRange(0,1000);
    return xyz;
}
}
 class RadioButtonListener extends JPanel implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        Sniffer.go.setEnabled(true);

        for (JRadioButton radio : Sniffer.radioButtonArray) {
            if (radio.isSelected()) {

                Sniffer.setSelectedDevice(Sniffer.radioButtonArray.indexOf(radio));

            }
        }
    }
}


class PacketPrinter implements PacketReceiver {
static long tlen;
public void receivePacket(Packet packet) {
  Sniffer.packets.add(packet);
  }
}


Don't block the EDT. Put the time consuming task in a SwingWorker.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜