-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathr2-gui-server.r
250 lines (224 loc) · 7.07 KB
/
r2-gui-server.r
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
REBOL [
Title: "R2 GUI server"
Date: 19-May-2018
File: %t2-gui-server.r
Purpose: {
}
History: [
]
Notes: {
use: view -s r2-gui-server.r
}
]
; will be passing blocks to Ren-c
_: :none
listen-on: 8081
; download latest r3
ver: reverse rebol/Version
ver: rejoin [ "0." ver/2 "." ver/1]
r3binary: either system/version/4 = 3 [%r3.exe][%r3]
if not exists? r3binary [
downloads: read http://metaeducation.s3.amazonaws.com/index.html
either parse downloads [thru <rebol> copy data to </rebol> to end][
data: load data
binary: select data ver
fl: flash join "Download binary from " binary/1
write/binary r3binary read/binary binary/1
set-modes r3binary [owner-execute: true]
unview/only fl
; in linux should now set the permissions to executable
][
print "Unable to find a current binary"
halt
]
]
; setup web server
web-dir: %. ; the path to where you store your web files
attempt [
unview/all
close web
]
if not exists? %httpd.r [
write %httpd.r read https://raw.githubusercontent.com/gchiu/Scripts-For-Rebol-2/059e962af47e740fbf8963af9327674c5070dd48/httpd.r
]
do %httpd.r
makeUUID: func [
"Generates a Version 4 UUID that is compliant with RFC 4122"
/local data ; so 'data doesn't leak
][
; COLLECT/KEEP is a handy accumulator
data: collect [
loop 16 [keep -1 + random/secure 256]
]
; don't need to wrap each expression with DO
; Rebol infix evaluation is left to right, so don't need to parenthesize
data/7: data/7 and 15 or 64
data/9: data/9 and 63 or 128
; TO BINARY! converts a block of integers to code points
; ENBASE converts the codepoints to hex
data: enbase/base to binary! data 16
; We'll just modify this new string and return the head
data: insert skip data 8 "-"
data: insert skip data 4 "-"
data: insert skip data 4 "-"
data: insert skip data 4 "-"
head data
]
random/seed now/precise ; needed for the makeUUID
task: make object! [
id:
client-id:
callback:
created:
start:
end:
cmd: _
cancelled: false
]
view/new layout [
origin 0
b: banner 140x32 rate 1
effect [gradient 0x1 0.0.150 0.0.50]
feel [engage: func [f a e] [set-face b now/time]]
button "Print hello" [print "hello"]
button "Rebol.com" 100 [
t: make task compose [
id: (makeUUID)
created: (now/precise)
cmd: "read http://www.rebol.com"
callback: func [data][
set-face textarea data
]
]
append task-queue t
]
button "https://forum.rebol.info" 150 [
t: make task compose [
id: (makeUUID)
created: (now/precise)
cmd: "read https://forum.rebol.info"
callback: func [data][
set-face textarea data
]
]
append task-queue t
]
text "This won't work on R2/View since it lacks the correct cipher suite"
button "Download 200 Mb file" 150 [
t: make task compose [
id: (makeUUID)
created: (now/precise)
cmd: "{download complete} | read http://ipv4.download.thinkbroadband.com:8080/200MB.zip"
callback: func [data][
set-face textarea data
]
]
append task-queue t
]
button "Browse Index" [
append task-queue make task compose [
id: (makeUUID)
created: (now/precise)
cmd: "browse http://metaeducation.s3.amazonaws.com/index.html"
callback: func [data][
set-face textarea "Browsed to http://metaeducation.s3.amazonaws.com/index.html"
]
]
]
button "Task Queue" [probe task-queue]
button "Halt" [unview/all halt]
textarea: area [300x400] join "Listening on port: " listen-on
]
task-queue: []
script: rejoin [{
Rebol [file: client.r3]
system/options/dump-size: 400
client-id: uuid/to-text uuid/generate
dump client-id
print "waiting 5 seconds ..."
wait 5 ; for things to start up
print "Now grabbing tasks"
forever [
; grab a task
; attempt [
print ["requesting a task" now/time]
task: to text! trim read join-of http://localhost:listen-on/tasks?client-id= client-id
if task <> "none" [
t1: now/precise
print "loading task"
task: do task
probe task
cmd: load task/cmd
probe cmd
print "doing task"
result: do cmd
print "finished task"
t2: now/precise
data: spaced ["Task Received by instance" client-id newline
"Task commenced at:" t1 newline
"Task finished at:" t2
#{0D0A0D0A}
]
if binary? result [
append data result
]
write http://localhost:listen-on/done-tasks compose [POST [content-type: "text/text"] (data)]
]
; ]
wait 5
]
}]
replace/all script "listen-on" listen-on
write %script.reb script
process: reform [to-local-file clean-path r3binary "-cs" %script.reb]
loop 1 [ ; try different numbers to see how many processes the r2 webserver can handle
call/show process
]
open/custom web: join httpd://: listen-on [
; you have access here to two objects: REQUEST and RESPONSE
; you can set the response by altering the fields in the RESPONSE object
; by default, the server returns 404
; probe request
if request/action = "POST /done-tasks" [
response/status: 200
response/content: "OK"
result: to string! request/binary
parse/all result [copy text to #{0D0A0D0A} thru #{0D0A0D0A} copy binary to end]
trim text
trim/head/tail binary
set-face textarea text
binary: load binary
attempt [print to string! binary]
]
if request/action = "GET /tasks" [
either parse request/request-uri ["/tasks?client-id=" copy client-id to end][
response/status: 200
response/type: "text/text"
; got a valid request so return a task
new-task: none
foreach task task-queue [
if none? task/start [
task/start: now/precise
task/client-id: client-id
print mold task
print "======>sent a task"
new-task: mold task
replace/all new-task "none" "_"
response/content: new-task
break
]
]
if none? new-task [
; no tasks available to send blank task
print "No tasks available"
response/content: "none"
]
][
print "Unrecognized command received"
response/content: "Unrecognized command received"
response/status: 400
]
]
; setting RESPONSE/KILL? to TRUE will break the WAIT loop below
]
wait []