Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package netwrksim;

/**
 *
 * @author DEBASISH
 */
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.NetworkInterface[] 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);
  }
}

//program is not working!!!!
// errors in org.jfree.chart.ChartFrame
and many more
Posted
Updated 13-May-12 3:24am
v2
Comments
Sandeep Mewara 13-May-12 9:15am    
And the question is? You expect us to run and solve each error for you?

This is not a well framed question! We cannot work out what you are trying to do/ask from the post. Please elaborate and be specific.
Use the "Improve question" link to edit your question and provide better information.
Richard MacCutchan 13-May-12 9:28am    
Unless you tell us what the errors are we have no way of guessing what you are doing wrong, particularly as you never refer to a ChartFrame() object in your code.
bbirajdar 13-May-12 11:14am    
The program needs a Manager to make it work... If it still denies to work; then fire it...

1 solution

review your code.

You are using Netbeans - that's fine. There should be a code assist and a view that tells you where the problems are. And you've got a couple of them in here, that do not need to be made.

first some general points:

- NO static!
static is not intended to be used everywhere. So just do not do it. There is always a proper way to fetch data - public static is not.

- variables are written in small letters - objects are started with a capital letter:
Java
public static List<<b>Packet> packets;

This way you will soon find out that you tried to instance some objects instead of variables.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900