rerouterator.lua 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package.path = package.path .. ";.\\share\\lua\\5.1\\?.lua"
  2. local cli = require("cliargs")
  3. local socket = require("socket")
  4. local osc = require("osc")
  5. local routing = require("routing")
  6. local serverRoutine = require("serverRoutine")
  7. local watchFile = require("fileWatchRoutine")
  8. --set up cliargs
  9. local function print_version()
  10. print("rerouterator version: 1")
  11. print("\t- lua version: 1.5.1")
  12. print("\t- luabitop version: 1.0.2")
  13. print("\t- lua_cliargs version: " .. cli.VERSION)
  14. print("\t- " .. socket._VERSION)
  15. os.exit(0)
  16. end
  17. cli:set_name("rerouterator")
  18. cli:flag("-v, --version", "prints rerouterator's version, and the versions of it's dependencies", print_version)
  19. cli:option("-n, --hostname=HOSTNAME","the hostname to listen on.", "0.0.0.0")
  20. cli:option("-l, --listen-port=port","the port to listen on.", {6312})
  21. cli:option("-f, --forward-port=port","the ports to send routed messages to.", {4200})
  22. cli:option("-r, --route \"routing\"","add a routing", {})
  23. cli:option("-c, --config <config file>","add a file with routings in it. One per line", {})
  24. local args, cliErr = cli:parse(arg)
  25. if not args and cliErr then
  26. -- something wrong happened and an error was printed
  27. print(string.format('%s: %s', cli.name, cliErr))
  28. os.exit(1)
  29. end
  30. -- set up routing with the cli stuff
  31. routing.addFromTable(args.route)
  32. -- routines that get run constantly on the server
  33. local activities = {}
  34. -- make a watchfile coroutine for every file we need watched
  35. for _, filename in pairs(args.config) do
  36. table.insert(activities,coroutine.create(watchFile))
  37. coroutine.resume(activities[#activities],filename,routing.addFromFile)
  38. end
  39. -- add a routine to listen to every port in our list
  40. for i=1,#args["listen-port"],1 do
  41. table.insert(activities, coroutine.create(serverRoutine))
  42. coroutine.resume(activities[#activities],args.hostname,args["listen-port"][i],args["forward-port"])
  43. end
  44. -- coroutine that gets called once at the start then never again
  45. local function callOnStart()
  46. local udp = assert(socket.udp())
  47. local msg = {}
  48. msg.addr = osc.toOSCString("/rerouterator/started")
  49. msg.argString = ","
  50. udp:sendto(osc.pack(msg),"127.0.0.1",args["listen-port"][1])
  51. -- we no longer need to be called!
  52. table.remove(activities,#activities)
  53. coroutine.yield()
  54. end
  55. table.insert(activities, coroutine.create(callOnStart));
  56. while true do
  57. for i=1,#activities,1 do
  58. local success, err = coroutine.resume(activities[i])
  59. if not success then
  60. print(err .. ": " .. i)
  61. os.exit(1)
  62. end
  63. end
  64. end