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 +4 -4
- data/lib/rakie.rb +2 -0
- data/lib/rakie/channel.rb +38 -10
- data/lib/rakie/event.rb +42 -26
- data/lib/rakie/simple_server.rb +7 -11
- data/lib/rakie/tcp_server_channel.rb +5 -5
- data/test/test_rakie.rb +8 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8361c3cfc878d6edc052170cc5fbc5f555d173562c7325d9a939c567402e8b4f
|
4
|
+
data.tar.gz: cc2f7fa419c2fcb750ec70305b942336974cd7e35ad336edc38436027f74e6dd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1b39af452c4d98899dd787a38ce1394b1781eaee28ab3761b46b824ec4b426f76c27d451da8ae1fb01545676dd572a19f9c9dae09839221a17a5ee807a151f72
|
7
|
+
data.tar.gz: c7953919abc5572b8fba09a5afdf5e32034c0bc2e4fdbca4f649f67f9ec635a10cf49888a194ce67f17afda0424becd1e42467cd9b73e2a990670fd29e2c2c50
|
data/lib/rakie.rb
CHANGED
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
75
|
+
Log.debug("Channel write finished")
|
56
76
|
|
57
77
|
rescue IO::EAGAINWaitWritable
|
58
|
-
|
78
|
+
self.handle_write(len)
|
79
|
+
|
80
|
+
Log.debug("Channel write continue")
|
59
81
|
return Event::HANDLE_CONTINUED
|
60
82
|
|
61
83
|
rescue
|
62
|
-
|
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
|
-
|
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
|
-
|
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 == '
|
30
|
-
|
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
|
-
|
46
|
+
Log.debug("Event add all #{new_io.fileno} to #{new_event}")
|
34
47
|
|
35
|
-
elsif
|
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
|
-
|
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
|
-
|
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
|
-
|
109
|
+
Log.debug("Event remove read #{io}")
|
94
110
|
|
95
111
|
elsif result == HANDLE_FAILED
|
96
112
|
handler.on_close(io)
|
97
|
-
|
113
|
+
Log.debug("Event close #{io}")
|
98
114
|
|
99
115
|
@ios.delete(io)
|
100
116
|
@handlers.delete(io)
|
101
|
-
|
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
|
-
|
134
|
+
Log.debug("Event remove write #{io}")
|
119
135
|
|
120
136
|
elsif result == HANDLE_FAILED
|
121
137
|
handler.on_close(io)
|
122
|
-
|
138
|
+
Log.debug("Event close #{io}")
|
123
139
|
|
124
140
|
@ios.delete(io)
|
125
141
|
@handlers.delete(io)
|
126
|
-
|
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('
|
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('
|
165
|
+
@wait_ios.push([OPERATION_MODIFY, io, handler, event])
|
166
|
+
@signal_out.write('a')
|
151
167
|
@lock.unlock
|
152
168
|
end
|
153
169
|
|
data/lib/rakie/simple_server.rb
CHANGED
@@ -31,7 +31,7 @@ module Rakie
|
|
31
31
|
:entity => ""
|
32
32
|
}
|
33
33
|
}
|
34
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
25
|
+
Log.debug("TCPServerChannel has delegate")
|
26
26
|
@delegate.on_accept(channel)
|
27
27
|
|
28
28
|
else
|
29
|
-
|
29
|
+
Log.debug("TCPServerChannel no delegate")
|
30
30
|
@clients << channel
|
31
31
|
end
|
32
32
|
|
33
|
-
|
33
|
+
Log.debug("TCPServerChannel accept #{channel}")
|
34
34
|
|
35
35
|
rescue IO::EAGAINWaitReadable
|
36
|
-
|
36
|
+
Log.debug("TCPServerChannel accept wait")
|
37
37
|
|
38
38
|
rescue
|
39
|
-
|
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 '
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require "rakie"
|
2
3
|
|
3
|
-
class RakieTest < Test
|
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
|