Again, Thank you Raphael.
Cheers,
Ed
- My goal is to allow people to create their own world and letting
the coding their own behavior for parts of the world (avatar,
terrain, objects, ...) Those behavior would be coded with Sleep and
downloaded at runtime the same way other data (map, graphics, ...) are.
My internet relay chat client jIRCii operates in a similar way. Much
of the client is scripted in jIRCii and I allow users to write their
own scripts. I found scripting the default functionality helped me
better predict what scripters would want. The formula worked pretty
well and jIRCii has a healthy scripting community. 70+ scripts
contributed from users. http://jircii.dashnine.org/
My original motivation for writing Sleep was to build something
easily extensible with new syntax constructs. This way a novice
programmer could pick up the language and feel like your application
is a first-class part of the programming language.
- i'm thinking about being able to run the same scripts both on
client side (applet) and server side ('classic' java app).
This shouldn't be a problem. A concept you may want to think about.
Sleep supports a mechanism called continuations. A continuation is
basically a paused function with all its state. In Sleep you can
actually serialize a paused function to a socket. If you have a need
for it, you could write Sleep functions for your scripters that pause
the function, move it to the server, perform some action, and then
move it back to the client. You may have to find a way to shoe horn
this into one tcp connection since you're using an applet but it is
doable. http://today.java.net/pub/a/today/2008/07/24/fun-with-
continuations.html Then again depending on your needs this may be
over complicating things. I'm so used to working in this paradigm I
tend to flock to it.
If continuations are a bit heavy, consider looking at the yield
keyword. yield lets you pause a function. So if you create a
function for each "actor" you're simulation then it can run and
choose when to stop running with yield. Next time you call the actor
function it will resume execution where it left off. And you can
serialize this function between the client and server to let it do
some things on the server and some things on the client. Just a
thought.
You'll really want to read chapter 5 of the Sleep Manual. It talks
all about Sleep functions. Sleep is a dirty functional language and
often times Sleep scripts use functions to represent objects.
Functions have a definition (i.e. a subroutine, akin to a class) and
said definition can have multiple instances with their own paused
state, variables, etc.
- the script code will allow the 'world developper' to define
'behavior' when an event occurs (eg. user entered world ; avatar
moves on one terrain's tile, ...).
Pretty easy to do. jIRCii adds an 'on' keyword. Through this I have
on event { do this code }. Makes a very graspable mechanism for
scripters to add their own event handlers.
- the content of the script itself will mostly perform calls to the
java functions exposed by the API of my java framework.
> (code)
> sub onUserTouchObjectEvent {
>
> $objectUID = $1; // parameter received from Java App.
>
> // retrieving a java Object instance
> $world = [WorldAPI.getThisWorld];
>
> // call Java object methods to modify the world
> [$world removeObjectFromWorld: $objectUID]
> [$world addObjectToInventory: $objectUID]
> }
> (/code)
For something like this, consider extending Sleep (through its Java
API), to have an on keyword like I did in jIRCii. i.e.:
on UserTouchObjectEvent
{
# make your Sleep extension pass $world and $objectUID to the
event handler
# and document their availability
[$world removeObjectFromWorld: $objectUID];
[$world addobjectToInventory: $objectUID];
}
This type of extension is called an environment. Its well
documented how to do this.
- what about performance ? More especially : will my server support
donzen (hundred ?) of script invoked in parallel.
Yes. I wrote a network traffic generator/scorebot for a network
defense exercise. I simulated multiple users on the network
encapsulated into a mobile Sleep function (the continuation stuff
above). Each function generated random data, attempted to use a
service, slept, moved somewhere else, and attempted to use the same
service in different way (pretending to be another user). If the
data changed on the service then the function would take away points
from the team. We had this system running in a beta form for 6 weeks
24x7. Then during a 2 day competition it ran in final form with 83
of these functions moving around. In my own testing I had 1000
functions running in their own threads on one computer. You won't
have a problem scaling to a dozen or hundreds of scripts invoked in
parallel.
- what about the Sleep package compatibility ? Is is embeddable even
with a Java 1.4 code ?
My intention is to keep Sleep Java 1.4.2 compatible. In the past
I've made a few mistakes that have broke this and quickly corrected
them as soon as they were reported.