123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- local osc = require("osc")
- local routing = {}
- -- a map of incoming to outgoing addresses
- -- each entry will contain a collection of entries
- local routingTable = {}
- -- local exampleRtEntry = {
- -- incoming = "/incoming/addr",
- -- outgoing = "/outgoing/addr",
- -- swizzle = {1,22,333,22,1},
- -- originalString = "/incoming/addr /outgoing/addr 1 22 333 22 1"
- -- origin = "path/to/file.txt" or "cmd"
- -- }
- -- adds a single routing string to the table
- function routing.addFromString(rawRouting, originName)
- local newEntry = {
- origin = originName,
- originalString = rawRouting,
- }
- -- grab adresses
- local addressMatcher = rawRouting:gmatch("[^%s]+")
- newEntry.incoming = addressMatcher()
- newEntry.outgoing = addressMatcher()
- -- grab swizzles and literals
- local _, ogEnd = rawRouting:find(newEntry.outgoing)
- local swizzleString = rawRouting:sub(ogEnd+1)
- local swizzleMatcher = swizzleString:gmatch("[^ ]+", ogEnd)
- newEntry.swizzle = {}
- for n in swizzleMatcher do
- if(tonumber(n)) then
- table.insert(newEntry.swizzle, tonumber(n))
- elseif n:match("\"(.*)\"") then
- table.insert(newEntry.swizzle, n:match("\"(.*)\""))
- elseif n:match(".*\.lua") then
- print("oscoroutines coming soon")
- -- require the
- else
- table.insert(newEntry.swizzle, n)
- end
- end
- -- make an entry in the table for this incoming if it is needed
- if not routingTable[newEntry.incoming] then
- routingTable[newEntry.incoming] = {}
- end
- -- insert our entry
- table.insert(routingTable[newEntry.incoming], newEntry)
- end
- -- adds a osc message to the routing table
- function routing.addFromMessage(msg)
- end
- -- adds a table of routing strings
- function routing.addFromTable(table)
- for _, v in pairs(table) do
- -- i added the : because it basically cant appera in a filename on any major OS
- routing.addFromString(v,":table")
- end
- end
- -- adds a file of routing strings, one per line
- function routing.addFromFile(filename)
- -- remove anything from this origin
- for name, incoming in pairs(routingTable) do
- local toRemove = {}
- for i=1,#incoming,1 do
- if incoming[i].origin == filename then
- table.insert(toRemove,i)
- end
- end
- for i=1,#toRemove,1 do
- incoming[i] = incoming[#incoming]
- table.remove(incoming)
- end
- end
-
- -- add all the stuff back in
- for line in io.lines(filename) do
- if line:match("[^%s]+") then
- routing.addFromString(line,filename)
- end
- end
- end
- local function routerHelper(routings, incomingMessage)
- local outgoingMessages = {}
- for _, route in pairs(routings) do
- local newaddr = osc.toOSCString(route.outgoing)
- -- create a new message
- local newMsg = osc.create(newaddr)
- -- loop over swizzles and add the appropriate arguments
- for _, s in pairs(route.swizzle) do
- if type(s) == "number" and math.floor(s) == s then
- osc.appendArgument(newMsg, incomingMessage[s], incomingMessage.argString:sub(s+1,s+1))
- elseif s:match("#%d+") then
- local nStr = tonumber(s:match("%d+"))
- osc.appendArgument(newMsg,nStr)
- elseif s:match('[^%z]+') then
- local stringLit = s:match('[^%z]+')
- osc.appendArgument(newMsg, stringLit)
- else
- error("invalid swizzle" .. "(" .. s .. ")")
- end
- end
- table.insert(outgoingMessages, newMsg)
- end
- return outgoingMessages
- end
- function routing.createRoutedMessages(incomingMessage)
- local searchStr = incomingMessage.addr:match("[^%z]+")
- local routings = routingTable[searchStr]
- if incomingMessage.addr:match("/rerouterator/started") then
- if routings then
- return routerHelper(routings, incomingMessage)
- end
- elseif incomingMessage.addr:match("/rerouterator/.*") then
- print("control messages not yet supported")
- elseif not routings then
- -- we have no appropriate rules just forward the message unaltered
- return {incomingMessage}
- else
- return routerHelper(routings, incomingMessage)
- end
- return {}
- end
- return routing
|