8000 Beginning deployment · neumond/python-computer-craft@ddaadb9 · GitHub
[go: up one dir, main page]

Skip to content

Commit ddaadb9

Browse files
committed
Beginning deployment
1 parent 1e28ff9 commit ddaadb9

File tree

9 files changed

+197
-14
lines changed

9 files changed

+197
-14
lines changed

computercraft/back-debug.lua

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
local genv = getfenv()
2+
local temp = {}
3+
genv.temp = temp
4+
local url = 'http://127.0.0.1:8080/'
5+
local args = {...}
6+
local tasks = {}
7+
8+
-- https://www.lua.org/pil/11.4.html
9+
local Queue = {}
10+
function Queue.new()
11+
return {head = 0, tail = 0}
12+
end
13+
function Queue.push(q, value)
14+
q[q.head] = value
15+
q.head = q.head + 1
16+
end
17+
function Queue.pop(q)
18+
if q.tail >= q.head then return nil end
19+
local value = q[q.tail]
20+
q[q.tail] = nil
21+
q.tail = q.tail + 1
22+
return value
23+
end
24+
function Queue.length(q)
25+
return q.head - q.tail
26+
end
27+
local output = Queue.new()
28+
29+
local function is_special_task(task_id)
30+
return (task_id == '_fetch' or task_id == '_send')
31+
end
32+
33+
local function string_split(text)
34+
local pos = string.find(text, ';')
35+
return string.sub(text, 1, pos - 1), string.sub(text, pos + 1)
36+
end
37+
38+
local function inner_resume(task_id, ...)
39+
local r = {coroutine.resume(tasks[task_id].co, ...)}
40+
if coroutine.status(tasks[task_id].co) == 'dead' then
41+
if not is_special_task(task_id) then
42+
Queue.push(output, {task_id=task_id, result=r})
43+
end
44+
tasks[task_id] = nil
45+
return true
46+
end
47+
if r[1] then tasks[task_id].exp = r[2] end
48+
return false
49+
end
50+
51+
local function make_task(task_id, f, ...)
52+
tasks[task_id] = {co=coroutine.create(f)}
53+
return inner_resume(task_id, ...)
54+
end
55+
56+
local function resume_task(task_id, event, ...)
57+
local exp = tasks[task_id].exp
58+
if exp ~= nil and event ~= exp then return false end
59+
return inner_resume(task_id, event, ...)
60+
end
61+
62+
local function event_queue(task_id, event)
63+
while true do
64+
local estruct = {os.pullEvent(event)}
65+
Queue.push(output, {task_id=task_id, result=estruct})
66+
end
67+
end
68+
69+
local function attampt_post(url, data)
70+
print(url)
71+
local r = http.post(url, post)
72+
if (r == nil) then
73+
print("Respones nil")
74+
return false
75+
else
76+
local response = r.getResponseCode()
77+
if (response ~= 200) then
78+
print('Response '..response)
79+
return false
80+
end
81+
end
82+
return true
83+
end
84+
85+
local function fetch_fn()
86+
if (attampt_post(url..'start/'..os.getComputerID()..'/'..args[1]..'/', "")) then
87+
while true do
88+
if (attampt_post(url..'gettask/'..os.getComputerID()..'/', "")) then
89+
local cmd = r.readAll()
90+
print(cmd)
91+
92+
if cmd == 'END' then
93+
break
94+
elseif cmd ~= 'NOOP' then
95+
local cmd, text = string_split(cmd)
96+
if cmd == 'TASK' then
97+
local task_id, text = str F438 ing_split(text)
98+
local f = loadstring(text)
99+
setfenv(f, genv)
100+
make_task(task_id, f)
101+
elseif cmd == 'STARTQUEUE' then
102+
local task_id, text = string_split(text)
103+
make_task(task_id, event_queue, task_id, text)
104+
elseif cmd == 'STOPQUEUE' then
105+
tasks[text] = nil
106+
end
107+
end
108+
end
109+
end
110+
end
111+
end
112+
113+
local function send_fn()
114+
while true do
115+
local r = Queue.pop(output)
116+
if r == nil then break end
117+
local answer = textutils.serializeJSON(r.result)
118+
attampt_post(url..'taskresult/'..os.getComputerID()..'/'..r.task_id..'/', answer)
119+
end
120+
end
121+
122+
if make_task('_fetch', fetch_fn) then return end
123+
124+
while true do
125+
local event, p1, p2, p3, p4, p5 = os.pullEvent()
126+
if resume_task('_fetch', event, p1, p2, p3, p4, p5) then break end
127+
128+
-- Use of http API in user code is explicitly disallowed
129+
if event ~= 'http_success' and event ~= 'http_failure' then
130+
local local_task_ids = {}
131+
for task_id in pairs(tasks) do
132+
local_task_ids[task_id] = true
133+
end
134+
for task_id in pairs(local_task_ids) do
135+
if not is_special_task(task_id) then
136+
resume_task(task_id, event, p1, p2, p3, p4, p5)
137+
end
138+
end
139+
end
140+
141+
if tasks['_send'] ~= nil then
142+
resume_task('_send', event, p1, p2, p3, p4, p5)
143+
else
144+
if Queue.length(output) > 0 then make_task('_send', send_fn) end
145+
end
146+
end

computercraft/back.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
local genv = getfenv()
22
local temp = {}
33
genv.temp = temp
4-
local url = 'http://127.0.0.1:8080/'
4+
local url = 'http://127.0.0.1:4343/'
55
local args = {...}
66
local tasks = {}
77

@@ -67,13 +67,13 @@ local function event_queue(task_id, event)
6767
end
6868

6969
local function fetch_fn()
70-
local r = http.post(url..'start/'..os.getComputerID()..'/'..args[1]..'/', "")
70+
local r = http.post(url..'start/'..os.getComputerID()..'/'..args[1]..'/')
7171
if (r == nil or r.getResponseCode() ~= 200) then
7272
print('Failed to start program '..args[1])
7373
return
7474
end
7575
while true do
76-
r = http.post(url..'gettask/'..os.getComputerID()..'/', "")
76+
r = http.post(url..'gettask/'..os.getComputerID()..'/', answer)
7777
if (r == nil or r.getResponseCode() ~= 200) then
7878
print('Connection broken')
7979
return

computercraft/notice board.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--
2+
-- Created by IntelliJ IDEA.
3+
-- User: duck-
4+
-- Date: 05/04/2020
5+
-- Time: 01:55
6+
-- To change this template use File | Settings | File Templates.
7+
--
8+
9+
10+
local back = peripheral.wrap("back")
11+
rules = {
12+
""
13+
}

computercraft/server.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232

3333
THIS_DIR = dirname(abspath(__file__))
34-
LUA_FILE = join(THIS_DIR, 'back.lua')
34+
LUA_FILE = join(THIS_DIR, 'back-debug.lua')
3535
DIGITS = string.digits + string.ascii_lowercase
3636

3737

@@ -193,11 +193,11 @@ async def taskresult(self, request):
193193
def backdoor(request):
194194
with open(LUA_FILE, 'r') as f:
195195
fcont = f.read()
196-
new_url = "local url = '{}://{}/'".format(request.scheme, request.host)
197-
# fcont = fcont.replace(
198-
# "local url = 'http://127.0.0.1:8080/'",
199-
# new_url
200-
# )
196+
new_url = "local url = '{}://{}:8080/'".format(request.scheme, request.host)
197+
fcont = fcont.replace(
198+
"local url = 'http://127.0.0.1:8080/'",
199+
new_url
200+
)
201201
return web.Response(text=fcont)
202202

203203
def initialize(self, source_module):

computercraft/startup.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--
2+
-- Created by IntelliJ IDEA.
3+
-- User: duck-
4+
-- Date: 05/04/2020
5+
-- Time: 02:49
6+
-- To change this template use File | Settings | File Templates.
7+
--
8+
9+
10+
os.run({}, "test.lua")
11+

examples/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# wget http://127.0.0.1:8080/ py
21
from .hello import program as hello
32
from .dig import dig
43
from .test import test
4+
from .notice_board import notice_board
55

6-
__all__ = (hello, dig, test)
6+
__all__ = (hello, dig, test, notice_board)

examples/hello.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
2-
import time
1+
# wget http://95.145.43.219:8080/ disk/py
32

43
async def program(api):
54
print("moo")

examples/notice_board.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import computercraft.errors
2+
3+
async def notice_board(api):
4+
back = await api.peripheral.wrap("back")
5+
rules = """
6+
rule 1: don't fuck the ducks
7+
rule 2: or the geese
8+
"""
9+
await back.callRemote("monitor_0", "clear")
10+
await back.callRemote("monitor_0", "setCursorPos", 1, 1)
11+
await back.callRemote("monitor_0", "write", rules)
12+
await api.print('woot!')
13+
14+

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def is_register_command(a):
3535
],
3636
keywords='computercraft minecraft',
3737
packages=['computercraft', 'computercraft.subapis'],
38-
package_data={'computercraft': ['back.lua']},
38+
package_data={'computercraft': ['back-debug.lua']},
3939
install_requires=['aiohttp'],
4040
entry_points={
4141
'console_scripts': ['computercraft = computercraft.server:main'],

0 commit comments

Comments
 (0)
0