rakie 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 95d75f7ea14d2462c94b21bd025f17e5150075e295dd1dc9f927efb08826c424
4
- data.tar.gz: 2642f719d6ae1427e99efc89b3d841fe7c7471f5cfd2da0597ad4df89d347b0e
3
+ metadata.gz: 8361c3cfc878d6edc052170cc5fbc5f555d173562c7325d9a939c567402e8b4f
4
+ data.tar.gz: cc2f7fa419c2fcb750ec70305b942336974cd7e35ad336edc38436027f74e6dd
5
5
  SHA512:
6
- metadata.gz: ae10ff4d934dac0f08a066d2809930132b736e4cb4bda7f7cf25f30cc48f7554b4b474e8ee7cbb7fcd57b7b4e75157d9a592ff9a8351f3d9fbee431b4006e944
7
- data.tar.gz: 287525054ef601031d860c7a8b75b1985e91d7430cee7b489b7fb726c06cbe15223c3ebd63cad3fbdf590793d3cb343dbfc0a6890cba89a9cbf6b632541ab6a2
6
+ metadata.gz: 1b39af452c4d98899dd787a38ce1394b1781eaee28ab3761b46b824ec4b426f76c27d451da8ae1fb01545676dd572a19f9c9dae09839221a17a5ee807a151f72
7
+ data.tar.gz: c7953919abc5572b8fba09a5afdf5e32034c0bc2e4fdbca4f649f67f9ec635a10cf49888a194ce67f17afda0424becd1e42467cd9b73e2a990670fd29e2c2c50
data/lib/rakie.rb CHANGED
@@ -6,5 +6,7 @@ end
6
6
  require "rakie/channel"
7
7
  require "rakie/event"
8
8
  require "rakie/simple_server"
9
+ require "rakie/http_server"
9
10
  require "rakie/tcp_channel"
10
11
  require "rakie/tcp_server_channel"
12
+ require "rakie/log"
data/lib/rakie/channel.rb CHANGED
@@ -6,6 +6,7 @@ module Rakie
6
6
  @io = io
7
7
  @read_buffer = String.new
8
8
  @write_buffer = String.new
9
+ @write_task = []
9
10
  @delegate = delegate
10
11
  Event.push(io, self, Event::READ_EVENT)
11
12
  end
@@ -17,20 +18,19 @@ module Rakie
17
18
  end
18
19
 
19
20
  rescue IO::EAGAINWaitReadable
20
- puts("Channel read finished")
21
+ Log.debug("Channel read finished")
21
22
 
22
- rescue
23
+ rescue Exception => e
23
24
  # Process the last message on exception
24
25
  if @delegate != nil
25
26
  @delegate.on_recv(self, @read_buffer)
26
27
  @read_buffer = String.new # Reset buffer
27
28
  end
28
29
 
29
- puts("Channel error #{io}")
30
+ Log.debug("Channel error #{io}: #{e}")
30
31
  return Event::HANDLE_FAILED
31
32
  end
32
33
 
33
- puts("Channel has delegate?: #{@delegate}")
34
34
  if @delegate != nil
35
35
  len = @delegate.on_recv(self, @read_buffer)
36
36
 
@@ -39,30 +39,54 @@ module Rakie
39
39
  end
40
40
 
41
41
  @read_buffer = @read_buffer[len .. -1]
42
- puts("Channel handle on_recv")
42
+ Log.debug("Channel handle on_recv")
43
43
  end
44
44
 
45
45
  return Event::HANDLE_CONTINUED
46
46
  end
47
47
 
48
+ def handle_write(len)
49
+ task = @write_task[0]
50
+
51
+ while len > 0
52
+ if len < task
53
+ @write_task[0] = task - len
54
+ end
55
+
56
+ len -= task
57
+ @write_task.shift
58
+
59
+ if @delegate != nil
60
+ @delegate.on_send(self)
61
+ Log.debug("Channel handle on_send")
62
+ end
63
+ end
64
+ end
65
+
48
66
  def on_write(io)
67
+ len = 0
68
+
49
69
  begin
50
70
  while @write_buffer.length > 0
51
71
  len = io.write_nonblock(@write_buffer)
52
72
  @write_buffer = @write_buffer[len .. -1]
53
73
  end
54
74
 
55
- puts("Channel write finished")
75
+ Log.debug("Channel write finished")
56
76
 
57
77
  rescue IO::EAGAINWaitWritable
58
- puts("Channel write continue")
78
+ self.handle_write(len)
79
+
80
+ Log.debug("Channel write continue")
59
81
  return Event::HANDLE_CONTINUED
60
82
 
61
83
  rescue
62
- puts("Channel close #{io}")
84
+ Log.debug("Channel close #{io}")
63
85
  return Event::HANDLE_FAILED
64
86
  end
65
87
 
88
+ self.handle_write(len)
89
+
66
90
  return Event::HANDLE_FINISHED
67
91
  end
68
92
 
@@ -71,8 +95,10 @@ module Rakie
71
95
  io.close
72
96
 
73
97
  rescue
74
- puts("Channel is already closed")
98
+ Log.debug("Channel is already closed")
75
99
  end
100
+
101
+ Log.debug("Channel close ok")
76
102
  end
77
103
 
78
104
  def read(size)
@@ -96,7 +122,9 @@ module Rakie
96
122
  end
97
123
 
98
124
  @write_buffer << data
99
- Event.modify(@io, self, Event::READ_EVENT | Event::WRITE_EVENT)
125
+ @write_task << data.length
126
+
127
+ Event.modify(@io, self, (Event::READ_EVENT | Event::WRITE_EVENT))
100
128
 
101
129
  return 0
102
130
  end
data/lib/rakie/event.rb CHANGED
@@ -9,6 +9,10 @@ module Rakie
9
9
  HANDLE_CONTINUED = 0
10
10
  HANDLE_FINISHED = 1
11
11
 
12
+ OPERATION_ADD = 0
13
+ OPERATION_MODIFY = 1
14
+ OPERATION_DELETE = 2
15
+
12
16
  def initialize
13
17
  @wait_ios = []
14
18
  @lock = Mutex.new
@@ -24,36 +28,39 @@ module Rakie
24
28
 
25
29
  def process_signal(io)
26
30
  signal = io.read(1)
27
- puts("Event handling #{signal}")
28
31
 
29
- if signal == 'a'
30
- new_io, new_handler, new_event = @wait_ios.shift
32
+ if signal == 'q'
33
+ return 1
34
+ end
35
+
36
+ operation, new_io, new_handler, new_event = @wait_ios.shift
37
+ if new_io.closed?
38
+ return 0
39
+ end
40
+
41
+ Log.debug("Event handling #{signal} with #{new_io.fileno} to #{new_event}")
42
+
43
+ if operation == OPERATION_ADD
31
44
  @ios[new_io] = new_event
32
45
  @handlers[new_io] = new_handler
33
- puts("Event add all #{new_io} to #{new_event}")
46
+ Log.debug("Event add all #{new_io.fileno} to #{new_event}")
34
47
 
35
- elsif signal == 'd'
36
- new_io, = @wait_ios.shift
48
+ elsif operation == OPERATION_DELETE
37
49
  handler = @handlers[new_io]
38
50
 
39
51
  if handler != nil
52
+ Log.debug("Event close #{new_io}")
40
53
  handler.on_close(new_io)
41
- puts("Event close #{new_io}")
42
54
  end
43
55
 
44
56
  @ios.delete(new_io)
45
57
  @handlers.delete(new_io)
58
+ Log.debug("Event remove all #{new_io}")
46
59
 
47
- puts("Event remove all #{new_io}")
48
-
49
- elsif signal == 'm'
50
- new_io, new_handler, new_event = @wait_ios.shift
60
+ elsif operation == OPERATION_MODIFY
51
61
  @ios[new_io] = new_event
52
62
  @handlers[new_io] = new_handler
53
- puts("Event modify all #{new_io} to #{new_event}")
54
-
55
- elsif signal == 'q'
56
- return 1
63
+ Log.debug("Event modify all #{new_io.fileno} to #{new_event}")
57
64
  end
58
65
 
59
66
  return 0
@@ -61,9 +68,18 @@ module Rakie
61
68
 
62
69
  def run_loop
63
70
  loop do
71
+ # p @ios
72
+ # begin
73
+ # read_ios = @ios.select {|k, v| v & READ_EVENT > 0}
74
+ # write_ios = @ios.select {|k, v| v & WRITE_EVENT > 0}
75
+ # rescue Exception => e
76
+ # p @ios
77
+ # end
78
+
64
79
  read_ios = @ios.select {|k, v| v & READ_EVENT > 0}
65
80
  write_ios = @ios.select {|k, v| v & WRITE_EVENT > 0}
66
81
 
82
+ # Log.debug("Event selecting ...")
67
83
  read_ready, write_ready = IO.select(read_ios.keys, write_ios.keys, [], 5)
68
84
 
69
85
  if read_ready != nil
@@ -90,15 +106,15 @@ module Rakie
90
106
 
91
107
  if result == HANDLE_FINISHED
92
108
  @ios[io] = @ios[io] & ~READ_EVENT
93
- puts("Event remove read #{io}")
109
+ Log.debug("Event remove read #{io}")
94
110
 
95
111
  elsif result == HANDLE_FAILED
96
112
  handler.on_close(io)
97
- puts("Event close #{io}")
113
+ Log.debug("Event close #{io}")
98
114
 
99
115
  @ios.delete(io)
100
116
  @handlers.delete(io)
101
- puts("Event remove all #{io}")
117
+ Log.debug("Event remove all #{io}")
102
118
  end
103
119
  end
104
120
  end
@@ -115,15 +131,15 @@ module Rakie
115
131
 
116
132
  if result == HANDLE_FINISHED
117
133
  @ios[io] = @ios[io] & ~WRITE_EVENT
118
- puts("Event remove write #{io}")
134
+ Log.debug("Event remove write #{io}")
119
135
 
120
136
  elsif result == HANDLE_FAILED
121
137
  handler.on_close(io)
122
- puts("Event close #{io}")
138
+ Log.debug("Event close #{io}")
123
139
 
124
140
  @ios.delete(io)
125
141
  @handlers.delete(io)
126
- puts("Event remove all #{io}")
142
+ Log.debug("Event remove all #{io}")
127
143
  end
128
144
  end
129
145
  end
@@ -132,22 +148,22 @@ module Rakie
132
148
 
133
149
  def push(io, handler, event)
134
150
  @lock.lock
135
- @wait_ios.push([io, handler, event])
151
+ @wait_ios.push([OPERATION_ADD, io, handler, event])
136
152
  @signal_out.write('a')
137
153
  @lock.unlock
138
154
  end
139
155
 
140
156
  def delete(io)
141
157
  @lock.lock
142
- @wait_ios.push([io, nil, nil])
143
- @signal_out.write('d')
158
+ @wait_ios.push([OPERATION_DELETE, io, nil, nil])
159
+ @signal_out.write('a')
144
160
  @lock.unlock
145
161
  end
146
162
 
147
163
  def modify(io, handler, event)
148
164
  @lock.lock
149
- @wait_ios.push([io, handler, event])
150
- @signal_out.write('m')
165
+ @wait_ios.push([OPERATION_MODIFY, io, handler, event])
166
+ @signal_out.write('a')
151
167
  @lock.unlock
152
168
  end
153
169
 
@@ -31,7 +31,7 @@ module Rakie
31
31
  :entity => ""
32
32
  }
33
33
  }
34
- puts("SimpleServer accept client: #{channel}")
34
+ Log.debug("SimpleServer accept client: #{channel}")
35
35
  end
36
36
 
37
37
  def parse_data_type(client, data)
@@ -41,7 +41,7 @@ module Rakie
41
41
  client[:parse_status] = PARSE_LEN
42
42
  client[:parse_offset] += 1
43
43
 
44
- puts("SimpleServer parse data type ok")
44
+ Log.debug("SimpleServer parse data type ok")
45
45
  return PARSE_OK
46
46
  end
47
47
 
@@ -63,7 +63,7 @@ module Rakie
63
63
  client[:parse_status] = PARSE_ENTITY
64
64
  client[:parse_offset] = offset + 4
65
65
 
66
- puts("SimpleServer parse data len ok")
66
+ Log.debug("SimpleServer parse data len ok")
67
67
  return PARSE_OK
68
68
  end
69
69
 
@@ -79,7 +79,7 @@ module Rakie
79
79
  client[:parse_status] = PARSE_TYPE
80
80
  client[:parse_offset] = offset + len
81
81
 
82
- puts("SimpleServer parse data entity ok")
82
+ Log.debug("SimpleServer parse data entity ok")
83
83
  return PARSE_COMPLETE
84
84
  end
85
85
 
@@ -104,7 +104,7 @@ module Rakie
104
104
  end
105
105
  end
106
106
 
107
- puts("SimpleServer parse data result #{result}")
107
+ Log.debug("SimpleServer parse data result #{result}")
108
108
 
109
109
  return result
110
110
  end
@@ -117,13 +117,11 @@ module Rakie
117
117
  end
118
118
 
119
119
  def on_recv(channel, data)
120
- puts("SimpleServer recv: #{data}")
120
+ Log.debug("SimpleServer recv: #{data}")
121
121
  client = @clients[channel]
122
122
  client[:parse_offset] = 0
123
123
  result = self.parse_data(client, data)
124
124
 
125
- pp client
126
-
127
125
  if result == PARSE_COMPLETE
128
126
  if @delegate != nil
129
127
  @delegate.handle(client[:request], client[:response])
@@ -137,7 +135,7 @@ module Rakie
137
135
  elsif result == PARSE_ERROR
138
136
  channel.close
139
137
  @clients.delete(channel)
140
- puts("SimpleServer: Illegal request")
138
+ Log.debug("SimpleServer: Illegal request")
141
139
  return client[:parse_offset]
142
140
  end
143
141
 
@@ -145,5 +143,3 @@ module Rakie
145
143
  end
146
144
  end
147
145
  end
148
-
149
- require "pp"
@@ -22,21 +22,21 @@ module Rakie
22
22
  channel = Channel.new(client_io)
23
23
 
24
24
  if @delegate != nil
25
- puts("TCPServerChannel has delegate")
25
+ Log.debug("TCPServerChannel has delegate")
26
26
  @delegate.on_accept(channel)
27
27
 
28
28
  else
29
- puts("TCPServerChannel no delegate")
29
+ Log.debug("TCPServerChannel no delegate")
30
30
  @clients << channel
31
31
  end
32
32
 
33
- puts("TCPServerChannel accept #{channel}")
33
+ Log.debug("TCPServerChannel accept #{channel}")
34
34
 
35
35
  rescue IO::EAGAINWaitReadable
36
- puts("TCPServerChannel accept wait")
36
+ Log.debug("TCPServerChannel accept wait")
37
37
 
38
38
  rescue
39
- puts("TCPServerChannel Accept failed #{io}")
39
+ Log.debug("TCPServerChannel Accept failed #{io}")
40
40
  return Event::HANDLE_FAILED
41
41
  end
42
42
 
data/test/test_rakie.rb CHANGED
@@ -1,5 +1,10 @@
1
- require 'test/unit'
1
+ require 'minitest/autorun'
2
+ require "rakie"
2
3
 
3
- class RakieTest < Test::Unit::TestCase
4
-
4
+ class RakieTest < Minitest::Test
5
+ def test_log
6
+ Rakie::Log.info('test message')
7
+ Rakie::Log.error('test message')
8
+ Rakie::Log.debug('test message')
9
+ end
5
10
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rakie
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jakit Liang