-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.rb
80 lines (71 loc) · 1.78 KB
/
server.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
require 'socket'
require 'zlib'
require './metaInfo.rb'
class Server
def initialize
@ipList = Array.new
@ip = "127.0.0.1"
@port = 50504
@u1 = UDPSocket.new
@u1.bind(@ip, @port)
end
def wait_for_clients
loop {
message = @u1.recvfrom(8)
newIP = message[1][2]
if MetaInfo::HELLO_MSG == message[0]
# puts "Connect:\t#{message[1][2]}"
# TODO: uncomment after debugging
# if([email protected]? newIP)
@ipList << newIP
serializedIpList = Marshal.dump(@ipList)
ipListSize = [serializedIpList.size]
@ipList.each do |singleIp|
puts singleIp
@u1.send getControlSum(serializedIpList), 0, singleIp, 50506
@u1.send ipListSize.pack('q'), 0, singleIp, 50506
@u1.send serializedIpList, 0, singleIp, 50506
end
# end
elsif MetaInfo::DISCON_MSG == message[0]
@ipList.delete newIP
# puts "Disconnect:\t#{newIP}"
else
puts "Ein Fehler ist beim verbinden aufgetreten."
end
}
end
def getControlSum(text)
return [Zlib::crc32(text)].pack('L')
end
def get_message
puts "in threrad"
loop {
hashSum = @u1.recvfrom(32)
size = @u1.recvfrom(8)
size = size[0].unpack('q')[0]
message = @u1.recvfrom(size)
# if(hashSum == getControlSum(message[0]))
puts message[1][3].to_s + ": " + message[0]
# end
}
end
def close
@u1.close
end
end
if __FILE__ == $0
server = Server.new
begin
server.wait_for_clients
rescue Interrupt
server.close
puts "\nP2P-Server stopped"
# rescue Exception => e
# server.close
# for debugging issues only
# puts e
# puts "\nAn unknown error occured."
end
# server.get_message
end