iodine 0.7.9 → 0.7.10

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of iodine might be problematic. Click here for more details.

@@ -1,62 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- # this will compile Iodine and run an HTTP server "Hello World" example.
4
- Dir.chdir(File.expand_path(File.join('..', '..'), __FILE__))
5
- puts `rake clean`
6
- puts `rake compile`
7
-
8
- require 'benchmark'
9
- $LOAD_PATH.unshift File.expand_path(File.join('..', '..', 'lib'), __FILE__)
10
- require 'bundler/setup'
11
- require 'iodine'
12
- require 'rack'
13
-
14
- # create the server object and setup any settings we might need.
15
- Iodine::Rack
16
- Iodine.threads ||= 1
17
- Iodine.processes ||= 1 # 4
18
- Iodine::Rack.public = '~/Documents/Scratch'
19
- count = 2
20
- Iodine::Rack.app = proc { |_env| [200, { 'Content-Length'.freeze => '12'.freeze }, ['He11o World!'.freeze]] }
21
- puts Iodine::Rack.address
22
- Iodine.start
23
-
24
- # puts env.to_a.map { |pair| pair.join(': ') } .join("\n").to_s;
25
-
26
- # puts "Press enter to start (#{Process.pid})"
27
- # gets
28
-
29
- ###############
30
- ## for testing:
31
-
32
- # def nag
33
- # puts `ab -n 200000 -c 2000 -k http://127.0.0.1:3000/`
34
- # sleep 2
35
- # end
36
- #
37
- # nag while true
38
- #
39
- # def nag
40
- # puts `wrk -c2000 -d10 -t4 http://localhost:3000/`
41
- # sleep 3
42
- # end
43
- #
44
- # nag while true
45
-
46
- # ab -n 100000 -c 200 -k http://127.0.0.1:3000/
47
- # ab -n 100000 -c 4000 -k http://127.0.0.1:3000/
48
- # ab -n 1000000 -c 20000 -k http://127.0.0.1:3000/
49
- # ~/ruby/wrk/wrk -c400 -d10 -t12 http://localhost:3000/
50
- # wrk -c200 -d4 -t12 http://localhost:3000/
51
- # RACK_ENV="production" rackup -p 3000 -s iodine
52
-
53
- # thor --amount 5000 ws://localhost:3000/echo
54
- # thor --amount 5000 ws://localhost:3000/broadcast
55
-
56
- # ws = new WebSocket("ws://localhost:3000"); ws.onmessage = function(e) {console.log("Got message!"); console.log(e.data);}; ws.onclose = function(e) {console.log("closed")}; ws.onopen = function(e) {ws.send("hi");};
57
- # for(i = 0; i< 256; i++) {
58
- # ws = new WebSocket("ws://localhost:3000");
59
- # ws.onmessage = function(e) {console.log("Got message!"); console.log(e.data); e.target.close(); };
60
- # ws.onclose = function(e) {console.log("closed")};
61
- # ws.onopen = function(e) {e.target.send("hi");};
62
- # };
@@ -1,124 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- # this is arbitrary code that will be executed after Iodine compiles. Used for ad-hok testing.
4
-
5
- Dir.chdir(File.expand_path(File.join('..', '..'), __FILE__))
6
- puts `rake clean`
7
- puts `rake compile`
8
-
9
- require 'benchmark'
10
- $LOAD_PATH.unshift File.expand_path(File.join('..', '..', 'lib'), __FILE__)
11
- require 'bundler/setup'
12
- require 'iodine'
13
- require 'rack'
14
-
15
- class WSEcho
16
- def self.call(env)
17
- if env['HTTP_UPGRADE'.freeze] =~ /websocket/i
18
- env['iodine.websocket'.freeze] = WSEcho
19
- return [0, {}, []]
20
- end
21
- out = "ENV:\r\n#{env.to_a.map { |h| "#{h[0]}: #{h[1]}" } .join "\n"}"
22
- request = Rack::Request.new(env)
23
- out += "\nRequest Path: #{request.path_info}\nParams:\r\n#{request.params.to_a.map { |h| "#{h[0]}: #{h[1]}" } .join "\n"}" unless request.params.empty?
24
- [200, { 'Content-Length' => out.length }, [out]]
25
- end
26
-
27
- # def on_open
28
- # puts "We have a websocket connection"
29
- # end
30
- # def on_close
31
- # puts "Bye Bye... only #{ws_count} left..."
32
- # end
33
- def on_shutdown
34
- puts "I'm shutting down #{self}"
35
- end
36
-
37
- def on_message(data)
38
- # puts data
39
- write data
40
- data_cp = data.dup
41
- # puts "broadcasting #{data_cp.bytesize} bytes"
42
- # each {|h| h.echo data_cp }
43
- end
44
-
45
- def echo(data)
46
- write "echo: #{data}"
47
- end
48
- end
49
-
50
- # create the server object and setup any settings we might need.
51
- Iodine.threads ||= 4
52
- Iodine.processes ||= 1
53
- Iodine::Rack.public = '/Users/2Be/Documents/Scratch'
54
- count = 2
55
- Iodine::Rack.app = WSEcho
56
-
57
- # server.on_http= Proc.new do |env|
58
- # # [200, {"Content-Length".freeze => "12".freeze}, ["Hello World!".freeze]];
59
- # if env["HTTP_UPGRADE".freeze] =~ /websocket/i.freeze
60
- # env['iodine.websocket'.freeze] = WSEcho.new
61
- # [0,{}, []]
62
- # else
63
- # req = Rack::Request.new env
64
- # res = Rack::Response.new
65
- # res.write "Hello World!".freeze
66
- # res.to_a
67
- # end
68
- # end
69
-
70
- server.on_start do
71
- # server.run {puts "I'm running!"}
72
- # server.run_after(5000) {puts "5 seconds have passed."}
73
- server.run_every(1000) { puts "#{server.count} clients connected." }
74
- # server.run_every(10000) do
75
- # begin
76
- # puts "making a system call"
77
- # puts `ab -n 100000 -c 200 -k http://127.0.0.1:3000/`
78
- # rescue => e
79
- # l = Logger.new STDOUT
80
- # l.error e
81
- # end
82
- # end
83
- end
84
-
85
- # server.on_start do
86
- # server.run_every(1000) {puts "#{server.connection_count} clients connected."}
87
- # end
88
-
89
- puts "Press enter to start (#{Process.pid})"
90
- # gets
91
-
92
- server.start
93
-
94
- # def nag
95
- # puts `ab -n 200000 -c 2000 -k http://127.0.0.1:3000/`
96
- # sleep 2
97
- # end
98
- #
99
- # nag while true
100
- #
101
- # def nag
102
- # puts `wrk -c2000 -d10 -t4 http://localhost:3000/`
103
- # sleep 3
104
- # end
105
- #
106
- # nag while true
107
-
108
- # ab -n 100000 -c 200 -k http://127.0.0.1:3000/
109
- # ab -n 100000 -c 4000 -k http://127.0.0.1:3000/
110
- # ab -n 1000000 -c 20000 -k http://127.0.0.1:3000/
111
- # ~/ruby/wrk/wrk -c400 -d10 -t12 http://localhost:3000/
112
- # wrk -c200 -d4 -t12 http://localhost:3000/
113
- # RACK_ENV="production" rackup -p 3000 -s iodine
114
-
115
- # thor --amount 5000 ws://localhost:3000/echo
116
- # thor --amount 5000 ws://localhost:3000/broadcast
117
-
118
- # ws = new WebSocket("ws://localhost:3000"); ws.onmessage = function(e) {console.log("Got message!"); console.log(e.data);}; ws.onclose = function(e) {console.log("closed")}; ws.onopen = function(e) {ws.send("hi");};
119
- # for(i = 0; i< 256; i++) {
120
- # ws = new WebSocket("ws://localhost:3000");
121
- # ws.onmessage = function(e) {console.log("Got message!"); console.log(e.data); e.target.close(); };
122
- # ws.onclose = function(e) {console.log("closed")};
123
- # ws.onopen = function(e) {e.target.send("hi");};
124
- # };
@@ -1,62 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- # this is arbitrary code that will be executed after Iodine compiles. Used for ad-hok testing.
4
-
5
- Dir.chdir(File.expand_path(File.join('..', '..'), __FILE__))
6
- puts `rake clean`
7
- puts `rake compile`
8
-
9
- require 'benchmark'
10
- $LOAD_PATH.unshift File.expand_path(File.join('..', '..', 'lib'), __FILE__ )
11
- require "bundler/setup"
12
- require "iodine"
13
-
14
- # You can add fixtures and/or initialization code here to make experimenting
15
- # with your gem easier. You can also use a different console, if you like.
16
-
17
- # (If you use this, don't forget to add pry to your Gemfile!)
18
- # require "pry"
19
- # Pry.start
20
-
21
- class ShoooProtocol
22
- def on_message buffer
23
- write "what do you mean - #{buffer.strip} ?!\n"
24
- close if buffer =~ /^bye[\r\n]/i
25
- end
26
-
27
- end
28
- class EchoProtocol
29
- # `on_message` is an optional alternative to the `on_data` callback.
30
- # `on_message` has a 1Kb buffer that recycles itself for memory optimization.
31
- def on_message buffer
32
- # writing will never block and will use a buffer written in C when needed.
33
- write buffer
34
- puts buffer.dump
35
- # close will be performed only once all the data in the write buffer
36
- # was sent. use `force_close` to close early.
37
- close if buffer =~ /^bye[\r\n]/i
38
- # upgrade ShoooProtocol
39
- # # use buffer.dup to save the data from being recycled once we return.
40
- # data = buffer.dup
41
- # # run asynchronous tasks with ease
42
- # run do
43
- # sleep 1
44
- # puts "Echoed data: #{data}"
45
- # end
46
- end
47
- end
48
-
49
- # create the server object and setup any settings we might need.
50
- server = Iodine.new
51
- server.threads = 10
52
- server.processes = 1
53
- server.busy_msg = "To many connections, try again later."
54
- server.protocol = EchoProtocol
55
-
56
- b = server.on_start do
57
- server.run_after(1000) {puts "A seconds have passed?"}
58
- server.run_every(1000) {puts "#{server.connection_count} clients connected."}
59
- puts "Since we have 2 timers, we have #{server.connection_count} connections."
60
- end
61
-
62
- server.start
@@ -1,38 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- # this will compile Iodine and run a raw sockets server that emulates HTTP (without parsing any incoming requests).
4
-
5
- # # test using:
6
- # ab -n 100000 -c 200 -k http://127.0.0.1:3000/
7
-
8
- Dir.chdir(File.expand_path(File.join('..', '..'), __FILE__))
9
- puts `rake clean`
10
- puts `rake compile`
11
-
12
- require 'benchmark'
13
- $LOAD_PATH.unshift File.expand_path(File.join('..', '..', 'lib'), __FILE__)
14
- require 'bundler/setup'
15
- require 'iodine'
16
-
17
- module HttpProtocol
18
- def self.on_open client
19
- client.timeout = 10
20
- end
21
- # `on_message` is an optional alternative to the `on_data` callback.
22
- # `on_message` has a 1Kb buffer that recycles itself for memory optimization.
23
- def self.on_message client, _
24
- # writing will never block and will use a buffer written in C when needed.
25
- client.write "HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nKeep-Alive: timeout=1\r\nContent-Length: 12\r\n\r\nHello World!"
26
- end
27
- def self.call
28
- self
29
- end
30
- end
31
-
32
- puts "thread #{Iodine.threads}"
33
- # Listen
34
- Iodine.listen port: '3000', handler: HttpProtocol
35
- Iodine.threads = 4
36
- Iodine.workers = 1
37
- puts "now, thread #{Iodine.threads}"
38
- Iodine.start
@@ -1,63 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- # this will compile Iodine and run a raw sockets server that emulates HTTP (without parsing any incoming requests).
4
-
5
- # # test using:
6
- # ab -n 100000 -c 200 -k http://127.0.0.1:3000/
7
- # wrk -c200 -d4 -t12 http://localhost:3000/
8
-
9
- Dir.chdir(File.expand_path(File.join('..', '..'), __FILE__))
10
- # puts `rake clean`
11
- # puts `rake compile`
12
-
13
- require 'benchmark'
14
- # $LOAD_PATH.unshift File.expand_path(File.join('..', '..', 'lib'), __FILE__)
15
- # require 'bundler/setup'
16
-
17
- require 'eventmachine'
18
-
19
- module HttpProtocol
20
- def receive_data data
21
- send_data "HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nKeep-Alive: timeout=1\r\nContent-Length: 12\r\n\r\nHello World!"
22
- end
23
- end
24
-
25
- puts "Running EM emulating HTTP."
26
-
27
- if EventMachine.kqueue?
28
- EventMachine.kqueue = true
29
- puts "using Kqueue."
30
- end
31
- if EventMachine.epoll?
32
- EventMachine.epoll = true
33
- puts "using epoll."
34
- end
35
-
36
- # Note that this will block current thread.
37
- EventMachine.run {
38
- EventMachine.start_server "127.0.0.1", 3030, HttpProtocol
39
- }
40
-
41
- #
42
- # require 'iodine'
43
- #
44
- # class HttpProtocol
45
- # @timeout = 5
46
- # # `on_message` is an optional alternative to the `on_data` callback.
47
- # # `on_message` has a 1Kb buffer that recycles itself for memory optimization.
48
- # def on_data
49
- # # writing will never block and will use a buffer written in C when needed.
50
- # data = nil
51
- # while (data = read)
52
- # write "HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nKeep-Alive: timeout=1\r\nContent-Length: 12\r\n\r\nHello World!"
53
- # end
54
- # end
55
- # end
56
- #
57
- # puts "thread #{Iodine.threads}"
58
- # # Listen
59
- # Iodine.listen '3000', HttpProtocol
60
- # Iodine.threads = 7
61
- # Iodine.processes = 1
62
- # puts "now, thread #{Iodine.threads}"
63
- # Iodine.start
@@ -1,64 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- # this will compile Iodine and run a raw sockets broadcasting (chatroom) server.
4
-
5
- # # test using:
6
- # telnet localhost 3000
7
-
8
- Dir.chdir(File.expand_path(File.join('..', '..'), __FILE__))
9
- puts `rake clean`
10
- puts `rake compile`
11
-
12
- require 'benchmark'
13
- $LOAD_PATH.unshift File.expand_path(File.join('..', '..', 'lib'), __FILE__)
14
- require 'bundler/setup'
15
- require 'iodine'
16
-
17
- # You can add fixtures and/or initialization code here to make experimenting
18
- # with your gem easier. You can also use a different console, if you like.
19
-
20
- # (If you use this, don't forget to add pry to your Gemfile!)
21
- # require "pry"
22
- # Pry.start
23
-
24
- # test the upgrade protocol using the ShoooProtocol
25
- class ShoooProtocol
26
- @timeout = 10
27
- def ping
28
- write "Wha, ya' still here?! Go away!\n"
29
- close
30
- end
31
-
32
- def on_message(buffer)
33
- write "what do you mean - #{buffer.strip} ?!\nShoo, away with you!\n"
34
- close
35
- end
36
- end
37
-
38
- # the broadcasting protocol
39
- class EchoProtocol
40
- def self.on_start
41
- Iodine.run_every(1000) { puts "#{Iodine.count} clients connected." }
42
- puts "We have 1 timer, and #{Iodine.count} connections."
43
- end
44
-
45
- def on_open
46
- subscribe(channel: :chat) {|ch, msg| write msg }
47
- subscribe channel: :quite
48
- end
49
-
50
- # `on_message` is an optional alternative to the `on_data` callback.
51
- # `on_message` has a 1Kb buffer that recycles itself for memory optimization.
52
- def on_message(buffer)
53
- # writing will never block and will use a buffer written in C when needed.
54
- publish channel: :chat, message: "Someone said: #{buffer}"
55
- switch_protocol ShoooProtocol if buffer =~ /^shoo[\r\n]/i
56
- write "You speak!\n" if unsubscribe(subscribed?(channel: :quite))
57
- end
58
- end
59
-
60
- # create the server object and setup any settings we might need.
61
- Iodine.listen 3000, EchoProtocol
62
- Iodine.threads = 10
63
- Iodine.processes = 1
64
- Iodine.start
@@ -1,44 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- Dir.chdir(File.expand_path(File.join('..', '..'), __FILE__))
4
- puts `rake clean`
5
- puts `rake compile`
6
-
7
- require 'benchmark'
8
- require 'faye/websocket'
9
- # require 'thin'
10
- # Faye::WebSocket.load_adapter('thin')
11
-
12
- $LOAD_PATH.unshift File.expand_path(File.join('..', '..', 'lib'), __FILE__)
13
- require 'bundler/setup'
14
- require 'iodine'
15
-
16
- APP = lambda do |env|
17
- if Faye::WebSocket.websocket?(env)
18
- ws = Faye::WebSocket.new(env)
19
-
20
- ws.on :message do |event|
21
- ws.send(event.data.to_s)
22
- end
23
-
24
- ws.on :close do |event|
25
- p [:close, event.code, event.reason]
26
- ws = nil
27
- end
28
-
29
- # Return async Rack response
30
- response = ws.rack_response
31
- p response
32
- response
33
- else
34
- # Normal HTTP request
35
- [200, { 'Content-Type' => 'text/plain' }, ["Hello\n"]]
36
- end
37
- end
38
-
39
- Iodine.listen2http app: APP
40
-
41
- Iodine.threads = 4
42
- Iodine.start
43
-
44
- # ws = new WebSocket("ws://localhost:3000"); ws.onmessage = function(e) {console.log("Got message!"); console.log(e.data);}; ws.onclose = function(e) {console.log("closed")}; ws.onopen = function(e) {ws.send("hi");};