Guest User

Untitled

a guest
May 26th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. require 'rubygems'
  2. require 'eventmachine'
  3. require 'em_netstring'
  4. require 'fuzzprotocol'
  5. require 'thread'
  6. require 'fuzzer'
  7.  
  8. prod_mutex=Mutex.new
  9. prod_queue=Queue.new
  10. prod_thread=Thread.new do
  11. begin
  12. gen=Generators::RollingCorrupt.new("XXXX",8,8)
  13. while gen.next?
  14. prod_mutex.synchronize {
  15. prod_queue << gen.next
  16. }
  17. end
  18. puts "Production Thread Finished."
  19. Thread.current.exit
  20. rescue
  21. puts "Error: Production Thread Exiting: #{$!}"
  22. Thread.current.exit
  23. end
  24. end
  25.  
  26. module FuzzServer
  27.  
  28. def post_init
  29. @handler=NetStringTokenizer.new
  30. @production_queue=nil
  31. @production_mutex=nil
  32. end
  33.  
  34. def receive_data(data)
  35. @handler.parse(data).each {|m|
  36. msg=FuzzMessage.new(m)
  37. if msg.verb=="CLIENT READY"
  38. # define a block to prepare the response
  39. get_data=proc do
  40. my_data=false
  41. loop do
  42. @production_mutex.synchronize {
  43. my_data=@production_queue.pop unless @production_queue.empty?
  44. }
  45. break if my_data
  46. sleep(rand(5))
  47. end
  48. # This is what will be passed to the callback
  49. @handler.pack(FuzzMessage.new({:verb=>"DELIVER",:data=>my_data}).to_yaml)
  50. end
  51. # This callback will be invoked once the response is ready.
  52. callback=proc do |data|
  53. send_data data
  54. end
  55. # Send the work to the thread queue, so we are ready for more connections.
  56. EM.defer(get_data, callback)
  57. end
  58. }
  59. end
  60.  
  61. def set_queue(q_obj,mutx)
  62. @production_queue=q_obj
  63. @production_mutex=mutx
  64. end
  65. end
  66.  
  67. EventMachine::run {
  68. EventMachine::start_server("0.0.0.0", 10000, FuzzServer) {|c|
  69. c.set_queue(prod_queue,prod_mutex)
  70. }
  71. }
Add Comment
Please, Sign In to add comment