Fandom

JerkLib Wiki

Getting Started

6pages on
this wiki
Add New Page
Talk0 Share

JerkLib Tutorial

Geting Started

If you have not already downloaded JerkLib, you should do so now. You can download it here


Running The Example

Jerklib.jar includes a few example apps. The simplest being jerklib.examples.Example. This example connects to an IRC network, joins a channel and says hello. To run it, make sure you are in the same directory as the jerklib.jar and run the following command:

java -cp jerklib.jar jerklib.examples.Example


The Example Code

Lets now take a look at the code for the Example app:

package jerklib.examples;
 
import jerklib.ConnectionManager;
import jerklib.Profile;
import jerklib.Session;
import jerklib.events.*;
import jerklib.events.IRCEvent.Type;
import jerklib.listeners.IRCEventListener;
 
/**
 * @author mohadib A simple example that demonsrates how to use JerkLib
 */
public class Example implements IRCEventListener
{
	private ConnectionManager manager;
 
	public Example()
	{
		/*
		 * ConnectionManager takes a Profile to use for new connections.
		 */
		manager = new ConnectionManager(new Profile("scripy"));
 
		/*
		 * One instance of ConnectionManager can connect to many IRC networks.
		 * ConnectionManager#requestConnection(String) will return a Session object.
		 * The Session is the main way users will interact with this library and IRC
		 * networks
		 */
		Session session = manager.requestConnection("irc.freenode.net");
 
		/*
		 * JerkLib fires IRCEvents to notify users of the lib of incoming events
		 * from a connected IRC server.
		 */
		session.addIRCEventListener(this);
 
	}
 
	/*
	 * This method is for implementing an IRCEventListener. This method will be
	 * called anytime Jerklib parses an event from the Session its attached to.
	 * All events are sent as IRCEvents. You can check its actual type and cast it
	 * to a more specific type.
	 */
	public void receiveEvent(IRCEvent e)
	{
		if (e.getType() == Type.CONNECT_COMPLETE)
		{
			e.getSession().join("#jerklib");
 
		}
		else if (e.getType() == Type.CHANNEL_MESSAGE)
		{
			MessageEvent me = (MessageEvent) e;
			System.out.println("<" + me.getNick() + ">"+ ":" + me.getMessage());
		}
		else if (e.getType() == Type.JOIN_COMPLETE)
		{
			JoinCompleteEvent jce = (JoinCompleteEvent) e;
 
			/* say hello and version number */
			jce.getChannel().say("Hello from Jerklib " + ConnectionManager.getVersion());
		}
		else
		{
			System.out.println(e.getType() + " " + e.getRawEventData());
		}
	}
 
	public static void main(String[] args)
	{
		new Example();
	}
}

A Closer Look At The Example

The comments in the code should explain whats happening , however lets take a closer look:

manager = new ConnectionManager(new ProfileImpl("scripy"));

ConnectionManager is responisble for making new connections. It will use the Profile passed to it as the default profile to use for connections. A new profile can be passed to requestConnection, thus making it possible to use a different profile for every connection. One instance of ConnectionManager can handle multiple server connections.In the code above a new instance of ConnectionManager is created using the nick 'scripy'.

Session session = manager.requestConnection("irc.freenode.net");

ConnectionManager.requestConnection() creates a new connection to an IRC server and returns a Session object. Sessions objects are the primary way you will interact with JerkLib and IRC servers. Session has methods for join channles , sending messages etc.

session.addIRCEventListener(this);

To be notified for events generating from a IRC server connection , you need to add an IRCEventListener to the Session. The listener will receive all events sent from that Session.

public void receiveEvent(IRCEvent e)

IRCEventListener defines one method that most be implemented: public void receiveEvent(IRCEvent e). IRCEvents will be dispatched to the listener as they come in from the Session.

if (e.getType() == Type.CHANNEL_MESSAGE)

Generally , the first thing you will do when receiving a new event is to check its type. All events are dispatched as the IRCEvent base interface. The event might be a more specific event like a MessageEvent. To find out you will check the result of event.getType(). This will return one of the enum members defined in IRCEvent.Type.

MessageEvent me = (MessageEvent) e;

Once you have determined the events true type you can cast it to a more specific event to get access to helper methods relavant for such an event.

That's the basics. You should now be able to connect to a network and handle events. To find out all the event types and methods available , please consult the javadocs.

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.