friendlier networking + adding tabs

This commit is contained in:
kepler155c@gmail.com 2017-10-09 13:08:38 -04:00
parent f5b99d91e5
commit 05c99b583a
7 changed files with 124 additions and 40 deletions

View File

@ -5,13 +5,13 @@ local Util = require('util')
local device = _G.device local device = _G.device
local os = _G.os local os = _G.os
local transport = _G.transport --local transport = _G.transport
local socketClass = { } local socketClass = { }
function socketClass:read(timeout) function socketClass:read(timeout)
local data, distance = transport.read(self) local data, distance = _G.transport.read(self)
if data then if data then
return data, distance return data, distance
end end
@ -27,7 +27,7 @@ function socketClass:read(timeout)
local e, id = os.pullEvent() local e, id = os.pullEvent()
if e == 'transport_' .. self.sport then if e == 'transport_' .. self.sport then
data, distance = transport.read(self) data, distance = _G.transport.read(self)
if data then if data then
os.cancelTimer(timerId) os.cancelTimer(timerId)
return data, distance return data, distance
@ -44,7 +44,7 @@ end
function socketClass:write(data) function socketClass:write(data)
if self.connected then if self.connected then
transport.write(self, { _G.transport.write(self, {
type = 'DATA', type = 'DATA',
seq = self.wseq, seq = self.wseq,
data = data, data = data,
@ -55,7 +55,7 @@ end
function socketClass:ping() function socketClass:ping()
if self.connected then if self.connected then
transport.write(self, { _G.transport.write(self, {
type = 'PING', type = 'PING',
seq = self.wseq, seq = self.wseq,
}) })
@ -72,7 +72,7 @@ function socketClass:close()
self.connected = false self.connected = false
end end
device.wireless_modem.close(self.sport) device.wireless_modem.close(self.sport)
transport.close(self) _G.transport.close(self)
end end
local Socket = { } local Socket = { }
@ -126,23 +126,29 @@ function Socket.connect(host, port)
local e, id, sport, dport, msg = os.pullEvent() local e, id, sport, dport, msg = os.pullEvent()
if e == 'modem_message' and if e == 'modem_message' and
sport == socket.sport and sport == socket.sport and
msg.dhost == socket.shost and msg.dhost == socket.shost then
msg.type == 'CONN' then
socket.dport = dport
socket.connected = true
Logger.log('socket', 'connection established to %d %d->%d',
host, socket.sport, socket.dport)
os.cancelTimer(timerId) os.cancelTimer(timerId)
transport.open(socket) if msg.type == 'CONN' then
return socket socket.dport = dport
socket.connected = true
Logger.log('socket', 'connection established to %d %d->%d',
host, socket.sport, socket.dport)
_G.transport.open(socket)
return socket
elseif msg.type == 'REJE' then
return false, 'Password not set on target or not trusted'
end
end end
until e == 'timer' and id == timerId until e == 'timer' and id == timerId
socket:close() socket:close()
return false, 'Connection timed out'
end end
local function trusted(msg, port) local function trusted(msg, port)
@ -176,13 +182,14 @@ function Socket.server(port)
msg.dhost == os.getComputerID() and msg.dhost == os.getComputerID() and
msg.type == 'OPEN' then msg.type == 'OPEN' then
local socket = newSocket(msg.shost == os.getComputerID())
socket.dport = dport
socket.dhost = msg.shost
socket.wseq = msg.wseq
socket.rseq = msg.rseq
if trusted(msg, port) then if trusted(msg, port) then
local socket = newSocket(msg.shost == os.getComputerID())
socket.dport = dport
socket.dhost = msg.shost
socket.connected = true socket.connected = true
socket.wseq = msg.wseq
socket.rseq = msg.rseq
socket.transmit(socket.dport, socket.sport, { socket.transmit(socket.dport, socket.sport, {
type = 'CONN', type = 'CONN',
dhost = socket.dhost, dhost = socket.dhost,
@ -190,9 +197,16 @@ function Socket.server(port)
}) })
Logger.log('socket', 'Connection established %d->%d', socket.sport, socket.dport) Logger.log('socket', 'Connection established %d->%d', socket.sport, socket.dport)
transport.open(socket) _G.transport.open(socket)
return socket return socket
end end
socket.transmit(socket.dport, socket.sport, {
type = 'REJE',
dhost = socket.dhost,
shost = socket.shost,
})
socket:close()
end end
end end
end end

View File

@ -2045,7 +2045,7 @@ function UI.MenuBar:init(args)
end end
self:addButtons(self.buttons) self:addButtons(self.buttons)
if self.showBackButton then if self.showBackButton then -- need to remove
table.insert(self.children, UI.MenuItem({ table.insert(self.children, UI.MenuItem({
x = UI.term.width - 2, x = UI.term.width - 2,
width = 3, width = 3,
@ -2086,7 +2086,9 @@ function UI.MenuBar:addButtons(buttons)
end end
end end
end end
self:initChildren() if self.parent then
self:initChildren()
end
end end
function UI.MenuBar:getActive(menuItem) function UI.MenuBar:getActive(menuItem)
@ -2129,6 +2131,13 @@ function UI.DropMenuItem:init(args)
UI.Button.init(self, UI:getDefaults(UI.DropMenuItem, args)) UI.Button.init(self, UI:getDefaults(UI.DropMenuItem, args))
end end
function UI.DropMenuItem:eventHandler(event)
if event.type == 'button_activate' then
self.parent:hide()
end
return UI.Button.eventHandler(self, event)
end
--[[-- DropMenu --]]-- --[[-- DropMenu --]]--
UI.DropMenu = class(UI.MenuBar) UI.DropMenu = class(UI.MenuBar)
UI.DropMenu.defaults = { UI.DropMenu.defaults = {
@ -3028,6 +3037,10 @@ function UI.TextArea:setText(text)
self:draw() self:draw()
end end
function UI.TextArea:focus()
-- allow keyboard scrolling
end
function UI.TextArea:draw() function UI.TextArea:draw()
self:clear() self:clear()
self:setCursorPos(1, 1) self:setCursorPos(1, 1)

View File

@ -3,8 +3,9 @@ _G.requireInjector()
local UI = require('ui') local UI = require('ui')
local Util = require('util') local Util = require('util')
local colors = _G.colors local colors = _G.colors
local help = _G.help local help = _G.help
local multishell = _ENV.multishell
multishell.setTitle(multishell.getCurrent(), 'Help') multishell.setTitle(multishell.getCurrent(), 'Help')
UI:configure('Help', ...) UI:configure('Help', ...)
@ -55,10 +56,6 @@ local topicPage = UI.Page {
}, },
} }
function topicPage.helpText:focus()
-- let the help text get focused so we consume key strokes
end
function topicPage:eventHandler(event) function topicPage:eventHandler(event)
if event.type == 'back' then if event.type == 'back' then
UI:setPreviousPage() UI:setPreviousPage()

View File

@ -28,10 +28,17 @@ end
local page = UI.Page { local page = UI.Page {
menuBar = UI.MenuBar { menuBar = UI.MenuBar {
buttons = { buttons = {
{ text = 'Telnet', event = 'telnet' }, { text = 'Connect', dropdown = {
{ text = 'VNC', event = 'vnc' }, { text = 'Telnet t', event = 'telnet' },
{ text = 'Trust', event = 'trust' }, { text = 'VNC v', event = 'vnc' },
{ text = 'Reboot', event = 'reboot' }, UI.MenuBar.spacer,
{ text = 'Reboot r', event = 'reboot' },
} },
{ text = 'Trust', dropdown = {
{ text = 'Establish', event = 'trust' },
{ text = 'Remove', event = 'untrust' },
} },
{ text = 'Help', event = 'help' },
}, },
}, },
grid = UI.ScrollingGrid { grid = UI.ScrollingGrid {
@ -43,6 +50,9 @@ local page = UI.Page {
}, },
notification = UI.Notification { }, notification = UI.Notification { },
accelerators = { accelerators = {
t = 'telnet',
v = 'vnc',
r = 'reboot',
q = 'quit', q = 'quit',
c = 'clear', c = 'clear',
}, },
@ -85,20 +95,62 @@ function page:eventHandler(event)
args = { t.id }, args = { t.id },
title = t.label, title = t.label,
}) })
elseif event.type == 'clear' then
Util.clear(network)
page.grid:update()
page.grid:draw()
elseif event.type == 'trust' then elseif event.type == 'trust' then
shell.openForegroundTab('trust ' .. t.id) shell.openForegroundTab('trust ' .. t.id)
elseif event.type == 'untrust' then
local trustList = Util.readTable('usr/.known_hosts') or { }
trustList[t.id] = nil
Util.writeTable('usr/.known_hosts', trustList)
elseif event.type == 'reboot' then elseif event.type == 'reboot' then
sendCommand(t.id, 'reboot') sendCommand(t.id, 'reboot')
elseif event.type == 'shutdown' then elseif event.type == 'shutdown' then
sendCommand(t.id, 'shutdown') sendCommand(t.id, 'shutdown')
end end
end end
if event.type == 'quit' then if event.type == 'help' then
UI:setPage(UI.Dialog {
title = 'Network Help',
height = 10,
backgroundColor = colors.white,
text = UI.TextArea {
x = 2, y = 2,
backgroundColor = colors.white,
value = [[
In order to connect to another computer:
1. The target computer must have a password set (run 'password' from the shell prompt).
2. From this computer, click trust and enter the password for that computer.
This only needs to be done once.
]],
},
accelerators = {
q = 'cancel',
}
})
elseif event.type == 'quit' then
Event.exitPullEvents() Event.exitPullEvents()
end end
UI.Page.eventHandler(self, event) UI.Page.eventHandler(self, event)
end end
function page.menuBar:getActive(menuItem)
local t = page.grid:getSelected()
if menuItem.event == 'untrust' then
local trustList = Util.readTable('usr/.known_hosts') or { }
return t and trustList[t.id]
end
return not not t
end
function page.grid:getRowTextColor(row, selected) function page.grid:getRowTextColor(row, selected)
if not row.active then if not row.active then
return colors.orange return colors.orange

View File

@ -13,6 +13,14 @@ local shell = _ENV.shell
multishell.setTitle(multishell.getCurrent(), 'System') multishell.setTitle(multishell.getCurrent(), 'System')
UI:configure('System', ...) UI:configure('System', ...)
local mcVersion = _G._MC_VERSION or 'unknown'
if _G._HOST then
local version = _G._HOST:match('%S+ %S+ %((%S.+)%)')
if version then
mcVersion = version:match('Minecraft (%S+)') or version
end
end
local env = { local env = {
path = shell.path(), path = shell.path(),
aliases = shell.aliases(), aliases = shell.aliases(),
@ -89,7 +97,7 @@ local systemPage = UI.Page {
{ name = '', value = '' }, { name = '', value = '' },
{ name = 'CC version', value = Util.getVersion() }, { name = 'CC version', value = Util.getVersion() },
{ name = 'Lua version', value = _VERSION }, { name = 'Lua version', value = _VERSION },
{ name = 'MC version', value = _G._MC_VERSION or 'unknown' }, { name = 'MC version', value = mcVersion },
{ name = 'Disk free', value = Util.toBytes(fs.getFreeSpace('/')) }, { name = 'Disk free', value = Util.toBytes(fs.getFreeSpace('/')) },
{ name = 'Computer ID', value = tostring(os.getComputerID()) }, { name = 'Computer ID', value = tostring(os.getComputerID()) },
{ name = 'Day', value = tostring(os.day()) }, { name = 'Day', value = tostring(os.day()) },
@ -123,7 +131,7 @@ if settings then
grid = UI.Grid { grid = UI.Grid {
y = 1, y = 1,
values = values, values = values,
--autospace = true, autospace = true,
sortColumn = 'name', sortColumn = 'name',
columns = { columns = {
{ heading = 'Setting', key = 'name' }, { heading = 'Setting', key = 'name' },

View File

@ -23,10 +23,10 @@ if not remoteId then
end end
print('connecting...') print('connecting...')
local socket = Socket.connect(remoteId, 23) local socket, msg = Socket.connect(remoteId, 23)
if not socket then if not socket then
error('Unable to connect to ' .. remoteId .. ' on port 23') error(msg)
end end
local ct = Util.shallowCopy(term.current()) local ct = Util.shallowCopy(term.current())

View File

@ -25,10 +25,10 @@ end
multishell.setTitle(multishell.getCurrent(), 'VNC-' .. remoteId) multishell.setTitle(multishell.getCurrent(), 'VNC-' .. remoteId)
print('connecting...') print('connecting...')
local socket = Socket.connect(remoteId, 5900) local socket, msg = Socket.connect(remoteId, 5900)
if not socket then if not socket then
error('Unable to connect to ' .. remoteId .. ' on port 5900') error(msg)
end end
local function writeTermInfo() local function writeTermInfo()