On this page:

7 A Universe for Two

All registered worlds are a part of a universe. Technically, the universe is a so-called server, a program that receives messages, processes them, replies and/or broadcasts messages of its own. Most of the time, the server just acts as a channel of communication. In the case of universe2.ss, it connects two world programs.

The server is parameterized over user-supplied code. It can thus act as either

The last option means that the server can plan the role of a "game referee."

An instantiated server accepts connections from exactly two world programs. Once it has two connections, it uses a programmer-specified function to create a server state. For every message it receives, it applies another function to determine the next server state and the messages it should broadcast.

ServerState any/c

Using the teachpack requires that you provide a data definition that captures the sate of the server. In principle, there are no constraints on this data definition. You can even keep it implicit, even if this violates the Design Recipe.


(struct player ())

Each registered world is represented with a player? structure, for which the teachpack doesn’t export any selectors. Use eq? to compare the identity of two players.

(universe initial process)  true
  initial : (-> player? player? (cons ServerState Mail))
  process : (-> ServerState player? sexp? (cons ServerState Mail))

starts the server, specifying how to initialize the server and how to process incoming messages from individual worlds.

Specifically, given the internal representations of the two participating worlds, initial produces a list that starts with the initial ServerState followed by Mails.

The process function is used every time the server receives a Message from a participating world. It is then applied to the current ServerState a representation of the message-sending world, and the Message that was received. The function produces that starts with the the new ServerState followed by Mails.

For both functions, the server deconstructs the resulting list, saves the server state, and sends out the messages specified via Mails to the appropriate players.

Mail (listof (list player? sexp?))

Mail is a list of two-element lists. The first element specifies the player to which the second element – a message–should be sent.

The picture provides a graphical overview of the server’s workings.