Advertisement
saleks28

BOS4_Lab

Dec 8th, 2019
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.11 KB | None | 0 0
  1. require 'prime'
  2. require 'socket'
  3.  
  4. def FindPrime(a, b)
  5.     primes = []
  6.     if a > b
  7.          a,b = b,a
  8.     end
  9.     for i in a..b do
  10.         if Prime.prime?(i.to_i)
  11.             primes << i
  12.         end
  13.     end
  14.     return primes
  15. end
  16.  
  17. def StartMaster(a, b)
  18.     puts "Start master"
  19.     a, b = a.to_i, b.to_i
  20.     c = (a + b) / 2
  21.     slaveThread = Thread.new {StartServer(c + 1, b)}
  22.     primes1 = FindPrime(a, c)
  23.     slaveThread.join
  24.     puts "Found primes:", primes1 + slaveThread.value
  25. end
  26.  
  27. def StartServer(c, b)
  28.     puts "Started server"
  29.     server = TCPServer.new 9000
  30.     p server
  31.     connection = server.accept
  32.     connection.puts (c.to_s + " " + b.to_s)
  33.     result =  connection.gets
  34.     connection.close
  35.     return result.split(" ").map{|s| s.to_i}
  36. end
  37.  
  38. def StartSlave()
  39.     puts "Slave's alive"
  40.     sock = TCPSocket.new '192.168.43.40', 9000
  41.     range = sock.gets.split
  42.     primes = FindPrime(range[0].to_i, range[1].to_i)
  43.     sock.puts(primes.join(" "))
  44.     sock.close
  45. end
  46.  
  47.  
  48. #main
  49. if ARGV[0] == "MASTER"
  50.     StartMaster(ARGV[1], ARGV[2])
  51. elsif ARGV[0] == "SLAVE"
  52.     StartSlave()
  53. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement