change white space to tabs instead of spaces

This commit is contained in:
payonel 2015-09-01 00:17:46 -07:00
parent 1b83c83d6d
commit 7e7851f0de
2 changed files with 203 additions and 198 deletions

View File

@ -25,228 +25,225 @@ modem_host.clients = {}
modem_host.open_ports = {} modem_host.open_ports = {}
local function createPacket(type, address, port, ...) local function createPacket(type, address, port, ...)
-- args are not checked here (unlike the modem api methods) -- args are not checked here (unlike the modem api methods)
-- address can be nil, which means broadcast -- address can be nil, which means broadcast
local packed = local packed =
{ {
type or "unknown_type", type or "unknown_type",
address or "no address", address or "no address",
modem_host.id or "no sender", modem_host.id or "no sender",
port or "no port", port or "no port",
0, -- distance 0, -- distance
table.unpack(table.pack(...)) table.unpack(table.pack(...))
} }
local datagram = ser.serialize(packed) local datagram = ser.serialize(packed)
return datagram .. '\n' return datagram .. '\n'
end end
local function parsePacket(raw) local function parsePacket(raw)
assert(raw) assert(raw)
local packed = ser.unserialize(raw) local packed = ser.unserialize(raw)
assert(packed ~= nil) assert(packed ~= nil)
assert(#packed >= 5) assert(#packed >= 5)
local packet = {} local packet = {}
packet.type = packed[1] packet.type = packed[1]
packet.target = packed[2] packet.target = packed[2]
packet.source = packed[3] packet.source = packed[3]
packet.port = packed[4] packet.port = packed[4]
packet.distance = packed[5] packet.distance = packed[5]
packet.payload = {} packet.payload = {}
for i=6,#packed do for i=6,#packed do
table.insert(packet.payload, packed[i]) table.insert(packet.payload, packed[i])
end end
return packet return packet
end end
local function packetToArray(packet) local function packetToArray(packet)
return return
{ {
packet.type, packet.type,
packet.target, packet.target,
packet.source, packet.source,
packet.port, packet.port,
packet.distance, packet.distance,
table.unpack(packet.payload) table.unpack(packet.payload)
} }
end end
function modem_host.broadcast(packet) function modem_host.broadcast(packet)
-- only host broadcasts -- only host broadcasts
-- this method will be hit for all broadcasted messages -- this method will be hit for all broadcasted messages
-- but nonhosting clients will simply not repeat the broadcast -- but nonhosting clients will simply not repeat the broadcast
if modem_host.hosting then if modem_host.hosting then
local plainArray = packetToArray(packet) local plainArray = packetToArray(packet)
local datagram = ser.serialize(plainArray) local datagram = ser.serialize(plainArray)
for addr,client in pairs(modem_host.clients) do for addr,client in pairs(modem_host.clients) do
client:send(datagram) client:send(datagram)
end end
end end
end end
function modem_host.validTarget(target) function modem_host.validTarget(target)
if target == modem_host.id then if target == modem_host.id then
return true return true
end end
if not modem_host.hosting then if not modem_host.hosting then
return false return false
end end
for address,client in pairs(modem_host.clients) do for address,client in pairs(modem_host.clients) do
if address == target then if address == target then
return true return true
end end
end end
return false return false
end end
-- backend private methods, these are not pushed to user machine environments -- backend private methods, these are not pushed to user machine environments
function modem_host.pushMessage(target, datagram) function modem_host.pushMessage(target, datagram)
if not modem_host.validTarget(target) then
return false, "invalid target, no such client listening" --ignored
end
if not modem_host.validTarget(target) then local packet = parsePacket(datagram)
return false, "invalid target, no such client listening" --ignored table.insert(modem_host.messages, packet)
end
local packet = parsePacket(datagram) return true
table.insert(modem_host.messages, packet)
return true
end end
function modem_host.processPendingMessages() function modem_host.processPendingMessages()
modem_host.recvPendingMessages() modem_host.recvPendingMessages()
-- computer address seems to be applied late -- computer address seems to be applied late
if not modem_host.id then if not modem_host.id then
modem_host.id = component.list("computer",true)() modem_host.id = component.list("computer",true)()
assert(modem_host.id) assert(modem_host.id)
end end
local i = 1; local i = 1;
while i <= #modem_host.messages do while i <= #modem_host.messages do
local packet = modem_host.messages[i] local packet = modem_host.messages[i]
local move = true local move = true
if packet.type == 'modem_message' then if packet.type == 'modem_message' then
-- broadcast if no target -- broadcast if no target
if packet.target == 0 then if packet.target == 0 then
modem_host.broadcast(packet) modem_host.broadcast(packet)
-- clean up for broadcasting to self -- clean up for broadcasting to self
packet.target = modem_host.id packet.target = modem_host.id
end end
if packet.target == modem_host.id then if packet.target == modem_host.id then
if obj.isOpen(packet.port) then if obj.isOpen(packet.port) then
table.insert(machine.signals, packetToArray(packet)) table.insert(machine.signals, packetToArray(packet))
end end
move = false move = false
end end
end end
if move then if move then
i = i + 1 i = i + 1
else else
table.remove(modem_host.messages, i) table.remove(modem_host.messages, i)
end end
end
end
end end
function modem_host.recvPendingMessages() function modem_host.recvPendingMessages()
if modem_host.hosting then if modem_host.hosting then
while 1 do while 1 do
local client = modem_host.socket:accept() local client = modem_host.socket:accept()
if not client then if not client then
break; break;
end end
local handshakeDatagram, err = client:receive() local handshakeDatagram, err = client:receive()
if err then if err then
client:close() client:close()
else else
client:settimeout(0, 't') client:settimeout(0, 't')
local handshake = parsePacket(handshakeDatagram) local handshake = parsePacket(handshakeDatagram)
modem_host.clients[handshake.source] = client modem_host.clients[handshake.source] = client
end end
end end
-- recv all pending packets -- recv all pending packets
for source, client in pairs(modem_host.clients) do for source, client in pairs(modem_host.clients) do
local line, err = client:receive() local line, err = client:receive()
if not err then if not err then
modem_host.pushMessage(source, line) modem_host.pushMessage(source, line)
end end
end end
elseif modem_host.socket then elseif modem_host.socket then
while 1 do while 1 do
local line, err = modem_host.socket:receive() local line, err = modem_host.socket:receive()
if not err then if not err then
modem_host.pushMessage(modem_host.id, line) modem_host.pushMessage(modem_host.id, line)
else else
break break
end end
end end
end end
end end
function modem_host.createNewMessageBoard() function modem_host.createNewMessageBoard()
local why local why
modem_host.socket, why = socket.bind(modem_host.comms_ip, modem_host.comms_port) modem_host.socket, why = socket.bind(modem_host.comms_ip, modem_host.comms_port)
if modem_host.socket then if modem_host.socket then
modem_host.hosting = true modem_host.hosting = true
end end
return modem_host.socket, why return modem_host.socket, why
end end
function modem_host.joinExistingMessageBoard() function modem_host.joinExistingMessageBoard()
local why local why
modem_host.socket, why = socket.connect(modem_host.comms_ip, modem_host.comms_port) modem_host.socket, why = socket.connect(modem_host.comms_ip, modem_host.comms_port)
if modem_host.socket then if modem_host.socket then
modem_host.hosting = nil modem_host.hosting = nil
-- send handshake data -- send handshake data
local datagram = createPacket("client_handshake") local datagram = createPacket("client_handshake")
modem_host.send(datagram) modem_host.send(datagram)
end end
return modem_host.socket, why return modem_host.socket, why
end end
function modem_host.connectMessageBoard() function modem_host.connectMessageBoard()
if modem_host.connected then
return true
end
if modem_host.connected then local ok, reason =
return true modem_host.joinExistingMessageBoard() or
end modem_host.createNewMessageBoard()
local ok, reason = if not ok then
modem_host.joinExistingMessageBoard() or return nil, reason
modem_host.createNewMessageBoard() end
if not ok then modem_host.socket:settimeout(0, 't') -- accept calls must be already pending
return nil, reason modem_host.connected = true
end modem_host.clients = {}
modem_host.messages = {}
modem_host.socket:settimeout(0, 't') -- accept calls must be already pending return true
modem_host.connected = true
modem_host.clients = {}
modem_host.messages = {}
return true
end end
function modem_host.send(datagram) function modem_host.send(datagram)
-- if we are the host, we simply call pushMessage directly -- if we are the host, we simply call pushMessage directly
if modem_host.hosting then if modem_host.hosting then
return modem_host.pushMessage(modem_host.id, datagram) return modem_host.pushMessage(modem_host.id, datagram)
else else
return not not modem_host.socket:send(datagram) return not not modem_host.socket:send(datagram)
end end
end end
local wakeMessage local wakeMessage
@ -268,19 +265,21 @@ function obj.send(address, port, ...) -- Sends the specified data to the specifi
compCheckArg(2,port,"number") compCheckArg(2,port,"number")
port=checkPort(port) port=checkPort(port)
local datagram = createPacket("modem_message", address, port, ...) local datagram = createPacket("modem_message", address, port, ...)
return modem_host.send(datagram) return modem_host.send(datagram)
end end
function obj.getWakeMessage() -- Get the current wake-up message. function obj.getWakeMessage() -- Get the current wake-up message.
cprint("modem.getWakeMessage") cprint("modem.getWakeMessage")
return wakeMessage return wakeMessage
end end
function obj.setWakeMessage(message) -- Set the wake-up message. function obj.setWakeMessage(message) -- Set the wake-up message.
cprint("modem.setWakeMessage",message) cprint("modem.setWakeMessage",message)
compCheckArg(1,message,"string","nil") compCheckArg(1,message,"string","nil")
wakeMessage = message wakeMessage = message
end end
function obj.close(port) -- Closes the specified port (default: all ports). Returns true if ports were closed. function obj.close(port) -- Closes the specified port (default: all ports). Returns true if ports were closed.
cprint("modem.close",port) cprint("modem.close",port)
compCheckArg(1,port,"number","nil") compCheckArg(1,port,"number","nil")
@ -288,17 +287,19 @@ function obj.close(port) -- Closes the specified port (default: all ports). Retu
port=checkPort(port) port=checkPort(port)
end end
if not obj.isOpen(port) then if not obj.isOpen(port) then
return false; return false;
end end
modem_host.open_ports[port] = nil modem_host.open_ports[port] = nil
return true return true
end end
function obj.maxPacketSize() -- Gets the maximum packet size (config setting). function obj.maxPacketSize() -- Gets the maximum packet size (config setting).
cprint("modem.maxPacketSize") cprint("modem.maxPacketSize")
return settings.maxNetworkPacketSize return settings.maxNetworkPacketSize
end end
if wireless then if wireless then
function obj.getStrength() -- Get the signal strength (range) used when sending messages. function obj.getStrength() -- Get the signal strength (range) used when sending messages.
cprint("modem.getStrength") cprint("modem.getStrength")
@ -310,60 +311,64 @@ if wireless then
strength = newstrength strength = newstrength
end end
end end
function obj.isOpen(port) -- Whether the specified port is open. function obj.isOpen(port) -- Whether the specified port is open.
cprint("modem.isOpen",port) cprint("modem.isOpen",port)
compCheckArg(1,port,"number") compCheckArg(1,port,"number")
return modem_host.open_ports[port] ~= nil return modem_host.open_ports[port] ~= nil
end end
function obj.open(port) -- Opens the specified port. Returns true if the port was opened. function obj.open(port) -- Opens the specified port. Returns true if the port was opened.
cprint("modem.open",port) cprint("modem.open",port)
compCheckArg(1,port,"number") compCheckArg(1,port,"number")
port=checkPort(port) port=checkPort(port)
if obj.isOpen(port) then if obj.isOpen(port) then
return false return false
end end
-- make sure we are connected to the message board -- make sure we are connected to the message board
if not modem_host.connectMessageBoard() then if not modem_host.connectMessageBoard() then
return false return false
end end
modem_host.open_ports[port] = true modem_host.open_ports[port] = true
return true return true
end end
function obj.isWireless() -- Whether this is a wireless network card. function obj.isWireless() -- Whether this is a wireless network card.
cprint("modem.isWireless") cprint("modem.isWireless")
return wireless return wireless
end end
function obj.broadcast(port, ...) -- Broadcasts the specified data on the specified port. function obj.broadcast(port, ...) -- Broadcasts the specified data on the specified port.
cprint("modem.broadcast",port, ...) cprint("modem.broadcast",port, ...)
compCheckArg(1,port,"number") compCheckArg(1,port,"number")
port=checkPort(port) port=checkPort(port)
-- we cannot broadcast unless we are connected to the message board -- we cannot broadcast unless we are connected to the message board
if not modem_host.connectMessageBoard() then if not modem_host.connectMessageBoard() then
return false return false
end end
local datagram = createPacket("modem_message", 0, port, ...) local datagram = createPacket("modem_message", 0, port, ...)
return modem_host.send(datagram) return modem_host.send(datagram)
end end
local cec = {} local cec = {}
local doc = { local doc = {
["send"]="function(address:string, port:number, data...) -- Sends the specified data to the specified target.", ["send"]="function(address:string, port:number, data...) -- Sends the specified data to the specified target.",
["getWakeMessage"]="function():string -- Get the current wake-up message.", ["getWakeMessage"]="function():string -- Get the current wake-up message.",
["setWakeMessage"]="function(message:string):string -- Set the wake-up message.", ["setWakeMessage"]="function(message:string):string -- Set the wake-up message.",
["close"]="function([port:number]):boolean -- Closes the specified port (default: all ports). Returns true if ports were closed.", ["close"]="function([port:number]):boolean -- Closes the specified port (default: all ports). Returns true if ports were closed.",
["maxPacketSize"]="function():number -- Gets the maximum packet size (config setting).", ["maxPacketSize"]="function():number -- Gets the maximum packet size (config setting).",
["getStrength"]="function():number -- Get the signal strength (range) used when sending messages.", ["getStrength"]="function():number -- Get the signal strength (range) used when sending messages.",
["setStrength"]="function(strength:number):number -- Set the signal strength (range) used when sending messages.", ["setStrength"]="function(strength:number):number -- Set the signal strength (range) used when sending messages.",
["isOpen"]="function(port:number):boolean -- Whether the specified port is open.", ["isOpen"]="function(port:number):boolean -- Whether the specified port is open.",
["open"]="function(port:number):boolean -- Opens the specified port. Returns true if the port was opened.", ["open"]="function(port:number):boolean -- Opens the specified port. Returns true if the port was opened.",
["isWireless"]="function():boolean -- Whether this is a wireless network card.", ["isWireless"]="function():boolean -- Whether this is a wireless network card.",
["broadcast"]="function(port:number, data...) -- Broadcasts the specified data on the specified port.", ["broadcast"]="function(port:number, data...) -- Broadcasts the specified data on the specified port.",
} }
return obj,cec,doc return obj,cec,doc

View File

@ -53,7 +53,7 @@ if settings.components == nil then
{"filesystem",nil,7,"loot/OpenOS",true}, {"filesystem",nil,7,"loot/OpenOS",true},
{"filesystem",nil,nil,"tmpfs",false}, {"filesystem",nil,nil,"tmpfs",false},
{"filesystem",nil,5,nil,false}, {"filesystem",nil,5,nil,false},
{"modem",nil,nil,false}, {"modem",nil,nil,false},
{"internet"}, {"internet"},
{"computer"}, {"computer"},
{"ocemu"}, {"ocemu"},
@ -314,7 +314,7 @@ function elsa.update(dt)
table.remove(kbdcodes,1) table.remove(kbdcodes,1)
table.insert(machine.signals,{kbdcode.type,kbdcode.addr,kbdcode.char or 0,kbdcode.code}) table.insert(machine.signals,{kbdcode.type,kbdcode.addr,kbdcode.char or 0,kbdcode.code})
end end
modem_host.processPendingMessages() modem_host.processPendingMessages()
if #machine.signals > 0 then if #machine.signals > 0 then
signal = machine.signals[1] signal = machine.signals[1]
table.remove(machine.signals, 1) table.remove(machine.signals, 1)