async2 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0f41a32b29f0a5a37f837420a952c121881d3457
4
+ data.tar.gz: 82fe5b7178594450e2570cd5baa6829dd025232d
5
+ SHA512:
6
+ metadata.gz: 369674691d2f00991cca3f1b41e2cd241e0106ef955404066d7b5e175773ea0059e120304dd8c1173a14f031bcca1135a07b377525cd1039c2431af396705377
7
+ data.tar.gz: f77a8131679885929399ed4dcb8d2956a9dd0eb5c9ffe9a5492f6d0872a4c415d0e441ee01c4cbe44db7ceaa60f241731ef926ec94f31065819cae98007adbe1
Binary file
Binary file
@@ -0,0 +1,16 @@
1
+ #encoding: utf-8
2
+
3
+ task :default => [:test]
4
+
5
+ task :test do
6
+ ruby "test/unit_test.rb"
7
+ end
8
+
9
+ namespace :test do
10
+
11
+ desc "Unitary tests."
12
+ task :test do
13
+ ruby "test/unit_test.rb"
14
+ end
15
+
16
+ end
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "async2/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "async2"
7
+ s.version = Async2::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Arthur Poulet"]
10
+ s.email = ["arthur.poulet@mailoo.org"]
11
+ s.homepage = "https://github.com/Nephos/async2"
12
+ s.summary = %q{Asynchronous concurrent stuff lib.}
13
+ s.description = %q{Asynchronous concurrent stuff lib. IO and HTTP layers.}
14
+
15
+ s.add_development_dependency "rspec", "~> 2.5"
16
+ s.add_development_dependency 'nomorebeer', '~> 1.1', '>= 1.1.1'
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = ["lib"]
22
+
23
+ s.license = 'WTFPL'
24
+
25
+ s.cert_chain = ['certs/nephos.pem']
26
+ s.signing_key = File.expand_path('~/.ssh/gem-private_key.pem') if $0 =~ /gem\z/
27
+
28
+ end
@@ -0,0 +1,22 @@
1
+ -----BEGIN CERTIFICATE-----
2
+ MIIDljCCAn6gAwIBAgIBATANBgkqhkiG9w0BAQUFADBIMRYwFAYDVQQDDA1hcnRo
3
+ dXIucG91bGV0MRkwFwYKCZImiZPyLGQBGRYJY3J5cHRvbGFiMRMwEQYKCZImiZPy
4
+ LGQBGRYDbmV0MB4XDTE1MTAwMzIxNTQyMFoXDTE2MTAwMjIxNTQyMFowSDEWMBQG
5
+ A1UEAwwNYXJ0aHVyLnBvdWxldDEZMBcGCgmSJomT8ixkARkWCWNyeXB0b2xhYjET
6
+ MBEGCgmSJomT8ixkARkWA25ldDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC
7
+ ggEBANnLB9vH7/O0aUWi0f9VtC7F73zMC1OvxDaJq+qIcmGBFiKPOngDCRyKffWf
8
+ A6gyf7VsJAlDVFYoyNw7bzLFjBT9gkb5xmED3Uaxvt8Ax131YvKv0sTsQeiTVHHW
9
+ 1crq7266hW1McrFU2A1uQRV8FHuruUJBuP7UUYK7fZi0barbkdAIb4VvQFZxXByI
10
+ lKdQSuhu8k0RcG1vTACEsQXJIUImc8CWMi8/TG3HidJdASMvIWtV2rNQXlQ+UgIc
11
+ UeHC0p+SNTrc09H6q+5eH8NRYhJ91xKuLXv7NNy8M3quAMHcQ1GM6DWljc+Jc4cm
12
+ uAPQMYJf7Vy6OzWLoEiv2QU6sE0CAwEAAaOBijCBhzAJBgNVHRMEAjAAMAsGA1Ud
13
+ DwQEAwIEsDAdBgNVHQ4EFgQU08cDEZOTZNjP/0+E2unJWyw4NzQwJgYDVR0RBB8w
14
+ HYEbYXJ0aHVyLnBvdWxldEBjcnlwdG9sYWIubmV0MCYGA1UdEgQfMB2BG2FydGh1
15
+ ci5wb3VsZXRAY3J5cHRvbGFiLm5ldDANBgkqhkiG9w0BAQUFAAOCAQEAo60BuRZM
16
+ 5Vtn0H0kHHTPiXz09lwxx4cFpTqrTTM0T6qM3gKsR5eLyR1Rt5hGmYLPgY4ZW4zI
17
+ XcuDbgRJrczZs7AZNy5NXyVWpxCwkZzklpbGTuYQdFz7mKfVyALjLjrilOIktXcz
18
+ PvB7EoLlqYvq9cnwV3WE1MkRcBd07xpofcp0kxae2dCOZPUq24TcNaRe7Utw6jjA
19
+ iL0bcH7zZkzMsNoNpPAgsd3ovU4Mf9M+A3M8COmd7KzKyZ0k7P12u5InPWUsNQS2
20
+ tcYkgfqUJPitIJx1RvWZpIyH5uJhRUYK3+vU9nMOxez5WbIlC1TtpByKAPMX+sht
21
+ gib3AoIT8jh/2w==
22
+ -----END CERTIFICATE-----
@@ -0,0 +1,56 @@
1
+ require "singleton"
2
+ require_relative "async2/http"
3
+
4
+ class Async2
5
+ include Singleton
6
+ attr_accessor :wait
7
+
8
+ def initialize wait = 0.01
9
+ @wait = wait
10
+ @read = {}
11
+ @write = {}
12
+ @t = Thread.new do
13
+ loop do
14
+ begin
15
+ read, write = IO.select @read.keys, @write.keys, nil, 0
16
+ read&.each { |io| @read[io].call(io); @read.delete io }
17
+ write&.each { |io| @write[io].call(io); @write.delete io }
18
+ sleep wait unless read || write
19
+ rescue => err
20
+ STDERR.puts err, err.backtrace
21
+ raise
22
+ retry
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ def read(io, &b)
29
+ @read[io] = b
30
+ end
31
+
32
+ def write(io, &b)
33
+ @write[io] = b
34
+ end
35
+ end
36
+
37
+ # if __FILE__ == $0
38
+ # puts "First test"
39
+ # f = File.open "/tmp/a", "r"
40
+ # Async2.instance.read(f) { |io| STDERR.puts "1 It worked:"; STDERR.puts io.gets }
41
+ # puts "sleep 1"
42
+ # sleep 1
43
+ # puts "Second test"
44
+ # require "socket"
45
+ # s = TCPServer.new "0.0.0.0", 9999
46
+ # Thread.new {
47
+ # sleep 0.5
48
+ # io = TCPSocket.new("127.0.0.1", 9999)
49
+ # sleep 0.5
50
+ # io.puts "mdr"
51
+ # }
52
+ # f = s.accept
53
+ # Async2.instance.read(f) { |io| STDERR.puts "2 It worked:"; STDERR.puts io.gets }
54
+ # puts "sleep 1"
55
+ # sleep 2
56
+ # end
@@ -0,0 +1,91 @@
1
+ require_relative "../async2"
2
+ require "socket"
3
+ require "net/http"
4
+ require "uri"
5
+
6
+ class Async2
7
+ module HTTP
8
+
9
+ BUFF_LEN = 4096
10
+ private def read_all(io)
11
+ buff = ""
12
+ while (l = io.read_nonblock BUFF_LEN)
13
+ buff += l
14
+ break if l.size < BUFF_LEN
15
+ end
16
+ buff
17
+ end
18
+
19
+ private def to_response(buff, io = nil)
20
+ split = buff.split("\r\n")
21
+ first = split.first
22
+ rep = Net::HTTPResponse.new first.split[0], Integer(first.split[1]), first.split[2]
23
+ rep.instance_variable_set "@socket", io
24
+ idx = split.index ""
25
+ if idx
26
+ headers = split[1 .. (idx - 1)]
27
+ body = split[idx .. -1].join if idx < split.size - 1
28
+ headers.each { |e| head = e.match(/([^:]+): (.+)/) ; rep[head[1]] = head[2] }
29
+ rep.body = body
30
+ return [rep, body]
31
+ end
32
+ return [rep, nil]
33
+ end
34
+
35
+ def get(uri, headers = {})
36
+ request(uri, "GET", headers) do |body, res, buff|
37
+ yield body, res, buff
38
+ end
39
+ end
40
+
41
+ def request(uri, verb = "GET", headers = {}, body = nil)
42
+ uri = URI.parse uri
43
+ uri.path = "/" if uri.path == ""
44
+ headers["User-Agent"] = "ruby/#{RUBY_VERSION}"
45
+ headers["Host"] = uri.hostname
46
+ headers["Accept"] = "*/*"
47
+ buffer = "#{verb} #{uri.path} HTTP/1.1\r\n" + headers.map { |k, v| "#{k}: #{v}" }.join("\r\n") + "\r\n\r\n"
48
+ buffer += "\r\n#{body}" if body
49
+ socket = TCPSocket.new uri.hostname, uri.port
50
+ p "client> #{buffer}"
51
+ socket.print buffer
52
+ Async2.instance.read(socket) do
53
+ buff = read_all(socket)
54
+ rep, body = to_response(buff, socket)
55
+ yield body, rep, buff
56
+ end
57
+ end
58
+ end
59
+
60
+ include HTTP
61
+ end
62
+
63
+ if __FILE__ == $0
64
+
65
+ # serv = TCPServer.new "0.0.0.0", 9999
66
+ # Thread.new {
67
+ # sleep 0.1
68
+ # begin
69
+ # Async2.instance.get("http://127.0.0.1:9999") { |body, rep, buff|
70
+ # puts "client< BODY: #{body.inspect}, BUFF: #{buff.inspect}, REP: #{rep.inspect}" }
71
+ # rescue => err
72
+ # puts err, err.backtrace
73
+ # end
74
+ # }
75
+ # cli = serv.accept
76
+ # Async2.instance.read(cli) {
77
+ # req = cli.read_nonblock 4096
78
+ # # p "server< #{req}"
79
+ # # p "server> HTTP/1.1 200 OK\r\n"
80
+ # cli.print "HTTP/1.1 200 OK\r\nTest: 1\r\n\r\nbodybodybody"
81
+ # }
82
+ # sleep 0.8
83
+
84
+ Async2.instance.get("http://linuxfr.org") { |body, rep, buff|
85
+ puts
86
+ p "body: #{body.inspect}"
87
+ p "rep: #{rep.inspect}"
88
+ p "buff: #{buff.inspect}"
89
+ }
90
+ sleep 1
91
+ end
@@ -0,0 +1,3 @@
1
+ class Async2
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: async2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Arthur Poulet
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain:
11
+ - |
12
+ -----BEGIN CERTIFICATE-----
13
+ MIIDljCCAn6gAwIBAgIBATANBgkqhkiG9w0BAQUFADBIMRYwFAYDVQQDDA1hcnRo
14
+ dXIucG91bGV0MRkwFwYKCZImiZPyLGQBGRYJY3J5cHRvbGFiMRMwEQYKCZImiZPy
15
+ LGQBGRYDbmV0MB4XDTE1MTAwMzIxNTQyMFoXDTE2MTAwMjIxNTQyMFowSDEWMBQG
16
+ A1UEAwwNYXJ0aHVyLnBv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1TtpByKAPMX+sht
32
+ gib3AoIT8jh/2w==
33
+ -----END CERTIFICATE-----
34
+ date: 2016-09-25 00:00:00.000000000 Z
35
+ dependencies:
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - "~>"
41
+ - !ruby/object:Gem::Version
42
+ version: '2.5'
43
+ type: :development
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '2.5'
50
+ - !ruby/object:Gem::Dependency
51
+ name: nomorebeer
52
+ requirement: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - "~>"
55
+ - !ruby/object:Gem::Version
56
+ version: '1.1'
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 1.1.1
60
+ type: :development
61
+ prerelease: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - "~>"
65
+ - !ruby/object:Gem::Version
66
+ version: '1.1'
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: 1.1.1
70
+ description: Asynchronous concurrent stuff lib. IO and HTTP layers.
71
+ email:
72
+ - arthur.poulet@mailoo.org
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - Rakefile
78
+ - async2.gemspec
79
+ - certs/nephos.pem
80
+ - lib/async2.rb
81
+ - lib/async2/http.rb
82
+ - lib/async2/version.rb
83
+ homepage: https://github.com/Nephos/async2
84
+ licenses:
85
+ - WTFPL
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 2.5.1
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: Asynchronous concurrent stuff lib.
107
+ test_files: []
108
+ has_rdoc:
@@ -0,0 +1,3 @@
1
+ yC��3O]�<����V�e�£�7��v��%��v‘k��v�m��cf�y�bZh��HI��Pb��?A�X)�YJ�9�r����!'�h�h�AY� |�-
2
+ *�<O�B���l9g$^�ǞK��� �urt�
3
+ W���W�nv����ɢ���3W@�*�� Ww�"]�u�_le.� H�|f��zISч�.Ȩh�R�ג��BDZ/�p�<WJ�1�)x ��#�²�(�]&��΂�}I�S��L��q�'u�}_C