This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
at:byexample [2008/08/11 20:34] tvcutsem * |
at:byexample [2009/10/08 14:11] tvcutsem |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Key Expressions ====== | ||
- | This page lists some of AmbientTalk' | ||
- | |||
- | ==== Remote Communication ==== | ||
- | |||
- | Here is how to send a message to a remote object, receive a reply, and deal with the exception that the reply was not received in time: | ||
- | |||
- | <code javascript> | ||
- | import / | ||
- | |||
- | when: lightbulb< | ||
- | // code to execute when the light bulb switched on | ||
- | // lightStatus refers to the return value of the | ||
- | // toggleLight method. | ||
- | } catch: TimeoutException using: { |e| | ||
- | // code to execute when the light bulb did not | ||
- | // respond before 10 seconds | ||
- | // note: this block may be executed either because | ||
- | // the message toggleLight could not be sent or was | ||
- | // not succesfully acknowledged, | ||
- | // carrying the reply was not received, or has not yet | ||
- | // been received (missed the deadline) | ||
- | } | ||
- | </ | ||
- | |||
- | The expression '' | ||
- | |||
- | ==== Discovery ==== | ||
- | |||
- | To make an object available to other actors in the ad hoc network, execute: | ||
- | |||
- | < | ||
- | def pub := export: obj as: Type; | ||
- | </ | ||
- | |||
- | where '' | ||
- | |||
- | <code javascript> | ||
- | def sub := when: Type discovered: { |obj| | ||
- | // code to execute upon discovery | ||
- | } | ||
- | </ | ||
- | |||
- | Here, '' | ||
- | |||
- | The above block is triggered upon discovering one object of the appropriate (sub)type. If one wants to trigger a block every time an object of the appropriate type is discovered, execute: | ||
- | <code javascript> | ||
- | whenever: Type discovered: { |obj| | ||
- | // code to execute upon every discovery | ||
- | } | ||
- | </ | ||
- | |||
- | ==== Failure Detection ==== | ||
- | |||
- | Given a far reference '' | ||
- | |||
- | <code javascript> | ||
- | def sub := when: ref disconnected: | ||
- | // code to execute upon disconnection | ||
- | } | ||
- | </ | ||
- | |||
- | And here is how to react to the object becoming available again: | ||
- | |||
- | <code javascript> | ||
- | def sub := when: ref reconnected: | ||
- | // code to execute upon reconnection | ||
- | } | ||
- | </ | ||
- | |||
- | In both cases, '' | ||
- | |||
- | In both of the above cases, the block is fired only once. If the block must be fired every time a disconnection/ | ||
- | |||
- | ==== Leasing ==== | ||
- | |||
- | Here's how to create a lease for an object: | ||
- | |||
- | <code javascript> | ||
- | import / | ||
- | |||
- | def l := lease: minutes(10) for: obj; | ||
- | </ | ||
- | |||
- | Here, '' | ||
- | |||
- | <code javascript> | ||
- | when: l expired: { | ||
- | // code to execute upon lease expiration | ||
- | } | ||
- | </ | ||
- | |||
- | ==== Timing ===== | ||
- | |||
- | Here's how to postpone the execution of a block of code until a certain period of time has elapsed: | ||
- | |||
- | <code javascript> | ||
- | import / | ||
- | |||
- | def sub := when: seconds(10) elapsed: { | ||
- | // code to execute after 10 seconds | ||
- | } | ||
- | </ | ||
- | |||
- | Here, '' | ||
- | |||
- | <code javascript> | ||
- | whenever: minutes(1) elapsed: { | ||
- | // code to execute after 1 minute | ||
- | } | ||
- | </ |