-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.lua
More file actions
158 lines (105 loc) · 3.47 KB
/
Copy pathserver.lua
File metadata and controls
158 lines (105 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
-- configuration
local config = {
side = {
monitor = "left",
modem = "back"
},
network = {
serverPort = 2000, -- port to send to server
clientPort = 2000, -- listen port for client
}
}
print("________ ______ ____")
print("/_ __/ |/ / _ |____/ __/")
print(" / / / /|_/ / __ /___/\\ \\ ")
print("/_/ /_/ /_/_/ |_| /___/ ")
local currentControl = 0 -- host 0 is all
local controlPrefix = "C>"
-- local monitor = peripheral.wrap(config.side.monitor)
local modem = peripheral.wrap(config.side.modem)
local function display(dir)
local runModule = true -- value so main loop can be killed
while runModule do -- main loop
local x, y = term.getCursorPos()
term.setCursorPos(1, 1)
term.clearLine()
write("Tehbb's MA V1.0 | #"..currentControl.. "/")
term.setCursorPos(x, y)
os.sleep(0.1)
end
end
function split(pString, pPattern)
local Table = {} -- NOTE: use {n = 0} in Lua-5.0
local fpat = "(.-)" .. pPattern
local last_end = 1
local s, e, cap = pString:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(Table,cap)
end
last_end = e+1
s, e, cap = pString:find(fpat, last_end)
end
if last_end <= #pString then
cap = pString:sub(last_end)
table.insert(Table, cap)
end
return Table
end
local function listen()
local runModule = true -- value so main loop can be killed
while runModule do -- main loop
local event, modemSide, senderChannel,
replyChannel, message, senderDistance = os.pullEvent("modem_message")
-- print("===== Message Recive ======")
-- print("Channel: "..senderChannel)
-- print("Reply channel: "..replyChannel)
-- print("Modem on "..modemSide.." side")
-- print("Message contents: \n"..message)
-- print("Sender is "..(senderDistance or "an unknown number of").." blocks away")
term.setCursorPos(1, 19)
term.write(message.host..">"..message.data)
term.scroll(1)
term.setCursorPos(1, 19)
term.clearLine()
term.write(controlPrefix)
end
end
local function ui()
local runModule = true -- value so main loop can be killed
while runModule do -- main loop
-- setup ui
term.setCursorPos(1, 19)
term.clearLine()
term.write(controlPrefix)
local input = read()
-- term.setCursorPos(1, 19)
-- term.write("C>"..input)
-- term.scroll(1)
local inputSplit = split(input, " ")
if inputSplit[1] == "BE" then -- set controlled host
currentControl = inputSplit[2]
else -- foward other commands
local times = 1
if inputSplit[2] == nil then
times = 1
else
times = tonumber(inputSplit[2])
end
local data = {host=currentControl, com=inputSplit[1], qty=times}
modem.transmit(config.network.serverPort, config.network.clientPort, data)
end
end
end
print("Display set to monitor: "..config.side.monitor.."\n")
modem.open(config.network.clientPort)
print("Opended server port: "..config.network.serverPort)
print("main function.")
term.setCursorPos(1, 18)
parallel.waitForAny(
listen,
ui,
display
)
-- display(displaySide)
-- os.sleep(0.5) -- just to limit main loop