This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
at:introduction [2007/04/07 11:50] tvcutsem added |
at:introduction [2008/07/15 12:19] tvcutsem * |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ===== What is AmbientTalk? | ||
- | {{: | ||
- | |||
- | For the programming language adepts: AmbientTalk is: | ||
- | * **dynamically typed**, which is **not** the same as being untyped: AmbientTalk //values// are typed, but // | ||
- | * object-oriented, | ||
- | * event-driven: | ||
- | * distributed: | ||
- | * **symbiotic**: | ||
- | |||
- | A word of warning, though: AmbientTalk is **not** a stable development platform. Rather, it is a research artifact used as a " | ||
- | |||
- | ==== AmbientTalk in a nutshell ==== | ||
- | |||
- | Enough talking, let's delve into some AmbientTalk code by means of a simple example to see whether we can spark your interest in AmbientTalk. The demo program we're about to explore is an extremely simple instant messenger program. Each instant messenger runs on e.g. a PDA, laptop or cell phone. When peers join the ad hoc network, the instant messenger should issue a warning to the user that a new buddy is available for chatting. The user can then send simple text messages to that buddy. When peers leave the ad hoc network, the user should also be notified. Off we go. | ||
- | |||
- | === Design === | ||
- | |||
- | Here is the skeleton code for the instant messenger: | ||
- | |||
- | < | ||
- | def createInstantMessenger(username) { | ||
- | def TextMessage := ...; // object encapsulating a text message | ||
- | def buddyList := ...; // maps buddy names to remote IM objects | ||
- | |||
- | // the interface to the IM for local objects | ||
- | def localInterface := object: { | ||
- | def sendTextMessage(to, | ||
- | }; | ||
- | // the interface to the IM for remote objects | ||
- | def remoteInterface := object: { | ||
- | def receiveTextMessage(textMessage, | ||
- | def getName(replyTo) { ... }; | ||
- | }; | ||
- | | ||
- | // engage in peer-to-peer service discovery | ||
- | defstripe InstantMessenger; | ||
- | export: remoteInterface as: InstantMessenger; | ||
- | whenever: InstantMessenger discovered: { |messenger| | ||
- | ... | ||
- | }; | ||
- | localInterface; | ||
- | }; | ||
- | </ | ||
- | |||
- | An instant messenger object is created by invoking a function called '' | ||
- | |||
- | Within the lexical scope of the '' | ||
- | |||
- | The '' | ||
- | |||
- | === Implementation === | ||
- | |||
- | Let's delve into the method bodies and see how the instant messengers engage in distributed communication: | ||
- | |||
- | < | ||
- | def createInstantMessenger(username) { | ||
- | def TextMessage := isolate: { | ||
- | def from := nil; | ||
- | def content := nil; | ||
- | def init(f,c) { | ||
- | from := f; | ||
- | content := c; | ||
- | }; | ||
- | def printMessage() { | ||
- | system.println(from + ": " + content) | ||
- | }; | ||
- | }; | ||
- | def buddyList := jlobby.java.util.HashMap.new(); | ||
- | </ | ||
- | |||
- | The '' | ||
- | |||
- | Also interesting is the initialisation code of the '' | ||
- | |||
- | < | ||
- | def localInterface := object: { | ||
- | def sendTextMessage(to, | ||
- | def buddy := buddyList.get(to); | ||
- | if: (nil == buddy) then: { | ||
- | system.println(" | ||
- | } else: { | ||
- | def msg := TextMessage.new(username, | ||
- | buddy< | ||
- | def uponReceipt() { | ||
- | msg.printMessage(); | ||
- | } | ||
- | }); | ||
- | }; | ||
- | }; | ||
- | }; | ||
- | |||
- | def remoteInterface := object: { | ||
- | def receiveTextMessage(textMessage, | ||
- | textMessage.printMessage(); | ||
- | replyTo< | ||
- | }; | ||
- | def getName(replyTo) { replyTo< | ||
- | }; | ||
- | | ||
- | defstripe InstantMessenger; | ||
- | export: remoteInterface as: InstantMessenger; | ||
- | whenever: InstantMessenger discovered: { |messenger| | ||
- | messenger< | ||
- | def receive(name) { | ||
- | if: (nil == buddyList.get(name)) then: { | ||
- | buddyList.put(name, | ||
- | system.println(" | ||
- | when: messenger disconnected: | ||
- | system.println(" | ||
- | }; | ||
- | when: messenger reconnected: | ||
- | system.println(" | ||
- | }; | ||
- | }; | ||
- | }; | ||
- | }); | ||
- | }; | ||
- | |||
- | localInterface; | ||
- | }; | ||
- | </ | ||
- | |||
- | Notice the initialisation of the '' | ||
- | < | ||
- | The buddy list is actually |