-2

I want to draw a chart by reading the /proc/net/dev file in ubuntu to show the received bytes and sent bytes in wlan,eth and lo. At the moment those details are shown in text format with live updating. So I want to show those live updates by a graph. At the momment I show these my text as shown below code.

package throughput;

import java.awt.*;
import java.io.*;
import java.util.*;
import javax.swing.*;

public class Throughput extends JPanel implements Runnable {

    private ArrayList<String> lines;

    public Throughput() {
    lines = new ArrayList<String>();
    new Thread(this).start();
}

@Override
public void run() {
    try {
        while (true) {
            String sContents = readFile();
            StringTokenizer tok = new StringTokenizer(sContents, "\n");
            lines = new ArrayList<String>();
            int x = 0;

            while (tok.hasMoreTokens()) {
                String line = tok.nextToken();
                if (x++ < 2) {//skip headers
                    continue;
                }
                lines.add(line);
            }
            repaint();
            Thread.sleep(500);//half a second
        }
    } catch (Exception exc) {
        exc.printStackTrace();
    }
}

@Override
public void paint(Graphics gr) {
    super.paint(gr);
    Graphics2D g = (Graphics2D) gr;
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setFont(new Font(Font.DIALOG_INPUT, Font.PLAIN, 14));
    g.drawString("Interface: receivedBytes/receivedPackets, sentBytes/sentPackets", 10, 20);
    for (int i = 0; i < lines.size(); i++) {
        drawLine(g, lines.get(i), i);
    }
}

private static final String readFile() {
    try {
        File file = new File("/proc/net/dev");
        StringBuilder sb = new StringBuilder();
        byte[] bytes = new byte[1024];
        int read = 0;
        InputStream in = new FileInputStream(file);
        while ((read = in.read(bytes)) != -1) {
            sb.append(new String(bytes, 0, read));
        }
        in.close();
        return sb.toString();
    } catch (Exception exc) {
        exc.printStackTrace();
    }
    return null;
}

private void drawLine(Graphics2D g, String line, int iLineIndex) {
    String[] words = line.split("\\s+");
    String sInterface = words[words[0].length() > 0 ? 0 : 1];
    int index = sInterface.indexOf(':');
    boolean jump = index != sInterface.length()-1;
    String receivedBytes = jump ? sInterface.substring(index+1) : words[2];
    String receivedPackets = jump ? words[2] : words[3];
    String sentBytes = jump ? words[9] : words[10];
    String sentPackets = jump ? words[10] : words[11];
    if(index > -1) {
        sInterface = sInterface.substring(0, index);
    }
    int x = 10, y = 50 + iLineIndex * 20;
    String sReceived = receivedBytes + "/" + receivedPackets;
    String sSent = sentBytes + "/" + sentPackets;
    g.drawString(sInterface + ":", x, y);
    g.drawString(sReceived, x+100, y);
    g.drawString(sSent, x+250, y);
}

}

package throughput;

import javax.swing.*;

public class Main extends JFrame{

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        setSize(550, 200);
        setTitle("Network Monitor");
        setContentPane(new Throughput());
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }
}
Philippe Blayo
  • 10,610
  • 14
  • 48
  • 65
  • What's the question? Where are you stuck? – The Archetypal Paul Oct 10 '14 at 16:52
  • I want to draw a chart by reading the /proc/net/dev file in ubuntu to show the received bytes and sent bytes in wlan,eth and lo. At the moment those details are shown only in text format with live updating.Thats the code above.Above code is not the issue. I want to draw a live chart using the above output.Please help! – Vidura Perera Oct 10 '14 at 16:58
  • So you want someone to write your code for you? That's not how SO works. You need to show what you've tried, then ask specific questions when you get stuck. – The Archetypal Paul Oct 10 '14 at 16:59
  • Ya actually i have wrote the above code by my own. I want to convert it to display in a graph format. I really have no idea how to get it done. I read about jfreechart and stuff. But no idea about using it to draw a chart. If you are familiar with this please do help because i need this for my assignment due. – Vidura Perera Oct 10 '14 at 17:08
  • So you do want us to write your code. That isn't going to happen. You need to show what you have tried (to display in a graph) and where you are stuck. SO is not a place where you can get your assignment code written for you – The Archetypal Paul Oct 10 '14 at 17:16

1 Answers1

1

To avoid blocking the event dispatch thread, you need to read the (virtual) file in the background using SwingWorker, as shown in this example that reads from /dev/random. In your implementation of process(), update your chart's model, typically a Dataset, and the enclosing ChartPanel will see that the chart updates itself. A complete example is seen here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045