minitcp 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/samples/plot.rb ADDED
@@ -0,0 +1,190 @@
1
+ # LGPL
2
+ ###############################################################
3
+ # plot.rb plot data(s) of stdin to Gui display
4
+ # Usage:
5
+ # > vmstat 1 | ruby plot.rb -2 0-value 100%-value cpu -- 10 0 5000 io auto
6
+ # ^input-column ^label ^in-column .. ^auto-scale
7
+ #
8
+ ###############################################################
9
+
10
+ require 'Ruiby'
11
+ $bgcolor=::Gdk::Color.parse("#023")
12
+ $fgcolor=[
13
+ ::Gdk::Color.parse("#FFAA00"),
14
+ ::Gdk::Color.parse("#99DDFF"),
15
+ ::Gdk::Color.parse("#00FF00"),
16
+ ::Gdk::Color.parse("#0000FF"),
17
+ ::Gdk::Color.parse("#FFFF00"),
18
+ ::Gdk::Color.parse("#00FFFF"),
19
+ ::Gdk::Color.parse("#FF00FF"),
20
+ ::Gdk::Color.parse("#999"),
21
+ ]
22
+ class Measure
23
+ class << self
24
+ def create(argv)
25
+ noc=argv.shift.to_i
26
+ y0=(argv.shift||"0.0").to_f
27
+ y1=(argv.shift||"100.0").to_f
28
+ label=argv.shift||"?"
29
+ autoscale=argv.size>0
30
+ @lcurve||=[]
31
+ @lcurve << Measure.new(noc,y0,y1,label,autoscale)
32
+ @lcurve.size-1
33
+ end
34
+ def add(noc,y0,y1,label,autoscale)
35
+ @lcurve << Measure.new(noc,y0,y1,label,autoscale)
36
+ @lcurve.size-1
37
+ end
38
+ def scan_line(line)
39
+ nums=line.scan(/[\d+.]+/)
40
+ @lcurve.each { |m| m.register_value(nums) }
41
+ end
42
+ def add_value(index,value)
43
+ @lcurve[index].register_value(value)
44
+ end
45
+ def draw_measures(ctx)
46
+ @lcurve.each_with_index { |m,index| m.plot_curve(index,ctx) }
47
+ @lcurve.each_with_index { |m,index| m.plot_label(index,ctx) }
48
+ end
49
+ end
50
+ def initialize(noc,min,max,label,auto_scale)
51
+ @noc=noc
52
+ @div,@offset=calc_coef(min,0.0,max,1.0)
53
+ @name=label
54
+ @value= 0
55
+ @curve=[]
56
+ @label=@name
57
+ @autoscale=auto_scale
58
+ end
59
+ def register_value(data)
60
+ if data.is_a? Array
61
+ svalue=data[@noc]
62
+ return if !svalue || svalue !~ /[\d.]+/
63
+ @value=svalue.to_f
64
+ else
65
+ @value=data
66
+ end
67
+ @label = "%s %5.2f" % [@name,@value]
68
+ v= @value * @div + @offset
69
+ py=[0.0,(H-HHEAD)*1.0,(H-HHEAD)*(1.0-v)].sort[1]+HHEAD
70
+ @curve << [W+PAS,py,v,@value]
71
+ @curve.select! {|pt| pt[0]-=PAS; pt[0]>=0}
72
+ p [i,@value,v,py] if $DEBUG
73
+ auto_scale if @autoscale && @curve.size>5
74
+ end
75
+ def auto_scale()
76
+ min,max=@curve.minmax_by {|pt| pt[2]}
77
+ if min!=max && (min[2]<-0.01 || max[2]>1.01)
78
+ p "correction1 #{@name} #{min} // #{max}"
79
+ @div,@offset=calc_coef(min[3],0.0,max[3],1.0)
80
+ @curve.each {|a| a[2]=a[3]*@div+@offset ; a[1] = (H-HHEAD)*(1-a[2])}
81
+ elsif (d=(max[2]-min[2]))< 0.1 && (@curve.size-1) >= W/PAS && d>0.0001
82
+ p "correction2 #{@name} #{min} // #{max}"
83
+ @div,@offset=calc_coef(min[3],min[2]-3*d,max[3],max[2]+3*d)
84
+ @curve.each {|a| a[2]=a[3]*@div+@offset ; a[1] = (H-HHEAD)*(1.0-a[2])}
85
+ end
86
+ end
87
+ def calc_coef(x0,y0,x1,y1)
88
+ y0=[0.0,1.0,y0].sort[1]
89
+ y1=[0.0,1.0,y1].sort[1]
90
+ a=1.0*(y0-y1)/(x0-x1)
91
+ b= (y0+y1-(x0+x1)*a)/2
92
+ [a,b]
93
+ end
94
+ def plot_curve(index,ctx)
95
+ return if @curve.size<2
96
+ a,*l=@curve
97
+ style(ctx,3,$fgcolor.last) ; draw(ctx,a,l)
98
+ style(ctx,1,$fgcolor[index]) ; draw(ctx,a,l)
99
+ end
100
+ def style(ctx,width,color)
101
+ ctx.set_line_width(width)
102
+ ctx.set_source_rgba(color.red/65000.0,color.green/65000.0,color.blue/65000.0, 1.0)
103
+ end
104
+ def draw(ctx,h,t)
105
+ ctx.move_to(h.first,h[1])
106
+ t.each {|x,y,*q| ctx.line_to(x,y) }
107
+ ctx.stroke
108
+ end
109
+ def plot_label(index,ctx)
110
+ style(ctx,3,$fgcolor[index])
111
+ ctx.move_to(5+60*index,HHEAD-5)
112
+ ctx.show_text(@label)
113
+ end
114
+ end
115
+
116
+
117
+ def run(app)
118
+ $str=$stdin.gets
119
+ if $str
120
+ p $str if $DEBUG
121
+ Measure.scan_line($str)
122
+ gui_invoke { @cv.redraw }
123
+ else
124
+ exit!(0)
125
+ end
126
+ end
127
+
128
+ def run_window()
129
+ Ruiby.app width: W, height: H, title: "Curve" do
130
+ stack do
131
+ @cv=canvas(W,H) do
132
+ on_canvas_draw { |w,ctx| expose(w,ctx) }
133
+ end
134
+ popup(@cv) do
135
+ pp_item(" Plot ") { }
136
+ pp_separator
137
+ pp_item("htop") { system("lxterminal", "-e", "htop") }
138
+ pp_item("Gnome Monitor") { Process.spawn("gnome-system-monitor") }
139
+ pp_item("Terminal") { system("lxterminal") }
140
+ pp_separator
141
+ pp_item("Exit") { ask("Exit ?") && exit(0) }
142
+ end
143
+ end
144
+ chrome(false)
145
+ move($posxy[0],$posxy[1])
146
+ @ow,@oh=size
147
+ def expose(cv,ctx)
148
+ ctx.set_source_rgba($bgcolor.red/65000.0, $bgcolor.green/65000.0, $bgcolor.blue/65000.0, 1)
149
+ ctx.rectangle(0,0,W,H)
150
+ ctx.fill()
151
+ ctx.set_source_rgba($bgcolor.red/65000.0, $bgcolor.green/65000.0, 05+$bgcolor.blue/65000.0, 0.3)
152
+ ctx.rectangle(0,0,W,HHEAD)
153
+ ctx.fill()
154
+ Measure.draw_measures(ctx)
155
+ (puts "source modified!!!";exit!(0)) if File.mtime(__FILE__)!=$mtime
156
+ end
157
+ $mtime=File.mtime(__FILE__)
158
+
159
+ Thread.new(self) { |app| loop { run(app) } }
160
+ end
161
+ end
162
+
163
+ ############################### Main #################################
164
+
165
+ if $0==__FILE__
166
+ trap("TERM") { exit!(0) }
167
+
168
+ PAS=2
169
+ HHEAD=20
170
+ $posxy=[0,0]
171
+
172
+ if ARGV.size>=2 && ARGV[0]=="--pos"
173
+ _,posxy=ARGV.shift,ARGV.shift
174
+ $posxy=posxy.split(/[x,:]/).map(&:to_i)
175
+ end
176
+ if ARGV.size>=2 && ARGV[0]=="--dim"
177
+ _,geom=ARGV.shift,ARGV.shift
178
+ W,H=geom.split(/[x,:]/).map(&:to_i)
179
+ else
180
+ W,H=200,100
181
+ end
182
+
183
+ while ARGV.size>0
184
+ argv=[]
185
+ argv << ARGV.shift while ARGV.size>0 && ARGV.first!="--"
186
+ Measure.create(argv)
187
+ ARGV.shift if ARGV.size>0 && ARGV.first=="--"
188
+ end
189
+ run_window
190
+ end
data/samples/proxy.rb ADDED
@@ -0,0 +1,46 @@
1
+ #!/usr/bin/ruby
2
+ # LGPL
3
+ ####################################################
4
+ # proxy.rb : pure tcp proxy, for dev/admin/fun :)
5
+ #
6
+ # Usage :
7
+ # > ruby proxy.rb target-hostname target-port [mode]
8
+ # mode= ascii/bin/none : style of printing
9
+ ####################################################
10
+ require_relative '../lib/minitcp.rb'
11
+
12
+ $host,$port,$opt=ARGV[0]||"localhost",ARGV[1]||80,ARGV[2]||"ascii"
13
+ puts "Server on port 2200, proxy to #{$host}:#{$port}..."
14
+
15
+
16
+ MServer.service(2200,"0.0.0.0",22) do |s_cli|
17
+ puts "> ======== client Connected ========"
18
+ srv=MClient.run_one_shot($host,$port) do |s_srv|
19
+ puts "< ======== server Concected ========"
20
+ s_srv.on_any_receive { |data| spy 1,data; s_cli.print data }
21
+ s_cli.on_any_receive { |data| spy 2,data; s_srv.print data}
22
+ s_srv.wait_end
23
+ s_cli.close rescue nil
24
+ end
25
+ s_cli.wait_end
26
+ p "end cli, stop proxy"
27
+ srv.kill
28
+ end
29
+
30
+ def spy(sens,data)
31
+ return if $opt=="none"
32
+ prefix=(sens==1 ? "< " : "> ")
33
+ if $opt=="ascii"
34
+ data.each_line {|line| puts "#{prefix}#{line.chomp}" }
35
+ else
36
+ data.chars.each_slice(16) do |aline|
37
+ a=(aline.map { |char| "%02X " % char.ord }).join.ljust(16*3)+prefix+
38
+ aline.map { |char| (char.ord>30 ? char : "~") }.join()
39
+ puts "#{prefix}#{a}"
40
+ end
41
+ end
42
+ end
43
+
44
+ sleep
45
+
46
+
@@ -0,0 +1,41 @@
1
+ relaiproxy.rb
2
+ ==============
3
+
4
+ An extranet server want to send command (http/soap) to a intranet http-server.
5
+ this intranet server (equipments/soap) are many and vairables.
6
+
7
+ So realiproxy.rb define 2 process :
8
+ * intranet side: **relai** : maintain pool of sockets open with the proxy (initiated by a http request)
9
+ * extranet-side: **relaiproxy** : receive soap request and send them to relai via pool sockets
10
+
11
+
12
+ Schema
13
+ ======
14
+
15
+ ```
16
+ h[XX]=[ip,80,/]
17
+ request--------http://proxy_hostname:prox-port/XX-------------------------------->>>> http://ip:80/
18
+ <--------------------------------------------------------------------------response
19
+
20
+ ================= firewal =========
21
+ Server ---------------> | proxy-relai | <------//--------| relai | ------------>>>> hosts
22
+ ================= =========
23
+ proxy-hostname proxy-ip ( config h[borne]= [ip,port,path] )
24
+ proxy-port proxy-port
25
+ \___________________________________________/ \_______________/ .............
26
+ internet server server-in-intranet intranet hosts
27
+ ```
28
+
29
+
30
+ Usage
31
+ =====
32
+ Extranet:
33
+ > ruby relaiproxy.rb proxyhttp proxy-hostname proxy-port
34
+
35
+ Intranet:
36
+ > vi relai_config.rb
37
+ > ruby relaiproxy.rb relai proxy-ip proxy-port plugin-name
38
+
39
+ (actualy ocpp is the plugin)
40
+
41
+
@@ -0,0 +1,21 @@
1
+ require 'minitcp'
2
+ require 'open-uri'
3
+
4
+ Thread.abort_on_exception=true
5
+
6
+ lfile=Dir.glob("*.*").reject {|f| File.extname(f)==".pdf" || f=~/\s/}.select {|f| !File.directory?(f) && f.size<20000}[0..3000]
7
+ p lfile
8
+
9
+ (ARGV[0]||"1").to_i.times do
10
+ Thread.new { loop {
11
+ fn=lfile[rand(lfile.size)-1]
12
+ s=Time.now
13
+ timeout(10) {
14
+ data=(open("http://localhost:2200/ruby/local/#{fn}?ChargeBoxId=%3ETOTO%3C").read rescue nil)
15
+ data||=""
16
+ puts "Received size=%-5d/%-5d/miss %10d for %-30s duree=%d ms" % [data.size,File.size(fn),File.size(fn)-data.size,fn,((Time.now.to_f-s.to_f)*1000).to_i]
17
+ #sleep 0.4
18
+ } rescue p $!
19
+ }}
20
+ end
21
+ sleep
@@ -0,0 +1,4 @@
1
+ {
2
+ "TOTO" => ["localhost",7708,""],
3
+ "TITI" => ["localhost",80,"/"]
4
+ }
@@ -0,0 +1,180 @@
1
+ #!/usr/bin/ruby
2
+ # LGPL
3
+ #################################################################################
4
+ # relaiproxy.rb : An extranet server want to send command (http/soap)
5
+ # to a intranet client_socket clients are many, variables,
6
+ # so NAT is not a solution.
7
+ #
8
+ # relai.rb : in intranet, maintain the socket open with the proxy (initiated by a http request)
9
+ # relaiproxy : receive soap request and send them to relai via open socket
10
+ # so
11
+ # a pool of sockets beetwen intranet/extranet is maintain, client is inside intranet,
12
+ # serveur is outside.
13
+ # server can send http request to intranet host via proxy-relai
14
+ # 'plugin' in relai can determine host adresse with the request contant (header SOAP...)
15
+ #
16
+ #################################################################################
17
+ =begin
18
+ h[XX]=[ip,80,/]
19
+ request--------http://proxy_hostname:prox-port/XX-------------------------------->>>> http://ip:80/
20
+ <--------------------------------------------------------------------------response
21
+ ================= firewal =========
22
+ Server ---------------> | proxy-relai | <------//--------| relai | ------------>>>> hosts
23
+ ================= =========
24
+ proxy-hostname proxy-ip ( config h[borne]= [ip,port,path] )
25
+ proxy-port proxy-port
26
+ \___________________________________________/ \_______________/ .............
27
+ internet server server-in-intranet intranet hosts
28
+ =end
29
+
30
+ #require 'minitcp'
31
+ require_relative 'minitcp/lib/minitcp.rb'
32
+
33
+ if ARGV.size<1
34
+ puts"Usage>"
35
+ puts" #{$0} type hostname port"
36
+ puts" #{$0} proxyhttp proxy-hostname proxy-port"
37
+ puts" #{$0} relai proxy-saia-ip proxy-saia-port plugin"
38
+ exit(1)
39
+ end
40
+
41
+ $opt,$host,$port=ARGV[0]||"proxyhttp",ARGV[1]||"localhost",ARGV[2]||80
42
+
43
+ NBDIGITHEAD=8
44
+ QUEUE_SIZE=100
45
+
46
+ def sformat(socket,mess)
47
+ socket.send("%-#{NBDIGITHEAD}d%s" % [mess.size,mess],0)
48
+ end
49
+ def receive(socket)
50
+ resp_length= socket.receive_n_bytes(NBDIGITHEAD)
51
+ len =resp_length.strip.to_i
52
+ #log "header: #{resp_length} => #{len}"
53
+ (len>0) ? socket.receive_n_bytes(len) : ""
54
+ end
55
+ def log(t) puts "%10s | %s" % [Time.now.to_s,t] end
56
+
57
+ if $opt=="proxyhttp"
58
+ ##################################################################################
59
+ ## P R O X Y
60
+ ##################################################################################
61
+
62
+ $client_sockets ={}
63
+ $queue=Queue.new
64
+ ############# Proxy serveur http saia >>> machine distante #######################
65
+
66
+ MServer.service($port.to_i,"0.0.0.0",22) do |s_cli|
67
+
68
+ begin relai=$queue.pop end until $client_sockets[relai]
69
+ begin
70
+ header=s_cli.receive_sep("\r\n\r\n")
71
+ log("request...#{header.inspect}")
72
+ if header.match(/^Content-length: (\d+)/i) || "0"=~/(.)/
73
+ length=$1.strip.to_i
74
+ log("length body==#{length}")
75
+ body= length>0 ? s_cli.receive_n_bytes(length) : ""
76
+ mess="#{header}\r\n\r\n#{body}"
77
+ log("transmit request...")
78
+ sformat(relai,mess)
79
+ log("wait response...")
80
+ response= (timeout(5) { receive(relai) } rescue nil)
81
+ log(response!=nil ? "response ok" : "timeout") unless response
82
+ s_cli.write(response ? response : "HTTP/1.0 500 NOK\r\n\r\n")
83
+ else
84
+ s_cli.write( "HTTP/1.0 501 NOK\r\n\r\n")
85
+ end
86
+ ensure
87
+ $queue.push(relai)
88
+ end
89
+ end
90
+
91
+ ############# serveur http machine distante #######################
92
+
93
+ MServer.service($port.to_i+1,"0.0.0.0",22) do |s_cli|
94
+ puts "> ======== relai is Connected ========"
95
+ $client_sockets[s_cli]=true
96
+ $queue.push s_cli
97
+ s_cli.wait_end
98
+ puts "> ======== relai deConnected "
99
+ $client_sockets.delete(s_cli)
100
+ end
101
+
102
+ else
103
+
104
+ ##################################################################################
105
+ ## R E L A Y
106
+ ##################################################################################
107
+ CONF="relai_config.data"
108
+ plugin=ARGV.last||"ocpp"
109
+ p plugin
110
+ $config=nil
111
+ $configtime=nil
112
+
113
+ def load_config()
114
+ return if $configtime!=nil && $configtime.to_f == File.mtime(CONF).to_f
115
+ begin
116
+ log("load config...")
117
+ File.open(CONF) { |f| $config=eval( f.read.strip ) }
118
+ log("load config ok, nb-host=#{$config.size} ...")
119
+ $configtime=File.mtime(CONF)
120
+ rescue
121
+ log("Error loading configration : #{$!}")
122
+ end
123
+ end
124
+
125
+ def transform_ocpp(request)
126
+ load_config
127
+ pos=0
128
+ puts "recieved request #{request}"
129
+ (log("request does not contain ChargeBoxId");return nil) unless pos=(request =~/ChargeBoxId/i)
130
+ (log("request does not contain ChargeBoxId");return nil) unless request[pos,pos+50] =~ /%3E(.*?)%3C/m
131
+ id=$1.strip
132
+ ip=$config[id]
133
+ (log("unknown CS #{id} in config"); return nil) unless ip
134
+ request.sub!(/\?[^\s]*/,"")
135
+ request.sub!(/GET [^\s]*/,"GET #{ip.last}") if ip.last!=""
136
+ [*ip,request]
137
+ end
138
+
139
+ def transmit(ip,port=nil,path=nil,request=nil)
140
+ return "HTTP/1.0 404 NOK\r\n\r\n" unless request
141
+ log("send data <#{request.inspect[0..70]}...\n > to #{ip}:#{port}#{path} len=#{request.size}")
142
+ response=nil
143
+ MClient.run_one_shot(ip,port) { |socket|
144
+ socket.send(request,0)
145
+ header=socket.receive_sep("\r\n\r\n")
146
+ puts header
147
+ response=header+"\r\n\r\n"+if header=~/Content-length: (\d+)/i && $1 && $1.to_i>0
148
+ puts "with content-length #{$1}"
149
+ socket.received_n_timeout($1.to_i,10) rescue "ERROR"
150
+ else
151
+ puts "until close"
152
+ rep=""
153
+ rep+=(a=socket.receive_any(1000_000)) until a==nil
154
+ rep
155
+ end
156
+ }.join
157
+ response
158
+ end
159
+
160
+ QUEUE_SIZE.times do
161
+ MClient.run_continious($host,$port.to_i+1,1000) do |socket|
162
+ nbr=0
163
+ socket.on_n_receive(NBDIGITHEAD) do |header|
164
+ p header
165
+ nbr+=1
166
+ len=header.strip.to_i
167
+ #p len
168
+ request=socket.receive_n_bytes(len)
169
+ #p request
170
+ response= transmit(*send("transform_#{plugin}",request))
171
+ puts "replay with len=#{(response||"").size} <<<\n#{(response.inspect||"")[0..40]}..#{(response.inspect||"")[-40..-1]}\n>>"
172
+ sformat( socket, response ? response : "NOK #{Time.now}")
173
+ (socket.close rescue nil) if nbr>100
174
+ end
175
+ socket.wait_end
176
+ end
177
+ end
178
+
179
+ end
180
+ sleep
data/samples/relay.rb ADDED
@@ -0,0 +1,102 @@
1
+ # LGPL, Author: Regis d'Aubarede <regis.aubarede@gmail.com>
2
+ #################################################################
3
+ # relay.rb : estabish connexion between 2 agents
4
+ # One agent open a (http like) connection as 'server', with his name
5
+ # A 'client' ask a connection to a 'server', with the name of server
6
+ # if sever connection is alive
7
+ # server receive an 'CONNECTED' message,with the name of client
8
+ # socket server is IO.copy with client socket
9
+ # server should establish a new 'server' connection for other client
10
+ #
11
+ #
12
+ #################################################################
13
+ require_relative '../lib/minitcp.rb'
14
+
15
+ BasicSocket.do_not_reverse_lookup = true
16
+ Thread.abort_on_exception = true
17
+
18
+ $SIZE=30
19
+ $dico_name={}
20
+ $dico_ip={}
21
+
22
+ def run_net_relay()
23
+ MServer.service(4410,"0.0.0.0",1000) do |socket|
24
+ socket.on_n_receive($SIZE) do |data|
25
+ ip=socket.remote_address.ip_address
26
+ cmd,url,http=data.split(' ')
27
+ name,method,*args=url.split('/').last
28
+ next if cmd!="GET"
29
+ case method
30
+ when "server"
31
+ old=ActiveConnection.search(name)
32
+ if old && old.state==:free
33
+ old.deconnect
34
+ old=nil
35
+ end
36
+ if !old || old.state==:connected
37
+ ActiveConnection.add(socket)
38
+ end
39
+ when "client"
40
+ if srv=ActiveConnection.search(args.first)
41
+ srv.connect_to(name)
42
+ else
43
+ smess("NOTCONNECT")
44
+ end
45
+ end
46
+ rep=""
47
+ end
48
+ end
49
+ end
50
+
51
+ def smess(socket,*args)
52
+ data= args.join(";")+";"
53
+ socket.send(data+" "*($SIZE-data.length),0) rescue p $!
54
+ end
55
+
56
+ def run_server_name()
57
+ MServer.service(4400,"0.0.0.0",100) do |socket|
58
+ socket.on_n_receive($SIZE) do |data|
59
+ ip=socket.remote_address.ip_address
60
+ cmd,*args=data.split(";")
61
+ name=args.first
62
+ rep=""
63
+ case cmd
64
+ when "update"
65
+ $dico_name[name]=ip
66
+ $dico_ip[ip]=name
67
+ rep="OK"
68
+ when "forget"
69
+ oldip=$dico_ip[ip]
70
+ if oldip && $dico_ip[ip]==name && $dico_name[name]==ip
71
+ $dico_name.delete(name)
72
+ $dico_ip.delete(ip)
73
+ rep="OK"
74
+ end
75
+ when "get"
76
+ rep=$dico_name[name].to_s
77
+ when "geti"
78
+ rep=$dico_ip[name].to_s
79
+ end
80
+ rep.size>0 ? smess(socket,"ACK",name,rep) : smess(socket,"NACK","NOK","NOK")
81
+ end
82
+ socket.after(10*1000) { socket.close rescue nil}
83
+ socket.wait_end
84
+ end
85
+ end
86
+
87
+ ################# Client api ########################
88
+
89
+ if $0==__FILE__
90
+ run_server_name
91
+
92
+ Thread.new { 4.times { update_server_name("glurp") ; sleep 1 } }
93
+ Thread.new { 4.times { update_server_name("glurp22") ; sleep 1 } }
94
+
95
+ sleep 2
96
+ puts "\n\n***************** get name *******************\n\n"
97
+
98
+ p fetch_name("glurp")
99
+ p fetch_ip("127.0.0.1")
100
+ forget_me("glurp22")
101
+ sleep
102
+ end
data/samples/spdy.rb ADDED
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/ruby
2
+ # LGPL
3
+ ####################################################
4
+ # spdy.rb : pure spdy relay : recieve SPDY cnnexion,
5
+ # send http request to a http server,
6
+ # > ruby spdy.rb target-hostname target-port
7
+ ####################################################
8
+ require_relative '../lib/minitcp.rb'
9
+
10
+ $host,$port,$opt=ARGV[0]||"localhost",ARGV[1]||80
11
+ puts "Server SPDY:// on port 2200, proxy to #{$host}:#{$port}..."
12
+
13
+ class SPDY
14
+ HEADER_SIZE=8 # 8 bytes, [2 version,2 type,1 flags,3 length(24)],data[length]
15
+ class << self
16
+ def control_frame_decode(data)
17
+ cbit=(data[0].ord&0x80)!=0
18
+ if cbit && data.size==8
19
+ version=(data[0].ord&0x7F)*256+data[1].ord
20
+ type= data[2].ord*256+data[3].ord
21
+ iflag= data[4].ord
22
+ length=(data[5].ord*256+data[6].ord)*256+data[7].ord
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+
29
+ MServer.service(2200,"0.0.0.0",22) do |s_cli|
30
+ puts "> ======== client Connected ========"
31
+ on_n_receive(SPDY::HEADER_SIZE) do |header|
32
+ version,type,flags,length=SPDY.control_frame_decode(header)
33
+ data= length>0 ? receive_n_bytes(length) : ""
34
+ srv=MClient.run_one_shot($host,$port) do |s_srv|
35
+ end
36
+ s_cli.wait_end
37
+ p "end cli, stop proxy"
38
+ srv.kill
39
+ end
40
+
41
+ sleep
42
+
43
+
@@ -0,0 +1,37 @@
1
+ # LGPL
2
+ # ruby b.rb 0.2 3 | ruby plot.rb --pos 300,0 0 -0.01 1 cpu
3
+
4
+ if File.exists?("c:/windows")
5
+ require 'ruby-wmi'
6
+
7
+ puts "Not implemented for Windows !!!"
8
+ exit(0)
9
+ else
10
+
11
+ def get_nbcpu()
12
+ stat = Hash[*(IO.read("/proc/stat").split("\n").map {|line| line.chomp.split(/\s+/,2)}.flatten)]
13
+ stat.keys.grep(/^cpu(\d+)/).size
14
+ end
15
+ def get_stat()
16
+ stat = Hash[*(IO.read("/proc/stat").split("\n").map {|line| line.chomp.split(/\s+/,2)}.flatten)]
17
+ p stat if $DEBUG
18
+ stat=stat["cpu"].split(/\s+/).map(&:to_i)
19
+ end
20
+
21
+
22
+ $stdout.sync=true
23
+ periode= ARGV.shift.to_f
24
+ lno=ARGV.map(&:to_i)
25
+ p periode
26
+ p lno
27
+ nbcpu=get_nbcpu()
28
+ p nbcpu
29
+ ref=get_stat()
30
+ loop {
31
+ ref=get_stat().tap {|t|
32
+ x=t.zip(ref).map {|a,b| ((a-b)*10)/(nbcpu*periode*1000)}
33
+ puts lno.map {|i| (100*x[i]).to_i}.join(" ")
34
+ }
35
+ sleep periode
36
+ }
37
+ end