org.jnetpcap
Class PcapTask<T>

java.lang.Object
  extended by org.jnetpcap.PcapTask<T>
Type Parameters:
T - user supplied type start() and stop(). These 2 methods perform various synchronization functions between the worker and the parent threads.
All Implemented Interfaces:
java.lang.Runnable

public abstract class PcapTask<T>
extends java.lang.Object
implements java.lang.Runnable

A pcap background task handle. This provides status and control over the background loop. The task provides 2 methods for controlling the thread.

Since:
1.2
Author:
Mark Bednarczyk, Sly Technologies, Inc.

Field Summary
protected  int count
          Number of packets to capture or 0 for infinate.
protected  Pcap pcap
          Pcap handle.
protected  int result
          Libpcap result code.
protected  java.lang.Thread thread
          Controlling thread.
protected  T user
          User data.
 
Constructor Summary
PcapTask(Pcap pcap, int count, T user)
          Creates a new task handle for controlling background thread.
 
Method Summary
protected  void breakLoop()
          Algorithm for breaking the loop, whatever it is.
 int getCount()
          The packet count that was supplied by the user.
 Pcap getPcap()
          Returns the underlying Pcap object being used by this task
 int getResult()
          Returns the result code that was returned from the user supplied pcap function.
 java.lang.Thread getThread()
          Gets the background thread this task is using.
 T getUser()
          User supplied data object.
 boolean isAlive()
          Checks if the background thread is running and is alive.
 void start()
          Creates and starts up the background thread while synchronizing with the background thread.
 void stop()
           Terminates the task after making sure that the pcap session and thread are active.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.lang.Runnable
run
 

Field Detail

result

protected int result
Libpcap result code.


thread

protected java.lang.Thread thread
Controlling thread.


pcap

protected final Pcap pcap
Pcap handle.


count

protected final int count
Number of packets to capture or 0 for infinate.


user

protected final T user
User data.

Constructor Detail

PcapTask

public PcapTask(Pcap pcap,
                int count,
                T user)
Creates a new task handle for controlling background thread.

Parameters:
pcap - pcap handle
count - number of packets to capture or 0 for infinite
user - user supplied object
Method Detail

getResult

public final int getResult()
Returns the result code that was returned from the user supplied pcap function.

Returns:
libpcap result code

getThread

public final java.lang.Thread getThread()
Gets the background thread this task is using. It is highly recommended though that the user interact with the thread using start() and

Returns:
background thread stop() methods.

start

public void start()
           throws java.lang.InterruptedException
Creates and starts up the background thread while synchronizing with the background thread. The user can be assured that when this method returns, the background thread has been started and has entered its Runnable.run method.

Throws:
java.lang.InterruptedException - if the synchronization between threads was interrupted

stop

public void stop()
          throws java.lang.InterruptedException

Terminates the task after making sure that the pcap session and thread are active.

Throws:
java.lang.InterruptedException - since this method waits for the background thread to terminate, it can be interrupted

breakLoop

protected void breakLoop()
Algorithm for breaking the loop, whatever it is. It can be overriden and a different algorithm supplied.


isAlive

public boolean isAlive()
Checks if the background thread is running and is alive.

Returns:
true means thread is alive

getPcap

public final Pcap getPcap()
Returns the underlying Pcap object being used by this task

Returns:
pcap capture session object

getCount

public final int getCount()
The packet count that was supplied by the user. This is the number of packets requested by the user. 0 indicates capture forever until

Returns:
number of packets to capture or 0 for infinite stop() is called or an error occures.

getUser

public final T getUser()
User supplied data object. This is an arbitrary user object.

Returns:
user object