Event Programming

L

Lorenzo Bettini

Hi

I'd like to use events in one of my framework; is it possible to somehow
reuse parts of event handling provided, e.g., in AWT?

Or should I write from scratch my event middleware?

These events I need to handle have nothing to do with GUI.

thanks in advance!

Lore

--
+-----------------------------------------------------+
| Lorenzo Bettini ICQ# lbetto, 16080134 |
| PhD in Computer Science |
| Dip. Sistemi e Informatica, Univ. di Firenze |
| Tel +39 055 4237441, Fax +39 055 4237437 |
| Florence - Italy (GNU/Linux User # 158233) |
| Home Page : http://www.lorenzobettini.it |
| http://music.dsi.unifi.it XKlaim language |
| http://www.lorenzobettini.it/purple Cover Band |
| http://www.gnu.org/software/src-highlite |
| http://www.gnu.org/software/gengetopt |
| http://www.lorenzobettini.it/software/gengen |
| http://www.lorenzobettini.it/software/doublecpp |
+-----------------------------------------------------+
 
J

Jacob

Lorenzo said:
I'd like to use events in one of my framework; is it possible to somehow
reuse parts of event handling provided, e.g., in AWT?

Or should I write from scratch my event middleware?

These events I need to handle have nothing to do with GUI.

I wouldn't recommend using AWT code for non-GUI modules
even if the AWT code in question is non-GUI. By principle.

You may use the java.util.Observer/Observable classes to
implement event mechanisms. I find it hard to apply these
in any natural way though, and I dislike the naming.

An alternative is the event module of

http://geosoft.no/software

This is a simple interface for event listening, and a central
event manager singleton for event broadcasting.
 
L

Lorenzo Bettini

Jacob said:
I wouldn't recommend using AWT code for non-GUI modules
even if the AWT code in question is non-GUI. By principle.

You may use the java.util.Observer/Observable classes to
implement event mechanisms. I find it hard to apply these
in any natural way though, and I dislike the naming.

Indeed, I once used the Observer/Observable pattern but I was not very
confortable with it, and I'd prefer events, which, in some sense, are
more "dynamic" than this pattern (let alone that, without multiple
inheritance, using this pattern is quite a nightmare).
An alternative is the event module of

http://geosoft.no/software

This is a simple interface for event listening, and a central
event manager singleton for event broadcasting.

thanks I'll take a look at it (from what I see it's GPL).

You made it, didn't you?

I cannot help but notice (and be pretty happy about it) that you
formatted the source in html
(http://geosoft.no/software/event/EventListener.java.html) by using my
program GNU source-highlight! :)

cheers
Lorenzo

--
+-----------------------------------------------------+
| Lorenzo Bettini ICQ# lbetto, 16080134 |
| PhD in Computer Science |
| Dip. Sistemi e Informatica, Univ. di Firenze |
| Tel +39 055 4237441, Fax +39 055 4237437 |
| Florence - Italy (GNU/Linux User # 158233) |
| Home Page : http://www.lorenzobettini.it |
| http://music.dsi.unifi.it XKlaim language |
| http://www.lorenzobettini.it/purple Cover Band |
| http://www.gnu.org/software/src-highlite |
| http://www.gnu.org/software/gengetopt |
| http://www.lorenzobettini.it/software/gengen |
| http://www.lorenzobettini.it/software/doublecpp |
+-----------------------------------------------------+
 
R

Remi Bastide

I have a few questions about your code :
- Why do you use WeakReferences to store the listeners ? In my
understanding, if there is no other reference to the listener, it can
be garbage-collected, and then the get() method would return null.
- In notify() you loop over a copy of the list, which prevents the
case where a notifed listener would modify the listener list. However,
since we can assume that notification is much more frequent than
adding/removing listener, would not it be more efficient to make the
copy in the add/remove code ?
Best regards.
 
J

Jacob

Lorenzo said:
thanks I'll take a look at it (from what I see it's GPL).

You made it, didn't you?

I did. It is LGPL.
I cannot help but notice (and be pretty happy about it) that you
formatted the source in html
(http://geosoft.no/software/event/EventListener.java.html) by using my
program GNU source-highlight! :)

It's a small world isn't it? :)

"source-highligh" is great, thanks! It does its job without
getting in my way; More software should work like that!
 
J

Jacob

Remi said:
I have a few questions about your code :
- Why do you use WeakReferences to store the listeners ? In my
understanding, if there is no other reference to the listener, it can
be garbage-collected, and then the get() method would return null.

If you enforce listeners to remove themselves as listener
before they die, the WeakReference would not be necessary.
This would be impractical though.

Without WeakReference the EventManager reference to an object
would prevent its garbage collection infinitely.
- In notify() you loop over a copy of the list, which prevents the
case where a notifed listener would modify the listener list. However,
since we can assume that notification is much more frequent than
adding/removing listener, would not it be more efficient to make the
copy in the add/remove code ?

This is a valid point. It is the usual tradeoff between
space and performance. As is, it optimize on space, but
in this case performance is probably a more valid issue.
Thanks for pointing it out.
 
R

Remi Bastide

Thanks for the clarifications,

Jacob said:
If you enforce listeners to remove themselves as listener
before they die, the WeakReference would not be necessary.
This would be impractical though.

Without WeakReference the EventManager reference to an object
would prevent its garbage collection infinitely.

I think the fact that you internally use WeakReferences needs to be
clearly documented, since it has a strong influence on the clients of
your EventManager class.

for instance, it does not make sense to write:

EventManager.getInstance().addListener("EventName", new
MyImplementationOfEventListener() );
 
L

Lorenzo Bettini

Jacob said:
I did. It is LGPL.



It's a small world isn't it? :)

IT IS :)
"source-highligh" is great, thanks! It does its job without
getting in my way; More software should work like that!

you're too kind! THANK YOU! :)

by the way, I'm developing a pretty new version of it, whose release
candidate can be found here:

ftp://alpha.gnu.org/gnu/src-highlite/

the main novelty it is that language definitions must not be provided as
flex files (and thus hardcoded source-highlight), but in (hopefully)
simple configuration files that are read dynamically. This should make
addition of new languages straightforward :)

here's an example of the Java language definition:

---------------------------------------------------------
preproc = "import","package"

include "c_comment.lang"

include "number.lang"

string delim "\"" "\"" escape "\\"
string delim "'" "'" escape "\\"

keyword = "abstract|assert|break|case|catch|class|const",
"continue|default|do|else|extends|false|final",
"finally|for|goto|if|implements|instanceof|interface"
keyword = "native|new|null|private|protected|public|return",
"static|strictfp|super|switch|synchronized|throw",
"throws|true|this|transient|try|volatile|while"

type = "int|byte|boolean|char|long|float|double|short|void"

include "symbols.lang"

cbracket = "{|}"

include "function.lang"
---------------------------------------------------------

if you have time to try this release candidate, I'd really appreciate
feedback (by the way, it now depends on the WONDERFUL regex library of
boost, www.boost.org).

cheers
Lorenzo


--
+-----------------------------------------------------+
| Lorenzo Bettini ICQ# lbetto, 16080134 |
| PhD in Computer Science |
| Dip. Sistemi e Informatica, Univ. di Firenze |
| Tel +39 055 4237441, Fax +39 055 4237437 |
| Florence - Italy (GNU/Linux User # 158233) |
| Home Page : http://www.lorenzobettini.it |
| http://music.dsi.unifi.it XKlaim language |
| http://www.lorenzobettini.it/purple Cover Band |
| http://www.gnu.org/software/src-highlite |
| http://www.gnu.org/software/gengetopt |
| http://www.lorenzobettini.it/software/gengen |
| http://www.lorenzobettini.it/software/doublecpp |
+-----------------------------------------------------+
 
A

alan

I have a few questions about your code :

I have a question!

What strategy do you use for error handling in your event mechanism? I
don't see an exception signature. Do you simply through unchecked
exceptions, or do you generate an error message and route that?

(Curious. Also, working on an event mechanism.)
 
J

Jacob

I have a question!

What strategy do you use for error handling in your event mechanism? I
don't see an exception signature. Do you simply through unchecked
exceptions, or do you generate an error message and route that?

The source of an event will (by definition) not know
anything about the code executed as response of it.
Consequently it makes no sense to create a specific
exception type and expect it to be thrown.

Then the best we can do is simply:

try {
eventManager.notify ("SomeEvent", source, data);
}
catch (Exception exception) {
// handle exception here
}

On the other hand, the source of an event is not in
the natural execution path of the response code, and
it doesn't really make sense to handle exceptions at
this point.

So this is an issues. Suggestions appreciated.
 
A

alan

The source of an event will (by definition) not know
anything about the code executed as response of it.
Consequently it makes no sense to create a specific
exception type and expect it to be thrown.

Then the best we can do is simply:

try {
eventManager.notify ("SomeEvent", source, data);
}
catch (Exception exception) {
// handle exception here
}
On the other hand, the source of an event is not in
the natural execution path of the response code, and
it doesn't really make sense to handle exceptions at
this point.
So this is an issues. Suggestions appreciated.

I'm generating an event in response to errors and using the event
mechanism to route the event. If no one intercepts the event through the
event mechanism, then an exception is thrown, with the understanding that
the process will probably be terminated as a result.

It is just as you say. Exceptions are pretty much fatal.

I guess my pattern is a double observable pattern. Since the event handler
and the error handler might come from separate vendors.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top