hara 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,7 +3,3 @@ rvm:
3
3
  - 2.0.0
4
4
  - 1.9.3
5
5
  - jruby-19mode
6
-
7
- matrix:
8
- allow_failures:
9
- - rvm: jruby-19mode
data/README.md CHANGED
@@ -9,11 +9,7 @@ Hara's async ability from [em-websocket](https://github.com/igrigorik/em-websock
9
9
 
10
10
  Hara's concurrent ability from [celluloid](https://github.com/celluloid/celluloid).
11
11
 
12
- Yes, hara just a combination of them, eventmachine and celluloid work well together :)
13
-
14
- ## Notice!!
15
-
16
- Current API is unstable, you should lock the version.
12
+ Yes, hara is a combination of them, eventmachine and celluloid work well together :)
17
13
 
18
14
  ## Installation
19
15
 
@@ -29,14 +25,16 @@ Or install it yourself as:
29
25
 
30
26
  $ gem install hara
31
27
 
32
- ## Usage
28
+ ## BasicUsage
33
29
 
34
30
  *server*
35
31
  ```ruby
36
32
  #test.rb
37
33
  require 'hara'
38
34
 
39
- class Echo < Hara::App
35
+ class Echo
36
+ include Hara::App
37
+
40
38
  define_action :echo do |str|
41
39
  socket.send str
42
40
  end
@@ -59,9 +57,43 @@ ws.send(msg)
59
57
 
60
58
  `ruby test.rb -h` to view options
61
59
 
62
- *Other Usage*
60
+ ## Other Usages & How it work
61
+
62
+ ```ruby
63
+ require 'hara/base'
64
+
65
+ class Echo
66
+ #include Hara::App, make you Echo class become Celluloid::Actor and per actor handle a socket
67
+ include Hara::App
68
+
69
+ #Hara::App provide some callbacks
70
+
71
+ def after_connect
72
+ puts 'first called'
73
+ p headers
74
+ end
75
+
76
+ def before_action action, *args
77
+ puts 'called when action comming'
78
+ end
79
+
80
+ define_action :echo do |str|
81
+ puts "#{client_ip} #{client_port}"
82
+ socket.send str
83
+ end
84
+
85
+ def after_action action, *args
86
+ puts 'called when action complete'
87
+ end
88
+ end
89
+
90
+ #if you require 'hara/base' you need start server you self, like below
91
+ Hara::Server.start 'localhost', '3000'
92
+ ```
93
+
94
+ ## RoadMap
63
95
 
64
- checkout tests
96
+ js client is processing
65
97
 
66
98
  ## Contributing
67
99
 
@@ -1,5 +1,6 @@
1
1
  require "hara/version"
2
2
  require 'celluloid'
3
+ require 'socket'
3
4
  require 'json'
4
5
 
5
6
  module Hara
@@ -12,6 +13,7 @@ module Hara
12
13
  @_env = env
13
14
  end
14
15
 
16
+ #decode message, return action and args
15
17
  def decode_msg msg
16
18
  msg = JSON.parse(msg)
17
19
  msg.values_at 'action', 'args'
@@ -22,21 +24,22 @@ module Hara
22
24
  end
23
25
  end
24
26
 
25
- class App
26
- include Celluloid
27
- include Celluloid::Logger
28
-
29
- attr_reader :socket, :handshake
30
-
31
- finalizer :app_finalizer
27
+ module App
28
+ attr_reader :socket, :handshake, :client_ip, :client_port
32
29
 
33
30
  Actions = {}
34
31
 
35
32
  class << self
36
- def inherited klass
33
+ def included klass
34
+ klass.send :include, Celluloid
35
+ klass.send :include, Celluloid::Logger
36
+ klass.send :finalizer, :app_finalizer
37
+ klass.send :extend, ClassMethods
37
38
  ::Hara.const_set :Application, klass
38
39
  end
40
+ end
39
41
 
42
+ module ClassMethods
40
43
  def define_action action, &block
41
44
  action = action.to_s
42
45
  warn "Action #{action} duplication defined" if Actions.has_key? action
@@ -47,6 +50,8 @@ module Hara
47
50
  end
48
51
  end
49
52
 
53
+ ##callback methods
54
+
50
55
  def after_connect
51
56
  end
52
57
 
@@ -56,39 +61,44 @@ module Hara
56
61
  def after_action action, args
57
62
  end
58
63
 
64
+ def on_close
65
+ end
66
+
67
+ ##################
68
+
69
+ #like method_missing
59
70
  def action_missing action, args
60
- info "#{headers['host']} request action: #{action} args: #{args.inspect}, action not defined"
71
+ info "#{client_ip} request action: #{action} args: #{args.inspect}, action not defined"
61
72
  raise NoMethodError, "undefined action '#{action}' for #{self}:#{self.class}"
62
73
  end
63
74
 
75
+ #get client headers
64
76
  def headers
65
77
  handshake.headers_downcased
66
78
  end
67
79
 
68
- def on_close
69
- end
70
-
71
80
  #below are internal functions(should not been overriding)
72
81
 
73
82
  def initialize handshake, socket
74
83
  @handshake = handshake
75
84
  @socket = socket
85
+ @client_port, @client_ip = Socket.unpack_sockaddr_in(socket.get_peername) #to get ip address of user
76
86
  async.hara_setup
77
87
  end
78
88
 
79
89
  def hara_setup
80
- info "#{headers['host']} coming"
90
+ info "#{client_ip} coming"
81
91
  after_connect
82
92
  end
83
93
 
84
94
  def process_msg message
85
95
  action, args = Hara.decode_msg(message)
86
- info "#{headers['host']} request action: #{action} args: #{args.inspect}"
96
+ info "#{client_ip} request action: #{action} args: #{args.inspect}"
87
97
  before_action action, *args
88
98
  call_action action, *args
89
99
  after_action action, *args
90
100
  rescue StandardError => e
91
- info "#{headers['host']} processing error:\n#{e.inspect}"
101
+ info "#{client_ip} processing error:\n#{e.inspect}"
92
102
  terminate
93
103
  end
94
104
 
@@ -1,3 +1,3 @@
1
1
  module Hara
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -2,7 +2,9 @@ require 'spec_helper'
2
2
 
3
3
  describe Hara::App do
4
4
  before :all do
5
- Class.new Hara::App do
5
+ Class.new do
6
+ include Hara::App
7
+
6
8
  def after_connect
7
9
  @states = []
8
10
  end
@@ -45,6 +47,12 @@ describe Hara::App do
45
47
  end
46
48
  end
47
49
 
50
+ it 'client_infos' do
51
+ @app.client_ip.should == '127.0.0.1'
52
+ @app.client_port.should.is_a? Integer
53
+ @app.headers.should.is_a? Hash
54
+ end
55
+
48
56
  it 'remote call actions' do
49
57
  @app.process_msg(Hara.encode_msg(:hello, ' world'))
50
58
  sleep 0.1 until msg = @socket.client_read
@@ -2,7 +2,9 @@ require 'spec_helper'
2
2
 
3
3
  describe Hara do
4
4
  it 'Hara::Application should equal with App' do
5
- App = Class.new Hara::App
5
+ App = Class.new do
6
+ include Hara::App
7
+ end
6
8
  App.should == Hara::Application
7
9
  end
8
10
 
@@ -14,6 +14,12 @@ class FayeSocket
14
14
  def initialize
15
15
  @client_messages = []
16
16
  @server_messages = []
17
+ @mri_peername = "\x02\x00\x00P\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00"
18
+ @jruby_peername = "\x00\x02\x8Av\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00"
19
+ end
20
+
21
+ def get_peername
22
+ defined?(JRuby) ? @jruby_peername : @mri_peername
17
23
  end
18
24
 
19
25
  def alive?
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hara
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: