On this page:
sexp?
package
on-tick-event
on-key-event
on-mouse-event
LOCALHOST
register
on-receive-event
Version: 4.1.2.4

6 The World is not Enough

The world teachpack empowers students to design programs with interactive graphical user interfaces, mostly simulations, animations, and games. The universe teachpack complements world, allowing students to connect two independent world-based programs running on different computers, possibly far away.

A world-based program may communicate with the rest of the universe via messages, which the program may both send and receive.

Message S-expressions sexp?

A message is an S-expression, which is one of:

Note the last clauses includes empty of course.

(sexp? x)  boolean?
  x : any/c

determines whether x is an S-expression.

From now on, each world-producing callback in a world program – those for handling tick events, keyboard events, and mouse events – may produce a Package in addition to just a World.

(struct package (world message))
  world : any/c
  message : sexp?

If a callback produces a package, the content of the world field becomes the next world and the message field specifies what the world broadcasts about itself. The world teachpack does not provide selectors for the package structure. [?]

(on-tick-event tock)  true
  tock : 
(-> World
    (or/c World package?))

(on-key-event change)  true
  change : 
(-> World key-event?
    (or/c World package?))

(on-mouse-event clack)  true
  clack : 
(-> World natural-number/c natural-number/c MouseEvent
    (or/c World package?))

Messages are only sent out if the world program has registered with a server. Registration must specify an internet address also known as a IP address.

Here a IP address is a string, e.g., "192.168.1.1". Alternatively, the constant LOCALHOST specifies a universe server that runs on your computer.

LOCALHOST : string?

your computer, in IP speak

(register ip)  true
  ip : string?
(register ip name)  true
  ip : string?
  name : sexp?

connect to the specified server at ip and set up capabilities for sending and receiving messages.

Finally, the receipt of a message from the server is an event, just like tick events, keyboard events, and mouse events. Of course, the world teachpack provides a mechanism for installing a callback for message reception.

(on-receive-event receive)  true
  receive : 
(-> World sexp?
    (or/c World package?))

tell DrScheme to call "receive" for every message receipt, on the current World and the received message. The result of the call becomes the current world. Because receive is a world-transforming function, it too can produce a package instead of just a world.

The picture below summarizes the extensions