libuv 0.10.0 → 0.10.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. data/.gitignore +17 -17
  2. data/.gitmodules +3 -3
  3. data/.rspec +1 -1
  4. data/.travis.yml +16 -16
  5. data/Gemfile +2 -2
  6. data/LICENSE +23 -23
  7. data/README.md +82 -73
  8. data/Rakefile +31 -31
  9. data/lib/libuv.rb +53 -34
  10. data/lib/libuv/async.rb +47 -33
  11. data/lib/libuv/check.rb +55 -48
  12. data/lib/libuv/error.rb +70 -70
  13. data/lib/libuv/ext/ext.rb +264 -256
  14. data/lib/libuv/ext/platform/darwin_x64.rb +12 -12
  15. data/lib/libuv/ext/platform/linux.rb +7 -7
  16. data/lib/libuv/ext/platform/unix.rb +13 -13
  17. data/lib/libuv/ext/platform/windows.rb +26 -26
  18. data/lib/libuv/ext/tasks.rb +27 -27
  19. data/lib/libuv/ext/tasks/mac.rb +23 -23
  20. data/lib/libuv/ext/tasks/unix.rb +23 -23
  21. data/lib/libuv/ext/tasks/win.rb +11 -11
  22. data/lib/libuv/ext/types.rb +234 -229
  23. data/lib/libuv/file.rb +192 -0
  24. data/lib/libuv/filesystem.rb +233 -0
  25. data/lib/libuv/fs_event.rb +31 -31
  26. data/lib/libuv/handle.rb +85 -81
  27. data/lib/libuv/idle.rb +56 -49
  28. data/lib/libuv/loop.rb +338 -310
  29. data/lib/libuv/{assertions.rb → mixins/assertions.rb} +23 -23
  30. data/lib/libuv/mixins/fs_checks.rb +55 -0
  31. data/lib/libuv/{listener.rb → mixins/listener.rb} +34 -34
  32. data/lib/libuv/{net.rb → mixins/net.rb} +37 -37
  33. data/lib/libuv/{resource.rb → mixins/resource.rb} +27 -27
  34. data/lib/libuv/{stream.rb → mixins/stream.rb} +143 -123
  35. data/lib/libuv/pipe.rb +197 -97
  36. data/lib/libuv/prepare.rb +56 -49
  37. data/lib/libuv/q.rb +1 -1
  38. data/lib/libuv/signal.rb +51 -0
  39. data/lib/libuv/tcp.rb +204 -193
  40. data/lib/libuv/timer.rb +88 -75
  41. data/lib/libuv/tty.rb +37 -34
  42. data/lib/libuv/udp.rb +273 -255
  43. data/lib/libuv/version.rb +3 -3
  44. data/lib/libuv/work.rb +63 -61
  45. data/libuv.gemspec +54 -54
  46. data/spec/async_spec.rb +60 -60
  47. data/spec/cpu_spec.rb +10 -0
  48. data/spec/defer_spec.rb +980 -980
  49. data/spec/filesystem_spec.rb +119 -0
  50. data/spec/idle_spec.rb +56 -56
  51. data/spec/pipe_spec.rb +153 -148
  52. data/spec/tcp_spec.rb +203 -188
  53. metadata +73 -49
  54. checksums.yaml +0 -15
  55. data/lib/libuv/simple_async.rb +0 -28
data/lib/libuv/file.rb ADDED
@@ -0,0 +1,192 @@
1
+ module Libuv
2
+ class File < Q::DeferredPromise
3
+ include Assertions, Resource, Listener, FsChecks
4
+
5
+
6
+ attr_reader :fileno, :closed
7
+
8
+
9
+ def initialize(loop, path, flags = 0, mode = 0)
10
+ super(loop, loop.defer)
11
+
12
+ @fileno = -1
13
+ @closed = true
14
+ @path, @flags, @mode = path, flags, mode
15
+
16
+ request = ::Libuv::Ext.create_request(:uv_fs)
17
+ pre_check @defer, request, ::Libuv::Ext.fs_open(@loop, request, @path, @flags, @mode, callback(:on_open))
18
+ end
19
+
20
+ def close
21
+ @closed = true
22
+ request = ::Libuv::Ext.create_request(:uv_fs)
23
+ pre_check(@defer, request, ::Libuv::Ext.fs_close(@loop.handle, request, @fileno, callback(:on_close)))
24
+ end
25
+
26
+ def read(length, offset = 0)
27
+ assert_type(Integer, length, "length must be an Integer")
28
+ assert_type(Integer, offset, "offset must be an Integer")
29
+ @read_deferred = @loop.defer
30
+
31
+ @read_buffer = FFI::MemoryPointer.new(length)
32
+
33
+ request = ::Libuv::Ext.create_request(:uv_fs)
34
+ pre_check(@read_deferred, request, ::Libuv::Ext.fs_read(@loop.handle, request, @fileno, @read_buffer, length, offset, callback(:on_read)))
35
+ @read_deferred.promise
36
+ end
37
+
38
+ def write(data, offset = 0)
39
+ assert_type(String, data, "data must be a String")
40
+ assert_type(Integer, offset, "offset must be an Integer")
41
+ @write_deferred = @loop.defer
42
+
43
+ length = data.respond_to?(:bytesize) ? data.bytesize : data.size
44
+ @write_buffer = FFI::MemoryPointer.from_string(data)
45
+
46
+ request = ::Libuv::Ext.create_request(:uv_fs)
47
+ pre_check(@write_deferred, request, ::Libuv::Ext.fs_write(@loop.handle, request, @fileno, @write_buffer, length, offset, callback(:on_write)))
48
+ @write_deferred.promise
49
+ end
50
+
51
+ def sync
52
+ @sync_deferred = @loop.defer
53
+
54
+ request = ::Libuv::Ext.create_request(:uv_fs)
55
+ pre_check @sync_deferred, request, ::Libuv::Ext.fs_fsync(@loop.handle, request, @fileno, callback(:on_sync))
56
+ @sync_deferred.promise
57
+ end
58
+
59
+ def datasync
60
+ @datasync_deferred = @loop.defer
61
+
62
+ request = ::Libuv::Ext.create_request(:uv_fs)
63
+ pre_check @datasync_deferred, request, ::Libuv::Ext.fs_fdatasync(@loop.handle, request, @fileno, callback(:on_datasync))
64
+ @datasync_deferred.promise
65
+ end
66
+
67
+ def truncate(offset)
68
+ assert_type(Integer, offset, "offset must be an Integer")
69
+ @truncate_deferred = @loop.defer
70
+
71
+ request = ::Libuv::Ext.create_request(:uv_fs)
72
+ pre_check @truncate_deferred, request, ::Libuv::Ext.fs_ftruncate(@loop.handle, request, @fileno, offset, callback(:on_truncate))
73
+ @truncate_deferred.promise
74
+ end
75
+
76
+ def utime(atime, mtime)
77
+ assert_type(Integer, atime, "atime must be an Integer")
78
+ assert_type(Integer, mtime, "mtime must be an Integer")
79
+ @utime_deferred = @loop.defer
80
+
81
+ request = ::Libuv::Ext.create_request(:uv_fs)
82
+ pre_check @utime_deferred, request, ::Libuv::Ext.fs_futime(@loop.handle, request, @fileno, atime, mtime, callback(:on_utime))
83
+ @utime_deferred.promise
84
+ end
85
+
86
+ def chmod(mode)
87
+ assert_type(Integer, mode, "mode must be an Integer")
88
+ @chmod_deferred = @loop.defer
89
+
90
+ request = ::Libuv::Ext.create_request(:uv_fs)
91
+ pre_check @chmod_deferred, request, ::Libuv::Ext.fs_fchmod(@loop.handle, request, @fileno, mode, callback(:on_chmod))
92
+ @chmod_deferred.promise
93
+ end
94
+
95
+ def chown(uid, gid)
96
+ assert_type(Integer, uid, "uid must be an Integer")
97
+ assert_type(Integer, gid, "gid must be an Integer")
98
+ @chown_deferred = @loop.defer
99
+
100
+ request = ::Libuv::Ext.create_request(:uv_fs)
101
+ pre_check @chown_deferred, request, ::Libuv::Ext.fs_fchown(@loop.handle, request, @fileno, uid, gid, callback(:on_chown))
102
+ @chown_deferred.promise
103
+ end
104
+
105
+
106
+ private
107
+
108
+
109
+ def on_open(req)
110
+ if post_check(req, @defer)
111
+ @fileno = req[:result]
112
+ cleanup(req)
113
+ @closed = false
114
+ @defer.notify(self)
115
+ end
116
+ end
117
+
118
+ def on_close(req)
119
+ if post_check(req, @defer)
120
+ cleanup(req)
121
+ @defer.resolve(nil)
122
+ end
123
+ end
124
+
125
+ def on_read(req)
126
+ if post_check(req, @read_deferred)
127
+ data = @read_buffer.read_string(req[:result])
128
+ cleanup(req)
129
+ @read_deferred.resolve(data)
130
+ end
131
+ @read_buffer = nil
132
+ @read_deferred = nil
133
+ end
134
+
135
+ def on_write(req)
136
+ if post_check(req, @write_deferred)
137
+ cleanup(req)
138
+ @write_deferred.resolve(nil)
139
+ end
140
+ @write_buffer = nil
141
+ @write_deferred = nil
142
+ end
143
+
144
+ def on_sync(req)
145
+ if post_check(req, @sync_deferred)
146
+ cleanup(req)
147
+ @sync_deferred.resolve(nil)
148
+ end
149
+ @sync_deferred = nil
150
+ end
151
+
152
+ def on_datasync(req)
153
+ if post_check(req, @datasync_deferred)
154
+ cleanup(req)
155
+ @datasync_deferred.resolve(nil)
156
+ end
157
+ @datasync_deferred = nil
158
+ end
159
+
160
+ def on_truncate(req)
161
+ if post_check(req, @truncate_deferred)
162
+ cleanup(req)
163
+ @truncate_deferred.resolve(nil)
164
+ end
165
+ @truncate_deferred = nil
166
+ end
167
+
168
+ def on_utime(req)
169
+ if post_check(req, @utime_deferred)
170
+ cleanup(req)
171
+ @utime_deferred.resolve(nil)
172
+ end
173
+ @utime_deferred = nil
174
+ end
175
+
176
+ def on_chmod(req)
177
+ if post_check(req, @chmod_deferred)
178
+ cleanup(req)
179
+ @chmod_deferred.resolve(nil)
180
+ end
181
+ @chmod_deferred = nil
182
+ end
183
+
184
+ def on_chown(req)
185
+ if post_check(req, @chown_deferred)
186
+ cleanup(req)
187
+ @chown_deferred.resolve(nil)
188
+ end
189
+ @chown_deferred = nil
190
+ end
191
+ end
192
+ end
@@ -0,0 +1,233 @@
1
+ module Libuv
2
+ class Filesystem
3
+ include Assertions, Resource, Listener, FsChecks
4
+
5
+
6
+ def initialize(loop)
7
+ @loop = loop
8
+ end
9
+
10
+ def unlink(path)
11
+ assert_type(String, path, "path must be a String")
12
+ @unlink_deferred = @loop.defer
13
+
14
+ request = ::Libuv::Ext.create_request(:uv_fs)
15
+ pre_check @unlink_deferred, request, ::Libuv::Ext.fs_unlink(@loop, request, path, callback(:on_unlink))
16
+ @unlink_deferred.promise
17
+ end
18
+
19
+ def mkdir(path, mode = 0777)
20
+ assert_type(String, path, "path must be a String")
21
+ assert_type(Integer, mode, "mode must be an Integer")
22
+ @mkdir_deferred = @loop.defer
23
+
24
+ request = ::Libuv::Ext.create_request(:uv_fs)
25
+ pre_check @mkdir_deferred, request, ::Libuv::Ext.fs_mkdir(@loop, request, path, mode, callback(:on_mkdir))
26
+ @mkdir_deferred.promise
27
+ end
28
+
29
+ def rmdir(path)
30
+ assert_type(String, path, "path must be a String")
31
+ @rmdir_deferred = @loop.defer
32
+
33
+ request = ::Libuv::Ext.create_request(:uv_fs)
34
+ pre_check @rmdir_deferred, request, ::Libuv::Ext.fs_rmdir(@loop, request, path, callback(:on_rmdir))
35
+ @rmdir_deferred.promise
36
+ end
37
+
38
+ def readdir(path)
39
+ assert_type(String, path, "path must be a String")
40
+ @readdir_deferred = @loop.defer
41
+
42
+ request = ::Libuv::Ext.create_request(:uv_fs)
43
+ pre_check @readdir_deferred, request, ::Libuv::Ext.fs_readdir(@loop, request, path, 0, callback(:on_readdir))
44
+ @readdir_deferred.promise
45
+ end
46
+
47
+ def rename(old_path, new_path)
48
+ assert_type(String, old_path, "old_path must be a String")
49
+ assert_type(String, new_path, "new_path must be a String")
50
+ @rename_deferred = @loop.defer
51
+
52
+ request = ::Libuv::Ext.create_request(:uv_fs)
53
+ pre_check @rename_deferred, request, ::Libuv::Ext.fs_rename(@loop, request, old_path, new_path, callback(:on_rename))
54
+ @rename_deferred.promise
55
+ end
56
+
57
+ def chmod(path, mode)
58
+ assert_type(String, path, "path must be a String")
59
+ assert_type(Integer, mode, "mode must be an Integer")
60
+ @chmod_deferred = @loop.defer
61
+
62
+ request = ::Libuv::Ext.create_request(:uv_fs)
63
+ pre_check @chmod_deferred, request, ::Libuv::Ext.fs_chmod(@loop, request, path, mode, callback(:on_chmod))
64
+ @chmod_deferred.promise
65
+ end
66
+
67
+ def utime(path, atime, mtime)
68
+ assert_type(String, path, "path must be a String")
69
+ assert_type(Integer, atime, "atime must be an Integer")
70
+ assert_type(Integer, mtime, "mtime must be an Integer")
71
+ @utime_deferred = @loop.defer
72
+
73
+ request = ::Libuv::Ext.create_request(:uv_fs)
74
+ pre_check @utime_deferred, request, ::Libuv::Ext.fs_utime(@loop, request, path, atime, mtime, callback(:on_utime))
75
+ @utime_deferred.promise
76
+ end
77
+
78
+ def lstat(path)
79
+ assert_type(String, path, "path must be a String")
80
+ @stat_deferred = @loop.defer
81
+
82
+ request = ::Libuv::Ext.create_request(:uv_fs)
83
+ pre_check @stat_deferred, request, ::Libuv::Ext.fs_lstat(@loop, request, path, callback(:on_stat))
84
+ @stat_deferred.promise
85
+ end
86
+
87
+ def link(old_path, new_path)
88
+ assert_type(String, old_path, "old_path must be a String")
89
+ assert_type(String, new_path, "new_path must be a String")
90
+ @link_deferred = @loop.defer
91
+
92
+ request = ::Libuv::Ext.create_request(:uv_fs)
93
+ pre_check @link_deferred, request, ::Libuv::Ext.fs_link(@loop, request, old_path, new_path, callback(:on_link))
94
+ @link_deferred.promise
95
+ end
96
+
97
+ def symlink(old_path, new_path)
98
+ assert_type(String, old_path, "old_path must be a String")
99
+ assert_type(String, new_path, "new_path must be a String")
100
+ @symlink_deferred = @loop.defer
101
+
102
+ request = ::Libuv::Ext.create_request(:uv_fs)
103
+ pre_check @symlink_deferred, request, ::Libuv::Ext.fs_symlink(@loop, request, old_path, new_path, 0, callback(:on_symlink))
104
+ @symlink_deferred.promise
105
+ end
106
+
107
+ def readlink(path)
108
+ assert_type(String, path, "path must be a String")
109
+ @readlink_deferred = @loop.defer
110
+
111
+ request = ::Libuv::Ext.create_request(:uv_fs)
112
+ pre_check @readlink_deferred, request, ::Libuv::Ext.fs_readlink(@loop, request, path, callback(:on_readlink))
113
+ @readlink_deferred.promise
114
+ end
115
+
116
+ def chown(path, uid, gid)
117
+ assert_type(String, path, "path must be a String")
118
+ assert_type(Integer, uid, "uid must be an Integer")
119
+ assert_type(Integer, gid, "gid must be an Integer")
120
+ @chown_deferred = @loop.defer
121
+
122
+ request = ::Libuv::Ext.create_request(:uv_fs)
123
+ pre_check @chown_deferred, request, ::Libuv::Ext.fs_chown(@loop, request, path, uid, gid, callback(:on_chown))
124
+ @chown_deferred.promise
125
+ end
126
+
127
+
128
+ private
129
+
130
+
131
+ def on_unlink(req)
132
+ if post_check(req, @unlink_deferred)
133
+ path = req[:path]
134
+ cleanup(req)
135
+ @unlink_deferred.resolve(path)
136
+ end
137
+ @unlink_deferred = nil
138
+ end
139
+
140
+ def on_mkdir(req)
141
+ if post_check(req, @mkdir_deferred)
142
+ path = req[:path]
143
+ cleanup(req)
144
+ @mkdir_deferred.resolve(path)
145
+ end
146
+ @mkdir_deferred = nil
147
+ end
148
+
149
+ def on_rmdir(req)
150
+ if post_check(req, @rmdir_deferred)
151
+ path = req[:path]
152
+ cleanup(req)
153
+ @rmdir_deferred.resolve(path)
154
+ end
155
+ @rmdir_deferred = nil
156
+ end
157
+
158
+ def on_readdir(req)
159
+ if post_check(req, @readdir_deferred)
160
+ num_files = req[:result]
161
+ string_ptr = req[:ptr]
162
+ files = string_ptr.null? ? [] : string_ptr.read_array_of_type(FFI::TYPE_STRING, :read_string, num_files)
163
+ cleanup(req)
164
+ @readdir_deferred.resolve(files)
165
+ end
166
+ @readdir_deferred = nil
167
+ end
168
+
169
+ def on_rename(req)
170
+ if post_check(req, @rename_deferred)
171
+ path = req[:path]
172
+ cleanup(req)
173
+ @rename_deferred.resolve(path)
174
+ end
175
+ @rename_deferred = nil
176
+ end
177
+
178
+ def on_chmod(req)
179
+ if post_check(req, @chmod_deferred)
180
+ path = req[:path]
181
+ cleanup(req)
182
+ @chmod_deferred.resolve(path)
183
+ end
184
+ @chmod_deferred = nil
185
+ end
186
+
187
+ def on_utime(req)
188
+ if post_check(req, @utime_deferred)
189
+ path = req[:path]
190
+ cleanup(req)
191
+ @utime_deferred.resolve(path)
192
+ end
193
+ @utime_deferred = nil
194
+ end
195
+
196
+ def on_link(req)
197
+ if post_check(req, @link_deferred)
198
+ path = req[:path]
199
+ cleanup(req)
200
+ @link_deferred.resolve(path)
201
+ end
202
+ @link_deferred = nil
203
+ end
204
+
205
+ def on_symlink(req)
206
+ if post_check(req, @symlink_deferred)
207
+ path = req[:path]
208
+ cleanup(req)
209
+ @symlink_deferred.resolve(path)
210
+ end
211
+ @symlink_deferred = nil
212
+ end
213
+
214
+ def on_readlink(req)
215
+ if post_check(req, @readlink_deferred)
216
+ string_ptr = req[:ptr]
217
+ path = string_ptr.null? ? nil : string_ptr.read_string_to_null
218
+ cleanup(req)
219
+ @readlink_deferred.resolve(path)
220
+ end
221
+ @readlink_deferred = nil
222
+ end
223
+
224
+ def on_chown(req)
225
+ if post_check(req, @chown_deferred)
226
+ path = req[:path]
227
+ cleanup(req)
228
+ @chown_deferred.resolve(path)
229
+ end
230
+ @chown_deferred = nil
231
+ end
232
+ end
233
+ end
@@ -1,31 +1,31 @@
1
- module Libuv
2
- class FSEvent < Handle
3
-
4
-
5
- EVENTS = {1 => :rename, 2 => :change}.freeze
6
-
7
-
8
- def initialize(loop, path)
9
- @loop = loop
10
-
11
- fs_event_ptr = ::Libuv::Ext.create_handle(:uv_fs_event)
12
- error = check_result ::Libuv::Ext.fs_event_init(loop.handle, fs_event_ptr, path, callback(:on_fs_event), 0)
13
-
14
- super(fs_event_ptr, error)
15
- end
16
-
17
-
18
- private
19
-
20
-
21
- def on_fs_event(handle, filename, events, status)
22
- e = check_result(status)
23
-
24
- if e
25
- reject(e)
26
- else
27
- defer.notify(filename, EVENTS[events]) # notify of a change
28
- end
29
- end
30
- end
31
- end
1
+ module Libuv
2
+ class FSEvent < Handle
3
+
4
+
5
+ EVENTS = {1 => :rename, 2 => :change}.freeze
6
+
7
+
8
+ def initialize(loop, path)
9
+ @loop = loop
10
+
11
+ fs_event_ptr = ::Libuv::Ext.create_handle(:uv_fs_event)
12
+ error = check_result ::Libuv::Ext.fs_event_init(loop.handle, fs_event_ptr, path, callback(:on_fs_event), 0)
13
+
14
+ super(fs_event_ptr, error)
15
+ end
16
+
17
+
18
+ private
19
+
20
+
21
+ def on_fs_event(handle, filename, events, status)
22
+ e = check_result(status)
23
+
24
+ if e
25
+ reject(e)
26
+ else
27
+ defer.notify(filename, EVENTS[events]) # notify of a change
28
+ end
29
+ end
30
+ end
31
+ end