Programming Macaw OS

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.

CallWhat 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

CallWhat 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:

CallWaits?What it does
s:send(data)noSends data
s:readline()yesReturns a line without its line ending, or nil once the other end has gone
s:read(n)yesReturns exactly n bytes
s:readall([progress])yesReturns everything until the other end closes. progress(bytes) is called as data comes in
s:pollline()noReturns a whole line if one has arrived, or nil
s:receive()noReturns whatever has arrived
s:status()noReturns "connecting", "open", "closed" or "error", and a message
s:close()noCloses 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
CallWhat 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:

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.

SettingWhat it does
NameWhat the server calls itself to visitors
Web pages, FTP files, HotlineTurns each one on or off and sets its port. They start as 8080, 2121 and 5500
FolderWhere 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 pageWhen 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 passwordLets 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 uploadWhether people can put files in the FTP folder, make folders and delete files. With sign-in on, only the person who signed in can
WelcomeThe message Hotline shows people when they join
Admin and passwordA 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 passwordWhether 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 downloadWhat 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.