Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 1 | #include <ruby.h> |
| 2 | #include <protocol.h> |
| 3 | #include <stdbool.h> |
| 4 | #include <constants.h> |
| 5 | #include <struct.h> |
| 6 | |
| 7 | static VALUE skip(VALUE self, int ttype) { |
| 8 | if (ttype == TTYPE_STOP) { |
| 9 | return Qnil; |
| 10 | } else if (ttype == TTYPE_BOOL) { |
| 11 | rb_funcall(self, read_bool_method_id, 0); |
| 12 | } else if (ttype == TTYPE_BYTE) { |
| 13 | rb_funcall(self, read_byte_method_id, 0); |
| 14 | } else if (ttype == TTYPE_I16) { |
| 15 | rb_funcall(self, read_i16_method_id, 0); |
| 16 | } else if (ttype == TTYPE_I32) { |
| 17 | rb_funcall(self, read_i32_method_id, 0); |
| 18 | } else if (ttype == TTYPE_I64) { |
| 19 | rb_funcall(self, read_i64_method_id, 0); |
| 20 | } else if (ttype == TTYPE_DOUBLE) { |
| 21 | rb_funcall(self, read_double_method_id, 0); |
| 22 | } else if (ttype == TTYPE_STRING) { |
| 23 | rb_funcall(self, read_string_method_id, 0); |
| 24 | } else if (ttype == TTYPE_STRUCT) { |
| 25 | rb_funcall(self, read_struct_begin_method_id, 0); |
| 26 | while (true) { |
| 27 | VALUE field_header = rb_funcall(self, read_field_begin_method_id, 0); |
| 28 | if (NIL_P(field_header) || FIX2INT(rb_ary_entry(field_header, 1)) == TTYPE_STOP ) { |
| 29 | break; |
| 30 | } |
| 31 | skip(self, FIX2INT(rb_ary_entry(field_header, 1))); |
| 32 | rb_funcall(self, read_field_end_method_id, 0); |
| 33 | } |
| 34 | rb_funcall(self, read_struct_end_method_id, 0); |
| 35 | } else if (ttype == TTYPE_MAP) { |
| 36 | int i; |
| 37 | VALUE map_header = rb_funcall(self, read_map_begin_method_id, 0); |
| 38 | int ktype = FIX2INT(rb_ary_entry(map_header, 0)); |
| 39 | int vtype = FIX2INT(rb_ary_entry(map_header, 1)); |
| 40 | int size = FIX2INT(rb_ary_entry(map_header, 2)); |
| 41 | |
| 42 | for (i = 0; i < size; i++) { |
| 43 | skip(self, ktype); |
| 44 | skip(self, vtype); |
| 45 | } |
| 46 | rb_funcall(self, read_map_end_method_id, 0); |
| 47 | } else if (ttype == TTYPE_LIST || ttype == TTYPE_SET) { |
| 48 | int i; |
| 49 | VALUE collection_header = rb_funcall(self, ttype == TTYPE_LIST ? read_list_begin_method_id : read_set_begin_method_id, 0); |
| 50 | int etype = FIX2INT(rb_ary_entry(collection_header, 0)); |
| 51 | int size = FIX2INT(rb_ary_entry(collection_header, 1)); |
| 52 | for (i = 0; i < size; i++) { |
| 53 | skip(self, etype); |
| 54 | } |
| 55 | rb_funcall(self, ttype == TTYPE_LIST ? read_list_end_method_id : read_set_end_method_id, 0); |
| 56 | } else { |
| 57 | rb_raise(rb_eNotImpError, "don't know how to skip type %d", ttype); |
| 58 | } |
| 59 | |
| 60 | return Qnil; |
| 61 | } |
| 62 | |
| 63 | VALUE rb_thrift_protocol_native_qmark(VALUE self) { |
| 64 | return Qfalse; |
| 65 | } |
| 66 | |
| 67 | VALUE rb_thrift_protocol_skip(VALUE protocol, VALUE ttype) { |
| 68 | return skip(protocol, FIX2INT(ttype)); |
| 69 | } |
| 70 | |
| 71 | VALUE rb_thrift_write_message_end(VALUE self) { |
| 72 | return Qnil; |
| 73 | } |
| 74 | |
| 75 | VALUE rb_thrift_write_struct_begin(VALUE self, VALUE name) { |
| 76 | return Qnil; |
| 77 | } |
| 78 | |
| 79 | VALUE rb_thrift_write_struct_end(VALUE self) { |
| 80 | return Qnil; |
| 81 | } |
| 82 | |
| 83 | VALUE rb_thrift_write_field_end(VALUE self) { |
| 84 | return Qnil; |
| 85 | } |
| 86 | |
| 87 | VALUE rb_thrift_write_map_end(VALUE self) { |
| 88 | return Qnil; |
| 89 | } |
| 90 | |
| 91 | VALUE rb_thrift_write_list_end(VALUE self) { |
| 92 | return Qnil; |
| 93 | } |
| 94 | |
| 95 | VALUE rb_thrift_write_set_end(VALUE self) { |
| 96 | return Qnil; |
| 97 | } |
| 98 | |
| 99 | VALUE rb_thrift_read_message_end(VALUE self) { |
| 100 | return Qnil; |
| 101 | } |
| 102 | |
| 103 | VALUE rb_thift_read_struct_begin(VALUE self) { |
| 104 | return Qnil; |
| 105 | } |
| 106 | |
| 107 | VALUE rb_thift_read_struct_end(VALUE self) { |
| 108 | return Qnil; |
| 109 | } |
| 110 | |
| 111 | VALUE rb_thift_read_field_end(VALUE self) { |
| 112 | return Qnil; |
| 113 | } |
| 114 | |
| 115 | VALUE rb_thift_read_map_end(VALUE self) { |
| 116 | return Qnil; |
| 117 | } |
| 118 | |
| 119 | VALUE rb_thift_read_list_end(VALUE self) { |
| 120 | return Qnil; |
| 121 | } |
| 122 | |
| 123 | VALUE rb_thift_read_set_end(VALUE self) { |
| 124 | return Qnil; |
| 125 | } |
| 126 | |
| 127 | void Init_protocol() { |
| 128 | VALUE c_protocol = rb_const_get(thrift_module, rb_intern("Protocol")); |
| 129 | |
| 130 | rb_define_method(c_protocol, "skip", rb_thrift_protocol_skip, 1); |
| 131 | rb_define_method(c_protocol, "write_message_end", rb_thrift_write_message_end, 0); |
| 132 | rb_define_method(c_protocol, "write_struct_begin", rb_thrift_write_struct_begin, 1); |
| 133 | rb_define_method(c_protocol, "write_struct_end", rb_thrift_write_struct_end, 0); |
| 134 | rb_define_method(c_protocol, "write_field_end", rb_thrift_write_field_end, 0); |
| 135 | rb_define_method(c_protocol, "write_map_end", rb_thrift_write_map_end, 0); |
| 136 | rb_define_method(c_protocol, "write_list_end", rb_thrift_write_list_end, 0); |
| 137 | rb_define_method(c_protocol, "write_set_end", rb_thrift_write_set_end, 0); |
| 138 | rb_define_method(c_protocol, "read_message_end", rb_thrift_read_message_end, 0); |
| 139 | rb_define_method(c_protocol, "read_struct_begin", rb_thift_read_struct_begin, 0); |
| 140 | rb_define_method(c_protocol, "read_struct_end", rb_thift_read_struct_end, 0); |
| 141 | rb_define_method(c_protocol, "read_field_end", rb_thift_read_field_end, 0); |
| 142 | rb_define_method(c_protocol, "read_map_end", rb_thift_read_map_end, 0); |
| 143 | rb_define_method(c_protocol, "read_list_end", rb_thift_read_list_end, 0); |
| 144 | rb_define_method(c_protocol, "read_set_end", rb_thift_read_set_end, 0); |
| 145 | rb_define_method(c_protocol, "native?", rb_thrift_protocol_native_qmark, 0); |
| 146 | |
| 147 | // native_proto_method_table *npmt; |
| 148 | // npmt = ALLOC(native_proto_method_table); |
| 149 | // npmt->write_message_end = rb_thrift_write_message_end; |
| 150 | // npmt->write_struct_begin = rb_thrift_write_struct_begin; |
| 151 | // npmt->write_struct_end = rb_thrift_write_struct_end; |
| 152 | // npmt->write_field_end = rb_thrift_write_field_end; |
| 153 | // npmt->write_map_end = rb_thrift_write_map_end; |
| 154 | // npmt->write_list_end = rb_thrift_write_list_end; |
| 155 | // npmt->write_set_end = rb_thrift_write_set_end; |
| 156 | // npmt->read_message_end = rb_thrift_read_message_end; |
| 157 | // npmt->read_struct_begin = rb_thift_read_struct_begin; |
| 158 | // npmt->read_struct_end = rb_thift_read_struct_end; |
| 159 | // npmt->read_field_end = rb_thift_read_field_end; |
| 160 | // npmt->read_map_end = rb_thift_read_map_end; |
| 161 | // npmt->read_list_end = rb_thift_read_list_end; |
| 162 | // npmt->read_set_end = rb_thift_read_set_end; |
| 163 | // |
| 164 | // VALUE method_table_object = Data_Wrap_Struct(rb_cObject, 0, free, npmt); |
| 165 | // 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] | 166 | } |