Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'prime'
- require 'socket'
- def FindPrime(a, b)
- primes = []
- if a > b
- a,b = b,a
- end
- for i in a..b do
- if Prime.prime?(i.to_i)
- primes << i
- end
- end
- return primes
- end
- def StartMaster(a, b)
- puts "Start master"
- a, b = a.to_i, b.to_i
- c = (a + b) / 2
- slaveThread = Thread.new {StartServer(c + 1, b)}
- primes1 = FindPrime(a, c)
- slaveThread.join
- puts "Found primes:", primes1 + slaveThread.value
- end
- def StartServer(c, b)
- puts "Started server"
- server = TCPServer.new 9000
- p server
- connection = server.accept
- connection.puts (c.to_s + " " + b.to_s)
- result = connection.gets
- connection.close
- return result.split(" ").map{|s| s.to_i}
- end
- def StartSlave()
- puts "Slave's alive"
- sock = TCPSocket.new '192.168.43.40', 9000
- range = sock.gets.split
- primes = FindPrime(range[0].to_i, range[1].to_i)
- sock.puts(primes.join(" "))
- sock.close
- end
- #main
- if ARGV[0] == "MASTER"
- StartMaster(ARGV[1], ARGV[2])
- elsif ARGV[0] == "SLAVE"
- StartSlave()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement