-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwpp.lua
416 lines (338 loc) · 13.2 KB
/
wpp.lua
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
local debugMode = false
local CURRENT_VERSION = "1"
local THIS_COMPUTER_ID = os.getComputerID()
local currentProtocol = "wpp@default"
local prefetchCache = {}
local function splitString (inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={}
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
table.insert(t, str)
end
return t
end
local function log(message)
local logMessage = debug.getinfo(2).currentline ..": ".. message
if debugMode then
print(logMessage)
--rednet.broadcast({type="debug", version=CURRENT_VERSION, data=logMessage}, "debug_".. currentProtocol)
end
end
-- Start->Wireless Modem Setup
peripheral.find("modem", function(name, wrapped)
if wrapped.isWireless() then
rednet.open(name)
end
end)
if not rednet.isOpen() then
error("No wireless modem found", 2)
end
-- End->Wireless Modem Setup
-- Start->
local function parsePeripheralUrl(peripheralUrl)
local urlParts = splitString(peripheralUrl, "/")
if urlParts[1] == (currentProtocol ..":") and urlParts[2] and urlParts[3] then
return {clientId=tonumber(urlParts[2]), peripheralId=urlParts[3]}
else
return nil
end
end
local function sendMessage(clientId, type, data)
log("Sending message with type '".. type .."' to ".. currentProtocol .."://".. clientId .." with data: ".. textutils.serialize(data))
rednet.send(clientId, {type=type, version=CURRENT_VERSION, data=data}, currentProtocol)
end
local function sendReply(clientId, data)
sendMessage(clientId, "reply", data)
end
local function recieveReply()
local clientId, message = rednet.receive(currentProtocol, 10)
if message == nil then
return nil
end
if(message.type == "reply") then
return message
else
return nil
end
end
-- End->
local nativePeripheral = peripheral
-- Start->Wrapped Peripheral API funtcions
-- These are ran on the computers that are directly connected to the peripherals
local wrappedPeripheralApi = {
getNames=function(clientId)
log("Real getNames(".. clientId ..")")
sendReply(clientId, nativePeripheral.getNames())
end,
isPresent=function(clientId, peripheralName)
log("Real isPresent("..clientId..", ".. peripheralName ..")")
sendReply(clientId, nativePeripheral.isPresent(peripheralName))
end,
getType=function(clientId, peripheralName)
log("Real getType("..clientId..", ".. peripheralName ..")")
sendReply(clientId, nativePeripheral.getType(peripheralName))
end,
getMethods=function(clientId, peripheralName)
log("Real getMethods("..clientId..", ".. peripheralName ..")")
sendReply(clientId, nativePeripheral.getMethods(peripheralName))
end,
call=function(clientId, peripheralName, methodName, ...)
local args = ...
log("Real call("..clientId..", ".. peripheralName ..", ".. methodName ..", ".. textutils.serialize(args) ..")")
local status,result = pcall(
function()
local r = {nativePeripheral.call(peripheralName, methodName, unpack(args))}
return r
end)
sendReply(clientId, {returned=result, error=not status})
end,
wppPrefetch=function(clientId, peripheralName, methods)
log("Real wppPrefetch("..clientId..", ".. peripheralName ..", ".. textutils.serialize(methods) ..")")
local methodResults = {}
for possibleMethodName,methodInfo in pairs(methods) do
local methodName
local methodArgs
if type(methodInfo) == "table" then
methodName = possibleMethodName
methodArgs = methodInfo
else
methodName = methodInfo
methodArgs = {}
end
local status,result = pcall(
function()
local r = {nativePeripheral.call(peripheralName, methodName, unpack(methodArgs))}
return r
end)
if status then
methodResults[methodName] = result
end
end
sendReply(clientId, methodResults)
end,
}
-- End->Wrapped Peripheral API funtcions
local remotePeripheral = {}
local wireless = {}
-- Start->Public API functions.
function wireless.setDebugMode(mode)
debugMode = mode
end
function wireless.connect(networkId)
log("Changing protocol from ".. currentProtocol .." to wpp@".. networkId)
currentProtocol = "wpp@".. networkId
end
function wireless.host(networkId)
rednet.unhost(currentProtocol)
wireless.connect(networkId)
rednet.host(currentProtocol, tostring(THIS_COMPUTER_ID))
end
function wireless.localEventHandler(event)
-- event: {1="message type", 2="sender client id", 3="message data", 4="protocol"}
if event[1] == "rednet_message" then
if event[4] == currentProtocol then
if event[3].version and event[3].version == CURRENT_VERSION then
log("Recieved message: ".. textutils.serialize(event[3]))
if event[3].type == "function" then
wrappedPeripheralApi[event[3].data.func](event[2], unpack(event[3].data.args or {}))
end
else
log("Recieved event from an unsupported WPP version. Only version '".. CURRENT_VERSION .."' is supported.")
end
end
end
end
function wireless.listen(networkId)
rednet.unhost(currentProtocol)
wireless.connect(networkId)
rednet.host(currentProtocol, tostring(THIS_COMPUTER_ID))
print("Listening for WPP events on ".. currentProtocol)
print("Control+T to quit")
while(true) do
local event = {os.pullEvent()}
wireless.localEventHandler(event)
end
end
function wireless.prefetchMethods(peripheralUrl, methods)
log("New prefetchMethods(".. peripheralUrl ..", ".. textutils.serialize(methods) ..")")
local parsedPeripheralUrl = parsePeripheralUrl(peripheralUrl)
if parsedPeripheralUrl == nil then
prefetchCache[peripheralUrl] = {}
for possibleMethodName,methodInfo in pairs(methods) do
local methodName
local methodArgs
if type(methodInfo) == "table" then
methodName = possibleMethodName
methodArgs = methodInfo
else
methodName = methodInfo
methodArgs = {}
end
local status,result = pcall(
function()
local r = {remotePeripheral.call(peripheralUrl, methodName, unpack(methodArgs))}
return r
end)
if status then
prefetchCache[peripheralUrl][methodName] = result
end
end
else
sendMessage(parsedPeripheralUrl.clientId, "function", {func="wppPrefetch", args={parsedPeripheralUrl.peripheralId, methods}})
local reply = recieveReply()
if reply then
prefetchCache[peripheralUrl] = reply.data
end
end
end
-- Start->New peripheral API using WPP
function remotePeripheral.getNames()
local allNames = nativePeripheral.getNames()
local clients = table.pack(rednet.lookup(currentProtocol))
log("New getNames() found these clients: ".. textutils.serialize(clients))
for n,clientId in ipairs(clients) do
if clientId ~= THIS_COMPUTER_ID then
sendMessage(clientId, "function", {func="getNames"})
local reply = recieveReply()
log("New getNames() reply: ".. textutils.serialize(reply))
if reply then
for n,name in ipairs(reply.data) do
table.insert(allNames, currentProtocol .."://" .. clientId .. "/" .. name)
end
end
end
end
return allNames
end
function remotePeripheral.isPresent(peripheralUrl)
log("New isPresent(".. peripheralUrl ..")")
local parsedPeripheralUrl = parsePeripheralUrl(peripheralUrl)
if parsedPeripheralUrl == nil then
log("New isPresent(".. peripheralUrl ..") using local peripheral")
return nativePeripheral.isPresent(peripheralUrl)
else
sendMessage(parsedPeripheralUrl.clientId, "function", {func="isPresent", args={parsedPeripheralUrl.peripheralId}})
local reply = recieveReply()
log("New isPresent(".. peripheralUrl ..") reply: ".. textutils.serialize(reply))
if reply then
return reply.data;
else
return false
end
end
end
function remotePeripheral.getType(peripheralUrl)
log("New getType(".. peripheralUrl ..")")
local parsedPeripheralUrl = parsePeripheralUrl(peripheralUrl)
if parsedPeripheralUrl == nil then
log("New getType(".. peripheralUrl ..") using local peripheral")
return nativePeripheral.getType(peripheralUrl)
else
sendMessage(parsedPeripheralUrl.clientId, "function", {func="getType", args={parsedPeripheralUrl.peripheralId}})
local reply = recieveReply()
log("New getType(".. peripheralUrl ..") reply: ".. textutils.serialize(reply))
if reply then
return reply.data;
else
return nil
end
end
end
function remotePeripheral.getMethods(peripheralUrl)
log("New getMethods(".. peripheralUrl ..")")
local parsedPeripheralUrl = parsePeripheralUrl(peripheralUrl)
if parsedPeripheralUrl == nil then
log("New getMethods(".. peripheralUrl ..") using local peripheral")
return nativePeripheral.getMethods(peripheralUrl)
else
sendMessage(parsedPeripheralUrl.clientId, "function", {func="getMethods", args={parsedPeripheralUrl.peripheralId}})
local reply = recieveReply()
log("New getMethods(".. peripheralUrl ..") reply: ".. textutils.serialize(reply))
if reply then
return reply.data;
else
return nil
end
end
end
function remotePeripheral.call(peripheralUrl, method, ...)
log("New call(".. peripheralUrl ..", ".. method ..", ".. textutils.serialize({...}) ..")")
if prefetchCache[peripheralUrl] and prefetchCache[peripheralUrl][method] then
log("New call(".. peripheralUrl ..", ".. method ..") using prefetched return")
local returnValue = prefetchCache[peripheralUrl][method]
prefetchCache[peripheralUrl][method] = nil
return unpack(returnValue)
end
local parsedPeripheralUrl = parsePeripheralUrl(peripheralUrl)
if parsedPeripheralUrl == nil then
log("New call(".. peripheralUrl ..", ".. method ..") using local peripheral")
return nativePeripheral.call(peripheralUrl, method, ...)
else
sendMessage(parsedPeripheralUrl.clientId, "function", {func="call", args={parsedPeripheralUrl.peripheralId, method, {...}}})
local reply = recieveReply()
log("New call(".. peripheralUrl ..", ".. method ..") reply: ".. textutils.serialize(reply))
if reply then
if reply.data.error then
error(reply.data.returned)
end
return unpack(reply.data.returned);
else
return nil
end
end
end
function remotePeripheral.wrap(peripheralUrl)
log("New wrap(".. peripheralUrl ..")")
local parsedPeripheralUrl = parsePeripheralUrl(peripheralUrl)
if parsedPeripheralUrl == nil then
log("New wrap(".. peripheralUrl ..") using local peripheral")
return nativePeripheral.wrap(peripheralUrl)
else
if not remotePeripheral.isPresent(peripheralUrl) then
return nil
end
local peripheralMethods = remotePeripheral.getMethods(peripheralUrl)
log("New wrap(".. peripheralUrl ..") wrapping these methods: ".. textutils.serialize(peripheralMethods))
local wrappedMethodsTable = {}
if peripheralMethods then
for n,method in ipairs(peripheralMethods) do
wrappedMethodsTable[method] = function(...)
return remotePeripheral.call(peripheralUrl, method, ...)
end
end
end
wrappedMethodsTable["wppPrefetch"] = function(methods)
wireless.prefetchMethods(peripheralUrl, methods)
end
return wrappedMethodsTable;
end
end
function remotePeripheral.find(_type, filterFunction)
log("New find(".. _type ..", hasFilterFunction=".. tostring(not(not filterFunction) or false) ..")")
local foundToReturn = {nativePeripheral.find(_type, filterFunction)}
local allPeripherals = remotePeripheral.getNames()
for n,peripheralUrl in ipairs(allPeripherals) do
if remotePeripheral.getType(peripheralUrl) == _type then
local wrappedPeripheral = remotePeripheral.wrap(peripheralUrl)
if filterFunction then
local peripheralName = remotePeripheral.getName(peripheralUrl)
if(filterFunction(peripheralName, wrappedPeripheral)) then
table.insert(foundToReturn, wrappedPeripheral)
end
else
table.insert(foundToReturn, wrappedPeripheral)
end
end
end
local next = next
if next(foundToReturn) == nil then
return nil
else
return table.unpack(foundToReturn)
end
end
-- End->New peripheral API using WPP
-- End->Public API Functions
return {wireless=wireless, peripheral=remotePeripheral}