Missing Alternate TagMissing Alternate TagMissing Alternate TagMissing Alternate TagMissing Alternate TagMissing Alternate TagMissing Alternate TagMissing Alternate Tag

Programming NetMorph

JapaneseVersion Programming NetMorph

Since NetMorph 0.2 has eToy support, usually you do not have to write any Smalltalk programs. This page is for developers.


First Example


NetMorph 0.2 has well-defined API to control morphs remotely.
Here is a sample. Suppose other image's ip and port is #('192.168.1.2' 37459).


"Open a morph locally"
morph _ Morph new openInWorld.

"Migrate the morph to other image, leaving a proxy"
proxy _ morph leaveProxyDo: [morph warp: '192.168.1.2' port: 37459].

"Set the remote morph's position remotely"
proxy position: 100@100.

"Set the remote morph's color remotely"
proxy color: Color random.
"A cool visual effect"
proxy poofIn.

"Open a MakerMorph in the remote image"
"MarkerMorph can be used to retrieve the morph later"
proxy leaveMarkerMorph.

proxy showBalloon: 'Hi there! Please use this tile to bring me back!'.

"Changes the color of the remote World"
oldColor _ proxy world color.
3 timesRepeat: [proxy world color: Color random. (Delay forSeconds: 1) wait].
proxy world color: oldColor.

"Return to the local image"
proxy warpToHome.


After the trip, if you inspect the returned morph and send #path message, you can see the itinerary of the morph.


Callback

You can also subclass morph to do some special behaviors when it migrates.

  • Morph>>onBeforeWarp is automatically called when a morph leaves a local image.

  • Morph>>onAfterWarp is automatically called when a morph reaches a remote image.


Remote Perform

In NetMorph, any morph have its identifier. And if you know the identifier of a morph, you can invoke a method of the morph remotelly.

  • Morph>>perform: selector of: identifier
    • and its variations...


Morph Proxy

It is awkward to hold the identifier before the migration of morph.
So there is a convenient method to return a proxy of morph.

  • Morph>>leaveProxyDo: ["migration phrase"].
will return a proxy of morph. You can use the proxy to send messages remotely.

The proxy return values and arguments are limited. You can not pass Blocks, thisContext, etc. But when the morph returns another morph, the client can automatically get the proxy of the returned morph.

For example,

worldProxy _ proxy world.

will return the proxy of the remote World.


Overriding Morph>>step

If you override Morph>>step, you can add an autonomous behavior to morph. So, you can write a network agent very easily.

For example, if you would like to write a random wanderer in the net. The #step would be:

step
self warpTo: (self worldMap allWorldNames atRandom).


In the #step, these methods are useful.

  • Morph>>worldMap returns WorldMap to know all the areas connected.
  • Morph>>address returns the current location in the WorldMap.
  • Morph>>path returns the itinerary of the morph.


Using MarkerMorph

MarkerMorph is a visual remote pointer to a morph. By using it, you can control the morph remotely. This is usable to add a central control behavior to some autonomous mourphs.

MarkerMorph is a normal morph, so it is possible to override #step. For example, if you would like to call back the remote morph periodically, you can override #step such as:

step
self morph warpToHome.

Morph>>leaveMarkerMorph returns a new MarkerMorph of the morph. (See the first example code).


[:masashi | ^umezawa]