on 514b5fc help sign in
70 lines 1.9 kB history raw
1+++
2title = "Scry"
3weight = 5
4template = "doc.html"
5+++
7Here's an example of a thread that scries ames for the IP address & port of a ship and nicely prints it:
9#### `get-ip.hoon`
11```hoon
12/- spider
13/+ strandio
14=, strand=strand:spider
15=, strand-fail=strand-fail:libstrand:spider
16|%
17++ process-lanes
18 |= [target=@p lanes=(list lane:ames)]
19 =/ m (strand ,~)
20 ^- form:m
21 ?~ `(list lane:ames)`lanes
22 %- (slog leaf+"No route for {(scow %p target)}." ~)
23 (pure:m ~)
24 =/ lroute (skip lanes |=(a=lane:ames -.a))
25 ?~ lroute
26 %- (slog leaf+"No direct route for {(scow %p target)}." ~)
27 (pure:m ~)
28 =/ ip +:(scow %if p.i.lroute)
29 =/ port (skip (scow %ud (cut 5 [1 1] p.i.lroute)) |=(a=@tD =(a '.')))
30 %- (slog leaf+"{ip}:{port}" ~)
31 (pure:m ~)
32--
33^- thread:spider
34|= arg=vase
35=/ m (strand ,vase)
36^- form:m
37=/ utarget !< (unit @p) arg
38?~ utarget
39 (strand-fail %no-arg ~)
40=/ target u.utarget
41;< lanes=(list lane:ames) bind:m (scry:strandio (list lane:ames) /ax//peers/(scot %p target)/forward-lane)
42;< ~ bind:m (process-lanes target lanes)
43(pure:m !>(~))
44```
46**Note:** Pretty useless on a fake ship.
48Save as `ted/get-ip.hoon` in the `%base` desk, `|commit %base`, and run it with `-get-ip ~bitbet-bolbel`. You should see something like:
50```
5134.83.113.220:60659
52```
54### Analysis
56Here we use the `strandio` function `scry` which takes an argument of `[mold path]` where:
58- `mold` is the return type of the scry
59- `path` is the scry path formatted like:
60 1. vane letter and care
61 2. desk if scrying arvo or agent if scrying a gall agent
62 3. rest of path
64In our case the mold is `(list lane:ames)` and the path is `/ax//peers/(scot %p target)/forward-lane` like:
66```hoon
67;< lanes=(list lane:ames) bind:m (scry:strandio (list lane:ames) /ax//peers/(scot %p target)/forward-lane)
68```
70After that we just process the result in `++ process-lanes` and print it.