FANDOM


JerkLib Tutorial

Tasks

Tasks are another way to be notified of events coming from a given Session. Unlike adding an IRCEventListener , task can have events filtered for them so they only receive the event types they are looking for. Tasks can also be easily removed via Task.cancel(). This is convenient for task that might only run one time , like an authentication task. Here is a small example of using tasks. In the example a connection is made and a few tasks are added to the session.

package jerklib.examples;
 
import jerklib.ConnectionManager;
import jerklib.Profile;
import jerklib.Session;
import jerklib.events.IRCEvent;
import jerklib.events.JoinCompleteEvent;
import jerklib.events.MotdEvent;
import jerklib.events.IRCEvent.Type;
import jerklib.tasks.TaskImpl;
 
/**
 * An example of how to use tasks.
 * 
 * @author mohadib
 *
 */
public class TaskExample
{
	public TaskExample()
	{
		ConnectionManager conman = new ConnectionManager(new Profile("scripy"));
		Session session = conman.requestConnection("irc.freenode.net");
 
 
		/* Add a Task to join a channel when the connection is complete 
		   This task will only ever be notified of ConnectionCompleteEvents */
		session.onEvent(new TaskImpl("join_channels")
		{
			public void receiveEvent(IRCEvent e)
			{
				e.getSession().join("#jerklib");
			}
		}, Type.CONNECT_COMPLETE);
 
 
		/* Add a Task to say hello */
		session.onEvent(new TaskImpl("hello")
		{
			public void receiveEvent(IRCEvent e)
			{
				JoinCompleteEvent jce = (JoinCompleteEvent)e;
				jce.getChannel().say("Hello from JerkLib!");
			}
		}, Type.JOIN_COMPLETE);
 
 
 
		/* Add a Task to be notified on MOTD and JoinComplete events */
		session.onEvent(new TaskImpl("motd_join")
		{
			public void receiveEvent(IRCEvent e)
			{
				if(e.getType() == Type.MOTD)
				{
					MotdEvent me = (MotdEvent)e;
					System.out.println(me.getMotdLine());
				}
				else
				{
					JoinCompleteEvent je = (JoinCompleteEvent)e;
					je.getChannel().say("Yay tasks!");
				}
			}
		}, Type.MOTD , Type.JOIN_COMPLETE);
 
 
 
		/* Add a Task that will be notified of all events */
		session.onEvent(new TaskImpl("print")
		{
			public void receiveEvent(IRCEvent e)
			{
				System.out.println(e.getRawEventData());
			}
		});
 
	}
 
	public static void main(String[] args)
	{
		new TaskExample();
	}
 
}

TaskImpl is the default implementation of the Task interface. You just need to override public void receiveEvent(IRCEvent e). Session has two onEvent methods. Both take a Task as an argument , one will also take multiple Type arguments (varargs). The Types passed in are the types of events that the Task will receive. Using the other method , passing in no Type , will cause the Task to be notifed of all events.

Previous | Next

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.