Networking
Macaw OS is on the Macaw network, a small internet of its own where everyone dials in to a Macaw ISP. Programs can connect to anyone dialled in to the same ISP, to the sites registered with it, and to the ISP's own services. They can't reach the internet outside. The network is closed, so nobody can use a Macaw OS program to get at the outside world, or get at Macaw OS from it.
Everything goes over the dial-up line, at the line's speed. A 56k line brings in about 5 KB a second.
Tasks
Network code has to wait. It waits to dial, for the other end to answer, and for data to arrive. A program can't stop and wait without freezing its window, so network code runs in a task. A task is a function that runs a little each frame alongside your program, and it can wait as long as it needs to.
local status = "connecting..."
local function show(s)
status = s
redraw() -- the window only draws again when asked
end
spawn(function()
if not wait_online() then show("not online"); return end -- dials if it has to
local s, err = connect("macaw.com", 80)
if not s then show(err); return end
s:send("GET / HTTP/1.0\r\nHost: macaw.com\r\n\r\n")
show(s:readline() or "no answer") -- "HTTP/1.0 200 OK"
s:close()
end, function(err) show("went wrong: " .. err) end)
function _draw()
cls(colors.paper)
text(status, 4, 4, colors.night)
end
The window isn't realtime, so it only draws again when you call redraw(). That's why show calls it each time the status changes.
| Call | What it does |
|---|---|
spawn(fn, [on_error]) | Runs fn as a task. If it crashes, on_error(message) is called |
wait_online() | Dials if Macaw OS isn't online, and waits. Returns false if the person cancels |
online() | Returns whether Macaw OS is online now |
sleep(seconds) | Waits, in a task |
Connecting
| Call | What it does |
|---|---|
connect(host, port, [seconds]) | Connects, and waits until the connection is open (for 15 seconds at most). Returns a socket, or nil and the reason |
tcp(host, port) | Starts connecting without waiting. Check s:status() to see when it's open |
Hosts are names on the Macaw network. They can be macaw.com and the other sites people register, isp.macaw for the ISP itself, or name.macaw for someone called name who's dialled in.
These are a socket's calls:
| Call | Waits? | What it does |
|---|---|---|
s:send(data) | no | Sends data |
s:readline() | yes | Returns a line without its line ending, or nil once the other end has gone |
s:read(n) | yes | Returns exactly n bytes |
s:readall([progress]) | yes | Returns everything until the other end closes. progress(bytes) is called as data comes in |
s:pollline() | no | Returns a whole line if one has arrived, or nil |
s:receive() | no | Returns whatever has arrived |
s:status() | no | Returns "connecting", "open", "closed" or "error", and a message |
s:close() | no | Closes the connection |
The calls that wait only work in a task.
Serving
Other people can connect to you at yourname.macaw. This program answers everyone who connects on port 7000, and counts them:
local visitors = 0
spawn(function()
if not wait_online() then return end
local l = assert(listen(7000))
while true do
local c = l:accept() -- doesn't wait, and is nil if nobody's there
if c then
c:send("Hello from Macaw OS\r\n")
c:close()
visitors = visitors + 1
redraw()
end
sleep(0.1)
end
end)
function _draw()
cls(colors.paper)
text(visitors .. " visitors", 4, 4, colors.night)
end
| Call | What it does |
|---|---|
listen(port) | Listens on a port. Returns a listener, or nil and the reason |
l:accept() | Returns the next person who has connected, as a socket and where they're from. Returns nil if nobody has |
l:port() | Returns the port |
l:close() | Stops listening |
When your window closes, its sockets and tasks close with it.
Programs to learn from
Macaw OS's own network programs are in /system/apps, and they're worth reading:
- Browser (
browser.lua) shows web pages over HTTP, written in HTML or Markdown. - FTP (
ftp.lua) copies files to and from FTP servers. - Hotline (
hotline.lua) does chat, news and files. - Server (
server.lua) serves web pages, FTP and a Hotline server from your/home.
The simplest way to put something on the network is a website. You can write pages in Markdown in /home/www and run Server. Or you can register a name on the Macaw OS website and edit your pages there.
The Server app
Server shares web pages, files over FTP and a Hotline chat room from your computer. Everyone on the Macaw network can reach them while you're dialled in. Its settings are in its window, and it remembers them. Stop the server before you change them.
| Setting | What it does |
|---|---|
| Name | What the server calls itself to visitors |
| Web pages, FTP files, Hotline | Turns each one on or off and sets its port. They start as 8080, 2121 and 5500 |
| Folder | Where each one's files come from. It has to be a folder inside /home, so nothing else on the disk can be shared by mistake |
| List folders that have no index page | When someone visits a folder without an index.html or index.md, shows them what's in it. Untick it and they get "not found" instead |
| Ask for a name and password | Lets only someone who signs in with the name and password you give use FTP. The password is saved in Nestchain, never in a plain file |
| Let anyone upload / Let them upload | Whether people can put files in the FTP folder, make folders and delete files. With sign-in on, only the person who signed in can |
| Welcome | The message Hotline shows people when they join |
| Admin and password | A Hotline account for you. Log in with it in Hotline and you're the admin. Your name shows in red, and you can right-click someone to disconnect them, or disconnect and ban them until the server stops. Leave the login empty for no admin |
| Let guests in, and its password | Whether people can join Hotline without the admin's login. Give a password and guests need it too. Leave it empty and anyone can join |
| Guests can post news / Guests can download | What guests may do. The admin can always do both |
With sign-in off, anyone can use FTP with any name and password (most FTP programs send anonymous). They can only read files unless you let anyone upload.
Nothing on the Hotline server works until someone has logged in. Both Hotline passwords are saved in Nestchain.
Your address on the network is name.macaw, where name is the name you dial in with. Other people visit your pages at http://name.macaw:8080/, and connect to your FTP at name.macaw on port 2121.
The FTP app
FTP copies files between your computer and an FTP server. Type the server and port on the first row. If the server asks for a name and password, type them on the second row. For servers that don't, leave the name as anonymous. Your files are on the left and the server's are on the right. Select a file and use the arrows in the middle to copy it across.