Thread.run() = static or PircBot != thread safe?  
Author Message
Lion-O





PostPosted: 2006-3-10 7:01:00 Top

java-programmer, Thread.run() = static or PircBot != thread safe? Hi there,

After messing about with a few programs I'm now getting into IRC. Because a few
programs which I used for quite some time now stopped working and because I
became unhappy with some eggdrop options (and lack off) and suspect it to be
leaking memory I decided to write up something myself.

One of the main functions of this bot will be performing tutorial sessions. At
the moment this consists of reading an ascii file which defines what text has
to be send into the channel. All of this is working decently well (tested with
System.out.println();).

Because the bot still needs to respond to commands I decided to use a seperate
class for the tutorial session so I could use a seperate thread. The idea was
to start a thread which runs the tutorial while the bot still does whatever it
usually does.

To my pleasure my first approach on threading worked quite nicely. My class
extended Thread, setup a run() method to define the actual routine to perform.
In this method I try to send a message to the irc channel the bot is on.
However, this routine is giving me some errors when I try to compile this:

./irctutorial.java:47: non-static method sendMessage \
(java.lang.String,java.lang.String) cannot be referenced from a static context
PircBot.sendMessage(channel, tutline.substring(tutline.indexOf(' ')+1) \
trim());
^
1 error


The method which is mentioned here is Thread.run() and to my knowledge this
method isn't static at all:

---( irctutorial.java - note; lines have been cut using \ )---

public class irctutorial extends Thread {

...

public void run() {

String tutline;
int i = 0, pause = 0;
File tutorial = new File("/home/peter/tutorial.txt");

try {
LineNumberReader tutreader = new LineNumberReader \
(new FileReader(tutorial)); do {
tutline = tutreader.readLine();
pause = Integer.decode(tutline.substring(0,tutline.indexOf(' ')));
Thread.currentThread().sleep(pause*1000);
PircBot.sendMessage(channel, tutline.substring(tutline.indexOf(' ')+1) \
.trim());
tutreader.setLineNumber(i++);
} while (tutline != null);

---[ CUT ]---

The error occurs with PircBot.sendMessage() and this I don't understand at all.
When looking at the PircBot API documentation
(http://www.jibble.org/javadocs/pircbot/index.html) you can see that the
'sendMessage' method isn't static either. The method is of:

public final void sendMessage(String target,
String message)

While the class itself is:

public abstract class PircBot
extends Object
implements ReplyConstants


I'm rather new when it comes to using threads and as such a bit confused. Is
this happening because Thread.run() is indeed static or could this be an issue
with the PircBot API ?

Thanks in advance for any input you can give me.

--
Groetjes, Peter

.\\ PGP/GPG key: http://www.catslair.org/pubkey.asc
 
Mark Thomas





PostPosted: 2006-3-11 1:23:00 Top

java-programmer >> Thread.run() = static or PircBot != thread safe? Lion-O wrote:
<snip>
> PircBot.sendMessage(channel, tutline.substring(tutline.indexOf(' ')+1) \
> .trim());
> tutreader.setLineNumber(i++);
> } while (tutline != null);
>
> ---[ CUT ]---
>
> The error occurs with PircBot.sendMessage() and this I don't understand at all.
> When looking at the PircBot API documentation
> (http://www.jibble.org/javadocs/pircbot/index.html) you can see that the
> 'sendMessage' method isn't static either. The method is of:
>
> public final void sendMessage(String target,
> String message)
>
> While the class itself is:
>
> public abstract class PircBot
> extends Object
> implements ReplyConstants

Your sendMessage method isn't a static method, as you say, but you are
calling it as if it were: PircBot.sendMessage().

To call a non-static method, you need an instance to call it on, so you
would need to construct one:

PircBot myBot = new PircBot();
myBot.sendMessage(...);

Mark
 
Lion-O





PostPosted: 2006-3-11 3:54:00 Top

java-programmer >> Thread.run() = static or PircBot != thread safe? > To call a non-static method, you need an instance to call it on, so you
> would need to construct one:
>
> PircBot myBot = new PircBot();
> myBot.sendMessage(...);

Yes, that I had but I think I'm mixing a few things up.

My botcore class takes care of this. Its being instantiated from my javabot
class. The botcore class takes care of the bots behaviour and sets up the basic
commands (join/part/etc.). This class also initializes the tutorial thread
which is setup in the irctutorial class.

And in this class (irctutorial) I'm trying to utilize the sendMessage method.
But I think I'm finally grasping where I'm going wrong with all this; while the
botcore class initiated the tutorial thread it doesn't mean that the new thread
can also fully utilize the "botcore instance".

At this moment I have it setup in a way where it doesn't give errors during
compilation but also doesn't work. My theory is because the new thread doesn't
have access to the botcore instance (created in the javabot class).

Oh well, still a lot to learn and discover so it seems.

Thanks for your input.

--
Groetjes, Peter

.\\ PGP/GPG key: http://www.catslair.org/pubkey.asc