You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by ro...@apache.org on 2010/10/27 14:30:13 UTC
svn commit: r1027933 [4/6] - in /incubator/thrift/trunk: ./ compiler/cpp/
compiler/cpp/src/generate/ compiler/cpp/src/parse/ lib/ lib/c_glib/
lib/c_glib/src/ lib/c_glib/src/processor/ lib/c_glib/src/protocol/
lib/c_glib/src/server/ lib/c_glib/src/trans...
Added: incubator/thrift/trunk/lib/c_glib/src/protocol/thrift_protocol.h
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/c_glib/src/protocol/thrift_protocol.h?rev=1027933&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/c_glib/src/protocol/thrift_protocol.h (added)
+++ incubator/thrift/trunk/lib/c_glib/src/protocol/thrift_protocol.h Wed Oct 27 12:30:11 2010
@@ -0,0 +1,324 @@
+#ifndef _THRIFT_PROTOCOL_H
+#define _THRIFT_PROTOCOL_H
+
+#include <glib-object.h>
+
+#include "transport/thrift_transport.h"
+
+/**
+ * Enumerated definition of the types that the Thrift protocol supports.
+ * Take special note of the T_END type which is used specifically to mark
+ * the end of a sequence of fields.
+ */
+enum _ThriftType {
+ T_STOP = 0,
+ T_VOID = 1,
+ T_BOOL = 2,
+ T_BYTE = 3,
+ T_I08 = 3,
+ T_I16 = 6,
+ T_I32 = 8,
+ T_U64 = 9,
+ T_I64 = 10,
+ T_DOUBLE = 4,
+ T_STRING = 11,
+ T_UTF7 = 11,
+ T_STRUCT = 12,
+ T_MAP = 13,
+ T_SET = 14,
+ T_LIST = 15,
+ T_UTF8 = 16,
+ T_UTF16 = 17
+};
+typedef enum _ThriftType ThriftType;
+
+/**
+ * Enumerated definition of the message types that the Thrift protocol
+ * supports.
+ */
+enum _ThriftMessageType {
+ T_CALL = 1,
+ T_REPLY = 2,
+ T_EXCEPTION = 3,
+ T_ONEWAY = 4
+};
+typedef enum _ThriftMessageType ThriftMessageType;
+
+/*! \file thrift_protocol.h
+ * \brief Abstract class for Thrift protocol implementations.
+ */
+
+/* type macros */
+#define THRIFT_TYPE_PROTOCOL (thrift_protocol_get_type ())
+#define THRIFT_PROTOCOL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
+ THRIFT_TYPE_PROTOCOL, ThriftProtocol))
+#define THRIFT_IS_PROTOCOL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+ THRIFT_TYPE_PROTOCOL))
+#define THRIFT_PROTOCOL_CLASS(c) (G_TYPE_CHECK_CLASS_CAST ((c), \
+ THRIFT_TYPE_PROTOCOL, \
+ ThriftProtocolClass))
+#define THRIFT_IS_PROTOCOL_CLASS(c) (G_TYPE_CHECK_CLASS_TYPE ((c), \
+ THRIFT_TYPE_PROTOCOL))
+#define THRIFT_PROTOCOL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), \
+ THRIFT_TYPE_PROTOCOL, \
+ ThriftProtocolClass))
+
+/*!
+ * Thrift Protocol object
+ */
+struct _ThriftProtocol
+{
+ GObject parent;
+
+ /* protected */
+ ThriftTransport *transport;
+};
+typedef struct _ThriftProtocol ThriftProtocol;
+
+/*!
+ * Thrift Protocol class
+ */
+struct _ThriftProtocolClass
+{
+ GObjectClass parent;
+
+ gint32 (*write_message_begin) (ThriftProtocol *protocol, const gchar *name,
+ const ThriftMessageType message_type,
+ const gint32 seqid, GError **error);
+ gint32 (*write_message_end) (ThriftProtocol *protocol, GError **error);
+ gint32 (*write_struct_begin) (ThriftProtocol *protocol, const gchar *name,
+ GError **error);
+ gint32 (*write_struct_end) (ThriftProtocol *protocol, GError **error);
+ gint32 (*write_field_begin) (ThriftProtocol *protocol, const gchar *name,
+ const ThriftType field_type,
+ const gint16 field_id, GError **error);
+ gint32 (*write_field_end) (ThriftProtocol *protocol, GError **error);
+ gint32 (*write_field_stop) (ThriftProtocol *protocol, GError **error);
+ gint32 (*write_map_begin) (ThriftProtocol *protocol,
+ const ThriftType key_type,
+ const ThriftType value_type,
+ const guint32 size, GError **error);
+ gint32 (*write_map_end) (ThriftProtocol *protocol, GError **error);
+ gint32 (*write_list_begin) (ThriftProtocol *protocol,
+ const ThriftType element_type,
+ const guint32 size, GError **error);
+ gint32 (*write_list_end) (ThriftProtocol *protocol, GError **error);
+ gint32 (*write_set_begin) (ThriftProtocol *protocol,
+ const ThriftType element_type,
+ const guint32 size, GError **error);
+ gint32 (*write_set_end) (ThriftProtocol *protocol, GError **error);
+ gint32 (*write_bool) (ThriftProtocol *protocol, const gboolean value,
+ GError **error);
+ gint32 (*write_byte) (ThriftProtocol *protocol, const gint8 value,
+ GError **error);
+ gint32 (*write_i16) (ThriftProtocol *protocol, const gint16 value,
+ GError **error);
+ gint32 (*write_i32) (ThriftProtocol *protocol, const gint32 value,
+ GError **error);
+ gint32 (*write_i64) (ThriftProtocol *protocol, const gint64 value,
+ GError **error);
+ gint32 (*write_double) (ThriftProtocol *protocol, const gdouble value,
+ GError **error);
+ gint32 (*write_string) (ThriftProtocol *protocol, const gchar *str,
+ GError **error);
+ gint32 (*write_binary) (ThriftProtocol *protocol, const gpointer buf,
+ const guint32 len, GError **error);
+
+ gint32 (*read_message_begin) (ThriftProtocol *thrift_protocol, gchar **name,
+ ThriftMessageType *message_type,
+ gint32 *seqid, GError **error);
+ gint32 (*read_message_end) (ThriftProtocol *protocol, GError **error);
+ gint32 (*read_struct_begin) (ThriftProtocol *protocol, gchar **name,
+ GError **error);
+ gint32 (*read_struct_end) (ThriftProtocol *protocol, GError **error);
+ gint32 (*read_field_begin) (ThriftProtocol *protocol, gchar **name,
+ ThriftType *field_type, gint16 *field_id,
+ GError **error);
+ gint32 (*read_field_end) (ThriftProtocol *protocol, GError **error);
+ gint32 (*read_map_begin) (ThriftProtocol *protocol, ThriftType *key_type,
+ ThriftType *value_type, guint32 *size,
+ GError **error);
+ gint32 (*read_map_end) (ThriftProtocol *protocol, GError **error);
+ gint32 (*read_list_begin) (ThriftProtocol *protocol, ThriftType *element_type,
+ guint32 *size, GError **error);
+ gint32 (*read_list_end) (ThriftProtocol *protocol, GError **error);
+ gint32 (*read_set_begin) (ThriftProtocol *protocol, ThriftType *element_type,
+ guint32 *size, GError **error);
+ gint32 (*read_set_end) (ThriftProtocol *protocol, GError **error);
+ gint32 (*read_bool) (ThriftProtocol *protocol, gboolean *value,
+ GError **error);
+ gint32 (*read_byte) (ThriftProtocol *protocol, gint8 *value, GError **error);
+ gint32 (*read_i16) (ThriftProtocol *protocol, gint16 *value, GError **error);
+ gint32 (*read_i32) (ThriftProtocol *protocol, gint32 *value, GError **error);
+ gint32 (*read_i64) (ThriftProtocol *protocol, gint64 *value, GError **error);
+ gint32 (*read_double) (ThriftProtocol *protocol, gdouble *value,
+ GError **error);
+ gint32 (*read_string) (ThriftProtocol *protocol, gchar **str, GError **error);
+ gint32 (*read_binary) (ThriftProtocol *protocol, gpointer *buf,
+ guint32 *len, GError **error);
+};
+typedef struct _ThriftProtocolClass ThriftProtocolClass;
+
+/* used by THRIFT_TYPE_PROTOCOL */
+GType thrift_protocol_get_type (void);
+
+/* virtual public methods */
+gint32 thrift_protocol_write_message_begin (ThriftProtocol *protocol,
+ const gchar *name, const ThriftMessageType message_type,
+ const gint32 seqid, GError **error);
+
+gint32 thrift_protocol_write_message_end (ThriftProtocol *protocol,
+ GError **error);
+
+gint32 thrift_protocol_write_struct_begin (ThriftProtocol *protocol,
+ const gchar *name,
+ GError **error);
+
+gint32 thrift_protocol_write_struct_end (ThriftProtocol *protocol,
+ GError **error);
+
+gint32 thrift_protocol_write_field_begin (ThriftProtocol *protocol,
+ const gchar *name,
+ const ThriftType field_type,
+ const gint16 field_id,
+ GError **error);
+
+gint32 thrift_protocol_write_field_end (ThriftProtocol *protocol,
+ GError **error);
+
+gint32 thrift_protocol_write_field_stop (ThriftProtocol *protocol,
+ GError **error);
+
+gint32 thrift_protocol_write_map_begin (ThriftProtocol *protocol,
+ const ThriftType key_type,
+ const ThriftType value_type,
+ const guint32 size, GError **error);
+
+gint32 thrift_protocol_write_map_end (ThriftProtocol *protocol, GError **error);
+
+gint32 thrift_protocol_write_list_begin (ThriftProtocol *protocol,
+ const ThriftType element_type,
+ const guint32 size, GError **error);
+
+gint32 thrift_protocol_write_list_end (ThriftProtocol *protocol,
+ GError **error);
+
+gint32 thrift_protocol_write_set_begin (ThriftProtocol *protocol,
+ const ThriftType element_type,
+ const guint32 size, GError **error);
+
+gint32 thrift_protocol_write_set_end (ThriftProtocol *protocol,
+ GError **error);
+
+gint32 thrift_protocol_write_bool (ThriftProtocol *protocol,
+ const gboolean value, GError **error);
+
+gint32 thrift_protocol_write_byte (ThriftProtocol *protocol, const gint8 value,
+ GError **error);
+
+gint32 thrift_protocol_write_i16 (ThriftProtocol *protocol, const gint16 value,
+ GError **error);
+
+gint32 thrift_protocol_write_i32 (ThriftProtocol *protocol, const gint32 value,
+ GError **error);
+
+gint32 thrift_protocol_write_i64 (ThriftProtocol *protocol, const gint64 value,
+ GError **error);
+
+gint32 thrift_protocol_write_double (ThriftProtocol *protocol,
+ const gdouble value, GError **error);
+
+gint32 thrift_protocol_write_string (ThriftProtocol *protocol,
+ const gchar *str, GError **error);
+
+gint32 thrift_protocol_write_binary (ThriftProtocol *protocol,
+ const gpointer buf,
+ const guint32 len, GError **error);
+
+gint32 thrift_protocol_read_message_begin (ThriftProtocol *thrift_protocol,
+ gchar **name,
+ ThriftMessageType *message_type,
+ gint32 *seqid, GError **error);
+
+gint32 thrift_protocol_read_message_end (ThriftProtocol *protocol,
+ GError **error);
+
+gint32 thrift_protocol_read_struct_begin (ThriftProtocol *protocol,
+ gchar **name,
+ GError **error);
+
+gint32 thrift_protocol_read_struct_end (ThriftProtocol *protocol,
+ GError **error);
+
+gint32 thrift_protocol_read_field_begin (ThriftProtocol *protocol,
+ gchar **name,
+ ThriftType *field_type,
+ gint16 *field_id,
+ GError **error);
+
+gint32 thrift_protocol_read_field_end (ThriftProtocol *protocol,
+ GError **error);
+
+gint32 thrift_protocol_read_map_begin (ThriftProtocol *protocol,
+ ThriftType *key_type,
+ ThriftType *value_type, guint32 *size,
+ GError **error);
+
+gint32 thrift_protocol_read_map_end (ThriftProtocol *protocol, GError **error);
+
+gint32 thrift_protocol_read_list_begin (ThriftProtocol *protocol,
+ ThriftType *element_type,
+ guint32 *size, GError **error);
+
+gint32 thrift_protocol_read_list_end (ThriftProtocol *protocol, GError **error);
+
+gint32 thrift_protocol_read_set_begin (ThriftProtocol *protocol,
+ ThriftType *element_type,
+ guint32 *size, GError **error);
+
+gint32 thrift_protocol_read_set_end (ThriftProtocol *protocol, GError **error);
+
+gint32 thrift_protocol_read_bool (ThriftProtocol *protocol, gboolean *value,
+ GError **error);
+
+gint32 thrift_protocol_read_byte (ThriftProtocol *protocol, gint8 *value,
+ GError **error);
+
+gint32 thrift_protocol_read_i16 (ThriftProtocol *protocol, gint16 *value,
+ GError **error);
+
+gint32 thrift_protocol_read_i32 (ThriftProtocol *protocol, gint32 *value,
+ GError **error);
+
+gint32 thrift_protocol_read_i64 (ThriftProtocol *protocol, gint64 *value,
+ GError **error);
+
+gint32 thrift_protocol_read_double (ThriftProtocol *protocol,
+ gdouble *value, GError **error);
+
+gint32 thrift_protocol_read_string (ThriftProtocol *protocol,
+ gchar **str, GError **error);
+
+gint32 thrift_protocol_read_binary (ThriftProtocol *protocol,
+ gpointer *buf, guint32 *len,
+ GError **error);
+
+gint32 thrift_protocol_skip (ThriftProtocol *protocol, ThriftType type,
+ GError **error);
+
+/* define error types */
+typedef enum
+{
+ THRIFT_PROTOCOL_ERROR_UNKNOWN,
+ THRIFT_PROTOCOL_ERROR_INVALID_DATA,
+ THRIFT_PROTOCOL_ERROR_NEGATIVE_SIZE,
+ THRIFT_PROTOCOL_ERROR_SIZE_LIMIT,
+ THRIFT_PROTOCOL_ERROR_BAD_VERSION,
+ THRIFT_PROTOCOL_ERROR_NOT_IMPLEMENTED
+} ThriftProtocolError;
+
+/* define an error domain for GError to use */
+GQuark thrift_protocol_error_quark (void);
+#define THRIFT_PROTOCOL_ERROR (thrift_protocol_error_quark ())
+
+#endif /* _THRIFT_PROTOCOL_H */
Added: incubator/thrift/trunk/lib/c_glib/src/protocol/thrift_protocol_factory.c
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/c_glib/src/protocol/thrift_protocol_factory.c?rev=1027933&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/c_glib/src/protocol/thrift_protocol_factory.c (added)
+++ incubator/thrift/trunk/lib/c_glib/src/protocol/thrift_protocol_factory.c Wed Oct 27 12:30:11 2010
@@ -0,0 +1,50 @@
+#include "thrift.h"
+#include "protocol/thrift_protocol_factory.h"
+
+/* forward declarations */
+static void thrift_protocol_factory_class_init (ThriftProtocolFactoryClass *cls);
+ThriftProtocol *thrift_protocol_factory_get_protocol(ThriftProtocolFactory *factory, ThriftTransport *transport);
+
+
+/* define ThriftProtocolFactoryInterface's type */
+GType
+thrift_protocol_factory_get_type (void)
+{
+ static GType type = 0;
+
+ if (type == 0)
+ {
+ static const GTypeInfo info = {
+ sizeof (ThriftProtocolFactoryClass),
+ NULL, /* base_init */
+ NULL, /* base_finalize */
+ (GClassInitFunc) thrift_protocol_factory_class_init,
+ NULL, /* class_finalize */
+ NULL, /* class_data */
+ sizeof (ThriftProtocolFactory),
+ 0, /* n_preallocs */
+ NULL, /* instance_init */
+ NULL, /* value_table */
+ };
+
+ type = g_type_register_static (G_TYPE_OBJECT, "ThriftProtocolFactory",
+ &info, G_TYPE_FLAG_ABSTRACT);
+ }
+
+ return type;
+}
+
+static void
+thrift_protocol_factory_class_init (ThriftProtocolFactoryClass *cls)
+{
+ cls->get_protocol = thrift_protocol_factory_get_protocol;
+}
+
+ThriftProtocol *
+thrift_protocol_factory_get_protocol(ThriftProtocolFactory *factory,
+ ThriftTransport *transport)
+{
+ return THRIFT_PROTOCOL_FACTORY_GET_CLASS (factory)->get_protocol (factory,
+ transport);
+}
+
Added: incubator/thrift/trunk/lib/c_glib/src/protocol/thrift_protocol_factory.h
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/c_glib/src/protocol/thrift_protocol_factory.h?rev=1027933&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/c_glib/src/protocol/thrift_protocol_factory.h (added)
+++ incubator/thrift/trunk/lib/c_glib/src/protocol/thrift_protocol_factory.h Wed Oct 27 12:30:11 2010
@@ -0,0 +1,57 @@
+#ifndef _THRIFT_PROTOCOL_FACTORY_H
+#define _THRIFT_PROTOCOL_FACTORY_H
+
+#include <glib-object.h>
+
+#include "transport/thrift_transport.h"
+#include "protocol/thrift_protocol.h"
+
+/*! \file thrift_protocol_factory.h
+ * \brief Abstract class for Thrift protocol factory implementations.
+ */
+
+/* type macros */
+#define THRIFT_TYPE_PROTOCOL_FACTORY (thrift_protocol_factory_get_type ())
+#define THRIFT_PROTOCOL_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
+ THRIFT_TYPE_PROTOCOL_FACTORY, \
+ ThriftProtocolFactory))
+#define THRIFT_IS_PROTOCOL_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+ THRIFT_TYPE_PROTOCOL_FACTORY))
+#define THRIFT_PROTOCOL_FACTORY_CLASS(c) (G_TYPE_CHECK_CLASS_CAST ((c), \
+ THRIFT_TYPE_PROTOCOL_FACTORY, \
+ ThriftProtocolFactoryClass))
+#define THRIFT_IS_PROTOCOL_FACTORY_CLASS(c) (G_TYPE_CHECK_CLASS_TYPE ((c), \
+ THRIFT_TYPE_PROTOCOL_FACTORY))
+#define THRIFT_PROTOCOL_FACTORY_GET_CLASS(obj) \
+ (G_TYPE_INSTANCE_GET_CLASS ((obj), \
+ THRIFT_TYPE_PROTOCOL_FACTORY, \
+ ThriftProtocolFactoryClass))
+
+/*!
+ * Thrift Protocol Factory object
+ */
+struct _ThriftProtocolFactory
+{
+ GObject parent;
+};
+typedef struct _ThriftProtocolFactory ThriftProtocolFactory;
+
+/*!
+ * Thrift Protocol Factory class
+ */
+struct _ThriftProtocolFactoryClass
+{
+ GObjectClass parent;
+
+ ThriftProtocol *(*get_protocol) (ThriftProtocolFactory *factory,
+ ThriftTransport *transport);
+};
+typedef struct _ThriftProtocolFactoryClass ThriftProtocolFactoryClass;
+
+/* used by THRIFT_TYPE_PROTOCOL_FACTORY */
+GType thrift_protocol_factory_get_type (void);
+
+/* virtual public methods */
+ThriftProtocol *thrift_protocol_factory_get_protocol(ThriftProtocolFactory *factory, ThriftTransport *transport);
+
+#endif /* _THRIFT_PROTOCOL_FACTORY_H */
Added: incubator/thrift/trunk/lib/c_glib/src/server/thrift_server.c
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/c_glib/src/server/thrift_server.c?rev=1027933&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/c_glib/src/server/thrift_server.c (added)
+++ incubator/thrift/trunk/lib/c_glib/src/server/thrift_server.c Wed Oct 27 12:30:11 2010
@@ -0,0 +1,192 @@
+#include "thrift.h"
+#include "thrift_server.h"
+
+/* object properties */
+enum _ThriftServerProperties
+{
+ PROP_0,
+ PROP_THRIFT_SERVER_PROCESSOR,
+ PROP_THRIFT_SERVER_SERVER_TRANSPORT,
+ PROP_THRIFT_SERVER_INPUT_TRANSPORT_FACTORY,
+ PROP_THRIFT_SERVER_OUTPUT_TRANSPORT_FACTORY,
+ PROP_THRIFT_SERVER_INPUT_PROTOCOL_FACTORY,
+ PROP_THRIFT_SERVER_OUTPUT_PROTOCOL_FACTORY
+};
+
+/* forward declarations */
+static void thrift_server_instance_init (ThriftServer *server);
+static void thrift_server_class_init (ThriftServerClass *cls);
+void thrift_server_get_property (GObject *object, guint property_id,
+ GValue *value, GParamSpec *pspec);
+void thrift_server_set_property (GObject *object, guint property_id,
+ const GValue *value, GParamSpec *pspec);
+
+
+/* define ThriftServerClass's type */
+GType
+thrift_server_get_type (void)
+{
+ static GType type = 0;
+
+ if (type == 0)
+ {
+ static const GTypeInfo info =
+ {
+ sizeof (ThriftServerClass),
+ NULL, /* base_init */
+ NULL, /* base_finalize */
+ (GClassInitFunc) thrift_server_class_init,
+ NULL, /* class_finalize */
+ NULL, /* class_data */
+ sizeof (ThriftServer),
+ 0, /* n_preallocs */
+ (GInstanceInitFunc) thrift_server_instance_init,
+ NULL, /* value_table */
+ };
+
+ type = g_type_register_static (G_TYPE_OBJECT, "ThriftServer",
+ &info, G_TYPE_FLAG_ABSTRACT);
+ }
+
+ return type;
+}
+
+/* instance initializer for Thrift Server */
+static void
+thrift_server_instance_init (ThriftServer *server)
+{
+ server->processor = NULL;
+ server->server_transport = NULL;
+ server->input_transport_factory = NULL;
+ server->output_transport_factory = NULL;
+ server->input_protocol_factory = NULL;
+ server->output_protocol_factory = NULL;
+}
+
+/* class initializer for ThriftServer
+ * TODO: implement ServerEventHandler as a GClosure
+ */
+static void
+thrift_server_class_init (ThriftServerClass *cls)
+{
+ GObjectClass *gobject_class = G_OBJECT_CLASS (cls);
+
+ gobject_class->get_property = thrift_server_get_property;
+ gobject_class->set_property = thrift_server_set_property;
+
+ g_object_class_install_property (gobject_class,
+ PROP_THRIFT_SERVER_PROCESSOR,
+ g_param_spec_object ("processor", "Processor", "Thrift Processor",
+ THRIFT_TYPE_PROCESSOR,
+ G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
+ g_object_class_install_property (gobject_class,
+ PROP_THRIFT_SERVER_SERVER_TRANSPORT,
+ g_param_spec_object ("server_transport", "Server Transport",
+ "Thrift Server Transport",
+ THRIFT_TYPE_SERVER_TRANSPORT,
+ G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
+ g_object_class_install_property (gobject_class,
+ PROP_THRIFT_SERVER_INPUT_TRANSPORT_FACTORY,
+ g_param_spec_object ("input_transport_factory", "Input Transport Factory",
+ "Thrift Server Input Transport Factory",
+ THRIFT_TYPE_TRANSPORT_FACTORY,
+ G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
+ g_object_class_install_property (gobject_class,
+ PROP_THRIFT_SERVER_OUTPUT_TRANSPORT_FACTORY,
+ g_param_spec_object ("output_transport_factory",
+ "Output Transport Factory",
+ "Thrift Server Output Transport Factory",
+ THRIFT_TYPE_TRANSPORT_FACTORY,
+ G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
+ g_object_class_install_property (gobject_class,
+ PROP_THRIFT_SERVER_INPUT_PROTOCOL_FACTORY,
+ g_param_spec_object ("input_protocol_factory", "Input Protocol Factory",
+ "Thrift Server Input Protocol Factory",
+ THRIFT_TYPE_PROTOCOL_FACTORY,
+ G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
+ g_object_class_install_property (gobject_class,
+ PROP_THRIFT_SERVER_OUTPUT_PROTOCOL_FACTORY,
+ g_param_spec_object ("output_protocol_factory", "Output Protocol Factory",
+ "Thrift Server Output Protocol Factory",
+ THRIFT_TYPE_PROTOCOL_FACTORY,
+ G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
+
+ /* set these as virtual methods to be implemented by a subclass */
+ cls->serve = thrift_server_serve;
+ cls->stop = thrift_server_stop;
+}
+
+void
+thrift_server_get_property (GObject *object, guint property_id,
+ GValue *value, GParamSpec *pspec)
+{
+ ThriftServer *server = THRIFT_SERVER (object);
+
+ THRIFT_UNUSED_VAR (pspec);
+
+ switch (property_id)
+ {
+ case PROP_THRIFT_SERVER_PROCESSOR:
+ g_value_set_object (value, server->processor);
+ break;
+ case PROP_THRIFT_SERVER_SERVER_TRANSPORT:
+ g_value_set_object (value, server->server_transport);
+ break;
+ case PROP_THRIFT_SERVER_INPUT_TRANSPORT_FACTORY:
+ g_value_set_object (value, server->input_transport_factory);
+ break;
+ case PROP_THRIFT_SERVER_OUTPUT_TRANSPORT_FACTORY:
+ g_value_set_object (value, server->output_transport_factory);
+ break;
+ case PROP_THRIFT_SERVER_INPUT_PROTOCOL_FACTORY:
+ g_value_set_object (value, server->input_protocol_factory);
+ break;
+ case PROP_THRIFT_SERVER_OUTPUT_PROTOCOL_FACTORY:
+ g_value_set_object (value, server->output_protocol_factory);
+ break;
+ }
+}
+
+void
+thrift_server_set_property (GObject *object, guint property_id,
+ const GValue *value, GParamSpec *pspec)
+{
+ ThriftServer *server = THRIFT_SERVER (object);
+
+ THRIFT_UNUSED_VAR (pspec);
+
+ switch (property_id)
+ {
+ case PROP_THRIFT_SERVER_PROCESSOR:
+ server->processor = g_value_get_object (value);
+ break;
+ case PROP_THRIFT_SERVER_SERVER_TRANSPORT:
+ server->server_transport = g_value_get_object (value);
+ break;
+ case PROP_THRIFT_SERVER_INPUT_TRANSPORT_FACTORY:
+ server->input_transport_factory = g_value_get_object (value);
+ break;
+ case PROP_THRIFT_SERVER_OUTPUT_TRANSPORT_FACTORY:
+ server->output_transport_factory = g_value_get_object (value);
+ break;
+ case PROP_THRIFT_SERVER_INPUT_PROTOCOL_FACTORY:
+ server->input_protocol_factory = g_value_get_object (value);
+ break;
+ case PROP_THRIFT_SERVER_OUTPUT_PROTOCOL_FACTORY:
+ server->output_protocol_factory = g_value_get_object (value);
+ break;
+ }
+}
+
+void
+thrift_server_serve (ThriftServer *server)
+{
+ THRIFT_SERVER_GET_CLASS (server)->serve (server);
+}
+
+void
+thrift_server_stop (ThriftServer *server)
+{
+ THRIFT_SERVER_GET_CLASS (server)->stop (server);
+}
+
Added: incubator/thrift/trunk/lib/c_glib/src/server/thrift_server.h
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/c_glib/src/server/thrift_server.h?rev=1027933&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/c_glib/src/server/thrift_server.h (added)
+++ incubator/thrift/trunk/lib/c_glib/src/server/thrift_server.h Wed Oct 27 12:30:11 2010
@@ -0,0 +1,75 @@
+#ifndef _THRIFT_SERVER_H
+#define _THRIFT_SERVER_H
+
+#include <glib-object.h>
+
+#include "processor/thrift_processor.h"
+#include "transport/thrift_server_transport.h"
+#include "transport/thrift_transport_factory.h"
+#include "protocol/thrift_protocol_factory.h"
+
+/*! \file thrift_server.h
+ * \brief Abstract class for Thrift servers.
+ */
+
+/* type macros */
+#define THRIFT_TYPE_SERVER (thrift_server_get_type ())
+#define THRIFT_SERVER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
+ THRIFT_TYPE_SERVER, ThriftServer))
+#define THRIFT_IS_SERVER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+ THRIFT_TYPE_SERVER))
+#define THRIFT_SERVER_CLASS(c) (G_TYPE_CHECK_CLASS_CAST ((c), \
+ THRIFT_TYPE_SERVER, \
+ ThriftServerClass))
+#define THRIFT_IS_SERVER_CLASS(c) (G_TYPE_CHECK_CLASS_TYPE ((c), \
+ THRIFT_TYPE_SERVER))
+#define THRIFT_SERVER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), \
+ THRIFT_TYPE_SERVER, \
+ ThriftServerClass))
+
+/*!
+ * Thrift Server object
+ */
+struct _ThriftServer
+{
+ GObject parent;
+
+ /* protected */
+ ThriftProcessor *processor;
+ ThriftServerTransport *server_transport;
+ ThriftTransportFactory *input_transport_factory;
+ ThriftTransportFactory *output_transport_factory;
+ ThriftProtocolFactory *input_protocol_factory;
+ ThriftProtocolFactory *output_protocol_factory;
+};
+typedef struct _ThriftServer ThriftServer;
+
+/*!
+ * Thrift Server class
+ */
+struct _ThriftServerClass
+{
+ GObjectClass parent;
+
+ /* vtable */
+ void (*serve) (ThriftServer *server);
+ void (*stop) (ThriftServer *server);
+};
+typedef struct _ThriftServerClass ThriftServerClass;
+
+/* used by THRIFT_TYPE_SERVER */
+GType thrift_server_get_type (void);
+
+/*!
+ * Processes the request.
+ * \public \memberof ThriftServerClass
+ */
+void thrift_server_serve (ThriftServer *server);
+
+/*!
+ * Stop handling requests.
+ */
+void thrift_server_stop (ThriftServer *server);
+
+#endif /* _THRIFT_SERVER_H */
+
Added: incubator/thrift/trunk/lib/c_glib/src/server/thrift_simple_server.c
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/c_glib/src/server/thrift_simple_server.c?rev=1027933&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/c_glib/src/server/thrift_simple_server.c (added)
+++ incubator/thrift/trunk/lib/c_glib/src/server/thrift_simple_server.c Wed Oct 27 12:30:11 2010
@@ -0,0 +1,133 @@
+
+#include "server/thrift_simple_server.h"
+#include "transport/thrift_transport_factory.h"
+#include "protocol/thrift_protocol_factory.h"
+#include "protocol/thrift_binary_protocol_factory.h"
+
+static void thrift_simple_server_instance_init (ThriftServer *server);
+static void thrift_simple_server_class_init (ThriftServerClass *cls);
+
+/* forward declarations */
+void thrift_simple_server_serve (ThriftServer *server);
+void thrift_simple_server_stop (ThriftServer *server);
+
+GType
+thrift_simple_server_get_type (void)
+{
+ static GType type = 0;
+
+ if (type == 0)
+ {
+ static const GTypeInfo info =
+ {
+ sizeof (ThriftSimpleServerClass),
+ NULL, /* base_init */
+ NULL, /* base_finalize */
+ (GClassInitFunc) thrift_simple_server_class_init,
+ NULL, /* class_finalize */
+ NULL, /* class_data */
+ sizeof (ThriftSimpleServer),
+ 0, /* n_preallocs */
+ (GInstanceInitFunc) thrift_simple_server_instance_init,
+ NULL, /* value_table */
+ };
+
+ type = g_type_register_static (THRIFT_TYPE_SERVER,
+ "ThriftSimpleServerType",
+ &info, 0);
+ }
+
+ return type;
+}
+
+static void
+thrift_simple_server_instance_init (ThriftServer *server)
+{
+ (THRIFT_SIMPLE_SERVER (server))->running = FALSE;
+
+ if (server->input_transport_factory == NULL)
+ {
+ server->input_transport_factory =
+ g_object_new (THRIFT_TYPE_TRANSPORT_FACTORY, NULL);
+ }
+ if (server->output_transport_factory == NULL)
+ {
+ server->output_transport_factory =
+ g_object_new (THRIFT_TYPE_TRANSPORT_FACTORY, NULL);
+ }
+ if (server->input_protocol_factory == NULL)
+ {
+ server->input_protocol_factory =
+ g_object_new (THRIFT_TYPE_BINARY_PROTOCOL_FACTORY, NULL);
+ }
+ if (server->output_protocol_factory == NULL)
+ {
+ server->output_protocol_factory =
+ g_object_new (THRIFT_TYPE_BINARY_PROTOCOL_FACTORY, NULL);
+ }
+}
+
+
+/* initialize the class */
+static void
+thrift_simple_server_class_init (ThriftServerClass *cls)
+{
+ cls->serve = thrift_simple_server_serve;
+ cls->stop = thrift_simple_server_stop;
+}
+
+void
+thrift_simple_server_serve (ThriftServer *server)
+{
+ g_return_if_fail (THRIFT_IS_SIMPLE_SERVER (server));
+
+ ThriftTransport *t = NULL;
+ ThriftTransport *input_transport = NULL, *output_transport = NULL;
+ ThriftProtocol *input_protocol = NULL, *output_protocol = NULL;
+ ThriftSimpleServer *tss = THRIFT_SIMPLE_SERVER (server);
+
+ THRIFT_SERVER_TRANSPORT_GET_CLASS (server->server_transport)
+ ->listen (server->server_transport, NULL);
+
+ tss->running = TRUE;
+ while (tss->running == TRUE)
+ {
+ t = thrift_server_transport_accept (server->server_transport, NULL);
+ input_transport =
+ THRIFT_TRANSPORT_FACTORY_GET_CLASS (server->input_transport_factory)
+ ->get_transport (server->input_transport_factory, t);
+ output_transport =
+ THRIFT_TRANSPORT_FACTORY_GET_CLASS (server->output_transport_factory)
+ ->get_transport (server->output_transport_factory, t);
+ input_protocol =
+ THRIFT_PROTOCOL_FACTORY_GET_CLASS (server->input_protocol_factory)
+ ->get_protocol (server->input_protocol_factory, t);
+ output_protocol =
+ THRIFT_PROTOCOL_FACTORY_GET_CLASS (server->output_protocol_factory)
+ ->get_protocol (server->output_protocol_factory, t);
+
+ while (THRIFT_PROCESSOR_GET_CLASS (server->processor)
+ ->process (server->processor, input_protocol, output_protocol))
+ {
+ // TODO: implement transport peek ()
+ }
+
+ // TODO: handle exceptions
+ THRIFT_TRANSPORT_GET_CLASS (input_transport)->close (input_transport, NULL);
+ THRIFT_TRANSPORT_GET_CLASS (output_transport)->close (output_transport,
+ NULL);
+ }
+
+ // attempt to shutdown
+ THRIFT_SERVER_TRANSPORT_GET_CLASS (server->server_transport)
+ ->close (server->server_transport, NULL);
+}
+
+void
+thrift_simple_server_stop (ThriftServer *server)
+{
+ g_return_if_fail (THRIFT_IS_SIMPLE_SERVER (server));
+ (THRIFT_SIMPLE_SERVER (server))->running = FALSE;
+}
+
+
Added: incubator/thrift/trunk/lib/c_glib/src/server/thrift_simple_server.h
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/c_glib/src/server/thrift_simple_server.h?rev=1027933&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/c_glib/src/server/thrift_simple_server.h (added)
+++ incubator/thrift/trunk/lib/c_glib/src/server/thrift_simple_server.h Wed Oct 27 12:30:11 2010
@@ -0,0 +1,53 @@
+#ifndef _THRIFT_SIMPLE_SERVER_H
+#define _THRIFT_SIMPLE_SERVER_H
+
+#include <glib-object.h>
+
+#include "server/thrift_server.h"
+
+/*! \file thrift_simple_server.h
+ * \brief A simple Thrift server, single-threaded.
+ */
+
+/* type macros */
+#define THRIFT_TYPE_SIMPLE_SERVER (thrift_simple_server_get_type ())
+#define THRIFT_SIMPLE_SERVER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
+ THRIFT_TYPE_SIMPLE_SERVER, \
+ ThriftSimpleServer))
+#define THRIFT_IS_SIMPLE_SERVER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+ THRIFT_TYPE_SIMPLE_SERVER))
+#define THRIFT_SIMPLE_SERVER_CLASS(c) (G_TYPE_CHECK_CLASS_CAST ((c) \
+ THRIFT_TYPE_SIMPLE_SERVER, \
+ ThriftSimpleServerClass))
+#define THRIFT_IS_SIMPLE_SERVER_CLASS(c) (G_TYPE_CHECK_CLASS_TYPE ((c), \
+ THRIFT_TYPE_SIMPLE_SERVER))
+#define THRIFT_SIMPLE_SERVER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), \
+ THRIFT_TYPE_SIMPLE_SERVER, \
+ ThriftSimpleServerClass))
+
+/**
+ * Thrift Simple Server instance.
+ */
+struct _ThriftSimpleServer
+{
+ ThriftServer parent;
+
+ /* private */
+ volatile gboolean running;
+};
+typedef struct _ThriftSimpleServer ThriftSimpleServer;
+
+/**
+ * Thrift Simple Server class.
+ */
+struct _ThriftSimpleServerClass
+{
+ ThriftServerClass parent;
+};
+typedef struct _ThriftSimpleServerClass ThriftSimpleServerClass;
+
+/* used by THRIFT_TYPE_SIMPLE_SERVER */
+GType thrift_simple_server_get_type (void);
+
+#endif /* _THRIFT_SIMPLE_SERVER_H */
+
Added: incubator/thrift/trunk/lib/c_glib/src/thrift.c
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/c_glib/src/thrift.c?rev=1027933&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/c_glib/src/thrift.c (added)
+++ incubator/thrift/trunk/lib/c_glib/src/thrift.c Wed Oct 27 12:30:11 2010
@@ -0,0 +1,13 @@
+#include "thrift.h"
+
+/**
+ * GHashTable callback to add keys to a GList.
+ */
+void
+thrift_hash_table_get_keys (gpointer key, gpointer value, gpointer user_data)
+{
+ THRIFT_UNUSED_VAR (value);
+ GList **list = (GList **) user_data;
+ *list = g_list_append (*list, key);
+}
+
Added: incubator/thrift/trunk/lib/c_glib/src/thrift.h
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/c_glib/src/thrift.h?rev=1027933&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/c_glib/src/thrift.h (added)
+++ incubator/thrift/trunk/lib/c_glib/src/thrift.h Wed Oct 27 12:30:11 2010
@@ -0,0 +1,18 @@
+#ifndef _THRIFT_H
+#define _THRIFT_H
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <glib.h>
+
+/* this macro is called to satisfy -Wall hardcore compilation */
+#ifndef THRIFT_UNUSED_VAR
+# define THRIFT_UNUSED_VAR(x) ((void) x)
+#endif
+
+void thrift_hash_table_get_keys (gpointer key, gpointer value,
+ gpointer user_data);
+
+#endif // #ifndef _THRIFT_THRIFT_H
Added: incubator/thrift/trunk/lib/c_glib/src/thrift_application_exception.c
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/c_glib/src/thrift_application_exception.c?rev=1027933&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/c_glib/src/thrift_application_exception.c (added)
+++ incubator/thrift/trunk/lib/c_glib/src/thrift_application_exception.c Wed Oct 27 12:30:11 2010
@@ -0,0 +1,192 @@
+#include "thrift_application_exception.h"
+#include "protocol/thrift_protocol.h"
+
+/* forward declarations */
+void thrift_application_exception_instance_init (ThriftApplicationException *object);
+void thrift_application_exception_class_init (ThriftStructClass *cls);
+gint32 thrift_application_exception_read (ThriftStruct *object, ThriftProtocol *protocol, GError **error);
+gint32 thrift_application_exception_write (ThriftStruct *object, ThriftProtocol *protocol, GError **error);
+
+GType
+thrift_application_exception_get_type (void)
+{
+ static GType type = 0;
+
+ if (type == 0)
+ {
+ static const GTypeInfo type_info =
+ {
+ sizeof (ThriftApplicationExceptionClass),
+ NULL, /* base_init */
+ NULL, /* base_finalize */
+ (GClassInitFunc) thrift_application_exception_class_init,
+ NULL, /* class_finalize */
+ NULL, /* class_data */
+ sizeof (ThriftApplicationException),
+ 0, /* n_preallocs */
+ (GInstanceInitFunc) thrift_application_exception_instance_init,
+ NULL, /* value_table */
+ };
+
+ type = g_type_register_static (THRIFT_TYPE_STRUCT,
+ "ThriftApplicationExceptionType",
+ &type_info, 0);
+ }
+ return type;
+}
+
+void
+thrift_application_exception_instance_init (ThriftApplicationException *object)
+{
+ object->type = 0;
+ object->__isset_type = FALSE;
+ object->message = NULL;
+ object->__isset_message = FALSE;
+}
+
+void
+thrift_application_exception_class_init (ThriftStructClass *cls)
+{
+ cls->read = thrift_application_exception_read;
+ cls->write = thrift_application_exception_write;
+}
+
+gint32
+thrift_application_exception_read (ThriftStruct *object,
+ ThriftProtocol *protocol, GError **error)
+{
+ gint32 ret;
+ gint32 xfer = 0;
+ gchar *name;
+ ThriftType ftype;
+ gint16 fid;
+ ThriftApplicationException *this = THRIFT_APPLICATION_EXCEPTION (object);
+
+ /* read the struct begin marker */
+ if ((ret = thrift_protocol_read_struct_begin (protocol, &name, error)) < 0)
+ {
+ if (name) g_free (name);
+ return -1;
+ }
+ xfer += ret;
+ if (name) g_free (name);
+
+ while (1)
+ {
+ if ((ret = thrift_protocol_read_field_begin (protocol, &name, &ftype,
+ &fid, error)) < 0)
+ {
+ if (name) g_free (name);
+ return -1;
+ }
+ xfer += ret;
+ if (name) g_free (name);
+
+ /* break if we get a STOP field */
+ if (ftype == T_STOP)
+ {
+ break;
+ }
+
+ switch (fid)
+ {
+ case 1:
+ if (ftype == T_STRING)
+ {
+ if ((ret = thrift_protocol_read_string (protocol, &this->message,
+ error)) < 0)
+ return -1;
+ xfer += ret;
+ this->__isset_message = TRUE;
+ } else {
+ if ((ret = thrift_protocol_skip (protocol, ftype, error)) < 0)
+ return -1;
+ xfer += ret;
+ }
+ break;
+ case 2:
+ if (ftype == T_I32)
+ {
+ if ((ret = thrift_protocol_read_i32 (protocol, &this->type,
+ error)) < 0)
+ return -1;
+ xfer += ret;
+ this->__isset_type = TRUE;
+ } else {
+ if ((ret = thrift_protocol_skip (protocol, ftype, error)) < 0)
+ return -1;
+ xfer += ret;
+ }
+ break;
+ default:
+ if ((ret = thrift_protocol_skip (protocol, ftype, error)) < 0)
+ return -1;
+ xfer += ret;
+ break;
+ }
+ if ((ret = thrift_protocol_read_field_end (protocol, error)) < 0)
+ return -1;
+ xfer += ret;
+ }
+
+ if ((ret = thrift_protocol_read_struct_end (protocol, error)) < 0)
+ return -1;
+ xfer += ret;
+
+ return xfer;
+}
+
+gint32
+thrift_application_exception_write (ThriftStruct *object,
+ ThriftProtocol *protocol, GError **error)
+{
+ gint32 ret;
+ gint32 xfer = 0;
+
+ ThriftApplicationException *this = THRIFT_APPLICATION_EXCEPTION (object);
+
+ if ((ret = thrift_protocol_write_struct_begin (protocol,
+ "TApplicationException",
+ error)) < 0)
+ return -1;
+ xfer += ret;
+ if ((ret = thrift_protocol_write_field_begin (protocol, "message",
+ T_STRING, 1, error)) < 0)
+ return -1;
+ xfer += ret;
+ if ((ret = thrift_protocol_write_string (protocol, this->message, error)) < 0)
+ return -1;
+ xfer += ret;
+ if ((ret = thrift_protocol_write_field_end (protocol, error)) < 0)
+ return -1;
+ xfer += ret;
+ if ((ret = thrift_protocol_write_field_begin (protocol, "type",
+ T_I32, 2, error)) < 0)
+ return -1;
+ xfer += ret;
+ if ((ret = thrift_protocol_write_i32 (protocol, this->type, error)) < 0)
+ return -1;
+ xfer += ret;
+ if ((ret = thrift_protocol_write_field_end (protocol, error)) < 0)
+ return -1;
+ xfer += ret;
+ if ((ret = thrift_protocol_write_field_stop (protocol, error)) < 0)
+ return -1;
+ xfer += ret;
+ if ((ret = thrift_protocol_write_struct_end (protocol, error)) < 0)
+ return -1;
+ xfer += ret;
+
+ return xfer;
+}
+
+
+/* GError domain */
+#define THRIFT_APPLICATION_EXCEPTION_ERROR_DOMAIN "thrift-application-exception-error-quark"
+
+GQuark
+thrift_application_exception_error_quark (void)
+{
+ return g_quark_from_static_string (THRIFT_APPLICATION_EXCEPTION_ERROR_DOMAIN);
+}
+
Added: incubator/thrift/trunk/lib/c_glib/src/thrift_application_exception.h
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/c_glib/src/thrift_application_exception.h?rev=1027933&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/c_glib/src/thrift_application_exception.h (added)
+++ incubator/thrift/trunk/lib/c_glib/src/thrift_application_exception.h Wed Oct 27 12:30:11 2010
@@ -0,0 +1,67 @@
+#ifndef _THRIFT_APPLICATION_EXCEPTION_H
+#define _THRIFT_APPLICATION_EXCEPTION_H
+
+#include <glib-object.h>
+#include "thrift_struct.h"
+
+/*! \file thrift_application_exception.h
+ * \brief C Implementation of a TApplicationException.
+ */
+
+/* type macros */
+#define THRIFT_TYPE_APPLICATION_EXCEPTION \
+ (thrift_application_exception_get_type ())
+#define THRIFT_APPLICATION_EXCEPTION(obj) \
+ (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
+ THRIFT_TYPE_APPLICATION_EXCEPTION, \
+ ThriftApplicationException))
+#define THRIFT_IS_APPLICATION_EXCEPTION(obj) \
+ (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+ THRIFT_TYPE_APPLICATION_EXCEPTION))
+#define THRIFT_APPLICATION_EXCEPTION_CLASS(c) \
+ (G_TYPE_CHECK_CLASS_CAST ((c), \
+ THRIFT_TYPE_APPLICATION_EXCEPTION, \
+ ThriftApplicationExceptionClass))
+#define THRIFT_IS_APPLICATION_EXCEPTION_CLASS(c) \
+ (G_TYPE_CHECK_CLASS_TYPE ((c), THRIFT_TYPE_APPLICATION_EXCEPTION))
+#define THRIFT_APPLICATION_EXCEPTION_GET_CLASS(obj) \
+ (G_TYPE_INSTANCE_GET_CLASS ((obj), \
+ THRIFT_TYPE_APPLICATION_EXCEPTION, \
+ ThriftApplicationExceptionClass))
+
+struct _ThriftApplicationException
+{
+ ThriftStruct parent;
+
+ /* public */
+ gint32 type;
+ gboolean __isset_type;
+ gchar *message;
+ gboolean __isset_message;
+};
+typedef struct _ThriftApplicationException ThriftApplicationException;
+
+struct _ThriftApplicationExceptionClass
+{
+ ThriftStructClass parent;
+};
+typedef struct _ThriftApplicationExceptionClass ThriftApplicationExceptionClass;
+
+GType thrift_application_exception_get_type (void);
+
+/* gerror codes */
+typedef enum
+{
+ THRIFT_APPLICATION_EXCEPTION_ERROR_UNKNOWN,
+ THRIFT_APPLICATION_EXCEPTION_ERROR_UNKNOWN_METHOD,
+ THRIFT_APPLICATION_EXCEPTION_ERROR_INVALID_MESSAGE_TYPE,
+ THRIFT_APPLICATION_EXCEPTION_ERROR_WRONG_METHOD_NAME,
+ THRIFT_APPLICATION_EXCEPTION_ERROR_BAD_SEQUENCE_ID,
+ THRIFT_APPLICATION_EXCEPTION_ERROR_MISSING_RESULT
+} ThriftApplicationExceptionError;
+
+/* define error domain for GError */
+GQuark thrift_application_exception_error_quark (void);
+#define THRIFT_APPLICATION_EXCEPTION_ERROR (thrift_application_exception_error_quark ())
+
+#endif /* _THRIFT_APPLICATION_EXCEPTION_H */
Added: incubator/thrift/trunk/lib/c_glib/src/thrift_struct.c
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/c_glib/src/thrift_struct.c?rev=1027933&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/c_glib/src/thrift_struct.c (added)
+++ incubator/thrift/trunk/lib/c_glib/src/thrift_struct.c Wed Oct 27 12:30:11 2010
@@ -0,0 +1,55 @@
+#include "thrift_struct.h"
+
+static void
+thrift_struct_class_init (ThriftStructClass *cls)
+{
+ ThriftStructClass *c = THRIFT_STRUCT_CLASS (cls);
+ c->read = thrift_struct_read;
+ c->write = thrift_struct_write;
+}
+
+GType
+thrift_struct_get_type (void)
+{
+ static GType type = 0;
+
+ if (type == 0)
+ {
+ static const GTypeInfo type_info =
+ {
+ sizeof (ThriftStructClass),
+ NULL, /* base_init */
+ NULL, /* base finalize */
+ (GClassInitFunc) thrift_struct_class_init,
+ NULL, /* class finalize */
+ NULL, /* class data */
+ sizeof (ThriftStruct),
+ 0, /* n_preallocs */
+ NULL, /* instance_init */
+ NULL, /* value_table */
+ };
+
+ type = g_type_register_static (G_TYPE_OBJECT,
+ "ThriftStructType",
+ &type_info, G_TYPE_FLAG_ABSTRACT);
+ }
+
+ return type;
+}
+
+gint32
+thrift_struct_read (ThriftStruct *object, ThriftProtocol *protocol,
+ GError **error)
+{
+ g_return_val_if_fail (THRIFT_IS_STRUCT (object), -1);
+ return THRIFT_STRUCT_GET_CLASS (object)->read (object, protocol, error);
+}
+
+gint32
+thrift_struct_write (ThriftStruct *object, ThriftProtocol *protocol,
+ GError **error)
+{
+ g_return_val_if_fail (THRIFT_IS_STRUCT (object), -1);
+ return THRIFT_STRUCT_GET_CLASS (object)->write (object, protocol, error);
+}
+
Added: incubator/thrift/trunk/lib/c_glib/src/thrift_struct.h
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/c_glib/src/thrift_struct.h?rev=1027933&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/c_glib/src/thrift_struct.h (added)
+++ incubator/thrift/trunk/lib/c_glib/src/thrift_struct.h Wed Oct 27 12:30:11 2010
@@ -0,0 +1,50 @@
+#ifndef THRIFT_STRUCT_H
+#define THRIFT_STRUCT_H
+
+#include <glib-object.h>
+
+#include "protocol/thrift_protocol.h"
+
+#define THRIFT_TYPE_STRUCT (thrift_struct_get_type ())
+#define THRIFT_STRUCT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
+ THRIFT_TYPE_STRUCT, ThriftStruct))
+#define THRIFT_STRUCT_CLASS(c) (G_TYPE_CHECK_CLASS_CAST ((c), \
+ THRIFT_TYPE_STRUCT, ThriftStructClass))
+#define THRIFT_IS_STRUCT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+ THRIFT_TYPE_STRUCT))
+#define THRIFT_IS_STRUCT_CLASS(c) (G_TYPE_CHECK_CLASS_TYPE ((c), \
+ THRIFT_TYPE_STRUCT))
+#define THRIFT_STRUCT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), \
+ THRIFT_TYPE_STRUCT, \
+ ThriftStructClass))
+
+/* struct */
+struct _ThriftStruct
+{
+ GObject parent;
+
+ /* private */
+};
+typedef struct _ThriftStruct ThriftStruct;
+
+struct _ThriftStructClass
+{
+ GObjectClass parent;
+
+ /* public */
+ gint32 (*read) (ThriftStruct *object, ThriftProtocol *protocol,
+ GError **error);
+ gint32 (*write) (ThriftStruct *object, ThriftProtocol *protocol,
+ GError **error);
+};
+typedef struct _ThriftStructClass ThriftStructClass;
+
+GType thrift_struct_get_type (void);
+
+gint32 thrift_struct_read (ThriftStruct *object, ThriftProtocol *protocol,
+ GError **error);
+
+gint32 thrift_struct_write (ThriftStruct *object, ThriftProtocol *protocol,
+ GError **error);
+
+#endif
Added: incubator/thrift/trunk/lib/c_glib/src/transport/thrift_buffered_transport.c
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/c_glib/src/transport/thrift_buffered_transport.c?rev=1027933&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/c_glib/src/transport/thrift_buffered_transport.c (added)
+++ incubator/thrift/trunk/lib/c_glib/src/transport/thrift_buffered_transport.c Wed Oct 27 12:30:11 2010
@@ -0,0 +1,406 @@
+#include <assert.h>
+#include <netdb.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "thrift.h"
+#include "transport/thrift_transport.h"
+#include "transport/thrift_buffered_transport.h"
+
+/* object properties */
+enum _ThriftBufferedTransportProperties
+{
+ PROP_0,
+ PROP_THRIFT_BUFFERED_TRANSPORT_TRANSPORT,
+ PROP_THRIFT_BUFFERED_TRANSPORT_READ_BUFFER_SIZE,
+ PROP_THRIFT_BUFFERED_TRANSPORT_WRITE_BUFFER_SIZE
+};
+
+/* forward declarations */
+static void thrift_buffered_transport_instance_init (ThriftBufferedTransport *self);
+static void thrift_buffered_transport_class_init (ThriftBufferedTransportClass *cls);
+
+
+gboolean thrift_buffered_transport_is_open (ThriftTransport *transport);
+gboolean thrift_buffered_transport_open (ThriftTransport *transport,
+ GError **error);
+gboolean thrift_buffered_transport_close (ThriftTransport *transport,
+ GError **error);
+gint32 thrift_buffered_transport_read (ThriftTransport *transport, gpointer buf,
+ guint32 len, GError **error);
+gboolean thrift_buffered_transport_read_end (ThriftTransport *transport,
+ GError **error);
+gint32 thrift_buffered_transport_read_slow (ThriftTransport *transport,
+ gpointer buf, guint32 len,
+ GError **error);
+gboolean thrift_buffered_transport_write (ThriftTransport *transport,
+ const gpointer buf,
+ const guint32 len, GError **error);
+gboolean thrift_buffered_transport_write_end (ThriftTransport *transport,
+ GError **error);
+gint32 thrift_buffered_transport_write_slow (ThriftTransport *transport,
+ gpointer buf, guint32 len,
+ GError **error);
+gboolean thrift_buffered_transport_flush (ThriftTransport *transport,
+ GError **error);
+
+GType
+thrift_buffered_transport_get_type (void)
+{
+ static GType type = 0;
+
+ if (type == 0)
+ {
+ static const GTypeInfo info =
+ {
+ sizeof (ThriftBufferedTransportClass),
+ NULL, /* base_init */
+ NULL, /* base_finalize */
+ (GClassInitFunc) thrift_buffered_transport_class_init,
+ NULL, /* class finalize */
+ NULL, /* class data */
+ sizeof (ThriftBufferedTransport),
+ 0, /* n_preallocs */
+ (GInstanceInitFunc) thrift_buffered_transport_instance_init,
+ NULL, /* value_table */
+ };
+
+ type = g_type_register_static (THRIFT_TYPE_TRANSPORT,
+ "ThriftBufferedTransport", &info, 0);
+ }
+
+ return type;
+}
+
+/* initializes the instance */
+static void
+thrift_buffered_transport_instance_init (ThriftBufferedTransport *transport)
+{
+ transport->transport = NULL;
+ transport->r_buf = g_byte_array_new ();
+ transport->w_buf = g_byte_array_new ();
+}
+
+/* destructor */
+static void
+thrift_buffered_transport_finalize (GObject *object)
+{
+ ThriftBufferedTransport *transport = THRIFT_BUFFERED_TRANSPORT (object);
+
+ if (transport->r_buf != NULL)
+ {
+ g_byte_array_free (transport->r_buf, TRUE);
+ }
+ transport->r_buf = NULL;
+
+ if (transport->w_buf != NULL)
+ {
+ g_byte_array_free (transport->w_buf, TRUE);
+ }
+ transport->w_buf = NULL;
+}
+
+/* property accessor */
+void
+thrift_buffered_transport_get_property (GObject *object, guint property_id,
+ GValue *value, GParamSpec *pspec)
+{
+ THRIFT_UNUSED_VAR (pspec);
+ ThriftBufferedTransport *transport = THRIFT_BUFFERED_TRANSPORT (object);
+
+ switch (property_id)
+ {
+ case PROP_THRIFT_BUFFERED_TRANSPORT_TRANSPORT:
+ g_value_set_object (value, transport->transport);
+ break;
+ case PROP_THRIFT_BUFFERED_TRANSPORT_READ_BUFFER_SIZE:
+ g_value_set_uint (value, transport->r_buf_size);
+ break;
+ case PROP_THRIFT_BUFFERED_TRANSPORT_WRITE_BUFFER_SIZE:
+ g_value_set_uint (value, transport->w_buf_size);
+ break;
+ }
+}
+
+/* property mutator */
+void
+thrift_buffered_transport_set_property (GObject *object, guint property_id,
+ const GValue *value, GParamSpec *pspec)
+{
+ THRIFT_UNUSED_VAR (pspec);
+ ThriftBufferedTransport *transport = THRIFT_BUFFERED_TRANSPORT (object);
+
+ switch (property_id)
+ {
+ case PROP_THRIFT_BUFFERED_TRANSPORT_TRANSPORT:
+ transport->transport = g_value_get_object (value);
+ break;
+ case PROP_THRIFT_BUFFERED_TRANSPORT_READ_BUFFER_SIZE:
+ transport->r_buf_size = g_value_get_uint (value);
+ break;
+ case PROP_THRIFT_BUFFERED_TRANSPORT_WRITE_BUFFER_SIZE:
+ transport->w_buf_size = g_value_get_uint (value);
+ break;
+ }
+}
+
+/* initializes the class */
+static void
+thrift_buffered_transport_class_init (ThriftBufferedTransportClass *cls)
+{
+ GObjectClass *gobject_class = G_OBJECT_CLASS (cls);
+ GParamSpec *param_spec = NULL;
+
+ /* setup accessors and mutators */
+ gobject_class->get_property = thrift_buffered_transport_get_property;
+ gobject_class->set_property = thrift_buffered_transport_set_property;
+
+ param_spec = g_param_spec_object ("transport", "transport (construct)",
+ "Thrift transport",
+ THRIFT_TYPE_TRANSPORT,
+ G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
+ g_object_class_install_property (gobject_class,
+ PROP_THRIFT_BUFFERED_TRANSPORT_TRANSPORT,
+ param_spec);
+
+ param_spec = g_param_spec_uint ("r_buf_size",
+ "read buffer size (construct)",
+ "Set the read buffer size",
+ 0, /* min */
+ 1048576, /* max, 1024*1024 */
+ 512, /* default value */
+ G_PARAM_CONSTRUCT_ONLY |
+ G_PARAM_READWRITE);
+ g_object_class_install_property (gobject_class,
+ PROP_THRIFT_BUFFERED_TRANSPORT_READ_BUFFER_SIZE,
+ param_spec);
+
+ param_spec = g_param_spec_uint ("w_buf_size",
+ "write buffer size (construct)",
+ "Set the write buffer size",
+ 0, /* min */
+ 1048576, /* max, 1024*1024 */
+ 512, /* default value */
+ G_PARAM_CONSTRUCT_ONLY |
+ G_PARAM_READWRITE);
+ g_object_class_install_property (gobject_class,
+ PROP_THRIFT_BUFFERED_TRANSPORT_WRITE_BUFFER_SIZE,
+ param_spec);
+
+
+ ThriftTransportClass *ttc = THRIFT_TRANSPORT_CLASS (cls);
+
+ gobject_class->finalize = thrift_buffered_transport_finalize;
+ ttc->is_open = thrift_buffered_transport_is_open;
+ ttc->open = thrift_buffered_transport_open;
+ ttc->close = thrift_buffered_transport_close;
+ ttc->read = thrift_buffered_transport_read;
+ ttc->read_end = thrift_buffered_transport_read_end;
+ ttc->write = thrift_buffered_transport_write;
+ ttc->write_end = thrift_buffered_transport_write_end;
+ ttc->flush = thrift_buffered_transport_flush;
+}
+
+/* implements thrift_transport_is_open */
+gboolean
+thrift_buffered_transport_is_open (ThriftTransport *transport)
+{
+ ThriftBufferedTransport *t = THRIFT_BUFFERED_TRANSPORT (transport);
+ return THRIFT_TRANSPORT_GET_CLASS (t->transport)->is_open (t->transport);
+}
+
+/* implements thrift_transport_open */
+gboolean
+thrift_buffered_transport_open (ThriftTransport *transport, GError **error)
+{
+ ThriftBufferedTransport *t = THRIFT_BUFFERED_TRANSPORT (transport);
+ return THRIFT_TRANSPORT_GET_CLASS (t->transport)->open (t->transport, error);
+}
+
+/* implements thrift_transport_close */
+gboolean
+thrift_buffered_transport_close (ThriftTransport *transport, GError **error)
+{
+ ThriftBufferedTransport *t = THRIFT_BUFFERED_TRANSPORT (transport);
+ return THRIFT_TRANSPORT_GET_CLASS (t->transport)->close (t->transport, error);
+}
+
+/* implements thrift_transport_read */
+gint32
+thrift_buffered_transport_read (ThriftTransport *transport, gpointer buf,
+ guint32 len, GError **error)
+{
+ ThriftBufferedTransport *t = THRIFT_BUFFERED_TRANSPORT (transport);
+
+ /* if we have enough buffer data to fulfill the read, just use
+ * a memcpy */
+ if (len <= t->r_buf->len)
+ {
+ memcpy (buf, t->r_buf->data, len);
+ g_byte_array_remove_range (t->r_buf, 0, len);
+ return len;
+ }
+
+ return thrift_buffered_transport_read_slow (transport, buf, len, error);
+}
+
+/* implements thrift_transport_read_end
+ * called when write is complete. nothing to do on our end. */
+gboolean
+thrift_buffered_transport_read_end (ThriftTransport *transport, GError **error)
+{
+ /* satisfy -Wall */
+ THRIFT_UNUSED_VAR (transport);
+ THRIFT_UNUSED_VAR (error);
+ return TRUE;
+}
+
+/* the actual read is "slow" because it calls the underlying transport */
+gint32
+thrift_buffered_transport_read_slow (ThriftTransport *transport, gpointer buf,
+ guint32 len, GError **error)
+{
+ ThriftBufferedTransport *t = THRIFT_BUFFERED_TRANSPORT (transport);
+ guint32 want = len;
+ guint32 got = 0;
+ guchar tmpdata[t->r_buf_size];
+ guint32 have = t->r_buf->len;
+
+ // we shouldn't hit this unless the buffer doesn't have enough to read
+ assert (t->r_buf->len < want);
+
+ // first copy what we have in our buffer.
+ if (have > 0)
+ {
+ memcpy (buf, t->r_buf, t->r_buf->len);
+ want -= t->r_buf->len;
+ t->r_buf = g_byte_array_remove_range (t->r_buf, 0, t->r_buf->len);
+ }
+
+ // if the buffer is still smaller than what we want to read, then just
+ // read it directly. otherwise, fill the buffer and then give out
+ // enough to satisfy the read.
+ if (t->r_buf_size < want)
+ {
+ got += THRIFT_TRANSPORT_GET_CLASS (t->transport)->read (t->transport,
+ tmpdata,
+ want,
+ error);
+
+ // copy the data starting from where we left off
+ memcpy (buf + have, tmpdata, got);
+ return got + have;
+ } else {
+ got += THRIFT_TRANSPORT_GET_CLASS (t->transport)->read (t->transport,
+ tmpdata,
+ t->r_buf_size,
+ error);
+ t->r_buf = g_byte_array_append (t->r_buf, tmpdata, got);
+
+ // hand over what we have up to what the caller wants
+ guint32 give = want < t->r_buf->len ? want : t->r_buf->len;
+
+
+ memcpy (buf + len - want, t->r_buf->data, give);
+ t->r_buf = g_byte_array_remove_range (t->r_buf, 0, give);
+ want -= give;
+
+ return (len - want);
+ }
+}
+
+
+/* implements thrift_transport_write */
+gboolean
+thrift_buffered_transport_write (ThriftTransport *transport,
+ const gpointer buf,
+ const guint32 len, GError **error)
+{
+ ThriftBufferedTransport *t = THRIFT_BUFFERED_TRANSPORT (transport);
+
+ /* the length of the current buffer plus the length of the data being read */
+ if (t->w_buf->len + len <= t->w_buf_size)
+ {
+ t->w_buf = g_byte_array_append (t->w_buf, buf, len);
+ return len;
+ }
+
+ return thrift_buffered_transport_write_slow (transport, buf, len, error);
+}
+
+/* implements thrift_transport_write_end
+ * called when write is complete. nothing to do on our end. */
+gboolean
+thrift_buffered_transport_write_end (ThriftTransport *transport, GError **error)
+{
+ /* satisfy -Wall */
+ THRIFT_UNUSED_VAR (transport);
+ THRIFT_UNUSED_VAR (error);
+ return TRUE;
+}
+
+gboolean
+thrift_buffered_transport_write_slow (ThriftTransport *transport, gpointer buf,
+ guint32 len, GError **error)
+{
+ ThriftBufferedTransport *t = THRIFT_BUFFERED_TRANSPORT (transport);
+ guint32 have_bytes = t->w_buf->len;
+ guint32 space = t->w_buf_size - t->w_buf->len;
+
+ // we need two syscalls because the buffered data plus the buffer itself
+ // is too big.
+ if ((have_bytes + len >= 2*t->w_buf->len) || (have_bytes == 0))
+ {
+ if (have_bytes > 0)
+ {
+ THRIFT_TRANSPORT_GET_CLASS (t->transport)->write (t->transport,
+ t->w_buf->data,
+ have_bytes,
+ error);
+ }
+ THRIFT_TRANSPORT_GET_CLASS (t->transport)->write (t->transport,
+ buf, len, error);
+ if (t->w_buf->len > 0)
+ {
+ t->w_buf = g_byte_array_remove_range (t->w_buf, 0, t->w_buf->len);
+ }
+
+ return TRUE;
+ }
+
+ t->w_buf = g_byte_array_append (t->w_buf, buf, space);
+ THRIFT_TRANSPORT_GET_CLASS (t->transport)->write (t->transport,
+ t->w_buf->data,
+ t->w_buf->len,
+ error);
+
+ t->w_buf = g_byte_array_remove_range (t->w_buf, 0, t->w_buf->len);
+ t->w_buf = g_byte_array_append (t->w_buf, buf+space, len-space);
+
+ return TRUE;
+}
+
+/* implements thrift_transport_flush */
+gboolean
+thrift_buffered_transport_flush (ThriftTransport *transport, GError **error)
+{
+ ThriftBufferedTransport *t = THRIFT_BUFFERED_TRANSPORT (transport);
+
+ if (t->w_buf != NULL && t->w_buf->len > 0)
+ {
+ // write the buffer and then empty it
+ THRIFT_TRANSPORT_GET_CLASS (t->transport)->write (t->transport,
+ t->w_buf->data,
+ t->w_buf->len,
+ error);
+ t->w_buf = g_byte_array_remove_range (t->w_buf, 0, t->w_buf->len);
+ }
+ THRIFT_TRANSPORT_GET_CLASS (t->transport)->flush (t->transport,
+ error);
+
+ return TRUE;
+}
+
+
Added: incubator/thrift/trunk/lib/c_glib/src/transport/thrift_buffered_transport.h
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/c_glib/src/transport/thrift_buffered_transport.h?rev=1027933&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/c_glib/src/transport/thrift_buffered_transport.h (added)
+++ incubator/thrift/trunk/lib/c_glib/src/transport/thrift_buffered_transport.h Wed Oct 27 12:30:11 2010
@@ -0,0 +1,61 @@
+#ifndef _THRIFT_BUFFERED_TRANSPORT_H
+#define _THRIFT_BUFFERED_TRANSPORT_H
+
+#include <glib.h>
+#include <glib-object.h>
+
+#include "transport/thrift_transport.h"
+
+/*! \file thrift_buffered_transport.h
+ * \brief Implementation of a Thrift buffered transport. Subclasses
+ * the ThriftTransport class.
+ */
+
+/* type macros */
+#define THRIFT_TYPE_BUFFERED_TRANSPORT (thrift_buffered_transport_get_type ())
+#define THRIFT_BUFFERED_TRANSPORT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
+ THRIFT_TYPE_BUFFERED_TRANSPORT, \
+ ThriftBufferedTransport))
+#define THRIFT_IS_BUFFERED_TRANSPORT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+ THRIFT_TYPE_BUFFERED_TRANSPORT))
+#define THRIFT_BUFFERED_TRANSPORT_CLASS(c) (G_TYPE_CHECK_CLASS_CAST ((c), \
+ THRIFT_TYPE_BUFFERED_TRANSPORT, \
+ ThriftBufferedTransportClass))
+#define THRIFT_IS_BUFFERED_TRANSPORT_CLASS(c) (G_TYPE_CHECK_CLASS_TYPE ((c), \
+ THRIFT_TYPE_BUFFERED_TRANSPORT)
+#define THRIFT_BUFFERED_TRANSPORT_GET_CLASS(obj) \
+ (G_TYPE_INSTANCE_GET_CLASS ((obj), \
+ THRIFT_TYPE_BUFFERED_TRANSPORT, \
+ ThriftBufferedTransportClass))
+
+/*!
+ * ThriftBufferedTransport instance.
+ */
+struct _ThriftBufferedTransport
+{
+ ThriftTransport parent;
+
+ /* protected */
+ ThriftTransport *transport;
+
+ /* private */
+ GByteArray *r_buf;
+ GByteArray *w_buf;
+ guint32 r_buf_size;
+ guint32 w_buf_size;
+};
+typedef struct _ThriftBufferedTransport ThriftBufferedTransport;
+
+/*!
+ * ThriftBufferedTransport class.
+ */
+struct _ThriftBufferedTransportClass
+{
+ ThriftTransportClass parent;
+};
+typedef struct _ThriftBufferedTransportClass ThriftBufferedTransportClass;
+
+/* used by THRIFT_TYPE_BUFFERED_TRANSPORT */
+GType thrift_buffered_transport_get_type (void);
+
+#endif
Added: incubator/thrift/trunk/lib/c_glib/src/transport/thrift_framed_transport.c
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/c_glib/src/transport/thrift_framed_transport.c?rev=1027933&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/c_glib/src/transport/thrift_framed_transport.c (added)
+++ incubator/thrift/trunk/lib/c_glib/src/transport/thrift_framed_transport.c Wed Oct 27 12:30:11 2010
@@ -0,0 +1,397 @@
+#include <assert.h>
+#include <netdb.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "thrift.h"
+#include "transport/thrift_transport.h"
+#include "transport/thrift_framed_transport.h"
+
+/* object properties */
+enum _ThriftFramedTransportProperties
+{
+ PROP_0,
+ PROP_THRIFT_FRAMED_TRANSPORT_TRANSPORT,
+ PROP_THRIFT_FRAMED_TRANSPORT_READ_BUFFER_SIZE,
+ PROP_THRIFT_FRAMED_TRANSPORT_WRITE_BUFFER_SIZE
+};
+
+/* forward declarations */
+static void thrift_framed_transport_instance_init (ThriftFramedTransport *self);
+static void thrift_framed_transport_class_init (ThriftFramedTransportClass *cls);
+
+
+gboolean thrift_framed_transport_is_open (ThriftTransport *transport);
+gboolean thrift_framed_transport_open (ThriftTransport *transport,
+ GError **error);
+gboolean thrift_framed_transport_close (ThriftTransport *transport,
+ GError **error);
+gint32 thrift_framed_transport_read (ThriftTransport *transport, gpointer buf,
+ guint32 len, GError **error);
+gboolean thrift_framed_transport_read_end (ThriftTransport *transport,
+ GError **error);
+gint32 thrift_framed_transport_read_slow (ThriftTransport *transport,
+ gpointer buf, guint32 len,
+ GError **error);
+gboolean thrift_framed_transport_read_frame (ThriftTransport *transport,
+ GError **error);
+gboolean thrift_framed_transport_write (ThriftTransport *transport,
+ const gpointer buf,
+ const guint32 len, GError **error);
+gboolean thrift_framed_transport_write_end (ThriftTransport *transport,
+ GError **error);
+gint32 thrift_framed_transport_write_slow (ThriftTransport *transport,
+ gpointer buf, guint32 len,
+ GError **error);
+gboolean thrift_framed_transport_flush (ThriftTransport *transport,
+ GError **error);
+
+GType
+thrift_framed_transport_get_type (void)
+{
+ static GType type = 0;
+
+ if (type == 0)
+ {
+ static const GTypeInfo info =
+ {
+ sizeof (ThriftFramedTransportClass),
+ NULL, /* base_init */
+ NULL, /* base_finalize */
+ (GClassInitFunc) thrift_framed_transport_class_init,
+ NULL, /* class finalize */
+ NULL, /* class data */
+ sizeof (ThriftFramedTransport),
+ 0, /* n_preallocs */
+ (GInstanceInitFunc) thrift_framed_transport_instance_init,
+ NULL, /* value_table */
+ };
+
+ type = g_type_register_static (THRIFT_TYPE_TRANSPORT,
+ "ThriftFramedTransport", &info, 0);
+ }
+
+ return type;
+}
+
+/* initializes the instance */
+static void
+thrift_framed_transport_instance_init (ThriftFramedTransport *transport)
+{
+ transport->transport = NULL;
+ transport->r_buf = g_byte_array_new ();
+ transport->w_buf = g_byte_array_new ();
+}
+
+/* destructor */
+static void
+thrift_framed_transport_finalize (GObject *object)
+{
+ ThriftFramedTransport *transport = THRIFT_FRAMED_TRANSPORT (object);
+
+ if (transport->r_buf != NULL)
+ {
+ g_byte_array_free (transport->r_buf, TRUE);
+ }
+ transport->r_buf = NULL;
+
+ if (transport->w_buf != NULL)
+ {
+ g_byte_array_free (transport->w_buf, TRUE);
+ }
+ transport->w_buf = NULL;
+}
+
+/* property accessor */
+void
+thrift_framed_transport_get_property (GObject *object, guint property_id,
+ GValue *value, GParamSpec *pspec)
+{
+ THRIFT_UNUSED_VAR (pspec);
+ ThriftFramedTransport *transport = THRIFT_FRAMED_TRANSPORT (object);
+
+ switch (property_id)
+ {
+ case PROP_THRIFT_FRAMED_TRANSPORT_TRANSPORT:
+ g_value_set_object (value, transport->transport);
+ break;
+ case PROP_THRIFT_FRAMED_TRANSPORT_READ_BUFFER_SIZE:
+ g_value_set_uint (value, transport->r_buf_size);
+ break;
+ case PROP_THRIFT_FRAMED_TRANSPORT_WRITE_BUFFER_SIZE:
+ g_value_set_uint (value, transport->w_buf_size);
+ break;
+ }
+}
+
+/* property mutator */
+void
+thrift_framed_transport_set_property (GObject *object, guint property_id,
+ const GValue *value, GParamSpec *pspec)
+{
+ THRIFT_UNUSED_VAR (pspec);
+ ThriftFramedTransport *transport = THRIFT_FRAMED_TRANSPORT (object);
+
+ switch (property_id)
+ {
+ case PROP_THRIFT_FRAMED_TRANSPORT_TRANSPORT:
+ transport->transport = g_value_get_object (value);
+ break;
+ case PROP_THRIFT_FRAMED_TRANSPORT_READ_BUFFER_SIZE:
+ transport->r_buf_size = g_value_get_uint (value);
+ break;
+ case PROP_THRIFT_FRAMED_TRANSPORT_WRITE_BUFFER_SIZE:
+ transport->w_buf_size = g_value_get_uint (value);
+ break;
+ }
+}
+
+/* initializes the class */
+static void
+thrift_framed_transport_class_init (ThriftFramedTransportClass *cls)
+{
+ GObjectClass *gobject_class = G_OBJECT_CLASS (cls);
+ GParamSpec *param_spec = NULL;
+
+ /* setup accessors and mutators */
+ gobject_class->get_property = thrift_framed_transport_get_property;
+ gobject_class->set_property = thrift_framed_transport_set_property;
+
+ param_spec = g_param_spec_object ("transport", "transport (construct)",
+ "Thrift transport",
+ THRIFT_TYPE_TRANSPORT,
+ G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
+ g_object_class_install_property (gobject_class,
+ PROP_THRIFT_FRAMED_TRANSPORT_TRANSPORT,
+ param_spec);
+
+ param_spec = g_param_spec_uint ("r_buf_size",
+ "read buffer size (construct)",
+ "Set the read buffer size",
+ 0, /* min */
+ 1048576, /* max, 1024*1024 */
+ 512, /* default value */
+ G_PARAM_CONSTRUCT_ONLY |
+ G_PARAM_READWRITE);
+ g_object_class_install_property (gobject_class,
+ PROP_THRIFT_FRAMED_TRANSPORT_READ_BUFFER_SIZE,
+ param_spec);
+
+ param_spec = g_param_spec_uint ("w_buf_size",
+ "write buffer size (construct)",
+ "Set the write buffer size",
+ 0, /* min */
+ 1048576, /* max, 1024*1024 */
+ 512, /* default value */
+ G_PARAM_CONSTRUCT_ONLY |
+ G_PARAM_READWRITE);
+ g_object_class_install_property (gobject_class,
+ PROP_THRIFT_FRAMED_TRANSPORT_WRITE_BUFFER_SIZE,
+ param_spec);
+
+
+ ThriftTransportClass *ttc = THRIFT_TRANSPORT_CLASS (cls);
+
+ gobject_class->finalize = thrift_framed_transport_finalize;
+ ttc->is_open = thrift_framed_transport_is_open;
+ ttc->open = thrift_framed_transport_open;
+ ttc->close = thrift_framed_transport_close;
+ ttc->read = thrift_framed_transport_read;
+ ttc->read_end = thrift_framed_transport_read_end;
+ ttc->write = thrift_framed_transport_write;
+ ttc->write_end = thrift_framed_transport_write_end;
+ ttc->flush = thrift_framed_transport_flush;
+}
+
+/* implements thrift_transport_is_open */
+gboolean
+thrift_framed_transport_is_open (ThriftTransport *transport)
+{
+ ThriftFramedTransport *t = THRIFT_FRAMED_TRANSPORT (transport);
+ return THRIFT_TRANSPORT_GET_CLASS (t->transport)->is_open (t->transport);
+}
+
+/* implements thrift_transport_open */
+gboolean
+thrift_framed_transport_open (ThriftTransport *transport, GError **error)
+{
+ ThriftFramedTransport *t = THRIFT_FRAMED_TRANSPORT (transport);
+ return THRIFT_TRANSPORT_GET_CLASS (t->transport)->open (t->transport, error);
+}
+
+/* implements thrift_transport_close */
+gboolean
+thrift_framed_transport_close (ThriftTransport *transport, GError **error)
+{
+ ThriftFramedTransport *t = THRIFT_FRAMED_TRANSPORT (transport);
+ return THRIFT_TRANSPORT_GET_CLASS (t->transport)->close (t->transport, error);
+}
+
+/* implements thrift_transport_read */
+gint32
+thrift_framed_transport_read (ThriftTransport *transport, gpointer buf,
+ guint32 len, GError **error)
+{
+ ThriftFramedTransport *t = THRIFT_FRAMED_TRANSPORT (transport);
+
+ /* if we have enough buffer data to fulfill the read, just use
+ * a memcpy from the buffer */
+ if (len <= t->r_buf->len)
+ {
+ memcpy (buf, t->r_buf->data, len);
+ g_byte_array_remove_range (t->r_buf, 0, len);
+ return len;
+ }
+
+ return thrift_framed_transport_read_slow (transport, buf, len, error);
+}
+
+/* implements thrift_transport_read_end
+ * called when read is complete. nothing to do on our end. */
+gboolean
+thrift_framed_transport_read_end (ThriftTransport *transport, GError **error)
+{
+ /* satisfy -Wall */
+ THRIFT_UNUSED_VAR (transport);
+ THRIFT_UNUSED_VAR (error);
+ return TRUE;
+}
+
+/* the actual read is "slow" because it calls the underlying transport */
+gint32
+thrift_framed_transport_read_slow (ThriftTransport *transport, gpointer buf,
+ guint32 len, GError **error)
+{
+ ThriftFramedTransport *t = THRIFT_FRAMED_TRANSPORT (transport);
+ guint32 want = len;
+ guint32 have = t->r_buf->len;
+
+ // we shouldn't hit this unless the buffer doesn't have enough to read
+ assert (t->r_buf->len < want);
+
+ // first copy what we have in our buffer, if there is anything left
+ if (have > 0)
+ {
+ memcpy (buf, t->r_buf, t->r_buf->len);
+ want -= t->r_buf->len;
+ t->r_buf = g_byte_array_remove_range (t->r_buf, 0, t->r_buf->len);
+ }
+
+ // read a frame of input and buffer it
+ thrift_framed_transport_read_frame (transport, error);
+
+ // hand over what we have up to what the caller wants
+ guint32 give = want < t->r_buf->len ? want : t->r_buf->len;
+
+ // copy the data into the buffer
+ memcpy (buf + len - want, t->r_buf->data, give);
+ t->r_buf = g_byte_array_remove_range (t->r_buf, 0, give);
+ want -= give;
+
+ return (len - want);
+}
+
+/* reads a frame and puts it into the buffer */
+gboolean
+thrift_framed_transport_read_frame (ThriftTransport *transport,
+ GError **error)
+{
+ ThriftFramedTransport *t = THRIFT_FRAMED_TRANSPORT (transport);
+ gint32 sz, bytes;
+
+ /* read the size */
+ THRIFT_TRANSPORT_GET_CLASS (t->transport)->read (t->transport,
+ (guint32 *) &sz,
+ sizeof (sz), error);
+ sz = ntohl (sz);
+
+ /* create a buffer to hold the data and read that much data */
+ guchar tmpdata[sz];
+ bytes = THRIFT_TRANSPORT_GET_CLASS (t->transport)->read (t->transport,
+ tmpdata,
+ sz - sizeof (sz),
+ error);
+
+ /* add the data to the buffer */
+ g_byte_array_append (t->r_buf, tmpdata, bytes);
+
+ return TRUE;
+}
+
+/* implements thrift_transport_write */
+gboolean
+thrift_framed_transport_write (ThriftTransport *transport,
+ const gpointer buf,
+ const guint32 len, GError **error)
+{
+ ThriftFramedTransport *t = THRIFT_FRAMED_TRANSPORT (transport);
+
+ /* the length of the current buffer plus the length of the data being read */
+ if (t->w_buf->len + len <= t->w_buf_size)
+ {
+ t->w_buf = g_byte_array_append (t->w_buf, buf, len);
+ return TRUE;
+ }
+
+ return thrift_framed_transport_write_slow (transport, buf, len, error);
+}
+
+/* implements thrift_transport_write_end
+ * called when write is complete. nothing to do on our end. */
+gboolean
+thrift_framed_transport_write_end (ThriftTransport *transport, GError **error)
+{
+ /* satisfy -Wall */
+ THRIFT_UNUSED_VAR (transport);
+ THRIFT_UNUSED_VAR (error);
+ return TRUE;
+}
+
+gboolean
+thrift_framed_transport_write_slow (ThriftTransport *transport, gpointer buf,
+ guint32 len, GError **error)
+{
+ THRIFT_UNUSED_VAR (error);
+ ThriftFramedTransport *t = THRIFT_FRAMED_TRANSPORT (transport);
+
+ // append the data to the buffer and we're done
+ g_byte_array_append (t->w_buf, buf, len);
+
+ return TRUE;
+}
+
+/* implements thrift_transport_flush */
+gboolean
+thrift_framed_transport_flush (ThriftTransport *transport, GError **error)
+{
+ ThriftFramedTransport *t = THRIFT_FRAMED_TRANSPORT (transport);
+ gint32 sz_hbo, sz_nbo;
+
+ // get the size of the frame in host and network byte order
+ sz_hbo = t->w_buf->len + sizeof(sz_nbo);
+ sz_nbo = (gint32) htonl ((guint32) sz_hbo);
+
+ // copy the size of the frame and then the frame itself
+ guchar tmpdata[sz_hbo];
+ memcpy (tmpdata, (guint8 *) &sz_nbo, sizeof (sz_nbo));
+
+ if (t->w_buf->len > 0)
+ {
+ memcpy (tmpdata + sizeof (sz_nbo), t->w_buf->data, t->w_buf->len);
+ t->w_buf = g_byte_array_remove_range (t->w_buf, 0, t->w_buf->len);
+ }
+
+ // write the buffer and then empty it
+ THRIFT_TRANSPORT_GET_CLASS (t->transport)->write (t->transport,
+ tmpdata, sz_hbo,
+ error);
+
+ THRIFT_TRANSPORT_GET_CLASS (t->transport)->flush (t->transport,
+ error);
+
+ return TRUE;
+}
+
+
Added: incubator/thrift/trunk/lib/c_glib/src/transport/thrift_framed_transport.h
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/c_glib/src/transport/thrift_framed_transport.h?rev=1027933&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/c_glib/src/transport/thrift_framed_transport.h (added)
+++ incubator/thrift/trunk/lib/c_glib/src/transport/thrift_framed_transport.h Wed Oct 27 12:30:11 2010
@@ -0,0 +1,61 @@
+#ifndef _THRIFT_FRAMED_TRANSPORT_H
+#define _THRIFT_FRAMED_TRANSPORT_H
+
+#include <glib.h>
+#include <glib-object.h>
+
+#include "transport/thrift_transport.h"
+
+/*! \file thrift_framed_transport.h
+ * \brief Implementation of a Thrift framed transport. Subclasses
+ * the ThriftTransport class.
+ */
+
+/* type macros */
+#define THRIFT_TYPE_FRAMED_TRANSPORT (thrift_framed_transport_get_type ())
+#define THRIFT_FRAMED_TRANSPORT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
+ THRIFT_TYPE_FRAMED_TRANSPORT, \
+ ThriftFramedTransport))
+#define THRIFT_IS_FRAMED_TRANSPORT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+ THRIFT_TYPE_FRAMED_TRANSPORT))
+#define THRIFT_FRAMED_TRANSPORT_CLASS(c) (G_TYPE_CHECK_CLASS_CAST ((c), \
+ THRIFT_TYPE_FRAMED_TRANSPORT, \
+ ThriftFramedTransportClass))
+#define THRIFT_IS_FRAMED_TRANSPORT_CLASS(c) (G_TYPE_CHECK_CLASS_TYPE ((c), \
+ THRIFT_TYPE_FRAMED_TRANSPORT)
+#define THRIFT_FRAMED_TRANSPORT_GET_CLASS(obj) \
+ (G_TYPE_INSTANCE_GET_CLASS ((obj), \
+ THRIFT_TYPE_FRAMED_TRANSPORT, \
+ ThriftFramedTransportClass))
+
+/*!
+ * ThriftFramedTransport instance.
+ */
+struct _ThriftFramedTransport
+{
+ ThriftTransport parent;
+
+ /* protected */
+ ThriftTransport *transport;
+
+ /* private */
+ GByteArray *r_buf;
+ GByteArray *w_buf;
+ guint32 r_buf_size;
+ guint32 w_buf_size;
+};
+typedef struct _ThriftFramedTransport ThriftFramedTransport;
+
+/*!
+ * ThriftFramedTransport class.
+ */
+struct _ThriftFramedTransportClass
+{
+ ThriftTransportClass parent;
+};
+typedef struct _ThriftFramedTransportClass ThriftFramedTransportClass;
+
+/* used by THRIFT_TYPE_FRAMED_TRANSPORT */
+GType thrift_framed_transport_get_type (void);
+
+#endif