1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-09-29 07:20:48 +00:00

Rewrite several doc introductions

Mostly focussing on rednet and modem here. Not sure if I made them any
better, we'll see!
This commit is contained in:
Jonathan Coates 2021-12-21 00:22:16 +00:00
parent 5eedea1bbb
commit 932b77d7ee
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
10 changed files with 276 additions and 136 deletions

View File

@ -2,7 +2,7 @@
module: [kind=event] modem_message module: [kind=event] modem_message
--- ---
The @{modem_message} event is fired when a message is received on an open channel on any modem. The @{modem_message} event is fired when a message is received on an open channel on any @{modem}.
## Return Values ## Return Values
1. @{string}: The event name. 1. @{string}: The event name.
@ -10,11 +10,15 @@ The @{modem_message} event is fired when a message is received on an open channe
3. @{number}: The channel that the message was sent on. 3. @{number}: The channel that the message was sent on.
4. @{number}: The reply channel set by the sender. 4. @{number}: The reply channel set by the sender.
5. @{any}: The message as sent by the sender. 5. @{any}: The message as sent by the sender.
6. @{number}: The distance between the sender and the receiver, in blocks (decimal). 6. @{number}: The distance between the sender and the receiver, in blocks.
## Example ## Example
Prints a message when one is sent: Wraps a @{modem} peripheral, opens channel 0 for listening, and prints all received messages.
```lua ```lua
local modem = peripheral.find("modem") or error("No modem attached", 0)
modem.open(0)
while true do while true do
local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message") local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
print(("Message received on side %s on channel %d (reply to %d) from %f blocks away with message %s"):format(side, channel, replyChannel, distance, tostring(message))) print(("Message received on side %s on channel %d (reply to %d) from %f blocks away with message %s"):format(side, channel, replyChannel, distance, tostring(message)))

View File

@ -12,16 +12,19 @@ rounded up to the nearest multiple of 0.05 seconds. If you are using coroutines
or the @{parallel|parallel API}, it will only pause execution of the current or the @{parallel|parallel API}, it will only pause execution of the current
thread, not the whole program. thread, not the whole program.
**Note** Because sleep internally uses timers, it is a function that yields. :::tip
This means that you can use it to prevent "Too long without yielding" errors, Because sleep internally uses timers, it is a function that yields. This means
however, as the minimum sleep time is 0.05 seconds, it will slow your program that you can use it to prevent "Too long without yielding" errors, however, as
down. the minimum sleep time is 0.05 seconds, it will slow your program down.
:::
**Warning** Internally, this function queues and waits for a timer event (using :::caution
Internally, this function queues and waits for a timer event (using
@{os.startTimer}), however it does not listen for any other events. This means @{os.startTimer}), however it does not listen for any other events. This means
that any event that occurs while sleeping will be entirely discarded. If you that any event that occurs while sleeping will be entirely discarded. If you
need to receive events while sleeping, consider using @{os.startTimer|timers}, need to receive events while sleeping, consider using @{os.startTimer|timers},
or the @{parallel|parallel API}. or the @{parallel|parallel API}.
:::
@tparam number time The number of seconds to sleep for, rounded up to the @tparam number time The number of seconds to sleep for, rounded up to the
nearest multiple of 0.05. nearest multiple of 0.05.

View File

@ -19,10 +19,10 @@ import javax.annotation.Nonnull;
* *
* This works with energy storage blocks, as well as generators and machines which consume energy. * This works with energy storage blocks, as well as generators and machines which consume energy.
* *
* <blockquote> * :::note
* <strong>Note:</strong> Due to limitations with Forge's energy API, it is not possible to measure throughput (i.e. RF * Due to limitations with Forge's energy API, it is not possible to measure throughput (i.e. RF
* used/generated per tick). * used/generated per tick).
* </blockquote> * :::
* *
* @cc.module energy_storage * @cc.module energy_storage
*/ */

View File

@ -21,9 +21,60 @@ import java.util.HashSet;
import java.util.Set; import java.util.Set;
/** /**
* The modem peripheral allows you to send messages between computers. * Modems allow you to send messages between computers over long distances.
*
* :::tip
* Modems provide a fairly basic set of methods, which makes them very flexible but often hard to work with. The
* {@literal @}{rednet} API is built on top of modems, and provides a more user-friendly interface.
* :::
*
* ## Sending and receiving messages
* Modems operate on a series of channels, a bit like frequencies on a radio. Any modem can send a message on a
* particular channel, but only those which have {@link #open opened} the channel and are "listening in" can receive
* messages.
*
* Channels are represented as an integer between 0 and 65535 inclusive. These channels don't have any defined meaning,
* though some APIs or programs will assign a meaning to them. For instance, the @{gps} module sends all its messages on
* channel 65534 (@{gps.CHANNEL_GPS}), while @{rednet} uses channels equal to the computer's ID.
*
* - Sending messages is done with the {@link #transmit(int, int, Object)} message.
* - Receiving messages is done by listening to the @{modem_message} event.
*
* ## Types of modem
* CC: Tweaked comes with three kinds of modem, with different capabilities.
*
* <ul>
* <li><strong>Wireless modems:</strong> Wireless modems can send messages to any other wireless modem. They can be placed next to a
* computer, or equipped as a pocket computer or turtle upgrade.
*
* Wireless modems have a limited range, only sending messages to modems within 64 blocks. This range increases
* linearly once the modem is above y=96, to a maximum of 384 at world height.</li>
* <li><strong>Ender modems:</strong> These are upgraded versions of normal wireless modems. They do not have a distance
* limit, and can send messages between dimensions.</li>
* <li><strong>Wired modems:</strong> These send messages to other any other wired modems connected to the same network
* (using <em>Networking Cable</em>). They also can be used to attach additional peripherals to a computer.</li></ul>
* *
* @cc.module modem * @cc.module modem
* @cc.see modem_message Queued when a modem receives a message on an {@link #open(int) open channel}.
* @cc.see rednet A networking API built on top of the modem peripheral.
* @cc.usage Wrap a modem and a message on channel 15, requesting a response on channel 43. Then wait for a message to
* arrive on channel 43 and print it.
*
* <pre>{@code
* local modem = peripheral.find("modem") or error("No modem attached", 0)
* modem.open(43) -- Open 43 so we can receive replies
*
* -- Send our message
* modem.transmit(15, 43, "Hello, world!")
*
* -- And wait for a reply
* local event, side, channel, replyChannel, message, distance
* repeat
* event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
* until channel == 43
*
* print("Received a reply: " .. tostring(message))
* }</pre>
*/ */
public abstract class ModemPeripheral implements IPeripheral, IPacketSender, IPacketReceiver public abstract class ModemPeripheral implements IPeripheral, IPacketSender, IPacketReceiver
{ {
@ -157,12 +208,23 @@ public abstract class ModemPeripheral implements IPeripheral, IPacketSender, IPa
* Sends a modem message on a certain channel. Modems listening on the channel will queue a {@code modem_message} * Sends a modem message on a certain channel. Modems listening on the channel will queue a {@code modem_message}
* event on adjacent computers. * event on adjacent computers.
* *
* <blockquote><strong>Note:</strong> The channel does not need be open to send a message.</blockquote> * :::note
* The channel does not need be open to send a message.
* :::
* *
* @param channel The channel to send messages on. * @param channel The channel to send messages on.
* @param replyChannel The channel that responses to this message should be sent on. * @param replyChannel The channel that responses to this message should be sent on. This can be the same as
* @param payload The object to send. This can be a boolean, string, number, or table. * {@code channel} or entirely different. The channel must have been {@link #open opened} on
* the sending computer in order to receive the replies.
* @param payload The object to send. This can be any primitive type (boolean, number, string) as well as
* tables. Other types (like functions), as well as metatables, will not be transmitted.
* @throws LuaException If the channel is out of range. * @throws LuaException If the channel is out of range.
* @cc.usage Wrap a modem and a message on channel 15, requesting a response on channel 43.
*
* <pre>{@code
* local modem = peripheral.find("modem") or error("No modem attached", 0)
* modem.transmit(15, 43, "Hello, world!")
* }</pre>
*/ */
@LuaFunction @LuaFunction
public final void transmit( int channel, int replyChannel, Object payload ) throws LuaException public final void transmit( int channel, int replyChannel, Object payload ) throws LuaException

View File

@ -80,8 +80,9 @@ public abstract class WiredModemPeripheral extends ModemPeripheral implements IW
* If this computer is attached to the network, it _will not_ be included in * If this computer is attached to the network, it _will not_ be included in
* this list. * this list.
* *
* <blockquote><strong>Important:</strong> This function only appears on wired modems. Check {@link #isWireless} * :::note
* returns false before calling it.</blockquote> * This function only appears on wired modems. Check {@link #isWireless} returns false before calling it.
* :::
* *
* @param computer The calling computer. * @param computer The calling computer.
* @return Remote peripheral names on the network. * @return Remote peripheral names on the network.
@ -95,8 +96,9 @@ public abstract class WiredModemPeripheral extends ModemPeripheral implements IW
/** /**
* Determine if a peripheral is available on this wired network. * Determine if a peripheral is available on this wired network.
* *
* <blockquote><strong>Important:</strong> This function only appears on wired modems. Check {@link #isWireless} * :::note
* returns false before calling it.</blockquote> * This function only appears on wired modems. Check {@link #isWireless} returns false before calling it.
* :::
* *
* @param computer The calling computer. * @param computer The calling computer.
* @param name The peripheral's name. * @param name The peripheral's name.
@ -112,8 +114,9 @@ public abstract class WiredModemPeripheral extends ModemPeripheral implements IW
/** /**
* Get the type of a peripheral is available on this wired network. * Get the type of a peripheral is available on this wired network.
* *
* <blockquote><strong>Important:</strong> This function only appears on wired modems. Check {@link #isWireless} * :::note
* returns false before calling it.</blockquote> * This function only appears on wired modems. Check {@link #isWireless} returns false before calling it.
* :::
* *
* @param computer The calling computer. * @param computer The calling computer.
* @param name The peripheral's name. * @param name The peripheral's name.
@ -132,8 +135,9 @@ public abstract class WiredModemPeripheral extends ModemPeripheral implements IW
/** /**
* Check a peripheral is of a particular type. * Check a peripheral is of a particular type.
* *
* <blockquote><strong>Important:</strong> This function only appears on wired modems. Check {@link #isWireless} * :::note
* returns false before calling it.</blockquote> * This function only appears on wired modems. Check {@link #isWireless} returns false before calling it.
* :::
* *
* @param computer The calling computer. * @param computer The calling computer.
* @param name The peripheral's name. * @param name The peripheral's name.
@ -153,8 +157,9 @@ public abstract class WiredModemPeripheral extends ModemPeripheral implements IW
/** /**
* Get all available methods for the remote peripheral with the given name. * Get all available methods for the remote peripheral with the given name.
* *
* <blockquote><strong>Important:</strong> This function only appears on wired modems. Check {@link #isWireless} * :::note
* returns false before calling it.</blockquote> * This function only appears on wired modems. Check {@link #isWireless} returns false before calling it.
* :::
* *
* @param computer The calling computer. * @param computer The calling computer.
* @param name The peripheral's name. * @param name The peripheral's name.
@ -174,8 +179,9 @@ public abstract class WiredModemPeripheral extends ModemPeripheral implements IW
/** /**
* Call a method on a peripheral on this wired network. * Call a method on a peripheral on this wired network.
* *
* <blockquote><strong>Important:</strong> This function only appears on wired modems. Check {@link #isWireless} * :::note
* returns false before calling it.</blockquote> * This function only appears on wired modems. Check {@link #isWireless} returns false before calling it.
* :::
* *
* @param computer The calling computer. * @param computer The calling computer.
* @param context The Lua context we're executing in. * @param context The Lua context we're executing in.
@ -204,8 +210,9 @@ public abstract class WiredModemPeripheral extends ModemPeripheral implements IW
* may be used by other computers on the network to wrap this computer as a * may be used by other computers on the network to wrap this computer as a
* peripheral. * peripheral.
* *
* <blockquote><strong>Important:</strong> This function only appears on wired modems. Check {@link #isWireless} * :::note
* returns false before calling it.</blockquote> * This function only appears on wired modems. Check {@link #isWireless} returns false before calling it.
* :::
* *
* @return The current computer's name. * @return The current computer's name.
* @cc.treturn string|nil The current computer's name on the wired network. * @cc.treturn string|nil The current computer's name on the wired network.

View File

@ -1,16 +1,18 @@
--- The Disk API allows you to interact with disk drives. --[[- The Disk API allows you to interact with disk drives.
--
-- These functions can operate on locally attached or remote disk drives. To use These functions can operate on locally attached or remote disk drives. To use a
-- a locally attached drive, specify “side” as one of the six sides locally attached drive, specify side as one of the six sides (e.g. `left`); to
-- (e.g. `left`); to use a remote disk drive, specify its name as printed when use a remote disk drive, specify its name as printed when enabling its modem
-- enabling its modem (e.g. `drive_0`). (e.g. `drive_0`).
--
-- **Note:** All computers (except command computers), turtles and pocket :::tip
-- computers can be placed within a disk drive to access it's internal storage All computers (except command computers), turtles and pocket computers can be
-- like a disk. placed within a disk drive to access it's internal storage like a disk.
-- :::
-- @module disk
-- @since 1.2 @module disk
@since 1.2
]]
local function isDrive(name) local function isDrive(name)
if type(name) ~= "string" then if type(name) ~= "string" then

View File

@ -1,27 +1,30 @@
--- The GPS API provides a method for turtles and computers to retrieve their --[[- The GPS API provides a method for turtles and computers to retrieve their
-- own locations. own locations.
--
-- It broadcasts a PING message over @{rednet} and wait for responses. In order It broadcasts a PING message over @{rednet} and wait for responses. In order for
-- for this system to work, there must be at least 4 computers used as gps hosts this system to work, there must be at least 4 computers used as gps hosts which
-- which will respond and allow trilateration. Three of these hosts should be in will respond and allow trilateration. Three of these hosts should be in a plane,
-- a plane, and the fourth should be either above or below the other three. The and the fourth should be either above or below the other three. The three in a
-- three in a plane should not be in a line with each other. You can set up plane should not be in a line with each other. You can set up hosts using the
-- hosts using the gps program. gps program.
--
-- **Note**: When entering in the coordinates for the host you need to put in :::note
-- the `x`, `y`, and `z` coordinates of the computer, not the modem, as all When entering in the coordinates for the host you need to put in the `x`, `y`,
-- rednet distances are measured from the block the computer is in. and `z` coordinates of the computer, not the modem, as all modem distances are
-- measured from the block the computer is in.
-- Also note that you may choose which axes x, y, or z refers to - so long as :::
-- your systems have the same definition as any GPS servers that're in range, it
-- works just the same. For example, you might build a GPS cluster according to Also note that you may choose which axes x, y, or z refers to - so long as your
-- [this tutorial][1], using z to account for height, or you might use y to systems have the same definition as any GPS servers that're in range, it works
-- account for height in the way that Minecraft's debug screen displays. just the same. For example, you might build a GPS cluster according to [this
-- tutorial][1], using z to account for height, or you might use y to account for
-- [1]: http://www.computercraft.info/forums2/index.php?/topic/3088-how-to-guide-gps-global-position-system/ height in the way that Minecraft's debug screen displays.
--
-- @module gps [1]: http://www.computercraft.info/forums2/index.php?/topic/3088-how-to-guide-gps-global-position-system/
-- @since 1.31
@module gps
@since 1.31
]]
local expect = dofile("rom/modules/main/cc/expect.lua").expect local expect = dofile("rom/modules/main/cc/expect.lua").expect

View File

@ -66,7 +66,7 @@ type.
What is a peripheral type though? This is a string which describes what a What is a peripheral type though? This is a string which describes what a
peripheral is, and so what functions are available on it. For instance, speakers peripheral is, and so what functions are available on it. For instance, speakers
are just called `"speaker"`, and monitors `"monitor"`. Some peripherals might are just called `"speaker"`, and monitors `"monitor"`. Some peripherals might
have more than one type; a Minecraft chest is both a `"minecraft:chest"` and have more than one type - a Minecraft chest is both a `"minecraft:chest"` and
`"inventory"`. `"inventory"`.
You can get all the types a peripheral has with @{peripheral.getType}, and check You can get all the types a peripheral has with @{peripheral.getType}, and check

View File

@ -1,21 +1,48 @@
--- The Rednet API allows systems to communicate between each other without --[[- The Rednet API allows computers to communicate between each other by using
-- using redstone. It serves as a wrapper for the modem API, offering ease of @{modem|modems}. It provides a layer of abstraction on top of the main @{modem}
-- functionality (particularly in regards to repeating signals) with some peripheral, making it slightly easier to use.
-- expense of fine control.
-- ## Basic usage
-- In order to send and receive data, a modem (either wired, wireless, or ender) In order to send a message between two computers, each computer must have a
-- is required. The data reaches any possible destinations immediately after modem on one of its sides (or in the case of pocket computers and turtles, the
-- sending it, but is range limited. modem must be equipped as an upgrade). The two computers should then call
-- @{rednet.open}, which sets up the modems ready to send and receive messages.
-- Rednet also allows you to use a "protocol" - simple string names indicating
-- what messages are about. Receiving systems may filter messages according to Once rednet is opened, you can send messages using @{rednet.send} and receive
-- their protocols, thereby automatically ignoring incoming messages which don't them using @{rednet.receive}. It's also possible to send a message to _every_
-- specify an identical string. It's also possible to @{rednet.lookup|lookup} rednet-using computer using @{rednet.broadcast}.
-- which systems in the area use certain protocols, hence making it easier to
-- determine where given messages should be sent in the first place. :::caution Network security
--
-- @module rednet While rednet provides a friendly way to send messages to specific computers, it
-- @since 1.2 doesn't provide any guarantees about security. Other computers could be
listening in to your messages, or even pretending to send messages from other computers!
If you're playing on a multi-player server (or at least one where you don't
trust other players), it's worth encrypting or signing your rednet messages.
:::
## Protocols and hostnames
Several rednet messages accept "protocol"s - simple string names describing what
a message is about. When sending messages using @{rednet.send} and
@{rednet.broadcast}, you can optionally specify a protocol for the message. This
same protocol can then be given to @{rednet.receive}, to ignore all messages not
using this protocol.
It's also possible to look-up computers based on protocols, providing a basic
system for service discovery and [DNS]. A computer can advertise that it
supports a particular protocol with @{rednet.host}, also providing a friendly
"hostname". Other computers may then find all computers which support this
protocol using @{rednet.lookup}.
[DNS]: https://en.wikipedia.org/wiki/Domain_Name_System "Domain Name System"
@module rednet
@since 1.2
@see rednet_message Queued when a rednet message is received.
@see modem Rednet is built on top of the modem peripheral. Modems provide a more
bare-bones but flexible interface.
]]
local expect = dofile("rom/modules/main/cc/expect.lua").expect local expect = dofile("rom/modules/main/cc/expect.lua").expect
@ -46,9 +73,17 @@ This will open the modem on two channels: one which has the same
@tparam string modem The name of the modem to open. @tparam string modem The name of the modem to open.
@throws If there is no such modem with the given name @throws If there is no such modem with the given name
@usage Open a wireless modem on the back of the computer. @usage Open rednet on the back of the computer, allowing you to send and receive
rednet messages using it.
rednet.open("back") rednet.open("back")
@usage Open rednet on all attached modems. This abuses the "filter" argument to
@{peripheral.find}.
peripheral.find("modem", rednet.open)
@see rednet.close
@see rednet.isOpen
]] ]]
function open(modem) function open(modem)
expect(1, modem, "string") expect(1, modem, "string")
@ -65,6 +100,7 @@ end
-- @tparam[opt] string modem The side the modem exists on. If not given, all -- @tparam[opt] string modem The side the modem exists on. If not given, all
-- open modems will be closed. -- open modems will be closed.
-- @throws If there is no such modem with the given name -- @throws If there is no such modem with the given name
-- @see rednet.open
function close(modem) function close(modem)
expect(1, modem, "string", "nil") expect(1, modem, "string", "nil")
if modem then if modem then
@ -90,6 +126,7 @@ end
-- modems will be checked. -- modems will be checked.
-- @treturn boolean If the given modem is open. -- @treturn boolean If the given modem is open.
-- @since 1.31 -- @since 1.31
-- @see rednet.open
function isOpen(modem) function isOpen(modem)
expect(1, modem, "string", "nil") expect(1, modem, "string", "nil")
if modem then if modem then
@ -109,15 +146,17 @@ function isOpen(modem)
end end
--[[- Allows a computer or turtle with an attached modem to send a message --[[- Allows a computer or turtle with an attached modem to send a message
intended for a system with a specific ID. At least one such modem must first intended for a sycomputer with a specific ID. At least one such modem must first
be @{rednet.open|opened} before sending is possible. be @{rednet.open|opened} before sending is possible.
Assuming the target was in range and also had a correctly opened modem, it Assuming the target was in range and also had a correctly opened modem, the
may then use @{rednet.receive} to collect the message. target computer may then use @{rednet.receive} to collect the message.
@tparam number recipient The ID of the receiving computer. @tparam number recipient The ID of the receiving computer.
@param message The message to send. This should not contain coroutines or @param message The message to send. Like with @{modem.transmit}, this can
functions, as they will be converted to @{nil}. contain any primitive type (numbers, booleans and strings) as well as
tables. Other types (like functions), as well as metatables, will not be
transmitted.
@tparam[opt] string protocol The "protocol" to send this message under. When @tparam[opt] string protocol The "protocol" to send this message under. When
using @{rednet.receive} one can filter to only receive messages sent under a using @{rednet.receive} one can filter to only receive messages sent under a
particular protocol. particular protocol.
@ -174,16 +213,19 @@ function send(recipient, message, protocol)
return sent return sent
end end
--- Broadcasts a string message over the predefined @{CHANNEL_BROADCAST} --[[- Broadcasts a string message over the predefined @{CHANNEL_BROADCAST}
-- channel. The message will be received by every device listening to rednet. channel. The message will be received by every device listening to rednet.
--
-- @param message The message to send. This should not contain coroutines or @param message The message to send. This should not contain coroutines or
-- functions, as they will be converted to @{nil}. functions, as they will be converted to @{nil}. @tparam[opt] string protocol
-- @tparam[opt] string protocol The "protocol" to send this message under. When The "protocol" to send this message under. When using @{rednet.receive} one can
-- using @{rednet.receive} one can filter to only receive messages sent under a filter to only receive messages sent under a particular protocol.
-- particular protocol. @see rednet.receive
-- @see rednet.receive @changed 1.6 Added protocol parameter.
-- @changed 1.6 Added protocol parameter. @usage Broadcast the words "Hello, world!" to every computer using rednet.
rednet.broadcast("Hello, world!")
]]
function broadcast(message, protocol) function broadcast(message, protocol)
expect(2, protocol, "string", "nil") expect(2, protocol, "string", "nil")
send(CHANNEL_BROADCAST, message, protocol) send(CHANNEL_BROADCAST, message, protocol)
@ -263,25 +305,25 @@ function receive(protocol_filter, timeout)
end end
end end
--- Register the system as "hosting" the desired protocol under the specified --[[- Register the system as "hosting" the desired protocol under the specified
-- name. If a rednet @{rednet.lookup|lookup} is performed for that protocol (and name. If a rednet @{rednet.lookup|lookup} is performed for that protocol (and
-- maybe name) on the same network, the registered system will automatically maybe name) on the same network, the registered system will automatically
-- respond via a background process, hence providing the system performing the respond via a background process, hence providing the system performing the
-- lookup with its ID number. lookup with its ID number.
--
-- Multiple computers may not register themselves on the same network as having Multiple computers may not register themselves on the same network as having the
-- the same names against the same protocols, and the title `localhost` is same names against the same protocols, and the title `localhost` is specifically
-- specifically reserved. They may, however, share names as long as their hosted reserved. They may, however, share names as long as their hosted protocols are
-- protocols are different, or if they only join a given network after different, or if they only join a given network after "registering" themselves
-- "registering" themselves before doing so (eg while offline or part of a before doing so (eg while offline or part of a different network).
-- different network).
-- @tparam string protocol The protocol this computer provides.
-- @tparam string protocol The protocol this computer provides. @tparam string hostname The name this protocol exposes for the given protocol.
-- @tparam string hostname The name this protocol exposes for the given protocol. @throws If trying to register a hostname which is reserved, or currently in use.
-- @throws If trying to register a hostname which is reserved, or currently in use. @see rednet.unhost
-- @see rednet.unhost @see rednet.lookup
-- @see rednet.lookup @since 1.6
-- @since 1.6 ]]
function host(protocol, hostname) function host(protocol, hostname)
expect(1, protocol, "string") expect(1, protocol, "string")
expect(2, hostname, "string") expect(2, hostname, "string")
@ -306,21 +348,38 @@ function unhost(protocol)
hostnames[protocol] = nil hostnames[protocol] = nil
end end
--- Search the local rednet network for systems @{rednet.host|hosting} the --[[- Search the local rednet network for systems @{rednet.host|hosting} the
-- desired protocol and returns any computer IDs that respond as "registered" desired protocol and returns any computer IDs that respond as "registered"
-- against it. against it.
--
-- If a hostname is specified, only one ID will be returned (assuming an exact If a hostname is specified, only one ID will be returned (assuming an exact
-- match is found). match is found).
--
-- @tparam string protocol The protocol to search for. @tparam string protocol The protocol to search for.
-- @tparam[opt] string hostname The hostname to search for. @tparam[opt] string hostname The hostname to search for.
--
-- @treturn[1] { number }|nil A list of computer IDs hosting the given @treturn[1] number... A list of computer IDs hosting the given protocol.
-- protocol, or @{nil} if none exist. @treturn[2] number|nil The computer ID with the provided hostname and protocol,
-- @treturn[2] number|nil The computer ID with the provided hostname and protocol, or @{nil} if none exists.
-- or @{nil} if none exists. @since 1.6
-- @since 1.6 @usage Find all computers which are hosting the `"chat"` protocol.
local computers = {rednet.lookup("chat")}
print(#computers .. " computers available to chat")
for _, computer in pairs(computers) do
print("Computer #" .. computer)
end
@usage Find a computer hosting the `"chat"` protocol with a hostname of `"my_host"`.
local id = rednet.lookup("chat", "my_host")
if id then
print("Found my_host at computer #" .. id)
else
printError("Cannot find my_host")
end
]]
function lookup(protocol, hostname) function lookup(protocol, hostname)
expect(1, protocol, "string") expect(1, protocol, "string")
expect(2, hostname, "string", "nil") expect(2, hostname, "string", "nil")

View File

@ -36,7 +36,7 @@ local encoder = dfpwm.make_encoder()
local decoder = dfpwm.make_decoder() local decoder = dfpwm.make_decoder()
local out = fs.open("speedy.dfpwm", "wb") local out = fs.open("speedy.dfpwm", "wb")
for input in io.lines("my_audio_track.dfpwm", 16 * 1024 * 2) do for input in io.lines("data/example.dfpwm", 16 * 1024 * 2) do
local decoded = decoder(input) local decoded = decoder(input)
local output = {} local output = {}