on 93773de help sign in
375 lines 13 kB history raw
1---
2next: false
3sort: 2
4title: Network messages
5---
7# Network messages
9Now we've learned enough hoon to do more interesting things.
10Let's get our planets to talk to each other.
12All we've written up until now are just shell
13commands that produce a value and then disappear. To listen for and receive messages from other planets, we'll need an app--let's look at a very simple one:
15```
16:: There is no love that is not an echo
17::
18:::: /hoon/echo/ape
19 ::
20/? 314
21!:
22|_ {bowl state/$~}
23++ poke-noun
24 |= arg/*
25 ^- {(list) _+>.$}
26 ~& [%argument arg]
27 [~ +>.$]
28--
29```
32This is a very simple app that does only one thing. If you poke
33it with a value, it prints that value out. To try this out, you
34have to start the app, then you can poke it from the command line
35with the following commands:
37```
38~fintud-macrep:dojo> |start %echo
39>=
40~fintud-macrep:dojo> :echo 5
41[%argument 5]
42>=
43~fintud-macrep:dojo> :echo [1 2]
44[%argument [1 2]]
45>=
46```
48<blockquote class="blockquote">
49There is currently a bug where the `%argument` lines are
50printed *above* the line you entered, so your output may not
51look exactly like this.
52`>=` means that a command was successfully received and
53executed.
54</blockquote>
56Most of the app code should be simple enough to guess its
57function. The important part of this code is the definition of
58`++poke-noun`.
60Once an app starts, it's always running in the background and you
61interact with it by sending it messages. The most
62straightforward way to do that is to poke it from the command
63line, which we we did with `:echo 5` (`:[app-name]
64[argument(s)]).
66In this case, `++poke-noun` takes an argument `arg` and prints it
67out with `~&`. This is an unusual rune that formally "does
68nothing", but the interpreter detects it and printfs the first
69child, before executing the second as if the first didn't exist.
70This is a slightly hacky way of printing to the console, but
71we'll get to the correct way later on.
73But what does `++poke-noun` produce? Recall that `^-` casts to a
74type. In this case, it's declaring that end result of the
75function will be of type `{(list) _+>.$}`. But what does this
76mean?
78The phrase to remember is "a list of moves and our state". Urbit
79is a message passing system, so whenever we want to do something that
80interacts with the rest of the system, we send a message. Thus, a
81move is arvo's equivalent of a syscall. The first thing that
82`++poke-noun` produces is a list of messages, called
83"moves". In this case, we don't actually want the system to do
84anything, so we produce the empty list, `~` (in the `[~ +>.$]`
85line).
87The second thing `++poke-noun` produces is our state. `+>.$`
88refers to a particular address in our subject where our formal
89app state is stored. It'll become clear why this is later on,
90but for now pretend that `+>.$` is a magic invocation that means
91"app state".
93Let's look at another example. Say we want to only accept a
94number, and then print out the square of that number.
96```
97/? 314
98!:
99|_ {bowl state=~}
100::
101++ poke-atom
102 |= arg/@
103 ^- {(list) _+>.$}
104 ~& [%square (mul arg arg)]
105 [~ +>.$]
106--
107```
109A few things have changed. Firstly, we no longer accept
110arbitrary nouns because we can only square atoms (integers, in
111this case an unsigned one). Thus, our argument is now `arg=@`.
112Secondly, it's `++poke-atom` rather than `++poke-noun`.
114# Intro to marks
116Are there other `++poke`s? Yes. In fact, `noun` and
117`atom` are just two of arbitrarily many "marks". A mark is
118fundamentally a type definition, but accessible at the arvo
119level. Each mark is defined in the `/mar` directory. Some marks
120have conversion routines to other marks, and some have diff,
121patch, and merge algorithms. None of these are required for a
122mark to exist, however.
124`noun` and `atom` are two of dozens of predefined marks (that will
125already be in your `/mar` directory), and the
126user may add more at will. The type associated with `noun` is
127`*`, and the type associated with `atom` is `@`.
129When we poke an app from anywhere, we do so with a mark that
130searches for the corresponding (`++poke-[mark]`) arm. Data
131constructed on the command line is by default marked with `noun`.
132In this case, the app is expecting an atom, so we have to
133explicitly mark the data with `atom` using `&[mark]`. Try the following
134commands:
136```
137~fintud-macrep:dojo> |start %square
138>=
139~fintud-macrep:dojo> :square 6
140gall: %square: no poke arm for noun
141~fintud-macrep:dojo> :square &atom 6
142[%square 36]
143>=
144```
146> Recall the bug where `%square` may get printed above the input
147> line.
149Marks are powerful, and they're the backbone of urbit's data
150pipeline, so we'll be getting quite used to them.
152**Exercises**:
154- Write an app that computes fizzbuzz on its input (as in the
155 previous section).
157- One way of representing strings is with double quoted strings
158 called "tapes". The hoon type is `++tape`, Write an app that
159 accepts a tape and prints out `(flop argument)`, where
160 `argument` is the input. What does this do?
163# Sending a message to another ship
165Let's write our first network message! Here's `/app/pong.hoon`:
167```
168/? 314
169|%
170 ++ move {bone term wire *}
171--
172!:
173|_ {bowl state=$~}
174::
175++ poke-urbit
176 |= to/@p
177 ^- [(list move) _+>.$]
178 [[[ost %poke /sending [to %pong] %atom 'howdy'] ~] +>.$]
179::
180++ poke-atom
181 |= arg/@
182 ^- (list move) _+>.$}
183 ~& [%receiving (@t arg)]
184 [~ +>.$]
185::
186++ coup |=(* [~ +>.$])
187--
188```
190Run it with these commands:
192```
193~fintud-macrep:dojo> |start %pong
194>=
195~fintud-macrep:dojo> :pong &urbit ~sampel-sipnym
196>=
197```
199Replace `~sampel-sipnym` with another urbit. The easiest thing to
200do is to start a [moon](), a sub-identity of your urbit. Don't
201forget to start the `%pong` app on that urbit too. You should
202see, on the foreign urbit, this output:
204```
205[%receiving 'howdy']
206```
208Most of the code should be straightforward. In `++poke-atom`,
209the only new thing is the expression `(@t arg)`. As we already
210know, `@t` is the type of "cord" text strings. In Hoon, when types are called as functions, they serve as a validator function (called a "clam" in hoon) -- that is, a function whose domain is all nouns and range is the given type.
212In simpler terms, if a validator function is passed a value that
213falls within its type, that value is produced. Otherwise, it
214produces the default value (aka the "bunt") of its type. Here we call
215this `@t` function on the argument. This coerces the argument
216to text, so that we can print it out prettily no matter what
217we're passed.
219The more interesting part is in `++poke-urbit`. The `urbit` mark
220is an urbit identity, and the hoon type associated with it is
221`@p` (the "p" stands for "phonetic base").
223Recall that in a `++poke` arm we produce "a list of moves and our
224state". Until now, we've left the list of moves empty, since we
225haven't wanted to tell arvo to do anything in particular. Now we
226want to send a message to another urbit. Thus, we produce a list
227with one element:
229```
230[ost %poke /sending [to-urbit-address %pong] %atom 'howdy']
231```
233### Moves
235The general form of a move is
237-arvo part---->
238`[bone term wire *]`
239 >-app/vane-specific part of move
241Or, in pseudo-code:
243`["cause" sys-call/action tack-new-layer-on-cause action-specific
244information]`
246Let's walk through each of these elements step by step
248#### Bones ("cause")
250If you look up `++bone` in `hoon.hoon`, you'll see that it's a
251number (`@ud`), and that it's an opaque reference to a duct.
252`++duct` in hoon.hoon is a list of `wire`s, where `wire` is an
253alias for `path`. `++path` is a list of `span`s, which are ASCII
254text. Thus, a duct is a list of paths, and a bone is an opaque
255reference to it (in the same way that a Unix file
256descriptor is an opaque reference to a file structure). Thus, to
257truly understand bones, we must understand ducts.
259A duct is stack of causes, again, represented as paths, which are
260called wires. At the bottom of every duct is a unix event, such
261as a keystroke, network packet, file change, or timer event.
262When arvo is given this event, it routes the event to appropriate
263kernel module for handling.
265Sometimes, the module can immediately handle the event and
266produce any necessary results. For example, when we poked the
267`++poke-atom` arm above, we poked %gall, our application server,
268which was able to respond to our poke directly. When the module
269cannot service the request itself, it sends instructions to another kernel
270module or application (the the %gall module) to do a specified action, and produces the result from that.
272Furthermore, when one module sends a message to another kernel module or application, it also sends along the duct it was given
273with its new wire tacked onto the end. Now the duct has two entries,
274with the unix even on the bottom and the kernel module that
275handled it next. This process can continue indefinitely, adding
276more and more layers onto the duct. When an entity finally
277produces a result, a layer is popped off the duct, and the result
278is passed all the way back down.
280In effect, a duct is an arvo-level call stack. It's worth noting
281that while in traditional call stacks a function call happens
282synchronously and returns exactly once. In arvo, multiple moves
283can be sent at once, are evaluated asynchronously, and each
284one may be responded to zero or more times.
286The point to take home is that whatever caused `++poke-urbit` to be
287called is also the root cause for the network message we're
288trying to send. Thus, we say to send the network message along the given bone
289`ost`.
291##### Wire ('tack on new layer to duct')
293Of course, we have to push a new layer onto our duct
294before passing it along (or responding to it directly) anywhere. This
295layer can have any data we want in it, but we don't need anything
296specific here, so we just use the wire `/sending`
297(`/elem1/elem2/elem n` is one syntax used to create `++path`s and
298`++wire`s of n elements). If we were expecting a response (which
299we're not), it would come back along the `/sending` wire, meaning
300that the path `/sending` will be passed back to the response
301handler as an argument. Although it's not required, it's
302generally a good idea to make the wire human-readable for
303bug-handling purposes.
305##### Term (sys-call)
307Looking back at the general form of a move, there is a
308`term`--which is text composed lowercase ascii and `-`, and has
309the sign of `@tas`-- which in this case is `%poke`. This is the
310name of the particular kind of move we're sending. If you think
311of a move as a syscall (which you should), then this `term` is
312the name of the syscall. Common ones include: `%poke`, to
313message an app; `%warp`, to read from the filesystem; `%wait`, to
314set a timer; and `%them`, to send an http request.
316The general form ends with `*` (a noun) since each type of move takes
317different data. In our case, a `%poke` move takes a target
318(urbit and app) and marked data and pokes the arm of the
319corresponding mark on that app on that urbit
320with that data. `[to-urbit-address %pong]` is the target urbit and app,
321`%atom` is the mark`, and `'howdy'` is the data.
323When arvo receives a `%poke` move, it calls the appropriate
324`++poke`. The same mechanism is used for sending messages
325between apps on the same urbit as for sending messages between
326apps on different urbits.
328> We said earlier that we're not expecting a response. This is
329> not entirely true: the `++coup` is called when we receive
330> acknowledgment that the `++poke` was called. We don't do
331> anything with this information right now, but we could.
333**Exercises**:
335- Extend either of the apps in the first two exercises to accept
336 input over the network in the same way as `pong`.
338- Modify `pong` to print out a message when it receives acknowledgement.
340- Write two apps, `even` and `odd`. When you pass an atom to
341 `even`, check whether it's even. If so, divide it by two and
342 recurse; otherwise, poke `odd` with it. When `odd` recieves
343 an atom, check whether it's equal to one. If so, terminate,
344 printing "%success". Otherwise, check whether it's odd. If
345 so, multiply it by three, add one, and recurse; otherwise, poke
346 `even` with it. When either app receives a number, print it out along with the name of the app. In the end, you should be able to watch Collatz's
347 conjecture play out between the two apps. Sample output:
349```
350~fintud-macrep:dojo> :even &atom 18
351[%even 18]
352[%odd 9]
353[%even 28]
354[%even 14]
355[%odd 7]
356[%even 22]
357[%odd 11]
358[%even 34]
359[%odd 17]
360[%even 52]
361[%even 26]
362[%odd 13]
363[%even 40]
364[%even 20]
365[%even 10]
366[%odd 5]
367[%even 16]
368[%even 8]
369[%even 4]
370[%even 2]
371%success
372```
374- Put `even` and `odd` on two separate ships and pass the
375 messages over the network.