irssibot.modules
Class AbstractModule

java.lang.Object
  |
  +--java.lang.Thread
        |
        +--irssibot.modules.AbstractModule
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
Alko, ChannelTools, HTMLTools, Logger, SodexHo, TopicTools, UserTools

public abstract class AbstractModule
extends java.lang.Thread

Base class for all modules for IrssiBot. Implements some basic functionality common for all modules. The module acts as a 'consumer' for the bot core, who 'produces' IrcMessage objects to the module's processing queue.

Version:
$Name: $ $Revision: 1.13 $
Author:
Matti Dahlbom

Field Summary
private  boolean alive
          Indicates whether the module thread should continue executing its run() loop.
private  java.lang.Throwable consumerException
          A state variable indicating whether the module is executing ok.
private  IrcMessage consumerMessage
          The message causing the consumer exception.
private  java.util.Vector messageQueue
          Vector containing MessageData objects waiting for processing.
private  java.lang.Object processLock
          A lock object used for producer-consumer synchronization
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
protected AbstractModule(java.lang.String name)
          Constructs with named thread.
 
Method Summary
 void addMessage(IrcMessage message, ServerConnection serverConnection)
          Appends the new IrcMessage+ServerConnection pair to the end of the message queue.
private  void fetchNextMessage()
          Fetches next message in queue and pass it on to processMessage().
abstract  java.lang.String getModuleInfo()
          Module should return a descriptive info string containing at least module's name and version.
 java.util.Properties getState()
          Gets module's state as a Properties object.
 void killModule()
          Kills module thread.
 boolean onLoad(java.util.Properties state, Core core)
          Called upon loading the module.
 void onUnload()
          Called upon unloading the module.
protected abstract  void processMessage(IrcMessage message, ServerConnection serverConnection)
          Processes messages as they come in.
 void run()
          Thread loop.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

messageQueue

private final java.util.Vector messageQueue
Vector containing MessageData objects waiting for processing.


processLock

private final java.lang.Object processLock
A lock object used for producer-consumer synchronization


alive

private boolean alive
Indicates whether the module thread should continue executing its run() loop.


consumerException

private java.lang.Throwable consumerException
A state variable indicating whether the module is executing ok. When an exception occurs in the consumer thread, this variable is set to the thrown exception and it is thrown to the producer thread next time addMessage() is called.

See Also:
addMessage(IrcMessage,ServerConnection)

consumerMessage

private IrcMessage consumerMessage
The message causing the consumer exception.

Constructor Detail

AbstractModule

protected AbstractModule(java.lang.String name)
Constructs with named thread.

Parameters:
name - name for thread
Method Detail

getModuleInfo

public abstract java.lang.String getModuleInfo()
Module should return a descriptive info string containing at least module's name and version.

Returns:
module info string

getState

public java.util.Properties getState()
Gets module's state as a Properties object. If module does not wish to save state OR its state has not been changed since last getState(), it should return null. This is to minimize disk access.

Returns:
state of module as a Properties object

onLoad

public boolean onLoad(java.util.Properties state,
                      Core core)
Called upon loading the module.

Parameters:
state - the initial state of module as an Properties object, or null if no state was saved for module.
core - a Core instance. this can be used to initialize module if no state was retrieved.
Returns:
true if ok, false if error. modules returning false from onLoad() will be unloaded immediately.
See Also:
Core

onUnload

public void onUnload()
Called upon unloading the module.


addMessage

public final void addMessage(IrcMessage message,
                             ServerConnection serverConnection)
                      throws java.lang.Throwable
Appends the new IrcMessage+ServerConnection pair to the end of the message queue. Access to the queue is synchronized. When a new message is added to the queue, the waiting consumer thread is notify()'ed.

Parameters:
message - the IrcMessage to append to the message queue
Throws:
java.lang.Exception - thrown if an exception was thrown by the consumer thread in processMessage().
java.lang.Throwable
See Also:
processMessage(IrcMessage,ServerConnection)

fetchNextMessage

private final void fetchNextMessage()
Fetches next message in queue and pass it on to processMessage(). If message is empty, the thread wait()s.

See Also:
#processMessage(IrcMessage)

processMessage

protected abstract void processMessage(IrcMessage message,
                                       ServerConnection serverConnection)
Processes messages as they come in. Classes inheriting this class must override this method.

Parameters:
message - the incoming IrcMessage

killModule

public void killModule()
Kills module thread.


run

public final void run()
Thread loop. Continues execution until variable alive is set to value false.

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread


IrssiBot (c) 2000-2002 Matti Dahlbom