-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.rb
228 lines (211 loc) · 6.99 KB
/
test.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# LGPL
require_relative 'lib/minitcp.rb'
BasicSocket.do_not_reverse_lookup = true
Thread.abort_on_exception = true
if ARGV.size==0 || ARGV[0]=="1"
puts "**********************************************************"
puts "** Test basic, one client, multi client, any receive"
puts "**********************************************************"
srv=MServer.service(2200,"0.0.0.0",22) do |socket|
socket.after(100) { socket.puts "Hello client" }
socket.on_any_receive { |data| puts " Server recieved: #{data.inspect}" }
socket.on_timer(2000) { socket.puts "CouCou say server @ #{Time.now}" rescue nil }
puts " srv waiting..."
socket.wait_end
puts " end server connection!"
end
MClient.run_one_shot("localhost",2200) do |socket|
socket.on_any_receive { |data| p "client recieved #{data.inspect}"}
p "connected in client"
3.times { |j| socket.puts "Hello #{j}..." ; sleep(1) }
p "end client"
end.join
puts "server connection should be stoped !!!"
sleep(1)
puts "\n"*5
sleep(1)
srv.stop rescue nil
sleep(3)
puts "Tread list : #{Thread.list} / current= #{Thread.current.inspect}"
end
if ARGV.size==0 || ARGV[0]=="2"
puts "**********************************************************"
puts "** Test serial protocole-like : header/body => ack/timeout"
puts "**********************************************************"
srv=MServer.service(2200,"0.0.0.0",22) do |socket|
socket.on_n_receive(11) do |data|
s,data=data[0,1],data[1..-1]
(socket.close; next) if s!="e"
size=data.to_i
puts " Server waiting for #{size} Bytes of data"
socket.received_n_timeout(size,100_000) do |data|
puts " Server recieved buffer : #{data.size} Bytes"
puts " emit ack..."
socket.send("o",0)
end
end
#socket.on_timer(120*1000) { puts " serv close after 40 seconds"; socket.close }
socket.wait_end
end
MClient.run_one_shot("localhost",2200) do |socket|
30.times { |j|
size=rand((j+1)*100..(j+1)*100000)
puts "Sending #{size} data..."
data='%'*size
socket.print "e%010d" % size
while data && data.size>0
s,data=data[0..(1024-1)],data[1024..-1]
socket.send s,0
end
p socket.received_n_timeout(1,[size/1000,1000].max) ? "ack ok" : "!!! timeout ack"
#puts "\n"*7
}
p "end client"
end.join
sleep 1
puts "\n"*3
sleep 3
puts "srv stop..."
srv.stop rescue nil
sleep 1
puts "Tread list : #{Thread.list} / current= #{Thread.current.inspect}"
end
if ARGV.size==0 || ARGV[0]=="3"
puts "**********************************************************"
puts "** Test tcp proxy : one echo server, one proxy, on client"
puts "**********************************************************"
srv1=MServer.service(2201,"0.0.0.0",22) do |socket|
socket.on_any_receive { |data| socket.puts "ECHO:#{data}" }
socket.on_timer(2000) { socket.puts "Hello say server @ #{Time.now}" rescue nil }
socket.on_timer(4000) { socket.puts "byebye" rescue nil; socket.close rescue nil }
socket.wait_end
end
srv2=MServer.service(2200,"0.0.0.0",22) do |s_cli|
puts "> ======== client Connected ========"
srv=MClient.run_one_shot("127.0.0.1",2201) do |s_srv|
puts "< ======== server Concected ========"
s_srv.on_any_receive { |data| puts "< "+data; s_cli.print data }
s_cli.on_any_receive { |data| puts "> "+data; s_srv.print data}
s_srv.wait_end
s_cli.close rescue nil
end
s_cli.wait_end
p "end cli, stop proxy"
srv.kill
end
sleep 1
MClient.run_one_shot("localhost",2200) do |socket|
socket.on_any_receive { |data| p "client recieved #{data}"}
p "connected in client"
10.times { |j| socket.print "C#{j} #{"+"*j*3}" ; sleep(0.1) }
p "end client"
end.join
sleep 1
puts "\n"*3
sleep 3
puts "srv stop..."
srv1.stop rescue nil
srv2.stop rescue nil
sleep 1
puts "Tread list : #{Thread.list} / current= #{Thread.current.inspect}"
end
if ARGV.size==0 || ARGV[0]=="4"
puts "**********************************************************"
puts "** Test sending with separator"
puts "**********************************************************"
$tm=Time.now
srv=MServer.service(2200,"0.0.0.0",22) do |socket|
l,ll=[],[]
socket.on_receive_sep(/([\.;$!])/) { |(data,sep)|
case sep
when ";" then l << data
when "."
ll << l ; l=[]
when "$"
p ll ; ll=[]
puts "Latency: #{(Time.now.to_f - $tm.to_f)*1000} ms"
socket.receive_n_bytes(10) { |data| p data }
when "!"
socket.close
end
}
socket.wait_end
end
MClient.run_one_shot("localhost",2200) do |socket|
p "connected in client"
$tm=Time.now
p 1; 4.times { |j| 3.times { |p| socket.print "#{j}/#{p};" } ; socket.print(".") }
socket.print '$1234567890'
$tm=Time.now
p 2; 4.times { |j| 3.times { |p| socket.print "#{j}/#{p};" } ; socket.print(".") }
socket.print '$1234567890!'
p "end client"
end.join
sleep 1
puts "\n"*3
sleep 3
puts "srv stop..."
srv.stop rescue p $!
sleep 1
puts "Tread list : #{Thread.list} / current= #{Thread.current.inspect}"
end
if ARGV.size==0 || ARGV[0]=="5"
SRV_PORT=2234
## ############################# Client UDP : send datagram to anybody, serv response from them
UDPAgent.on_timer(1000,
port: 2232,
on_timer: proc do
data=Time.now.to_i.to_s
puts "\n\n\non timer send <#{data}>"
{mess: data,host: "127.0.0.2",port: SRV_PORT}
end,
on_receive: proc { |data,from,sock|
puts "Client: received #{data} from #{from}"
UDPAgent.send_datagram_on_socket(sock,from.last,from[1],'ack')
}
)
## ############################# Server UDP : receive datagrram from anybody, response to sender
UDPAgent.on_datagramme("127.0.0.2",SRV_PORT ) { |data,from,p|
puts "Agent: received #{data} from #{from}:#{p}"
data && data.size>3 ? "OK-#{data}." : nil
}
sleep 1
UDPAgent.send_datagram("127.0.0.2",SRV_PORT,"Hello")
sleep 10
end
if ARGV.size==0 || ARGV[0]=="6"
th=MServerAgent.run(2222,"localhost",22) do |chan|
puts "sv: connect"
@lchan||={}
@lchan[chan]=chan
chan.on_message do |mess|
puts "sv: broadcast #{mess.inspect}..."
@lchan.keys.each {|chan1| chan1.send_message(mess) if chan1!=chan }
nil
end
chan.wait_end
@lchan.delete(chan)
end
lth=[]
5.times {
puts "####################### Run Client ########################"
lth << MClientAgent.run("localhost",2222) do |chan|
nb=0
chan.on_timer(100*rand(5..15)) {
chan.send_message(["C",1,2,1.2,Time.now.to_f,true])
nb+=1
(chan.close rescue nil)if nb>50
}
chan.on_message { |mess| p "cli: receive: #{mess.inspect}" ; nil}
chan.wait_end
puts "client closed"
end
sleep rand(1..2)
}
sleep 10
puts "\n\n############# Kill all agents...\n\n"
lth.each { |th| th.kill}
th.shutdown
sleep 2
end
puts "Test End !"