what I am trying to do is , that there is one single computer that works as a server for my bank system. But now it should be possible that several Clients connect to this server.
I thought of this like that:
Client connects to the Server
C—————> S
Server detects Client and assigns a modem-freqency only for this client. This freqency is sent to the Client so he knows which freqency he can use.
C <————- S
I thought that this could work with Coroutines. One coroutine waiting for Clients to connect and one handling the Clients when they have connected.
My (perhaps extremely wrong) code for this:
local main = coroutine.create(main)
local user = coroutine.create(newUser)
local evt = {}
while true do
tst=coroutine.resume(main, unpack(evt))
if tst==true then
coroutine.resume(user, unpack(evt))
end
if coroutine.status(main) == "dead" then
local main = coroutine.create(main)
end
evt = {os.pullEvent()}
end
And the code of my main function is :
local function main()
modem=peripheral.wrap(getSide())
modem.open(CONST_FREQ)
modem.open(CONST_RFREQ)
while true do
local e = {os.pullEvent()}
if e[1] == "modem_message" and e[3] == CONST_FREQ then
table.insert(USED_FREQENCIES,tostring(math.random(128)))
modem.transmit(CONST_FREQ, CONST_RFREQ, CONST_RHANDSHAKE)
modem.transmit(CONST_FREQ,CONST_RFREQ,tostring(math.random))
return true
else
end
end
end
Currently this is really far away from something that actually works.
When a Client connects with the new Freqency the Server doesn´t even connect.
My question is, if it is even possible that one Computer handles multiple connections at a time and if it is how would I have to realise this ?
I am sorry if this is a bit hard to understand or my code is just terrible :/ I am really no pro at Lua and this is my first time I am working with coroutines.