]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
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 | ||
20 | #include <thrift/c_glib/thrift.h> | |
21 | #include <thrift/c_glib/thrift_application_exception.h> | |
22 | #include <thrift/c_glib/processor/thrift_dispatch_processor.h> | |
23 | ||
24 | G_DEFINE_ABSTRACT_TYPE (ThriftDispatchProcessor, | |
25 | thrift_dispatch_processor, | |
26 | THRIFT_TYPE_PROCESSOR) | |
27 | ||
28 | gboolean | |
29 | thrift_dispatch_processor_process (ThriftProcessor *processor, | |
30 | ThriftProtocol *in, | |
31 | ThriftProtocol *out, | |
32 | GError **error) | |
33 | { | |
34 | gchar *fname; | |
35 | ThriftMessageType mtype; | |
36 | gint32 seqid; | |
37 | ThriftDispatchProcessor *dispatch_processor = | |
38 | THRIFT_DISPATCH_PROCESSOR (processor); | |
39 | ||
40 | /* Read the start of the message, which we expect to be a method call */ | |
41 | if (thrift_protocol_read_message_begin (in, | |
42 | &fname, | |
43 | &mtype, | |
44 | &seqid, | |
45 | error) < 0) { | |
46 | g_warning ("error reading start of message: %s", | |
47 | (error != NULL) ? (*error)->message : "(null)"); | |
48 | return FALSE; | |
49 | } | |
50 | else if (mtype != T_CALL && mtype != T_ONEWAY) { | |
51 | g_warning ("received invalid message type %d from client", mtype); | |
52 | return FALSE; | |
53 | } | |
54 | ||
55 | /* Dispatch the method call */ | |
56 | return THRIFT_DISPATCH_PROCESSOR_GET_CLASS (dispatch_processor) | |
57 | ->dispatch_call (dispatch_processor, | |
58 | in, | |
59 | out, | |
60 | fname, | |
61 | seqid, | |
62 | error); | |
63 | } | |
64 | ||
65 | static gboolean | |
66 | thrift_dispatch_processor_real_dispatch_call (ThriftDispatchProcessor *self, | |
67 | ThriftProtocol *in, | |
68 | ThriftProtocol *out, | |
69 | gchar *fname, | |
70 | gint32 seqid, | |
71 | GError **error) | |
72 | { | |
73 | ThriftTransport *transport; | |
74 | ThriftApplicationException *xception; | |
75 | gchar *message; | |
76 | gint32 result; | |
77 | gboolean dispatch_result = FALSE; | |
78 | ||
79 | THRIFT_UNUSED_VAR (self); | |
80 | ||
81 | /* By default, return an application exception to the client indicating the | |
82 | method name is not recognized. */ | |
83 | ||
84 | if ((thrift_protocol_skip (in, T_STRUCT, error) < 0) || | |
85 | (thrift_protocol_read_message_end (in, error) < 0)) | |
86 | return FALSE; | |
87 | ||
88 | g_object_get (in, "transport", &transport, NULL); | |
89 | result = thrift_transport_read_end (transport, error); | |
90 | g_object_unref (transport); | |
91 | if (result < 0) | |
92 | return FALSE; | |
93 | ||
94 | if (thrift_protocol_write_message_begin (out, | |
95 | fname, | |
96 | T_EXCEPTION, | |
97 | seqid, | |
98 | error) < 0) | |
99 | return FALSE; | |
100 | message = g_strconcat ("Invalid method name: '", fname, "'", NULL); | |
101 | g_free (fname); | |
102 | xception = | |
103 | g_object_new (THRIFT_TYPE_APPLICATION_EXCEPTION, | |
104 | "type", THRIFT_APPLICATION_EXCEPTION_ERROR_UNKNOWN_METHOD, | |
105 | "message", message, | |
106 | NULL); | |
107 | g_free (message); | |
108 | result = thrift_struct_write (THRIFT_STRUCT (xception), | |
109 | out, | |
110 | error); | |
111 | g_object_unref (xception); | |
112 | if ((result < 0) || | |
113 | (thrift_protocol_write_message_end (out, error) < 0)) | |
114 | return FALSE; | |
115 | ||
116 | g_object_get (out, "transport", &transport, NULL); | |
117 | dispatch_result = | |
118 | ((thrift_transport_write_end (transport, error) >= 0) && | |
119 | (thrift_transport_flush (transport, error) >= 0)); | |
120 | g_object_unref (transport); | |
121 | ||
122 | return dispatch_result; | |
123 | } | |
124 | ||
125 | static void | |
126 | thrift_dispatch_processor_init (ThriftDispatchProcessor *self) | |
127 | { | |
128 | THRIFT_UNUSED_VAR (self); | |
129 | } | |
130 | ||
131 | static void | |
132 | thrift_dispatch_processor_class_init (ThriftDispatchProcessorClass *klass) | |
133 | { | |
134 | ThriftProcessorClass *processor_class = | |
135 | THRIFT_PROCESSOR_CLASS (klass); | |
136 | ||
137 | /* Implement ThriftProcessor's process method */ | |
138 | processor_class->process = thrift_dispatch_processor_process; | |
139 | ||
140 | /* Provide a default implement for dispatch_call, which returns an exception | |
141 | to the client indicating the method name was not recognized */ | |
142 | klass->dispatch_call = thrift_dispatch_processor_real_dispatch_call; | |
143 | } |