summaryrefslogtreecommitdiff
path: root/lib/c_glib/src/thrift/c_glib/protocol/thrift_protocol.c
blob: b73cfb59173412a8b3094ca52892009b24adb266 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

#include <thrift/c_glib/thrift.h>
#include <thrift/c_glib/protocol/thrift_protocol.h>
#include <thrift/c_glib/transport/thrift_transport.h>

/* define the GError domain string */
#define THRIFT_PROTOCOL_ERROR_DOMAIN "thrift-protocol-error-quark"

/* object properties */
enum _ThriftProtocolProperties
{
  PROP_0,
  PROP_THRIFT_PROTOCOL_TRANSPORT
};

G_DEFINE_ABSTRACT_TYPE(ThriftProtocol, thrift_protocol, G_TYPE_OBJECT)

void
thrift_protocol_get_property (GObject *object, guint property_id,
                              GValue *value, GParamSpec *pspec)
{
  ThriftProtocol *protocol = THRIFT_PROTOCOL (object);

  THRIFT_UNUSED_VAR (pspec);

  switch (property_id)
  {
    case PROP_THRIFT_PROTOCOL_TRANSPORT:
      g_value_set_object (value, protocol->transport);
      break;
  }
}

void
thrift_protocol_set_property (GObject *object, guint property_id,
                              const GValue *value, GParamSpec *pspec)
{

  ThriftProtocol *protocol = THRIFT_PROTOCOL (object);

  THRIFT_UNUSED_VAR (pspec);

  switch (property_id)
  {
    case PROP_THRIFT_PROTOCOL_TRANSPORT:
      protocol->transport = g_value_get_object (value);
      break;
  }
}


gint32
thrift_protocol_write_message_begin (ThriftProtocol *protocol, 
                                     const gchar *name, 
                                     const ThriftMessageType message_type,
                                     const gint32 seqid, GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_message_begin
                                                   (protocol, name,
                                                    message_type, seqid,
                                                    error);
}

gint32
thrift_protocol_write_message_end (ThriftProtocol *protocol, GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_message_end (protocol,
                                                                  error);
}

gint32
thrift_protocol_write_struct_begin (ThriftProtocol *protocol, const gchar *name,
                                    GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_struct_begin (protocol,
                                                   name, error);
}

gint32
thrift_protocol_write_struct_end (ThriftProtocol *protocol, GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_struct_end (protocol,
                                                                 error);
}

gint32
thrift_protocol_write_field_begin (ThriftProtocol *protocol,
                                   const gchar *name,
                                   const ThriftType field_type,
                                   const gint16 field_id,
                                   GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_field_begin (protocol,
                                                   name, field_type,
                                                   field_id, error);
}

gint32
thrift_protocol_write_field_end (ThriftProtocol *protocol, GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_field_end (protocol,
                                                                error);
}

gint32
thrift_protocol_write_field_stop (ThriftProtocol *protocol, GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_field_stop (protocol,
                                                                 error);
}

gint32
thrift_protocol_write_map_begin (ThriftProtocol *protocol,
                                 const ThriftType key_type,
                                 const ThriftType value_type,
                                 const guint32 size, GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_map_begin (protocol,
                                                   key_type, value_type,
                                                   size, error);
}

gint32
thrift_protocol_write_map_end (ThriftProtocol *protocol, GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_map_end (protocol,
                                                              error);
}

gint32
thrift_protocol_write_list_begin (ThriftProtocol *protocol,
                                  const ThriftType element_type,
                                  const guint32 size, GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_list_begin (protocol,
                                                   element_type, size,
                                                   error);
}

gint32
thrift_protocol_write_list_end (ThriftProtocol *protocol, GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_list_end (protocol,
                                                               error);
}

gint32
thrift_protocol_write_set_begin (ThriftProtocol *protocol,
                                 const ThriftType element_type,
                                 const guint32 size, GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_set_begin (protocol,
                                                   element_type, size,
                                                   error);
}

gint32
thrift_protocol_write_set_end (ThriftProtocol *protocol, GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_set_end (protocol,
                                                              error);
}

gint32
thrift_protocol_write_bool (ThriftProtocol *protocol,
                            const gboolean value, GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_bool (protocol, value,
                                                           error);
}

gint32
thrift_protocol_write_byte (ThriftProtocol *protocol, const gint8 value,
                            GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_byte (protocol, value,
                                                           error);
}

gint32
thrift_protocol_write_i16 (ThriftProtocol *protocol, const gint16 value,
                           GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_i16 (protocol, value,
                                                          error);
}

gint32
thrift_protocol_write_i32 (ThriftProtocol *protocol, const gint32 value,
                           GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_i32 (protocol, value,
                                                          error);
}

gint32
thrift_protocol_write_i64 (ThriftProtocol *protocol, const gint64 value,
                           GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_i64 (protocol, value,
                                                          error);
}

gint32
thrift_protocol_write_double (ThriftProtocol *protocol,
                              const gdouble value, GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_double (protocol,
                                                             value, error);
}

gint32
thrift_protocol_write_string (ThriftProtocol *protocol,
                              const gchar *str, GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_string (protocol, str,
                                                             error);
}

gint32
thrift_protocol_write_binary (ThriftProtocol *protocol, const gpointer buf,
                              const guint32 len, GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_binary (protocol, buf,
                                                             len, error);
}

gint32 
thrift_protocol_read_message_begin (ThriftProtocol *protocol,
                                    gchar **name,
                                    ThriftMessageType *message_type,
                                    gint32 *seqid, GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_message_begin (protocol,
                                                   name, message_type,
                                                   seqid, error);
}

gint32 
thrift_protocol_read_message_end (ThriftProtocol *protocol,
                                  GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_message_end (protocol,
                                                                 error);
}

gint32 
thrift_protocol_read_struct_begin (ThriftProtocol *protocol,
                                   gchar **name,
                                   GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_struct_begin (protocol,
                                                                  name,
                                                                  error);
}

gint32
thrift_protocol_read_struct_end (ThriftProtocol *protocol, GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_struct_end (protocol,
                                                                error);
}

gint32 
thrift_protocol_read_field_begin (ThriftProtocol *protocol,
                                  gchar **name,
                                  ThriftType *field_type,
                                  gint16 *field_id,
                                  GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_field_begin (protocol,
                                                                 name,
                                                                 field_type,
                                                                 field_id,
                                                                 error);
}

gint32 
thrift_protocol_read_field_end (ThriftProtocol *protocol,
                                GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_field_end (protocol,
                                                               error);
}

gint32 
thrift_protocol_read_map_begin (ThriftProtocol *protocol,
                                ThriftType *key_type,
                                ThriftType *value_type, guint32 *size,
                                GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_map_begin (protocol,
                                                               key_type,
                                                               value_type,
                                                               size,
                                                               error); 
}

gint32 
thrift_protocol_read_map_end (ThriftProtocol *protocol, GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_map_end (protocol,
                                                             error);
}

gint32 
thrift_protocol_read_list_begin (ThriftProtocol *protocol,
                                 ThriftType *element_type,
                                 guint32 *size, GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_list_begin (protocol,
                                                                element_type,
                                                                size, error);
}

gint32
thrift_protocol_read_list_end (ThriftProtocol *protocol, GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_list_end (protocol,
                                                              error);
}

gint32
thrift_protocol_read_set_begin (ThriftProtocol *protocol,
                                ThriftType *element_type,
                                guint32 *size, GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_set_begin (protocol,
                                                               element_type,
                                                               size, error);
}

gint32
thrift_protocol_read_set_end (ThriftProtocol *protocol, GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_set_end (protocol,
                                                             error);
}

gint32
thrift_protocol_read_bool (ThriftProtocol *protocol, gboolean *value,
                           GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_bool (protocol, value,
                                                          error);
}

gint32
thrift_protocol_read_byte (ThriftProtocol *protocol, gint8 *value,
                           GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_byte (protocol, value,
                                                          error);
}

gint32
thrift_protocol_read_i16 (ThriftProtocol *protocol, gint16 *value,
                          GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_i16 (protocol, value,
                                                         error);
}

gint32
thrift_protocol_read_i32 (ThriftProtocol *protocol, gint32 *value,
                          GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_i32 (protocol, value,
                                                         error);
}

gint32
thrift_protocol_read_i64 (ThriftProtocol *protocol, gint64 *value,
                          GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_i64 (protocol, value,
                                                         error);
}

gint32
thrift_protocol_read_double (ThriftProtocol *protocol,
                             gdouble *value, GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_double (protocol, value,
                                                            error);
}

gint32
thrift_protocol_read_string (ThriftProtocol *protocol,
                             gchar **str, GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_string (protocol, str,
                                                            error);
}

gint32
thrift_protocol_read_binary (ThriftProtocol *protocol, gpointer *buf, 
                             guint32 *len, GError **error)
{
  return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_binary (protocol, buf,
                                                            len, error);
}

#define THRIFT_SKIP_RESULT_OR_RETURN(_RES, _CALL) \
  { \
    gint32 _x = (_CALL); \
    if (_x < 0) { return _x; } \
    (_RES) += _x; \
  }

gint32
thrift_protocol_skip (ThriftProtocol *protocol, ThriftType type, GError **error)
{
  switch (type)
  {
    case T_BOOL:
      {
        gboolean boolv;
        return thrift_protocol_read_bool (protocol, &boolv, error);
      }
    case T_BYTE:
      {
        gint8 bytev;
        return thrift_protocol_read_byte (protocol, &bytev, error);
      }

    case T_I16:
      {
        gint16 i16;
        return thrift_protocol_read_i16 (protocol, &i16, error);
      }
    case T_I32:
      {
        gint32 i32;
        return thrift_protocol_read_i32 (protocol, &i32, error);
      }
    case T_I64:
      {
        gint64 i64;
        return thrift_protocol_read_i64 (protocol, &i64, error);
      }
    case T_DOUBLE:
      {
        gdouble dub;
        return thrift_protocol_read_double (protocol, &dub, error);
      }
    case T_STRING:
      {
        gpointer data;
        guint32 len;
        gint32 ret = thrift_protocol_read_binary (protocol, &data, &len, error);
        g_free (data);
        return ret;
      }
    case T_STRUCT:
      {
        guint32 result = 0;
        gchar *name;
        gint16 fid;
        ThriftType ftype;
        THRIFT_SKIP_RESULT_OR_RETURN(result,
          thrift_protocol_read_struct_begin (protocol, &name, error))
        while (1)
        {
          THRIFT_SKIP_RESULT_OR_RETURN(result,
            thrift_protocol_read_field_begin (protocol, &name, &ftype,
                                              &fid, error))
          if (ftype == T_STOP)
          {
            break;
          }
          THRIFT_SKIP_RESULT_OR_RETURN(result,
            thrift_protocol_skip (protocol, ftype, error))
          THRIFT_SKIP_RESULT_OR_RETURN(result,
            thrift_protocol_read_field_end (protocol, error))
        }
        THRIFT_SKIP_RESULT_OR_RETURN(result,
          thrift_protocol_read_struct_end (protocol, error))
        return result;
      }
    case T_MAP:
      {
        gint32 result = 0;
        ThriftType elem_type;
        ThriftType key_type;
        guint32 i, size;
        THRIFT_SKIP_RESULT_OR_RETURN(result,
          thrift_protocol_read_map_begin (protocol, &key_type, &elem_type, &size,
                                          error))
        for (i = 0; i < size; i++)
        {
          THRIFT_SKIP_RESULT_OR_RETURN(result,
            thrift_protocol_skip (protocol, key_type, error))
          THRIFT_SKIP_RESULT_OR_RETURN(result,
            thrift_protocol_skip (protocol, elem_type, error))
        }
        THRIFT_SKIP_RESULT_OR_RETURN(result,
          thrift_protocol_read_map_end (protocol, error))
        return result;
      }
    case T_LIST:
      {
        guint32 result = 0;
        ThriftType elem_type;
        guint32 i, size;
        THRIFT_SKIP_RESULT_OR_RETURN(result,
          thrift_protocol_read_list_begin (protocol, &elem_type, &size,
                                           error))
        for (i = 0; i < size; i++)
        {
          THRIFT_SKIP_RESULT_OR_RETURN(result,
          thrift_protocol_skip (protocol, elem_type, error))
        }
        THRIFT_SKIP_RESULT_OR_RETURN(result,
          thrift_protocol_read_list_end (protocol, error))
        return result;
      }
    default:
      break;
  }

  g_set_error (error, THRIFT_PROTOCOL_ERROR,
               THRIFT_PROTOCOL_ERROR_INVALID_DATA,
               "unrecognized type");
  return -1;
}

/* define the GError domain for Thrift protocols */
GQuark
thrift_protocol_error_quark (void)
{
  return g_quark_from_static_string (THRIFT_PROTOCOL_ERROR_DOMAIN);
}


static void
thrift_protocol_init (ThriftProtocol *protocol)
{
  protocol->transport = NULL;
}

static void
thrift_protocol_class_init (ThriftProtocolClass *cls)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (cls);

  gobject_class->get_property = thrift_protocol_get_property;
  gobject_class->set_property = thrift_protocol_set_property;

  g_object_class_install_property (gobject_class,
      PROP_THRIFT_PROTOCOL_TRANSPORT,
      g_param_spec_object ("transport", "Transport", "Thrift Transport",
                           THRIFT_TYPE_TRANSPORT,
                           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

  cls->write_message_begin = thrift_protocol_write_message_begin;
  cls->write_message_end = thrift_protocol_write_message_end;
  cls->write_struct_begin = thrift_protocol_write_struct_begin;
  cls->write_struct_end = thrift_protocol_write_struct_end;
  cls->write_field_begin = thrift_protocol_write_field_begin;
  cls->write_field_end = thrift_protocol_write_field_end;
  cls->write_field_stop = thrift_protocol_write_field_stop;
  cls->write_map_begin = thrift_protocol_write_map_begin;
  cls->write_map_end = thrift_protocol_write_map_end;
  cls->write_list_begin = thrift_protocol_write_list_begin;
  cls->write_list_end = thrift_protocol_write_list_end;
  cls->write_set_begin = thrift_protocol_write_set_begin;
  cls->write_set_end = thrift_protocol_write_set_end;
  cls->write_bool = thrift_protocol_write_bool;
  cls->write_byte = thrift_protocol_write_byte;
  cls->write_i16 = thrift_protocol_write_i16;
  cls->write_i32 = thrift_protocol_write_i32;
  cls->write_i64 = thrift_protocol_write_i64;
  cls->write_double = thrift_protocol_write_double;
  cls->write_string = thrift_protocol_write_string;
  cls->write_binary = thrift_protocol_write_binary;
  cls->read_message_begin = thrift_protocol_read_message_begin;
  cls->read_message_end = thrift_protocol_read_message_end;
  cls->read_struct_begin = thrift_protocol_read_struct_begin;
  cls->read_struct_end = thrift_protocol_read_struct_end;
  cls->read_field_begin = thrift_protocol_read_field_begin;
  cls->read_field_end = thrift_protocol_read_field_end;
  cls->read_map_begin = thrift_protocol_read_map_begin;
  cls->read_map_end = thrift_protocol_read_map_end;
  cls->read_list_begin = thrift_protocol_read_list_begin;
  cls->read_set_begin = thrift_protocol_read_set_begin;
  cls->read_set_end = thrift_protocol_read_set_end;
  cls->read_bool = thrift_protocol_read_bool;
  cls->read_byte = thrift_protocol_read_byte;
  cls->read_i16 = thrift_protocol_read_i16;
  cls->read_i32 = thrift_protocol_read_i32;
  cls->read_i64 = thrift_protocol_read_i64;
  cls->read_double = thrift_protocol_read_double;
  cls->read_string = thrift_protocol_read_string;
  cls->read_binary = thrift_protocol_read_binary;
}