hprose 1.4.2 → 1.4.3

Sign up to get free protection for your applications and to get access to all the features.
data/examples/client.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'rubygems'
2
2
  require 'hprose'
3
3
 
4
- client = HproseHttpClient.new('http://localhost:3000/')
4
+ client = HproseClient.new('http://localhost:3000/')
5
5
  def error(name, e)
6
6
  puts name
7
7
  puts e
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'hprose'
3
+
4
+ client = HproseClient.new('tcp://127.0.0.1:4321/')
5
+ def error(name, e)
6
+ puts name
7
+ puts e
8
+ end
9
+ client.onerror = :error
10
+
11
+ puts(client.hello('World'))
data/lib/hprose/client.rb CHANGED
@@ -14,19 +14,37 @@
14
14
  # #
15
15
  # hprose client for ruby #
16
16
  # #
17
- # LastModified: Mar 8, 2014 #
17
+ # LastModified: Mar 10, 2014 #
18
18
  # Author: Ma Bingyao <andot@hprose.com> #
19
19
  # #
20
20
  ############################################################
21
21
 
22
22
  require "hprose/common"
23
23
  require "hprose/io"
24
+ require "uri"
24
25
 
25
26
  module Hprose
26
27
  class Client
27
- include Tags
28
- include ResultMode
28
+ class << self
29
+ alias :__new__ :new
30
+ def inherited(subclass)
31
+ class << subclass
32
+ alias :new :__new__
33
+ end
34
+ end
35
+ end
29
36
  public
37
+ def self.new(uri)
38
+ u = URI.parse(uri)
39
+ case u.scheme
40
+ when 'http', 'https' then
41
+ return HttpClient.new(uri)
42
+ when 'tcp', 'tcp4', 'tcp6' then
43
+ return TcpClient.new(uri)
44
+ else
45
+ raise Exception.exception("The " << u.scheme << " client isn't implemented.")
46
+ end
47
+ end
30
48
  def initialize(uri = nil)
31
49
  @onerror = nil
32
50
  @filter = Filter.new
@@ -82,6 +100,8 @@ module Hprose
82
100
  raise NotImplementedError.new("#{self.class.name}#send_and_receive is an abstract method")
83
101
  end
84
102
  private
103
+ include Tags
104
+ include ResultMode
85
105
  def do_output(methodname, args, byref, simple)
86
106
  stream = StringIO.new
87
107
  writer = Writer.new(stream, simple)
@@ -14,7 +14,7 @@
14
14
  # #
15
15
  # hprose http client for ruby #
16
16
  # #
17
- # LastModified: Mar 8, 2014 #
17
+ # LastModified: Mar 10, 2014 #
18
18
  # Author: Ma Bingyao <andot@hprose.com> #
19
19
  # #
20
20
  ############################################################
@@ -26,9 +26,6 @@ require "uri"
26
26
 
27
27
  module Hprose
28
28
  class HttpClient < Client
29
- include Tags
30
- @@cookie_manager = {}
31
- @@cookie_manager_mutex = Mutex.new
32
29
  public
33
30
  def initialize(uri = nil)
34
31
  super
@@ -87,6 +84,8 @@ module Hprose
87
84
  end
88
85
  end
89
86
  private
87
+ @@cookie_manager = {}
88
+ @@cookie_manager_mutex = Mutex.new
90
89
  def _set_cookie(cookielist, host)
91
90
  @@cookie_manager_mutex.synchronize do
92
91
  cookielist.each do |cookies|
@@ -0,0 +1,127 @@
1
+ ############################################################
2
+ # #
3
+ # hprose #
4
+ # #
5
+ # Official WebSite: http://www.hprose.com/ #
6
+ # http://www.hprose.net/ #
7
+ # http://www.hprose.org/ #
8
+ # #
9
+ ############################################################
10
+
11
+ ############################################################
12
+ # #
13
+ # hprose/tcpclient.rb #
14
+ # #
15
+ # hprose tcp client for ruby #
16
+ # #
17
+ # LastModified: Mar 10, 2014 #
18
+ # Author: Ma Bingyao <andot@hprose.com> #
19
+ # #
20
+ ############################################################
21
+
22
+ require "hprose/client"
23
+ require "socket"
24
+
25
+ module Hprose
26
+ class TcpClient < Client
27
+ private
28
+ module TcpConnStatus
29
+ Free = 0
30
+ Using = 1
31
+ Closing = 2
32
+ end
33
+ class TcpConnEntry
34
+ def initialize(uri)
35
+ @uri = uri
36
+ @status = TcpConnStatus::Using
37
+ @socket = nil
38
+ end
39
+ attr_accessor :uri, :status, :socket
40
+ end
41
+ class TcpConnPool
42
+ def initialize
43
+ @pool = []
44
+ @mutex = Mutex.new
45
+ end
46
+ def get(uri)
47
+ @mutex.synchronize do
48
+ @pool.each do |entry|
49
+ if entry.status == TcpConnStatus::Free then
50
+ if not entry.uri.nil? and entry.uri == uri then
51
+ entry.status = TcpConnStatus::Using
52
+ return entry
53
+ elsif entry.uri.nil? then
54
+ entry.status = TcpConnStatus::Using
55
+ entry.uri = uri
56
+ return entry
57
+ end
58
+ end
59
+ end
60
+ entry = TcpConnEntry.new(uri)
61
+ @pool << entry
62
+ return entry
63
+ end
64
+ end
65
+ def close(uri)
66
+ sockets = []
67
+ @mutex.synchronize do
68
+ @pool.each do |entry|
69
+ if not entry.uri.nil? and entry.uri == uri then
70
+ if entry.status == TcpConnStatus::Free then
71
+ sockets << entry.socket
72
+ entry.socket = nil
73
+ entry.uri = nil
74
+ else
75
+ entry.status = TcpConnStatus::Closing
76
+ end
77
+ end
78
+ end
79
+ end
80
+ freeSockets(sockets)
81
+ end
82
+ def free(entry)
83
+ if entry.status == TcpConnStatus::Closing then
84
+ if not entry.socket.nil? then
85
+ entry.socket.close
86
+ entry.socket = nil
87
+ end
88
+ entry.uri = nil
89
+ end
90
+ entry.status = TcpConnStatus::Free
91
+ end
92
+ private
93
+ def freeSockets(sockets)
94
+ Thread.start do
95
+ sockets.each do |socket|
96
+ socket.close
97
+ end
98
+ end
99
+ end
100
+ end
101
+ public
102
+ def initialize(uri = nil)
103
+ super
104
+ @pool = TcpConnPool.new
105
+ end
106
+ protected
107
+ def send_and_receive(data)
108
+ entry = @pool.get(@uri)
109
+ if entry.socket.nil? then
110
+ entry.socket = TCPSocket.new(@uri.host, @uri.port)
111
+ end
112
+ begin
113
+ n = data.size
114
+ entry.socket.send("" << (n >> 24 & 0xff) << (n >> 16 & 0xff) << (n >> 8 & 0xff) << (n & 0xff) << data, 0)
115
+ buf = entry.socket.recv(4, 0)
116
+ n = buf[0].ord << 24 | buf[1].ord << 16 | buf[2].ord << 8 | buf[3].ord
117
+ data = entry.socket.recv(n, 0)
118
+ rescue
119
+ entry.status = TcpConnStatus::Closing
120
+ raise
121
+ ensure
122
+ @pool.free(entry)
123
+ end
124
+ return data
125
+ end
126
+ end # class TcpClient
127
+ end # module Hprose
data/lib/hprose.rb CHANGED
@@ -14,7 +14,7 @@
14
14
  # #
15
15
  # hprose for ruby #
16
16
  # #
17
- # LastModified: Mar 8, 2014 #
17
+ # LastModified: Mar 10, 2014 #
18
18
  # Author: Ma Bingyao <andot@hprose.com> #
19
19
  # #
20
20
  ############################################################
@@ -31,6 +31,7 @@ module Hprose
31
31
  autoload :Formatter, 'hprose/io'
32
32
  autoload :Client, 'hprose/client'
33
33
  autoload :HttpClient, 'hprose/httpclient'
34
+ autoload :TcpClient, 'hprose/tcpclient'
34
35
  autoload :Service, 'hprose/service'
35
36
  autoload :HttpService, 'hprose/httpservice'
36
37
  end
@@ -46,5 +47,6 @@ Object.const_set(:HproseWriter, Hprose.const_get(:Writer))
46
47
  Object.const_set(:HproseFormatter, Hprose.const_get(:Formatter))
47
48
  Object.const_set(:HproseClient, Hprose.const_get(:Client))
48
49
  Object.const_set(:HproseHttpClient, Hprose.const_get(:HttpClient))
50
+ Object.const_set(:HproseTcpClient, Hprose.const_get(:TcpClient))
49
51
  Object.const_set(:HproseService, Hprose.const_get(:Service))
50
52
  Object.const_set(:HproseHttpService, Hprose.const_get(:HttpService))
data/lib/hproseclient.rb CHANGED
@@ -14,7 +14,7 @@
14
14
  # #
15
15
  # hprose client for ruby #
16
16
  # #
17
- # LastModified: May 19, 2010 #
17
+ # LastModified: Mar 10, 2014 #
18
18
  # Author: Ma Bingyao <andot@hprose.com> #
19
19
  # #
20
20
  ############################################################
@@ -22,7 +22,9 @@
22
22
  module Hprose
23
23
  autoload :Client, 'hprose/client'
24
24
  autoload :HttpClient, 'hprose/httpclient'
25
+ autoload :TcpClient, 'hprose/tcpclient'
25
26
  end
26
27
 
27
28
  Object.const_set(:HproseClient, Hprose.const_get(:Client))
28
- Object.const_set(:HproseHttpClient, Hprose.const_get(:HttpClient))
29
+ Object.const_set(:HproseHttpClient, Hprose.const_get(:HttpClient))
30
+ Object.const_set(:HproseTcpClient, Hprose.const_get(:TcpClient))
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hprose
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.2
4
+ version: 1.4.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-03-09 00:00:00.000000000 Z
12
+ date: 2014-03-10 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description:
15
15
  email: andot@hprose.com
@@ -19,12 +19,14 @@ extra_rdoc_files: []
19
19
  files:
20
20
  - examples/client.rb
21
21
  - examples/server.rb
22
+ - examples/tcpclient.rb
22
23
  - lib/hprose/client.rb
23
24
  - lib/hprose/common.rb
24
25
  - lib/hprose/httpclient.rb
25
26
  - lib/hprose/httpservice.rb
26
27
  - lib/hprose/io.rb
27
28
  - lib/hprose/service.rb
29
+ - lib/hprose/tcpclient.rb
28
30
  - lib/hprose.rb
29
31
  - lib/hproseclient.rb
30
32
  - lib/hprosecommon.rb