cheezmiz 0.0.1 → 0.0.2

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/Rakefile CHANGED
@@ -10,6 +10,7 @@ begin
10
10
  gem.email = "parasquid@gmail.com"
11
11
  gem.homepage = "http://github.com/parasquid/cheezmiz"
12
12
  gem.authors = ["parasquid"]
13
+ gem.add_dependency "actionpool", ">= 0.2.3"
13
14
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
15
  end
15
16
  Jeweler::GemcutterTasks.new
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{cheezmiz}
8
- s.version = "0.0.1"
8
+ s.version = "0.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["parasquid"]
12
- s.date = %q{2010-02-19}
12
+ s.date = %q{2010-02-20}
13
13
  s.description = %q{a ruby library for Chikka}
14
14
  s.email = %q{parasquid@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -60,9 +60,12 @@ Gem::Specification.new do |s|
60
60
  s.specification_version = 3
61
61
 
62
62
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
63
+ s.add_runtime_dependency(%q<actionpool>, [">= 0.2.3"])
63
64
  else
65
+ s.add_dependency(%q<actionpool>, [">= 0.2.3"])
64
66
  end
65
67
  else
68
+ s.add_dependency(%q<actionpool>, [">= 0.2.3"])
66
69
  end
67
70
  end
68
71
 
@@ -24,8 +24,7 @@ if __FILE__ == $0
24
24
  CHIKKA_PASSWORD = 'password'
25
25
  TEST_MESSAGE = 'a test message'
26
26
 
27
- socket = TCPSocket.open('ctp-a.chikka.com', 6301)
28
- broker = Cheezmiz::Broker.new :socket => socket
27
+ broker = Cheezmiz::Broker.new
29
28
  broker.register_callback(:connection_established) do |msg|
30
29
  puts 'connection has been established'
31
30
  message = Cheezmiz::LoginRequest.new(
@@ -53,14 +52,14 @@ if __FILE__ == $0
53
52
  broker.send(Cheezmiz::ClientReady.new)
54
53
  end
55
54
 
56
- broker.register_callback(:keep_alive_request) do |msg|
55
+ broker.register_callback(:keep_alive_request, :threaded => false) do |msg|
57
56
  broker.send(Cheezmiz::KeepAliveResponse.new, :response_to => msg)
58
57
  puts 'sending test message'
59
58
  message = Cheezmiz::SubmitRequest.new(
60
59
  :message => "#{TEST_MESSAGE}",
61
60
  :buddy_number => 0
62
61
  )
63
- broker.send(message)
62
+ #broker.send(message)
64
63
  end
65
64
 
66
65
  broker.register_callback(:submit_response) do
@@ -71,14 +70,16 @@ if __FILE__ == $0
71
70
  if msg.operation == :unknown_operation
72
71
  puts "unknown operation: #{msg.prototype}"
73
72
  else
74
- puts "incoming message #{msg.operation} #{msg.data_fields.inspect}"
73
+ puts "incoming message: #{msg.operation} #{msg.data_fields.inspect}"
75
74
  end
76
75
  end
77
76
 
78
77
  broker.register_callback(:outgoing) do |msg|
79
78
  puts "outgoing message: #{msg.operation} #{msg.data_fields.inspect}"
80
79
  end
81
-
80
+
81
+ socket = TCPSocket.open('ctp-a.chikka.com', 6301)
82
+ broker.connect :socket => socket
82
83
  broker.start
83
- broker.join
84
+ sleep
84
85
  end
@@ -17,6 +17,7 @@
17
17
  # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
18
 
19
19
  $: << File.expand_path(File.dirname(__FILE__))
20
+ require 'actionpool'
20
21
  require 'socket'
21
22
  require 'helper'
22
23
  require 'protocol'
@@ -25,32 +26,25 @@ module Cheezmiz
25
26
  class Broker
26
27
  STX = "\x02"
27
28
  ETX = "\x03"
29
+ MIN_THREADS = 5
30
+ MAX_THREADS = 15
28
31
  def initialize(params = {})
29
- @socket = params[:socket] || begin
30
- params[:host] ||= 'ctp-a.chikka.com'
31
- params[:port] ||= 6301
32
- TCPSocket.open(params[:host], params[:port])
33
- end
34
32
  @sequence_number = 1
35
33
  @callbacks = Hash.new { |hash, key| hash[key] = [] }
34
+ @pool = ActionPool::Pool.new(
35
+ :min_threads => (params[:min_threads] || MIN_THREADS),
36
+ :max_threads => (params[:max_threads] || MAX_THREADS)
37
+ )
36
38
  end
37
39
 
38
- def register_callback(operation, &proc)
39
- @callbacks[operation] << proc
40
+ def register_callback(operation, options = {:threaded => true}, &proc)
41
+ @callbacks[operation] << options.merge({ :callback => proc })
40
42
  end
41
43
 
42
- def register_observer(observer, &proc)
43
- @observers[observer] << proc
44
- end
45
-
46
44
  def callbacks
47
45
  @callbacks
48
46
  end
49
47
 
50
- def observers
51
- @observers
52
- end
53
-
54
48
  def send(message, params = {})
55
49
  raw = encode({ :message => message }.merge(params))
56
50
  result = @socket.send(raw, 0)
@@ -58,15 +52,19 @@ module Cheezmiz
58
52
  return raw, result
59
53
  end
60
54
 
55
+ def connect(params = {})
56
+ @socket = params[:socket] || begin
57
+ params[:host] ||= 'ctp-a.chikka.com'
58
+ params[:port] ||= 6301
59
+ TCPSocket.open(params[:host], params[:port])
60
+ end
61
+ end
62
+
61
63
  def start
62
- @thread = Thread.new do
64
+ @pool.process do
63
65
  @socket.while_reading { |buffer| process_messages(buffer) }
64
66
  end
65
67
  end
66
-
67
- def join
68
- @thread.join
69
- end
70
68
 
71
69
  private
72
70
 
@@ -87,8 +85,8 @@ module Cheezmiz
87
85
  def encode(options)
88
86
  raw = "#{STX}" + options[:message].prototype
89
87
  sequence_number = begin
90
- options[:response_to].sequence_number
91
- rescue NoMethodError
88
+ options[:response_to].respond_to?(:sequence_number) ?
89
+ options[:response_to].sequence_number :
92
90
  options[:response_to]
93
91
  end || @sequence_number
94
92
  raw.sub! /NNN/, sequence_number.to_s.rjust(3, '0')
@@ -106,14 +104,14 @@ module Cheezmiz
106
104
  end
107
105
 
108
106
  def process_callbacks(callbacks, message)
109
- if callbacks.respond_to? :each_pair
110
- callbacks[message.operation].each do |callback|
111
- callback.call(message)
112
- end
113
- else
114
- callbacks.each do |callback|
115
- callback.call(message)
116
- end
107
+ (begin
108
+ callbacks.respond_to?(:each_pair) ?
109
+ callbacks[message.operation] :
110
+ callbacks
111
+ end).each do |callback|
112
+ callback[:threaded] ?
113
+ @pool.process { callback[:callback].call(message) } :
114
+ callback[:callback].call(message)
117
115
  end
118
116
  end
119
117
  end
@@ -69,9 +69,7 @@ module Cheezmiz
69
69
 
70
70
  def data
71
71
  data = ''
72
- @options.each do |key, value|
73
- data << "#{key}:#{value}\t"
74
- end
72
+ @options.each { |key, value| data << "#{key}:#{value}\t" }
75
73
  data
76
74
  end
77
75
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cheezmiz
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - parasquid
@@ -9,10 +9,19 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-19 00:00:00 +08:00
12
+ date: 2010-02-20 00:00:00 +08:00
13
13
  default_executable:
14
- dependencies: []
15
-
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: actionpool
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.2.3
24
+ version:
16
25
  description: a ruby library for Chikka
17
26
  email: parasquid@gmail.com
18
27
  executables: []