Embedding Sleep in an unsigned applet, possible ?

4 views
Skip to first unread message

nouknouk

unread,
Oct 8, 2008, 7:06:58 AM10/8/08
to Sleep Developers
Hi,

I'm currently involved in the development of a game embedded in one
applet. My applet *must* be unsigned and will have to download and run
scripts.

I did research about script engines and i discovered that most of them
aren't 'pure java', which make them unusable in an unsigned applet's
sandbox.

I just discovered sleep and i would like to know if this engine will
be runnable in my unsigned applet.
If not, and if you know another script engine that could fit my needs,
tell me please :-)

Many thanks in advance.
Best regards,

Nouk²

Raphael Mudge

unread,
Oct 8, 2008, 12:45:28 PM10/8/08
to sleep-de...@googlegroups.com
Hi Nouk**2,

This should be more than feasible. I had a Sleep user awhile back
embedding Sleep into an applet for a similar purpose. We fleshed out
issues with Sleep's compiler unnecessarily reaching back to the
server when checking if a token is a class or not.

Embedding Sleep into an applet should be the same as embedding Sleep
into any other Java application. Sleep's lack of external
dependencies and small size should be beneficial here. Here are some
resources that will help you with this task:

- Chapter 9 of the Sleep Manual covers embedding in detail: http://
sleep.dashnine.org/manual/
- Sleep Javadoc API: http://sleep.dashnine.org/docs/api/

Some shortcut sources to help you on your way quicker:

- Scripting Java Applications with Sleep: http://www.ddj.com/java/
184406145 (note that the API for sleep.interfaces.Loadable has
changed since this was written)
- Common Embedding Techniques Cheat sheet: http://sleep.dashnine.org/
common.html (a quick reference of how to do common things)

I haven't played much with Applets since helping out the
aforementioned user. If all else fails, you can unzip the sleep.jar
into the .jar you distribute your applet in. I think there is a way
to specify the applet classpath within the applet tag.

Good luck.

-- Raphael

cpm...@comcast.net

unread,
Oct 8, 2008, 12:56:23 PM10/8/08
to sleep-de...@googlegroups.com
While not allowed to get into specifics, I was involved in a project (over a year ago, sleep beta) which used sleep as a script player applet. The web page contained an applet which loaded sleep which loaded and ran a csv based script file from the server. It worked without a problem.

Again, Thank you Raphael.

Cheers,
Ed

nouknouk

unread,
Oct 8, 2008, 2:18:29 PM10/8/08
to Sleep Developers
Thanks for all those detailed informations !
Integration in an Applet smells good, I'll try soon and give here a
feedback.

To be more precise about my project: I'm currently developping kind of
2D isometric engine on top on the pulpcore framework (http://
www.interactivepulp.com/pulpcore/).
The isometric engine is used in kind of online virtual world (think
something technically close to 2D isometric mmorpg).
My goal is to allow people to create their own world and letting them
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.

About technical stuff:
- i'm thinking about being able to run the same scripts both on client
side (applet) and server side ('classic' java app).
- 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, ...).
- the content of the script itself will mostly perform calls to the
java functions exposed by the API of my java framework.

For example, a typical behavior could be:

(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)

Just two questions more:
- what about performance ? More especially : will my server support
donzen (hundred ?) of script invoked in parallel.
- what about the Sleep package compatibility ? Is is embeddable even
with a Java 1.4 code ?

That's it.
If you have any remark about my idea/concept, don't hesitate :-)


And, many many thanks for your previous answers (and maybe the
following ;-)


PS: I just realized my post is really, really too long.
Hope it wasn't too boring.

Raphael Mudge

unread,
Oct 8, 2008, 3:04:55 PM10/8/08
to sleep-de...@googlegroups.com
It sounds like you have a fun project you're putting together. I'm
going to extract some of the technical points and answer to them from
my own experience.

- 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.

Reply all
Reply to author
Forward
0 new messages