-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split up io to allow console vs network io
- Loading branch information
Showing
3 changed files
with
155 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
-- _____ _ _____ ____ | ||
-- / ____| | | |_ _/ __ \ | ||
-- | | ___ _ __ ___ ___ | | ___ | || | | | | ||
-- | | / _ \| '_ \/ __|/ _ \| |/ _ \ | || | | | | ||
-- | |___| (_) | | | \__ \ (_) | | __/ _| || |__| | | ||
-- \_____\___/|_| |_|___/\___/|_|\___| |_____\____/ | ||
|
||
-- IO module that reads/writes to console | ||
|
||
local console_io = {} | ||
console_io.__index = console_io | ||
|
||
function console_io:new(o) | ||
o = o or {} | ||
setmetatable(o, self) | ||
return o | ||
end | ||
|
||
function console_io:write(...) | ||
io.stdout:write(...) | ||
io.stdout:flush() | ||
end | ||
|
||
function console_io:print(...) | ||
self:write(...) | ||
self:write("\n") | ||
end | ||
|
||
function console_io:flush() | ||
io.stdout:flush() | ||
end | ||
|
||
function console_io:read(format) | ||
return io.stdin:read(format) | ||
end | ||
|
||
function console_io:command_loop() | ||
while true do | ||
self:write("yt> ") | ||
local cmd = self:read("*l") | ||
local chunk = load(cmd) | ||
local success, m2 = pcall(chunk) | ||
if not success then | ||
self:print("Error: " .. m2) | ||
end | ||
end | ||
end | ||
|
||
print("console_io.lua - loaded with " .. _VERSION) | ||
|
||
return console_io |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
-- _ _ _ _ _____ ____ | ||
-- | \ | | | | | | |_ _/ __ \ | ||
-- | \| | ___| |___ _____ _ __| | __ | || | | | | ||
-- | . ` |/ _ \ __\ \ /\ / / _ \| '__| |/ / | || | | | | ||
-- | |\ | __/ |_ \ V V / (_) | | | < _| || |__| | | ||
-- |_| \_|\___|\__| \_/\_/ \___/|_| |_|\_\ |_____\____/ | ||
|
||
-- IO module that works on a TCP connection | ||
|
||
local NETWORK_IO_PORT = 8023 | ||
|
||
local network_io = {} | ||
network_io.__index = network_io | ||
|
||
function network_io:finish() | ||
self.client:close() | ||
self.server:close() | ||
end | ||
|
||
function network_io:new(o) | ||
o = o or {} | ||
setmetatable(o, self) | ||
return o | ||
end | ||
|
||
function network_io:write(...) | ||
return self.client:send(string.format("%s", unpack({...}))) | ||
end | ||
|
||
function network_io:print(...) | ||
if self:write(...) then | ||
return self:write("\n") | ||
else | ||
return nil | ||
end | ||
end | ||
|
||
function network_io:flush() | ||
end | ||
|
||
function network_io:read(format) | ||
return self.client:receive(format) | ||
end | ||
|
||
function network_io:command_loop() | ||
local socket = require("socket") | ||
self.server = assert(socket.bind("*", NETWORK_IO_PORT)) | ||
self.server:setoption('tcp-nodelay', true) | ||
self.server:setoption('reuseaddr', true) | ||
print("network_io.lua - server started on port " .. NETWORK_IO_PORT) | ||
while true do | ||
self.client = self.server:accept() | ||
self.client:send("Welcome to Yellow Tree\n") | ||
print("network_io.lua - accepted connection ")-- from " .. self.client:getpeername()) | ||
while true do | ||
if not self:write("yt> ") then | ||
break | ||
end | ||
local cmd = self.client:receive("*l") | ||
print(string.format("cmd is '%s'", cmd)) | ||
if not cmd then | ||
break | ||
end | ||
local chunk = load(cmd) | ||
local success, m2 = pcall(chunk) | ||
if not success then | ||
self:print("Error: " .. m2) | ||
end | ||
end | ||
print("network_io.lua - client disconnected") | ||
self.client:close() | ||
end | ||
print("network_io.lua - closing server") | ||
self.server:close() | ||
end | ||
|
||
print("network_io.lua - loaded with " .. _VERSION) | ||
|
||
return network_io |