Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local channelAlloc = 2
- local channelsIn = {}
- local channelsCount = 0
- local channelOut = 5
- local nextChannel = 1000
- local modem = peripheral.wrap("right")
- local channelTables = {}
- for _,v in ipairs(channelsIn) do
- modem.open(v)
- channelTables[v] = {}
- end
- modem.open(channelAlloc)
- function send(channel, table)
- modem.transmit(channel,0,textutils.serialize(table))
- end
- function deserialize(message)
- return textutils.unserialize(message)
- end
- while true do
- local _, _, senderChannel,
- replyChannel, message, _ = os.pullEvent("modem_message")
- term.clear()
- term.setCursorPos(1,1)
- print("Received event")
- print("Message: "..(message or "nil"))
- print("SenderChannel: "..(senderChannel or "nil"))
- print("Replychannel: "..(replyChannel or "nil"))
- if senderChannel == channelAlloc then
- if channelsIn[message] ~= nil then
- local tosend = {["channel"]=channelsIn[message],["id"]=(message or "")}
- send(replyChannel, tosend)
- else
- local tosend = {["channel"]=nextChannel,["id"]=(message or "")}
- send(replyChannel, tosend)
- channelsCount = channelsCount + 1
- channelsIn[message] = nextChannel
- modem.open(nextChannel)
- nextChannel = nextChannel+1
- end
- else
- modem.transmit(replyChannel,0,"Ok")
- local table = deserialize(message)
- channelTables[senderChannel] = table
- local combined = {}
- for _,channel in pairs(channelsIn) do
- if combined == {} then
- combined = channelTables[channel]
- else
- for k,v in pairs(channelTables[channel]) do
- if combined[k] ~= nil then
- combined[k] = combined[k] + v
- else
- combined[k] = v
- end
- end
- end
- end
- send(channelOut, combined)
- end
- sleep(0.1)
- end
Add Comment
Please, Sign In to add comment