main.rb 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. script "log.rb"
  2. log "Main mruby script loaded OK."
  3. p Signal.constants
  4. script "client.rb"
  5. # Return an array of symbols of constants of klass that match value
  6. def const_syms(klass, value)
  7. res = []
  8. klass.constants.each do |c|
  9. cv = klass.const_get(c)
  10. res << c if cv == value
  11. end
  12. return res
  13. end
  14. def signal_syms(value)
  15. return const_syms(Signal, value)
  16. end
  17. def start_timers
  18. @started_timers ||= false
  19. if @started_timers
  20. log "Timers already started."
  21. else
  22. log "Staring timer(s)..."
  23. @timer_id = Woe::Server.new_timer()
  24. Woe::Server.set_timer(@timer_id, 1.0, 1.0);
  25. end
  26. @started_timers = true
  27. end
  28. def woe_on_connect(client_id)
  29. p "Client #{client_id} connected"
  30. Client.add(client_id)
  31. end
  32. def woe_on_disconnect(client_id)
  33. p "Client #{client_id} disconnected"
  34. Client.remove(client_id)
  35. end
  36. def woe_on_input(client_id, buf)
  37. p "Client #{client_id} input #{buf}"
  38. client = Client.get(client_id)
  39. unless client
  40. log "Unknown client #{client_id} in woe_on_input."
  41. Woe::Server.disconnect(client_id)
  42. else
  43. p "Client #{client} #{client.id} ok."
  44. client.on_input(buf)
  45. end
  46. end
  47. def woe_on_negotiate(client_id, how, option)
  48. p "Client #{client_id} negotiating."
  49. end
  50. def woe_on_subnegotiate(client_id, option, buffer)
  51. p "Client #{client_id} subnegotiating."
  52. end
  53. def woe_on_iac(client_id, option, command)
  54. p "Client #{client_id} iac #{command}."
  55. end
  56. def woe_on_ttype(client_id, cmd, name)
  57. p "Client #{client_id} ttype #{cmd} #{name}."
  58. end
  59. def woe_on_error(client_id, code, message)
  60. end
  61. def woe_on_warning(client_id, code, message)
  62. end
  63. def woe_begin_compress(client_id, state)
  64. end
  65. def woe_begin_zmp(client_id, size)
  66. end
  67. def woe_zmp_arg(client_id, index, value)
  68. end
  69. def woe_finish_zmp(client_id, size)
  70. end
  71. def woe_begin_environ(client_id, size)
  72. end
  73. def woe_environ_arg(client_id, index, type, key, value)
  74. end
  75. def woe_finish_environ(client_id, size)
  76. end
  77. def woe_begin_mssp(client_id, size)
  78. end
  79. def woe_mssp_arg(client_id, index, type, key, value)
  80. end
  81. def woe_finish_mssp(client_id, size)
  82. end
  83. def woe_on_signal(signal)
  84. log "Received signal #{signal} #{signal_syms(signal)} in script"
  85. case signal
  86. when 10 # SIGUSR1
  87. log "Reloading main script."
  88. script "main.rb"
  89. when 28
  90. # ignore this signal
  91. else
  92. Woe::Server.quit
  93. end
  94. end
  95. def woe_on_timer(timer, value, interval)
  96. log "Timer #{timer} #{value} #{interval} passed."
  97. end
  98. start_timers