cs_central-cl.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. fromto = ""
  38. from = ""
  39. cmd = ""
  40. payload = ""
  41. if line =~ /^\d+-\d+-\d+ \d+:\d+:\d+ \+\d+: (.+)$/
  42. line = $1
  43. if line =~ /^(from|to):([a-zA-Z\._]+): (.+)/
  44. fromto = $1
  45. from = $2
  46. line = $3
  47. end
  48. if line =~ /^([A-Z_]+)/
  49. cmd = $1
  50. end
  51. if line =~ /^[A-Z_]+ (.+)/
  52. payload = $1
  53. end
  54. if allowed(cmd)
  55. # an allowed command with a seemingly valid payload has been received
  56. put_log "from: #{from}"
  57. put_log "cmd: #{cmd}"
  58. put_log "line: #{line}"
  59. put_log "payload #{payload}"
  60. if cmd == "REQ_BC"
  61. if payload =~ /^(.+),(.+),(.+),'(.+)','(.+)'$/
  62. # n f s ni txt
  63. # $&
  64. # 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.
  65. network = $1
  66. freqname = $2
  67. source = $3
  68. nickname = $4
  69. text = $5
  70. # we can fiddle with source here!
  71. bcid = Digest::MD5.hexdigest("%d %s %s %s %s %s" % [Time.now.utc.to_i, network, freqname, source, nickname, text])
  72. # so it only reaches the issuer of REQ_BC
  73. put_to(from, "BC_ID", "#{bcid} for: #{network},#{freqname},#{source}")
  74. #$broadcasts[bcid] = from
  75. finalmessage = "%s %s,%s,%s,'%s','%s'" % [bcid, network, freqname, source, nickname, text]
  76. put_not_to(from, "BC", finalmessage)
  77. end # of match not nil
  78. end # of REQ_BC
  79. else # of allowed cmd - means .. we don't handle these:
  80. put_log "'#{line}'"
  81. end
  82. end
  83. end
  84. def consuming()
  85. put_log "SYS Starting reading thread\n"
  86. loop do
  87. begin
  88. line = $s.gets.chomp
  89. work(line)
  90. rescue
  91. end
  92. end
  93. end
  94. def giving
  95. inputmessage = ""
  96. while inputmessage != "quit"
  97. inputmessage = ""
  98. inputmessage = STDIN.gets.chomp
  99. # put to ssh
  100. $s.puts inputmessage
  101. end
  102. end
  103. def main
  104. raise "ARGV[0] must be valid ssh commands with the uri (user@host) in the beginning!" unless ARGV[0] =~ /^.+@.+$/
  105. sshcmd = "%s" % [ARGV[0]]
  106. $s = IO.popen("ssh #{sshcmd}", "r+")
  107. thread_consuming = Thread.new {consuming}
  108. giving
  109. end
  110. main
  111. =begin
  112. #client sending message and awaiting replies
  113. #stomp.subscribe("/temp-queue/random_replies")
  114. #stomp.publish("/queue/random_generator", "random", {"reply-to" => "/temp-queue/random_replies"})
  115. #Timeout::timeout(2) do
  116. # msg = stomp.receive
  117. # puts "Got the response: #{msg.body}"
  118. #end
  119. #
  120. #stomp.publish(msg.headers["reply-to"], number)
  121. #
  122. #
  123. =end