handler_socket 0.0.0

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.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 miyucy
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,17 @@
1
+ = handler_socket
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 miyucy. See LICENSE for details.
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "handler_socket"
8
+ gem.summary = %Q{libhsclient(HandlerSocket) binding for Ruby}
9
+ gem.description = %Q{libhsclient(HandlerSocket) binding for Ruby}
10
+ gem.email = "miyucy@gmail.com"
11
+ gem.homepage = "http://github.com/miyucy/handler_socket"
12
+ gem.authors = ["miyucy"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+ task :spec => :check_dependencies
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "handler_socket #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,7 @@
1
+ require 'mkmf'
2
+
3
+ $CFLAGS += "-I/usr/include/handlersocket"
4
+ $libs += "-lhsclient"
5
+
6
+ have_library("stdc++")
7
+ create_makefile('handler_socket')
@@ -0,0 +1,345 @@
1
+ #include <ruby.h>
2
+ #include "hstcpcli.hpp"
3
+
4
+ typedef struct {
5
+ dena::hstcpcli_i *ptr;
6
+ } HandlerSocket;
7
+
8
+ void parse_options(dena::config&, dena::socket_args&, VALUE);
9
+
10
+ VALUE hs_free(HandlerSocket* hs)
11
+ {
12
+ fprintf(stderr, "hs=%p\n", hs);
13
+ if (hs) {
14
+ fprintf(stderr, "hs->ptr=%p\n", hs->ptr);
15
+ if (hs->ptr) {
16
+ hs->ptr->close();
17
+ delete hs->ptr;
18
+ }
19
+ xfree(hs);
20
+ }
21
+ }
22
+
23
+ VALUE hs_alloc(VALUE self)
24
+ {
25
+ return Data_Wrap_Struct(self, NULL, hs_free, 0);
26
+ }
27
+
28
+ VALUE hs_initialize(VALUE self, VALUE options)
29
+ {
30
+ HandlerSocket* hs;
31
+
32
+ Data_Get_Struct(self, HandlerSocket, hs);
33
+ fprintf(stderr, "hs=%p\n", hs);
34
+ if (!hs) {
35
+ hs = (HandlerSocket*)xmalloc(sizeof(HandlerSocket));
36
+ MEMZERO(hs, HandlerSocket, 1);
37
+ DATA_PTR(self) = hs;
38
+ fprintf(stderr, "hs=%p\n", hs);
39
+ }
40
+
41
+ dena::config conf;
42
+ dena::socket_args args;
43
+ parse_options(conf, args, options);
44
+
45
+ dena::hstcpcli_ptr ptr = dena::hstcpcli_i::create(args);
46
+ fprintf(stderr, "ptr=%p\n", &ptr);
47
+ hs->ptr = ptr.get();
48
+ fprintf(stderr, "hs->ptr=%p\n", hs->ptr);
49
+ fprintf(stderr, "ptr.get()=%p\n", ptr.get());
50
+ ptr.release();
51
+
52
+ return self;
53
+ }
54
+
55
+ VALUE hs_close(VALUE self)
56
+ {
57
+ HandlerSocket* hs;
58
+ Data_Get_Struct(self, HandlerSocket, hs);
59
+
60
+ hs->ptr->close();
61
+ return Qnil;
62
+ }
63
+
64
+ VALUE hs_reconnect(VALUE self)
65
+ {
66
+ HandlerSocket* hs;
67
+ Data_Get_Struct(self, HandlerSocket, hs);
68
+
69
+ int retval = hs->ptr->reconnect();
70
+ return INT2FIX(retval);
71
+ }
72
+
73
+ VALUE hs_stable_point(VALUE self)
74
+ {
75
+ HandlerSocket* hs;
76
+ Data_Get_Struct(self, HandlerSocket, hs);
77
+
78
+ return hs->ptr->stable_point() ? Qtrue : Qfalse;
79
+ }
80
+
81
+ VALUE hs_error(VALUE self)
82
+ {
83
+ HandlerSocket* hs;
84
+ Data_Get_Struct(self, HandlerSocket, hs);
85
+
86
+ VALUE val = Qnil;
87
+ std::string error = hs->ptr->get_error();
88
+
89
+ if (!error.empty()) {
90
+ val = rb_str_new2(error.c_str());
91
+ }
92
+ return val;
93
+ }
94
+
95
+ VALUE hs_error_code(VALUE self)
96
+ {
97
+ HandlerSocket* hs;
98
+ Data_Get_Struct(self, HandlerSocket, hs);
99
+
100
+ return INT2FIX(hs->ptr->get_error_code());
101
+ }
102
+
103
+ VALUE hs_open_index(VALUE self, VALUE id, VALUE db, VALUE table, VALUE index, VALUE fields)
104
+ {
105
+ HandlerSocket* hs;
106
+ Data_Get_Struct(self, HandlerSocket, hs);
107
+ dena::hstcpcli_i *const ptr = hs->ptr;
108
+
109
+ do {
110
+ size_t pst_id = NUM2INT(id);
111
+ ptr->request_buf_open_index(pst_id, StringValuePtr(db), StringValuePtr(table), StringValuePtr(index), StringValuePtr(fields));
112
+ if (ptr->request_send() != 0) {
113
+ break;
114
+ }
115
+
116
+ size_t nflds = 0;
117
+ ptr->response_recv(nflds);
118
+ const int e = ptr->get_error_code();
119
+ fprintf(stderr, "errcode=%d\n", e);
120
+ if (e >= 0) {
121
+ ptr->response_buf_remove();
122
+ }
123
+ fprintf(stderr, "errcode=%d\n", ptr->get_error_code());
124
+ } while(0);
125
+
126
+ return INT2FIX(ptr->get_error_code());
127
+ }
128
+
129
+ VALUE hs_execute_single(int argc, VALUE *argv, VALUE self)
130
+ {
131
+ HandlerSocket* hs;
132
+ Data_Get_Struct(self, HandlerSocket, hs);
133
+ dena::hstcpcli_i *const ptr = hs->ptr;
134
+
135
+ VALUE id, op, keys, limit, skip, modop, modvals;
136
+ rb_scan_args(argc, argv, "52", &id, &op, &keys, &limit, &skip, &modop, &modvals);
137
+
138
+ StringValue(op);
139
+ Check_Type(keys, T_ARRAY);
140
+
141
+ if (!NIL_P(modop)) {
142
+ StringValue(modop);
143
+ Check_Type(modvals, T_ARRAY);
144
+ }
145
+
146
+ dena::string_ref op_ref;
147
+ std::vector<dena::string_ref> keyary;
148
+
149
+ op_ref = dena::string_ref(RSTRING_PTR(op), RSTRING_LEN(op));
150
+
151
+ keyary.reserve(RARRAY_LEN(keys));
152
+ for (size_t i=0; i<RARRAY_LEN(keys); i++) {
153
+ VALUE key = rb_ary_entry(keys, i);
154
+ StringValuePtr(key);
155
+ keyary.push_back(dena::string_ref(RSTRING_PTR(key), RSTRING_LEN(key)));
156
+ }
157
+
158
+ dena::string_ref modop_ref;
159
+ std::vector<dena::string_ref> modary;
160
+
161
+ if (!NIL_P(modop)) {
162
+ modop_ref = dena::string_ref(RSTRING_PTR(modop), RSTRING_LEN(modop));
163
+
164
+ modary.reserve(RARRAY_LEN(modvals));
165
+ for (size_t i=0; i<RARRAY_LEN(modvals); i++) {
166
+ VALUE key = rb_ary_entry(modvals, i);
167
+ // StringValue(key);
168
+ modary.push_back(dena::string_ref(RSTRING_PTR(key), RSTRING_LEN(key)));
169
+ }
170
+ }
171
+
172
+ ptr->request_buf_exec_generic(NUM2INT(id),
173
+ op_ref,
174
+ &keyary[0], keyary.size(),
175
+ NUM2INT(limit), NUM2INT(skip),
176
+ modop_ref,
177
+ &modary[0], modary.size());
178
+
179
+ if (ptr->request_send() != 0) {
180
+ return Qnil;
181
+ }
182
+
183
+ size_t nflds = 0;
184
+ ptr->response_recv(nflds);
185
+ fprintf(stderr, "nflds=%zu\n", nflds);
186
+
187
+ VALUE retval = rb_ary_new();
188
+
189
+ const int e = ptr->get_error_code();
190
+ fprintf(stderr, "e=%d nflds=%zu\n", e, nflds);
191
+ rb_ary_push(retval, FIX2INT(e));
192
+
193
+ if (e != 0) {
194
+ const std::string s = ptr->get_error();
195
+ rb_ary_push(retval, rb_str_new2(s.c_str()));
196
+ } else {
197
+ VALUE arys, ary, val;
198
+
199
+ arys = rb_ary_new();
200
+ const dena::string_ref *row = 0;
201
+ while ((row = ptr->get_next_row()) != 0) {
202
+ fprintf(stderr, "row=%p\n", row);
203
+ ary = rb_ary_new2(nflds);
204
+ for (size_t i = 0; i < nflds; ++i) {
205
+ const dena::string_ref& v = row[i];
206
+ fprintf(stderr, "FLD %zu v=%s vbegin=%p\n", i, std::string(v.begin(), v.size()).c_str(), v.begin());
207
+ if (v.begin() != 0) {
208
+ val = rb_str_new(v.begin(), v.size());
209
+ rb_ary_push(ary, val);
210
+ } else {
211
+ rb_ary_push(ary, Qnil);
212
+ }
213
+ }
214
+ rb_ary_push(arys, ary);
215
+ }
216
+ rb_ary_push(retval, arys);
217
+ }
218
+
219
+ if (e >= 0) {
220
+ ptr->response_buf_remove();
221
+ }
222
+
223
+ return retval;
224
+ }
225
+
226
+ VALUE hs_execute_multi(int argc, VALUE *argv, VALUE self)
227
+ {
228
+ HandlerSocket* hs;
229
+ Data_Get_Struct(self, HandlerSocket, hs);
230
+ dena::hstcpcli_i *const ptr = hs->ptr;
231
+
232
+ return Qnil;
233
+ }
234
+
235
+ VALUE hs_execute_find(int argc, VALUE *argv, VALUE self)
236
+ {
237
+ HandlerSocket* hs;
238
+ Data_Get_Struct(self, HandlerSocket, hs);
239
+ dena::hstcpcli_i *const ptr = hs->ptr;
240
+
241
+ return Qnil;
242
+ }
243
+
244
+ VALUE hs_execute_update(int argc, VALUE *argv, VALUE self)
245
+ {
246
+ HandlerSocket* hs;
247
+ Data_Get_Struct(self, HandlerSocket, hs);
248
+ dena::hstcpcli_i *const ptr = hs->ptr;
249
+
250
+ return Qnil;
251
+ }
252
+
253
+ VALUE hs_execute_delete(int argc, VALUE *argv, VALUE self)
254
+ {
255
+ HandlerSocket* hs;
256
+ Data_Get_Struct(self, HandlerSocket, hs);
257
+ dena::hstcpcli_i *const ptr = hs->ptr;
258
+
259
+ return Qnil;
260
+ }
261
+
262
+ VALUE hs_execute_insert(int argc, VALUE *argv, VALUE self)
263
+ {
264
+ HandlerSocket* hs;
265
+ Data_Get_Struct(self, HandlerSocket, hs);
266
+ dena::hstcpcli_i *const ptr = hs->ptr;
267
+
268
+ return Qnil;
269
+ }
270
+
271
+ void parse_options(dena::config& conf, dena::socket_args& args, VALUE options)
272
+ {
273
+ VALUE val;
274
+
275
+ Check_Type(options, T_HASH);
276
+
277
+ val = rb_hash_aref(options, ID2SYM(rb_intern("host")));
278
+ if(val != Qnil)
279
+ {
280
+ conf["host"] = std::string(StringValuePtr(val));
281
+ fprintf(stderr, "host=%s\n", conf["host"].c_str());
282
+ }
283
+
284
+ val = rb_hash_aref(options, ID2SYM(rb_intern("port")));
285
+ if(val != Qnil)
286
+ {
287
+ conf["port"] = std::string(StringValuePtr(val));
288
+ fprintf(stderr, "port=%s\n", conf["port"].c_str());
289
+ }
290
+
291
+ val = rb_hash_aref(options, ID2SYM(rb_intern("timeout")));
292
+ if(val != Qnil)
293
+ {
294
+ conf["timeout"] = std::string(StringValuePtr(val));
295
+ fprintf(stderr, "timeout=%s\n", conf["timeout"].c_str());
296
+ }
297
+
298
+ val = rb_hash_aref(options, ID2SYM(rb_intern("listen_backlog")));
299
+ if(val != Qnil)
300
+ {
301
+ conf["listen_backlog"] = std::string(StringValuePtr(val));
302
+ fprintf(stderr, "listen_backlog=%s\n", conf["listen_backlog"].c_str());
303
+ }
304
+
305
+ val = rb_hash_aref(options, ID2SYM(rb_intern("sndbuf")));
306
+ if(val != Qnil)
307
+ {
308
+ conf["sndbuf"] = std::string(StringValuePtr(val));
309
+ fprintf(stderr, "sndbuf=%s\n", conf["sndbuf"].c_str());
310
+ }
311
+
312
+ val = rb_hash_aref(options, ID2SYM(rb_intern("rcvbuf")));
313
+ if(val != Qnil)
314
+ {
315
+ conf["rcvbuf"] = std::string(StringValuePtr(val));
316
+ fprintf(stderr, "rcvbuf=%s\n", conf["rcvbuf"].c_str());
317
+ }
318
+
319
+ args.set(conf);
320
+ }
321
+
322
+ extern "C" {
323
+ void Init_handler_socket(void)
324
+ {
325
+ VALUE rb_cHandlerSocket = rb_define_class("HandlerSocket", rb_cObject);
326
+
327
+ rb_define_alloc_func(rb_cHandlerSocket, hs_alloc);
328
+ rb_define_private_method(rb_cHandlerSocket, "initialize", (VALUE(*)(...))hs_initialize, 1);
329
+
330
+ rb_define_method(rb_cHandlerSocket, "close", (VALUE(*)(...))hs_close, 0);
331
+ rb_define_method(rb_cHandlerSocket, "reconnect", (VALUE(*)(...))hs_reconnect, 0);
332
+ rb_define_method(rb_cHandlerSocket, "stable_point", (VALUE(*)(...))hs_stable_point, 0);
333
+
334
+ rb_define_method(rb_cHandlerSocket, "error", (VALUE(*)(...))hs_error, 0);
335
+ rb_define_method(rb_cHandlerSocket, "error_code", (VALUE(*)(...))hs_error_code, 0);
336
+
337
+ rb_define_method(rb_cHandlerSocket, "open_index", (VALUE(*)(...))hs_open_index, 5);
338
+ rb_define_method(rb_cHandlerSocket, "execute_single", (VALUE(*)(...))hs_execute_single, -1);
339
+ rb_define_method(rb_cHandlerSocket, "execute_multi", (VALUE(*)(...))hs_execute_multi, -1);
340
+ rb_define_method(rb_cHandlerSocket, "execute_find", (VALUE(*)(...))hs_execute_find, -1);
341
+ rb_define_method(rb_cHandlerSocket, "execute_update", (VALUE(*)(...))hs_execute_update, -1);
342
+ rb_define_method(rb_cHandlerSocket, "execute_delete", (VALUE(*)(...))hs_execute_delete, -1);
343
+ rb_define_method(rb_cHandlerSocket, "execute_insert", (VALUE(*)(...))hs_execute_insert, -1);
344
+ }
345
+ }
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "HandlerSocket" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'handler_socket'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: handler_socket
3
+ version: !ruby/object:Gem::Version
4
+ hash: 31
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 0
10
+ version: 0.0.0
11
+ platform: ruby
12
+ authors:
13
+ - miyucy
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-08-26 00:00:00 +09:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 13
30
+ segments:
31
+ - 1
32
+ - 2
33
+ - 9
34
+ version: 1.2.9
35
+ type: :development
36
+ version_requirements: *id001
37
+ description: libhsclient(HandlerSocket) binding for Ruby
38
+ email: miyucy@gmail.com
39
+ executables: []
40
+
41
+ extensions:
42
+ - ext/extconf.rb
43
+ extra_rdoc_files:
44
+ - LICENSE
45
+ - README.rdoc
46
+ files:
47
+ - .document
48
+ - .gitignore
49
+ - LICENSE
50
+ - README.rdoc
51
+ - Rakefile
52
+ - VERSION
53
+ - ext/extconf.rb
54
+ - ext/handler_socket.cc
55
+ - spec/handler_socket_spec.rb
56
+ - spec/spec.opts
57
+ - spec/spec_helper.rb
58
+ has_rdoc: true
59
+ homepage: http://github.com/miyucy/handler_socket
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options:
64
+ - --charset=UTF-8
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ hash: 3
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ requirements: []
86
+
87
+ rubyforge_project:
88
+ rubygems_version: 1.3.7
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: libhsclient(HandlerSocket) binding for Ruby
92
+ test_files:
93
+ - spec/handler_socket_spec.rb
94
+ - spec/spec_helper.rb