central.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #!/usr/bin/ruby
  2. # vim: expandtab tabstop=2 shiftwidth=2 softtabstop=2 autoindent:
  3. require 'rubygems'
  4. require 'digest/md5'
  5. STDOUT.sync = true
  6. $role_receives = ["ASSIGN_RE", "UNASSIGN_RE", "PING_RE", "ASSIGNMENTS_RE", "REQ_BC", "BC_RE"]
  7. def put_debug(msg)
  8. if $debug == 1
  9. puts msg
  10. end
  11. end
  12. def put_log(msg)
  13. puts "Z #{Time.now.strftime("%Y-%m-%d %H:%M:%S %z")}: #{msg}"
  14. end
  15. def allowed(inputcmd)
  16. $role_receives.each{
  17. |c|
  18. #print "A #{c} B #{inputcmd}\n"
  19. if inputcmd =~ /^#{c}/
  20. return true
  21. end
  22. }
  23. return false
  24. end
  25. def put_to(to, cmd, payload)
  26. if to && cmd && payload
  27. puts "to:#{to}: #{cmd} #{payload}"
  28. $s.puts "to:#{to}: #{cmd} #{payload}"
  29. end
  30. end
  31. def put_not_to(to, cmd, payload)
  32. puts "!to:#{to}: #{cmd} #{payload}"
  33. $s.puts "!to:#{to}: #{cmd} #{payload}"
  34. end
  35. def work(line)
  36. #put_log "GOT LINE: #{line}"
  37. line =~ /^\d+-\d+-\d+ \d+:\d+:\d+ \+\d+: (.+)$/
  38. fromto = ""
  39. from = ""
  40. cmd = ""
  41. payload = ""
  42. if not $& == nil
  43. line = $1
  44. if line =~ /^(from|to):([a-zA-Z_]+): (.+)/
  45. fromto = $1
  46. from = $2
  47. line = $3
  48. end
  49. if line =~ /^([A-Z_]+)/
  50. cmd = $1
  51. end
  52. if line =~ /^[A-Z_]+ (.+)/
  53. payload = $1
  54. end
  55. if allowed(cmd)
  56. # an allowed command with a seemingly valid payload has been received
  57. put_log "from: #{from}"
  58. put_log "cmd: #{cmd}"
  59. put_log "line: #{line}"
  60. put_log "payload #{payload}"
  61. if cmd == "REQ_BC"
  62. if payload =~ /^(.+),(.+),(.+),'(.+)','(.+)'$/
  63. # n f s ni txt
  64. # $&
  65. # The string matched by the last successful pattern match in this scope, or nil if the last pattern match failed. (Mnemonic: like & in some editors.) This variable is read-only.
  66. network = $1
  67. freqname = $2
  68. source = $3
  69. nickname = $4
  70. text = $5
  71. # we can fiddle with source here!
  72. bcid = Digest::MD5.hexdigest("%d %s %s %s %s %s" % [Time.now.utc.to_i, network, freqname, source, nickname, text])
  73. # so it only reaches the issuer of REQ_BC
  74. put_to(from, "BC_ID", "#{bcid} for: #{network},#{freqname},#{source}")
  75. $broadcasts[bcid] = from
  76. finalmessage = "%s %s,%s,%s,'%s','%s'" % [bcid, network, freqname, source, nickname, text]
  77. put_not_to(from, "BC", finalmessage)
  78. end # of match not nil
  79. end # of REQ_BC
  80. else # of allowed cmd - means .. we don't handle these:
  81. put_log "'#{line}'"
  82. end
  83. end
  84. end
  85. def consuming()
  86. put_log "SYS Starting reading thread\n"
  87. loop do
  88. begin
  89. line = $s.gets.chomp
  90. work(line)
  91. rescue
  92. end
  93. end
  94. end
  95. def giving
  96. inputmessage = ""
  97. while inputmessage != "quit"
  98. inputmessage = ""
  99. inputmessage = STDIN.gets.chomp
  100. # put to ssh
  101. $s.puts inputmessage
  102. end
  103. end
  104. def main
  105. raise "ARGV[0] must be valid ssh commands with the uri (user@host) in the beginning!" unless ARGV[0] =~ /^.+@.+\..+$/
  106. sshcmd = "%s" % [ARGV[0]]
  107. $s = IO.popen("ssh #{sshcmd}", "r+")
  108. thread_consuming = Thread.new {consuming}
  109. giving
  110. end
  111. main
  112. =begin
  113. #client sending message and awaiting replies
  114. #stomp.subscribe("/temp-queue/random_replies")
  115. #stomp.publish("/queue/random_generator", "random", {"reply-to" => "/temp-queue/random_replies"})
  116. #Timeout::timeout(2) do
  117. # msg = stomp.receive
  118. # puts "Got the response: #{msg.body}"
  119. #end
  120. #
  121. #stomp.publish(msg.headers["reply-to"], number)
  122. #
  123. #
  124. =end