This commit is contained in:
2025-12-12 17:14:05 -05:00
parent facaef5983
commit 5890c68d8e
5 changed files with 101 additions and 11 deletions

View File

@@ -40,6 +40,16 @@ local function addSound(fileName)
return
end
local function addServerHandler(fileName)
local extension = ".lua"
local fullFile = fileName .. extension
shell.execute("rm", fullFile)
local baseRoute = "https://git.astrocore.space/root/nova-corp/raw/branch/main/"
shell.execute("wget", baseRoute .. "server" .. "/" .. fullFile)
sleep(2)
return require(fileName)
end
addDriver("task-manager")
addDriver("monitor-driver")
@@ -47,4 +57,4 @@ addDriver("disk-driver")
addDriver("speaker-driver")
return { addDriver = addDriver, addFolderDriver = addFolderDriver, addProgram = addProgram, addSound = addSound }
return { addDriver = addDriver, addFolderDriver = addFolderDriver, addProgram = addProgram, addSound = addSound, addServerHandler = addServerHandler }

View File

@@ -1,19 +1,22 @@
local modem = peripheral.find("modem")
local PORT = 1
local speaker = require("speaker-driver")
local SERVER_PORT = 100
local CLIENT_PORT = 101
local username = os.getComputerLabel() or ("User" .. os.getComputerID())
local function start()
if modem then
modem.open(PORT)
modem.transmit(PORT, PORT, "User connected")
modem.open(CLIENT_PORT)
modem.transmit(SERVER_PORT, CLIENT_PORT, { type = "join", username = username })
end
print("Chat program started.")
print("Chat program started as " .. username)
end
local function stop()
if modem then
modem.transmit(PORT, PORT, "User disconnected")
modem.close(PORT)
modem.transmit(SERVER_PORT, CLIENT_PORT, { type = "leave", username = username })
modem.close(CLIENT_PORT)
end
print("Chat program stopped.")
end
@@ -26,12 +29,19 @@ end
local function receiveLoop()
while true do
local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
if channel == PORT then
if channel == CLIENT_PORT and type(message) == "table" then
local x, y = term.getCursorPos()
term.setCursorPos(1, y)
term.clearLine()
print("Received: " .. tostring(message))
speaker.play("notification")
if message.type == "system" then
print("[System] " .. message.content)
speaker.play("notification")
elseif message.type == "chat" then
print(message.username .. ": " .. message.content)
speaker.play("notification")
end
write("> ")
end
end
@@ -42,7 +52,7 @@ local function sendLoop()
write("> ")
local input = read()
if modem then
modem.transmit(PORT, PORT, input)
modem.transmit(SERVER_PORT, CLIENT_PORT, { type = "chat", username = username, content = input })
end
end
end

51
server/chat-server.lua Normal file
View File

@@ -0,0 +1,51 @@
local modem = peripheral.find("modem")
local SERVER_PORT = 100
local CLIENT_PORT = 101
local function log(msg)
print("[Server]: " .. tostring(msg))
end
local function broadcast(message)
if modem then
modem.transmit(CLIENT_PORT, SERVER_PORT, message)
end
end
local function handleMessage(event, side, channel, replyChannel, message, distance)
if type(message) == "table" then
if message.type == "join" then
log(message.username .. " connected.")
broadcast({ type = "system", content = message.username .. " joined the chat." })
elseif message.type == "leave" then
log(message.username .. " disconnected.")
broadcast({ type = "system", content = message.username .. " left the chat." })
elseif message.type == "chat" then
log(message.username .. ": " .. message.content)
broadcast({ type = "chat", username = message.username, content = message.content })
end
end
end
local function listenLoop()
if not modem then
log("No modem found!")
return
end
modem.open(SERVER_PORT)
log("Listening on port " .. SERVER_PORT)
while true do
local eventData = {os.pullEvent("modem_message")}
-- event, side, channel, replyChannel, message, distance
if eventData[3] == SERVER_PORT then
handleMessage(table.unpack(eventData))
end
end
end
local function main()
parallel.waitForAll(listenLoop)
end
return { main = main }

12
server/main.lua Normal file
View File

@@ -0,0 +1,12 @@
local kernel = require("kernel")
local chat = kernel.addServerHandler("chat-server")
local function run()
print("Starting Server...")
local tasks = {}
table.insert(tasks, chat.main)
parallel.waitForAll(table.unpack(tasks))
end
return { run = run }

7
server/startup.lua Normal file
View File

@@ -0,0 +1,7 @@
shell.execute("rm", "kernel.lua")
shell.execute("wget", "https://git.astrocore.space/root/nova-corp/raw/branch/main/kernel.lua")
sleep(5)
local kernel = require("kernel")
local main = kernel.addFolderDriver("server", "main")
main.run()