The Artima Developer Community
Sponsored Link

Java Answers Forum
I need to implement a worker thread for Jpcap Class JPcapCaptor methods

1 reply on 1 page. Most recent reply: Dec 3, 2008 12:04 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Samuel Picture

Posts: 2
Nickname: spicture
Registered: Nov, 2008

I need to implement a worker thread for Jpcap Class JPcapCaptor methods Posted: Nov 28, 2008 3:32 PM
Reply to this message Reply
Advertisement
This Java swing application works, but there is a problem. The Jpcap Class JPcapCaptor loopPacket method and its PacketReceiver callback receivePacket method block the swing event dispatching thread. I need to move them to a separate thread, a worker thread (preferably a SwingWorker worker thread). Ok, I give up! After several hours of looking on the internet to see how to use SwingWorker to implement a worker thread, I cam away as confused as when I began. This is probably a "no brainer" for must java developers, but for a beginning developer it is extremely confusing. I will sure appreciate it if someone can show me how to do this.

<java>
import java.io.IOException;
import java.net.URL;
import java.text.MessageFormat;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Font;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.Toolkit;

import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.SwingWorker;

import jpcap.*;
import jpcap.packet.Packet;

public class JpcapPacketCapture extends JFrame implements PacketReceiver
{
JFrame frame;
JTextArea textArea;
JpcapCaptor jpcapCaptor;
JpcapPacketCapture jpcapPacketCapture;

int duration;
int interfaceNo;
int lineEndOffset;

static URL url1 = JpcapPacketCapture.class.getResource("images/Start.gif");
ImageIcon start = new ImageIcon(url1);

static URL url2 = JpcapPacketCapture.class.getResource("images/Stop.gif");
ImageIcon stop = new ImageIcon(url2);

static URL url3 = JpcapPacketCapture.class.getResource("images/Clear.gif");
static ImageIcon clear = new ImageIcon(url3);

static URL url4 = JpcapPacketCapture.class.getResource("images/Print.gif");
static ImageIcon print = new ImageIcon(url4);

static URL url5 = JpcapPacketCapture.class.getResource("images/Exit.gif");
static ImageIcon exit = new ImageIcon(url5);

static URL url6 = JpcapPacketCapture.class.getResource("images/About.gif");
static ImageIcon about = new ImageIcon(url6);

static URL url7 = JpcapPacketCapture.class.getResource("images/Logo_Small.gif");
static ImageIcon logoSmall = new ImageIcon(url7);

static URL url8 = JpcapPacketCapture.class.getResource("images/Logo_Large.gif");
static ImageIcon logoLarge = new ImageIcon(url8);

static URL url9 = JpcapPacketCapture.class.getResource("images/Splash_Scrn.jpg");
static ImageIcon splashScrn = new ImageIcon(url9);

public JpcapPacketCapture()
{
super("Jpcap Packet Capture Version 1.00");

ShowSplashScreen(4000);

FileMenu fileMenu = new FileMenu(this);
HelpMenu helpMenu = new HelpMenu(this);
MenuBar menuBar = new MenuBar();
menuBar.add(fileMenu);
menuBar.add(helpMenu);
setMenuBar(menuBar);

JToolBar toolBar = new JToolBar();
toolBar.setFloatable(true);
toolBar.setOrientation(JToolBar.HORIZONTAL);
toolBar.setRollover(true);
addButtons(toolBar);

textArea = new JTextArea(14, 42);
textArea.setTabSize(4);
textArea.setFont(new Font("sansserif", Font.PLAIN, 12));
textArea.setCaretPosition(textArea.getDocument().getLength());
textArea.setEditable(false);

Container contentPane = getContentPane();

JPanel panel = new JPanel();

contentPane.add(new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS));

contentPane.add(toolBar, "North");
}

class FileMenu extends Menu
{
public FileMenu(JpcapPacketCapture mw)
{
super("File");

jpcapPacketCapture = mw;
MenuItem mi;

add(mi = new MenuItem("Start Capure"));
mi.addActionListener
( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
StartCapture();
}
}
);

add(mi = new MenuItem("Stop Capure"));
mi.addActionListener
( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
StopCapture();
}
}
);

add(mi = new MenuItem("Clear Window"));
mi.addActionListener
( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
textArea.setText("");
}
}
);

add(mi = new MenuItem("Print Window"));
mi.addActionListener
( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
PrintWindow();
}
}
);

add(mi = new MenuItem("Exit"));
mi.addActionListener
( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
);
}
}

class HelpMenu extends Menu
{
public HelpMenu(JpcapPacketCapture mw)
{
super("Help");
jpcapPacketCapture = mw;
MenuItem mi;

add(mi = new MenuItem("About..."));
mi.addActionListener
( new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
JOptionPane.showMessageDialog(frame,
"Jpcap Packet Capture Version 1.00 \n" +
"Copyright (C) 2008 SHP Consulting & Systems \n\n" +
"This progoram uses Jpcap to capture packets for one of this system's \n" +
"network interfaces. The specific network interface to capture packets \n" +
"for is provided by the user through a program selection dialog. \n\n",
"About...", JOptionPane.INFORMATION_MESSAGE, new ImageIcon(logoLarge.getImage()));
}
}
);
}
}

public void addButtons(JToolBar toolBar)
{
JButton toolbarButtons = null;

toolbarButtons = new JButton(new ImageIcon(start.getImage()));
toolbarButtons.setToolTipText("Start capture.");
toolbarButtons.addActionListener
( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
StartCapture();
}
}
);
toolBar.add(toolbarButtons);

toolbarButtons = new JButton(new ImageIcon(stop.getImage()));
toolbarButtons.setToolTipText("Stop capture.");
toolbarButtons.addActionListener
( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
StopCapture();
}
}
);
toolBar.add(toolbarButtons);

toolbarButtons = new JButton(new ImageIcon(clear.getImage()));
toolbarButtons.setToolTipText("Clear window.");
toolbarButtons.addActionListener
( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
textArea.setText("");
}
}
);
toolBar.add(toolbarButtons);

toolbarButtons = new JButton(new ImageIcon(print.getImage()));
toolbarButtons.setToolTipText("Print window.");
toolbarButtons.addActionListener
( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
PrintWindow();
}
}
);
toolBar.add(toolbarButtons);

toolbarButtons = new JButton(new ImageIcon(exit.getImage()));
toolbarButtons.setToolTipText("Exit program.");
toolbarButtons.addActionListener
( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
);
toolBar.add(toolbarButtons);

toolbarButtons = new JButton(new ImageIcon(about.getImage()));
toolbarButtons.setToolTipText("About program.");
toolbarButtons.addActionListener
( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(frame,
"Jpcap Packet Capture Version 1.00 \n" +
"Copyright (C) 2008 SHP Consulting & Systems \n\n" +
"This progoram uses Jpcap to capture packets for one of this system's \n" +
"network interfaces. The specific network interface to capture packets \n" +
"for is provided by the user through a program selection dialog. \n\n",
"About...", JOptionPane.INFORMATION_MESSAGE, new ImageIcon(logoLarge.getImage()));
}
}
);
toolBar.add(toolbarButtons);
}

protected static ImageIcon createImageIcon(String path)
{
java.net.URL imgURL = JpcapPacketCapture.class.getResource(path);
if (imgURL != null)
{
return new ImageIcon(imgURL);
}
else
{
System.err.println("Couldn't find file: " + path);
return null;
}
}

public void ShowSplashScreen(int d)
{
duration = d;

JPanel splashScreen = (JPanel)getContentPane();
int width = 470;
int height =255;
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int x = (screen.width-width) / 2;
int y = (screen.height-height) / 2;
setBounds(x, y, width, height);
JLabel label = new JLabel(new ImageIcon(splashScrn.getImage()));
splashScreen.add(label, BorderLayout.CENTER);
setVisible(true);

try
{
Thread.sleep(duration);
}
catch (Exception e)
{
}

splashScreen.remove(label);
setVisible(false);
}

public void StartCapture()
{
//
// Get a list of eligible network interfaces.
//
NetworkInterface[] devices = JpcapCaptor.getDeviceList();

String[] names = new String[devices.length];

for (int i = 0; i < names.length; i++)
names = i + " " + (devices.description == null?devices.name:devices.description);

//
// Get which network interface to use for capturing from the user.
//
Object[] selectionValues = names;

String initialSelection = names[0];

Object selection = JOptionPane.showInputDialog(null, "Capture using which interface?\n",
"Input", JOptionPane.PLAIN_MESSAGE, null, selectionValues, initialSelection);

textArea.append(selection + "\n");
textArea.select(0, 1);
interfaceNo = Integer.parseInt(textArea.getSelectedText());

try
{
lineEndOffset = textArea.getLineEndOffset(0);
}
catch (Exception e)
{
}

textArea.select(3, lineEndOffset);
String interfaceDescription = textArea.getSelectedText();
textArea.setText("");

//
// Open the network interface for capturing.
//
try
{
jpcapCaptor = JpcapCaptor.openDevice(devices[interfaceNo], 65535, false, 20);
}
catch (IOException ioException)
{
textArea.append("Error: " + ioException);
}

//
// Verify which network interface the user selected to use for capturing.
//
textArea.append("Starting packet capture using...\n");
textArea.append(" • Interface " + interfaceNo + "\n");
textArea.append(" • " + interfaceDescription + "\n");

//
// Start capturing
//
jpcapCaptor.loopPacket(10, this);
}

public void receivePacket(Packet packet)
{
//
// Show captured packets in the application's text area.
//
if (packet == null)
{
textArea.append("Null packet receievd or timeout");
return;
}

textArea.append("" + packet + "\n");
}

public void StopCapture()
{
//
// Stop capturing.
//
jpcapCaptor.close();
}

public void PrintWindow()
{
final MessageFormat header = new MessageFormat("");
final MessageFormat footer = new MessageFormat("");
textArea.setFont(new Font("sansserif", Font.PLAIN, 9));;
try
{
textArea.print(header, footer, true, null, null, true);
}
catch(Exception e)
{
e.printStackTrace();
}

textArea.setFont(new Font("sansserif", Font.PLAIN, 12));
}

public static void main(String[] args)
{
JpcapPacketCapture jpcapPacketCapture = new JpcapPacketCapture();
JFrame.setDefaultLookAndFeelDecorated(true);
jpcapPacketCapture.setIconImage(logoSmall.getImage());
jpcapPacketCapture.setSize(600, 360);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int w = jpcapPacketCapture.getSize().width;
int h = jpcapPacketCapture.getSize().height;
int x = (dim.width - w) / 2;
int y = (dim.height - h) / 2;
jpcapPacketCapture.setLocation(x, y);
jpcapPacketCapture.setVisible(true);
}
}
</java>


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: I need to implement a worker thread for Jpcap Class JPcapCaptor methods Posted: Dec 3, 2008 12:04 AM
Reply to this message Reply
I don'T know much about worker threads (allmost nothing tbh), but it would help people here if you used the java tags as shown on the right side of the texxt input field.

It's not < java > but [ java ]

Flat View: This topic has 1 reply on 1 page
Topic: Mobile Innovators, Pay Attention, Nokia Comes Calling Previous Topic   Next Topic Topic: Java Developper

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use