05 Receive H
The receive handler’s job is to update a world’s model in response to receiving a message. The signature of the receive handler is:
receive-h: model message -> model
In a fancy multi-player system, your world would only receive messages that were intended for you.
We are using a very “generic” server, where every world gets messages for every other world, so the first thing you should do in your receive handler is to see if your own id matches the “to” id in the message.
Description
If the message is being sent to the current world, then the color in th model changes to be the one sent in the message. If the message is not being sent to the current world, there should be no change in the model.
Examples
This example is from the perspective of world 1, which is “orange” initially. A message comes from world 2 to world 1 with the color “light blue”. World 1 should react by changing color to light blue.
(check-expect (receive-h (make-ic 1 "orange")
(make-ic-msg 2 1 "light blue"))
(make-ic 1 "light blue"))
Note that other worlds will also “hear” the same message, and should ignore it. The check below shows that a “green” world 3 does not react to a message telling world 1 to change to light blue.
(check-expect (receive-h (make-ic 3 "light green")
(make-ic-msg 2 1 "light blue"))
(make-ic 3 "light green"))
Coding
You should have enough coding experience now to write the code for the receive handler.
Installation
When your receive handler passes the tests, add an on-receive
clause
to your big bang, like this:
(big-bang (make-ic 3 "light green")
(on-receive receive-h)
[...])
Warning: receive
is a commonly misspelled word!