Kevin Clark | 916f353 | 2009-03-20 04:21:39 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 20 | #include <ruby.h> |
| 21 | #include <protocol.h> |
| 22 | #include <stdbool.h> |
| 23 | #include <constants.h> |
| 24 | #include <struct.h> |
| 25 | |
| 26 | static VALUE skip(VALUE self, int ttype) { |
| 27 | if (ttype == TTYPE_STOP) { |
| 28 | return Qnil; |
| 29 | } else if (ttype == TTYPE_BOOL) { |
| 30 | rb_funcall(self, read_bool_method_id, 0); |
| 31 | } else if (ttype == TTYPE_BYTE) { |
| 32 | rb_funcall(self, read_byte_method_id, 0); |
| 33 | } else if (ttype == TTYPE_I16) { |
| 34 | rb_funcall(self, read_i16_method_id, 0); |
| 35 | } else if (ttype == TTYPE_I32) { |
| 36 | rb_funcall(self, read_i32_method_id, 0); |
| 37 | } else if (ttype == TTYPE_I64) { |
| 38 | rb_funcall(self, read_i64_method_id, 0); |
| 39 | } else if (ttype == TTYPE_DOUBLE) { |
| 40 | rb_funcall(self, read_double_method_id, 0); |
| 41 | } else if (ttype == TTYPE_STRING) { |
| 42 | rb_funcall(self, read_string_method_id, 0); |
| 43 | } else if (ttype == TTYPE_STRUCT) { |
| 44 | rb_funcall(self, read_struct_begin_method_id, 0); |
| 45 | while (true) { |
| 46 | VALUE field_header = rb_funcall(self, read_field_begin_method_id, 0); |
| 47 | if (NIL_P(field_header) || FIX2INT(rb_ary_entry(field_header, 1)) == TTYPE_STOP ) { |
| 48 | break; |
| 49 | } |
| 50 | skip(self, FIX2INT(rb_ary_entry(field_header, 1))); |
| 51 | rb_funcall(self, read_field_end_method_id, 0); |
| 52 | } |
| 53 | rb_funcall(self, read_struct_end_method_id, 0); |
| 54 | } else if (ttype == TTYPE_MAP) { |
| 55 | int i; |
| 56 | VALUE map_header = rb_funcall(self, read_map_begin_method_id, 0); |
| 57 | int ktype = FIX2INT(rb_ary_entry(map_header, 0)); |
| 58 | int vtype = FIX2INT(rb_ary_entry(map_header, 1)); |
| 59 | int size = FIX2INT(rb_ary_entry(map_header, 2)); |
| 60 | |
| 61 | for (i = 0; i < size; i++) { |
| 62 | skip(self, ktype); |
| 63 | skip(self, vtype); |
| 64 | } |
| 65 | rb_funcall(self, read_map_end_method_id, 0); |
| 66 | } else if (ttype == TTYPE_LIST || ttype == TTYPE_SET) { |
| 67 | int i; |
| 68 | VALUE collection_header = rb_funcall(self, ttype == TTYPE_LIST ? read_list_begin_method_id : read_set_begin_method_id, 0); |
| 69 | int etype = FIX2INT(rb_ary_entry(collection_header, 0)); |
| 70 | int size = FIX2INT(rb_ary_entry(collection_header, 1)); |
| 71 | for (i = 0; i < size; i++) { |
| 72 | skip(self, etype); |
| 73 | } |
| 74 | rb_funcall(self, ttype == TTYPE_LIST ? read_list_end_method_id : read_set_end_method_id, 0); |
| 75 | } else { |
| 76 | rb_raise(rb_eNotImpError, "don't know how to skip type %d", ttype); |
| 77 | } |
| 78 | |
| 79 | return Qnil; |
| 80 | } |
| 81 | |
| 82 | VALUE rb_thrift_protocol_native_qmark(VALUE self) { |
| 83 | return Qfalse; |
| 84 | } |
| 85 | |
| 86 | VALUE rb_thrift_protocol_skip(VALUE protocol, VALUE ttype) { |
| 87 | return skip(protocol, FIX2INT(ttype)); |
| 88 | } |
| 89 | |
| 90 | VALUE rb_thrift_write_message_end(VALUE self) { |
| 91 | return Qnil; |
| 92 | } |
| 93 | |
| 94 | VALUE rb_thrift_write_struct_begin(VALUE self, VALUE name) { |
| 95 | return Qnil; |
| 96 | } |
| 97 | |
| 98 | VALUE rb_thrift_write_struct_end(VALUE self) { |
| 99 | return Qnil; |
| 100 | } |
| 101 | |
| 102 | VALUE rb_thrift_write_field_end(VALUE self) { |
| 103 | return Qnil; |
| 104 | } |
| 105 | |
| 106 | VALUE rb_thrift_write_map_end(VALUE self) { |
| 107 | return Qnil; |
| 108 | } |
| 109 | |
| 110 | VALUE rb_thrift_write_list_end(VALUE self) { |
| 111 | return Qnil; |
| 112 | } |
| 113 | |
| 114 | VALUE rb_thrift_write_set_end(VALUE self) { |
| 115 | return Qnil; |
| 116 | } |
| 117 | |
| 118 | VALUE rb_thrift_read_message_end(VALUE self) { |
| 119 | return Qnil; |
| 120 | } |
| 121 | |
| 122 | VALUE rb_thift_read_struct_begin(VALUE self) { |
| 123 | return Qnil; |
| 124 | } |
| 125 | |
| 126 | VALUE rb_thift_read_struct_end(VALUE self) { |
| 127 | return Qnil; |
| 128 | } |
| 129 | |
| 130 | VALUE rb_thift_read_field_end(VALUE self) { |
| 131 | return Qnil; |
| 132 | } |
| 133 | |
| 134 | VALUE rb_thift_read_map_end(VALUE self) { |
| 135 | return Qnil; |
| 136 | } |
| 137 | |
| 138 | VALUE rb_thift_read_list_end(VALUE self) { |
| 139 | return Qnil; |
| 140 | } |
| 141 | |
| 142 | VALUE rb_thift_read_set_end(VALUE self) { |
| 143 | return Qnil; |
| 144 | } |
| 145 | |
| 146 | void Init_protocol() { |
Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame] | 147 | VALUE c_protocol = rb_const_get(thrift_module, rb_intern("BaseProtocol")); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 148 | |
| 149 | rb_define_method(c_protocol, "skip", rb_thrift_protocol_skip, 1); |
| 150 | rb_define_method(c_protocol, "write_message_end", rb_thrift_write_message_end, 0); |
| 151 | rb_define_method(c_protocol, "write_struct_begin", rb_thrift_write_struct_begin, 1); |
| 152 | rb_define_method(c_protocol, "write_struct_end", rb_thrift_write_struct_end, 0); |
| 153 | rb_define_method(c_protocol, "write_field_end", rb_thrift_write_field_end, 0); |
| 154 | rb_define_method(c_protocol, "write_map_end", rb_thrift_write_map_end, 0); |
| 155 | rb_define_method(c_protocol, "write_list_end", rb_thrift_write_list_end, 0); |
| 156 | rb_define_method(c_protocol, "write_set_end", rb_thrift_write_set_end, 0); |
| 157 | rb_define_method(c_protocol, "read_message_end", rb_thrift_read_message_end, 0); |
| 158 | rb_define_method(c_protocol, "read_struct_begin", rb_thift_read_struct_begin, 0); |
| 159 | rb_define_method(c_protocol, "read_struct_end", rb_thift_read_struct_end, 0); |
| 160 | rb_define_method(c_protocol, "read_field_end", rb_thift_read_field_end, 0); |
| 161 | rb_define_method(c_protocol, "read_map_end", rb_thift_read_map_end, 0); |
| 162 | rb_define_method(c_protocol, "read_list_end", rb_thift_read_list_end, 0); |
| 163 | rb_define_method(c_protocol, "read_set_end", rb_thift_read_set_end, 0); |
| 164 | rb_define_method(c_protocol, "native?", rb_thrift_protocol_native_qmark, 0); |
| 165 | |
| 166 | // native_proto_method_table *npmt; |
| 167 | // npmt = ALLOC(native_proto_method_table); |
| 168 | // npmt->write_message_end = rb_thrift_write_message_end; |
| 169 | // npmt->write_struct_begin = rb_thrift_write_struct_begin; |
| 170 | // npmt->write_struct_end = rb_thrift_write_struct_end; |
| 171 | // npmt->write_field_end = rb_thrift_write_field_end; |
| 172 | // npmt->write_map_end = rb_thrift_write_map_end; |
| 173 | // npmt->write_list_end = rb_thrift_write_list_end; |
| 174 | // npmt->write_set_end = rb_thrift_write_set_end; |
| 175 | // npmt->read_message_end = rb_thrift_read_message_end; |
| 176 | // npmt->read_struct_begin = rb_thift_read_struct_begin; |
| 177 | // npmt->read_struct_end = rb_thift_read_struct_end; |
| 178 | // npmt->read_field_end = rb_thift_read_field_end; |
| 179 | // npmt->read_map_end = rb_thift_read_map_end; |
| 180 | // npmt->read_list_end = rb_thift_read_list_end; |
| 181 | // npmt->read_set_end = rb_thift_read_set_end; |
| 182 | // |
| 183 | // VALUE method_table_object = Data_Wrap_Struct(rb_cObject, 0, free, npmt); |
| 184 | // rb_const_set(c_protocol, rb_intern("@native_method_table"), method_table_object); |
Bryan Duxbury | 1e80d44 | 2009-02-03 18:16:54 +0000 | [diff] [blame] | 185 | } |