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 [5/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/transport/thrift_memory_buffer.c
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/c_glib/src/transport/thrift_memory_buffer.c?rev=1027933&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/c_glib/src/transport/thrift_memory_buffer.c (added)
+++ incubator/thrift/trunk/lib/c_glib/src/transport/thrift_memory_buffer.c Wed Oct 27 12:30:11 2010
@@ -0,0 +1,260 @@
+#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_memory_buffer.h"
+
+/* object properties */
+enum _ThriftMemoryBufferProperties
+{
+ PROP_0,
+ PROP_THRIFT_MEMORY_BUFFER_BUFFER_SIZE,
+};
+
+/* forward declarations */
+static void thrift_memory_buffer_instance_init (ThriftMemoryBuffer *self);
+static void thrift_memory_buffer_class_init (ThriftMemoryBufferClass *cls);
+
+
+gboolean thrift_memory_buffer_is_open (ThriftTransport *transport);
+gboolean thrift_memory_buffer_open (ThriftTransport *transport,
+ GError **error);
+gboolean thrift_memory_buffer_close (ThriftTransport *transport,
+ GError **error);
+gint32 thrift_memory_buffer_read (ThriftTransport *transport, gpointer buf,
+ guint32 len, GError **error);
+gboolean thrift_memory_buffer_read_end (ThriftTransport *transport,
+ GError **error);
+gboolean thrift_memory_buffer_write (ThriftTransport *transport,
+ const gpointer buf,
+ const guint32 len, GError **error);
+gboolean thrift_memory_buffer_write_end (ThriftTransport *transport,
+ GError **error);
+gboolean thrift_memory_buffer_flush (ThriftTransport *transport,
+ GError **error);
+
+GType
+thrift_memory_buffer_get_type (void)
+{
+ static GType type = 0;
+
+ if (type == 0)
+ {
+ static const GTypeInfo info =
+ {
+ sizeof (ThriftMemoryBufferClass),
+ NULL, /* base_init */
+ NULL, /* base_finalize */
+ (GClassInitFunc) thrift_memory_buffer_class_init,
+ NULL, /* class finalize */
+ NULL, /* class data */
+ sizeof (ThriftMemoryBuffer),
+ 0, /* n_preallocs */
+ (GInstanceInitFunc) thrift_memory_buffer_instance_init,
+ NULL, /* value_table */
+ };
+
+ type = g_type_register_static (THRIFT_TYPE_TRANSPORT,
+ "ThriftMemoryBuffer", &info, 0);
+ }
+
+ return type;
+}
+
+/* initializes the instance */
+static void
+thrift_memory_buffer_instance_init (ThriftMemoryBuffer *transport)
+{
+ transport->buf = g_byte_array_new ();
+}
+
+/* destructor */
+static void
+thrift_memory_buffer_finalize (GObject *object)
+{
+ ThriftMemoryBuffer *transport = THRIFT_MEMORY_BUFFER (object);
+
+ if (transport->buf != NULL)
+ {
+ g_byte_array_free (transport->buf, TRUE);
+ }
+ transport->buf = NULL;
+}
+
+/* property accessor */
+void
+thrift_memory_buffer_get_property (GObject *object, guint property_id,
+ GValue *value, GParamSpec *pspec)
+{
+ THRIFT_UNUSED_VAR (pspec);
+ ThriftMemoryBuffer *transport = THRIFT_MEMORY_BUFFER (object);
+
+ switch (property_id)
+ {
+ case PROP_THRIFT_MEMORY_BUFFER_BUFFER_SIZE:
+ g_value_set_uint (value, transport->buf_size);
+ break;
+ }
+}
+
+/* property mutator */
+void
+thrift_memory_buffer_set_property (GObject *object, guint property_id,
+ const GValue *value, GParamSpec *pspec)
+{
+ THRIFT_UNUSED_VAR (pspec);
+ ThriftMemoryBuffer *transport = THRIFT_MEMORY_BUFFER (object);
+
+ switch (property_id)
+ {
+ case PROP_THRIFT_MEMORY_BUFFER_BUFFER_SIZE:
+ transport->buf_size = g_value_get_uint (value);
+ break;
+ }
+}
+
+/* initializes the class */
+static void
+thrift_memory_buffer_class_init (ThriftMemoryBufferClass *cls)
+{
+ GObjectClass *gobject_class = G_OBJECT_CLASS (cls);
+ GParamSpec *param_spec = NULL;
+
+ /* setup accessors and mutators */
+ gobject_class->get_property = thrift_memory_buffer_get_property;
+ gobject_class->set_property = thrift_memory_buffer_set_property;
+
+ param_spec = g_param_spec_uint ("buf_size",
+ "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_MEMORY_BUFFER_BUFFER_SIZE,
+ param_spec);
+
+ ThriftTransportClass *ttc = THRIFT_TRANSPORT_CLASS (cls);
+
+ gobject_class->finalize = thrift_memory_buffer_finalize;
+ ttc->is_open = thrift_memory_buffer_is_open;
+ ttc->open = thrift_memory_buffer_open;
+ ttc->close = thrift_memory_buffer_close;
+ ttc->read = thrift_memory_buffer_read;
+ ttc->read_end = thrift_memory_buffer_read_end;
+ ttc->write = thrift_memory_buffer_write;
+ ttc->write_end = thrift_memory_buffer_write_end;
+ ttc->flush = thrift_memory_buffer_flush;
+}
+
+/* implements thrift_transport_is_open */
+gboolean
+thrift_memory_buffer_is_open (ThriftTransport *transport)
+{
+ THRIFT_UNUSED_VAR (transport);
+ return TRUE;
+}
+
+/* implements thrift_transport_open */
+gboolean
+thrift_memory_buffer_open (ThriftTransport *transport, GError **error)
+{
+ THRIFT_UNUSED_VAR (transport);
+ THRIFT_UNUSED_VAR (error);
+ return TRUE;
+}
+
+/* implements thrift_transport_close */
+gboolean
+thrift_memory_buffer_close (ThriftTransport *transport, GError **error)
+{
+ THRIFT_UNUSED_VAR (transport);
+ THRIFT_UNUSED_VAR (error);
+ return TRUE;
+}
+
+/* implements thrift_transport_read */
+gint32
+thrift_memory_buffer_read (ThriftTransport *transport, gpointer buf,
+ guint32 len, GError **error)
+{
+ THRIFT_UNUSED_VAR (error);
+ ThriftMemoryBuffer *t = THRIFT_MEMORY_BUFFER (transport);
+ guint32 give = len;
+
+ /* if the requested bytes are more than what we have available,
+ * just give all that we have the buffer */
+ if (t->buf->len < len)
+ {
+ give = t->buf->len;
+ }
+
+ memcpy (buf, t->buf->data, give);
+ g_byte_array_remove_range (t->buf, 0, give);
+
+ return give;
+}
+
+/* implements thrift_transport_read_end
+ * called when read is complete. nothing to do on our end. */
+gboolean
+thrift_memory_buffer_read_end (ThriftTransport *transport, GError **error)
+{
+ /* satisfy -Wall */
+ THRIFT_UNUSED_VAR (transport);
+ THRIFT_UNUSED_VAR (error);
+ return TRUE;
+}
+
+/* implements thrift_transport_write */
+gboolean
+thrift_memory_buffer_write (ThriftTransport *transport,
+ const gpointer buf,
+ const guint32 len, GError **error)
+{
+ THRIFT_UNUSED_VAR (error);
+
+ ThriftMemoryBuffer *t = THRIFT_MEMORY_BUFFER (transport);
+
+ /* return an exception if the buffer doesn't have enough space. */
+ if (len > t->buf_size - t->buf->len)
+ {
+ g_set_error (error, THRIFT_TRANSPORT_ERROR, THRIFT_TRANSPORT_ERROR_SEND,
+ "unable to write %d bytes to buffer of length %d",
+ len, t->buf_size);
+ return FALSE;
+ } else {
+ t->buf = g_byte_array_append (t->buf, buf, len);
+ return TRUE;
+ }
+}
+
+/* implements thrift_transport_write_end
+ * called when write is complete. nothing to do on our end. */
+gboolean
+thrift_memory_buffer_write_end (ThriftTransport *transport, GError **error)
+{
+ /* satisfy -Wall */
+ THRIFT_UNUSED_VAR (transport);
+ THRIFT_UNUSED_VAR (error);
+ return TRUE;
+}
+
+/* implements thrift_transport_flush */
+gboolean
+thrift_memory_buffer_flush (ThriftTransport *transport, GError **error)
+{
+ THRIFT_UNUSED_VAR (transport);
+ THRIFT_UNUSED_VAR (error);
+
+ return TRUE;
+}
+
+
Added: incubator/thrift/trunk/lib/c_glib/src/transport/thrift_memory_buffer.h
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/c_glib/src/transport/thrift_memory_buffer.h?rev=1027933&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/c_glib/src/transport/thrift_memory_buffer.h (added)
+++ incubator/thrift/trunk/lib/c_glib/src/transport/thrift_memory_buffer.h Wed Oct 27 12:30:11 2010
@@ -0,0 +1,55 @@
+#ifndef _THRIFT_MEMORY_BUFFER_H
+#define _THRIFT_MEMORY_BUFFER_H
+
+#include <glib.h>
+#include <glib-object.h>
+
+#include "transport/thrift_transport.h"
+
+/*! \file thrift_memory_buffer.h
+ * \brief Implementation of a Thrift memory buffer transport.
+ */
+
+/* type macros */
+#define THRIFT_TYPE_MEMORY_BUFFER (thrift_memory_buffer_get_type ())
+#define THRIFT_MEMORY_BUFFER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
+ THRIFT_TYPE_MEMORY_BUFFER, \
+ ThriftMemoryBuffer))
+#define THRIFT_IS_MEMORY_BUFFER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+ THRIFT_TYPE_MEMORY_BUFFER))
+#define THRIFT_MEMORY_BUFFER_CLASS(c) (G_TYPE_CHECK_CLASS_CAST ((c), \
+ THRIFT_TYPE_MEMORY_BUFFER, \
+ ThriftMemoryBufferClass))
+#define THRIFT_IS_MEMORY_BUFFER_CLASS(c) (G_TYPE_CHECK_CLASS_TYPE ((c), \
+ THRIFT_TYPE_MEMORY_BUFFER)
+#define THRIFT_MEMORY_BUFFER_GET_CLASS(obj) \
+ (G_TYPE_INSTANCE_GET_CLASS ((obj), \
+ THRIFT_TYPE_MEMORY_BUFFER, \
+ ThriftMemoryBufferClass))
+
+/*!
+ * ThriftMemoryBuffer instance.
+ */
+struct _ThriftMemoryBuffer
+{
+ ThriftTransport parent;
+
+ /* private */
+ GByteArray *buf;
+ guint32 buf_size;
+};
+typedef struct _ThriftMemoryBuffer ThriftMemoryBuffer;
+
+/*!
+ * ThriftMemoryBuffer class.
+ */
+struct _ThriftMemoryBufferClass
+{
+ ThriftTransportClass parent;
+};
+typedef struct _ThriftMemoryBufferClass ThriftMemoryBufferClass;
+
+/* used by THRIFT_TYPE_MEMORY_BUFFER */
+GType thrift_memory_buffer_get_type (void);
+
+#endif
Added: incubator/thrift/trunk/lib/c_glib/src/transport/thrift_server_socket.c
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/c_glib/src/transport/thrift_server_socket.c?rev=1027933&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/c_glib/src/transport/thrift_server_socket.c (added)
+++ incubator/thrift/trunk/lib/c_glib/src/transport/thrift_server_socket.c Wed Oct 27 12:30:11 2010
@@ -0,0 +1,272 @@
+#include <errno.h>
+#include <netdb.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "thrift.h"
+#include "transport/thrift_socket.h"
+#include "transport/thrift_transport.h"
+#include "transport/thrift_server_transport.h"
+#include "transport/thrift_server_socket.h"
+
+/* object properties */
+enum _ThriftServerSocketProperties
+{
+ PROP_0,
+ PROP_THRIFT_SERVER_SOCKET_PORT,
+ PROP_THRIFT_SERVER_SOCKET_BACKLOG
+};
+
+/* define the GError domain string */
+#define THRIFT_SERVER_SOCKET_ERROR_DOMAIN "thrift-server-socket-error-quark"
+
+/* for errors coming from socket() and connect() */
+extern int errno;
+
+/* forward declarations */
+static void thrift_server_socket_instance_init (ThriftServerSocket *self);
+static void thrift_server_socket_class_init (ThriftServerSocketClass *cls);
+
+gboolean thrift_server_socket_listen (ThriftServerTransport *transport,
+ GError **error);
+ThriftTransport *thrift_server_socket_accept (ThriftServerTransport *transport,
+ GError **error);
+gboolean thrift_server_socket_close (ThriftServerTransport *transport,
+ GError **error);
+
+GType
+thrift_server_socket_get_type (void)
+{
+ static GType type = 0;
+
+ if (type == 0)
+ {
+ static const GTypeInfo info =
+ {
+ sizeof (ThriftServerSocketClass),
+ NULL, /* base_init */
+ NULL, /* base_finalize */
+ (GClassInitFunc) thrift_server_socket_class_init,
+ NULL, /* class finalize */
+ NULL, /* class data */
+ sizeof (ThriftServerSocket),
+ 0, /* n_preallocs */
+ (GInstanceInitFunc) thrift_server_socket_instance_init,
+ NULL, /* value_table */
+ };
+
+ type = g_type_register_static (THRIFT_TYPE_SERVER_TRANSPORT,
+ "ThriftServerSocket", &info, 0);
+ }
+
+ return type;
+}
+
+/* define the GError domain for this implementation */
+GQuark
+thrift_server_socket_error_quark (void)
+{
+ return g_quark_from_static_string(THRIFT_SERVER_SOCKET_ERROR_DOMAIN);
+}
+
+/* initializes the instance */
+static void
+thrift_server_socket_instance_init (ThriftServerSocket *socket)
+{
+ socket->sd = 0;
+}
+
+/* destructor */
+static void
+thrift_server_socket_finalize (GObject *object)
+{
+ ThriftServerSocket *socket = THRIFT_SERVER_SOCKET (object);
+
+ if (socket->sd != 0)
+ {
+ close (socket->sd);
+ }
+ socket->sd = 0;
+}
+
+/* property accessor */
+void
+thrift_server_socket_get_property (GObject *object, guint property_id,
+ GValue *value, GParamSpec *pspec)
+{
+ ThriftServerSocket *socket = THRIFT_SERVER_SOCKET (object);
+
+ switch (property_id)
+ {
+ case PROP_THRIFT_SERVER_SOCKET_PORT:
+ g_value_set_uint (value, socket->port);
+ break;
+ case PROP_THRIFT_SERVER_SOCKET_BACKLOG:
+ g_value_set_uint (value, socket->backlog);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+ break;
+ }
+}
+
+/* property mutator */
+void
+thrift_server_socket_set_property (GObject *object, guint property_id,
+ const GValue *value, GParamSpec *pspec)
+{
+ ThriftServerSocket *socket = THRIFT_SERVER_SOCKET (object);
+
+ switch (property_id)
+ {
+ case PROP_THRIFT_SERVER_SOCKET_PORT:
+ socket->port = g_value_get_uint (value);
+ break;
+ case PROP_THRIFT_SERVER_SOCKET_BACKLOG:
+ socket->backlog = g_value_get_uint (value);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+ break;
+ }
+}
+
+/* initializes the class */
+static void
+thrift_server_socket_class_init (ThriftServerSocketClass *cls)
+{
+ GObjectClass *gobject_class = G_OBJECT_CLASS (cls);
+ GParamSpec *param_spec = NULL;
+
+ /* setup accessors and mutators */
+ gobject_class->get_property = thrift_server_socket_get_property;
+ gobject_class->set_property = thrift_server_socket_set_property;
+
+ param_spec = g_param_spec_uint ("port",
+ "port (construct)",
+ "Set the port to listen to",
+ 0, /* min */
+ 65534, /* max */
+ 9090, /* default by convention */
+ G_PARAM_CONSTRUCT_ONLY |
+ G_PARAM_READWRITE);
+ g_object_class_install_property (gobject_class,
+ PROP_THRIFT_SERVER_SOCKET_PORT,
+ param_spec);
+
+ param_spec = g_param_spec_uint ("backlog",
+ "backlog (construct)",
+ "Set the accept backlog",
+ 0, /* max */
+ 65534, /* max */
+ 1024, /* default */
+ G_PARAM_CONSTRUCT_ONLY |
+ G_PARAM_READWRITE);
+ g_object_class_install_property (gobject_class,
+ PROP_THRIFT_SERVER_SOCKET_BACKLOG,
+ param_spec);
+
+ gobject_class->finalize = thrift_server_socket_finalize;
+
+ ThriftServerTransportClass *tstc = THRIFT_SERVER_TRANSPORT_CLASS (cls);
+ tstc->listen = thrift_server_socket_listen;
+ tstc->accept = thrift_server_socket_accept;
+ tstc->close = thrift_server_socket_close;
+}
+
+gboolean
+thrift_server_socket_listen (ThriftServerTransport *transport, GError **error)
+{
+ int enabled = 1; /* for setsockopt() */
+ struct sockaddr_in pin;
+ ThriftServerSocket *tsocket = THRIFT_SERVER_SOCKET (transport);
+
+ /* create a address structure */
+ memset (&pin, 0, sizeof(pin));
+ pin.sin_family = AF_INET;
+ pin.sin_addr.s_addr = INADDR_ANY;
+ pin.sin_port = htons(tsocket->port);
+
+ /* create a socket */
+ if ((tsocket->sd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
+ {
+ g_set_error (error, THRIFT_SERVER_SOCKET_ERROR,
+ THRIFT_SERVER_SOCKET_ERROR_SOCKET,
+ "failed to create socket - %s", strerror (errno));
+ return FALSE;
+ }
+
+ if (setsockopt(tsocket->sd, SOL_SOCKET, SO_REUSEADDR, &enabled,
+ sizeof(enabled)) == -1)
+ {
+ g_set_error (error, THRIFT_SERVER_SOCKET_ERROR,
+ THRIFT_SERVER_SOCKET_ERROR_SETSOCKOPT,
+ "unable to set SO_REUSEADDR - %s", strerror(errno));
+ return FALSE;
+ }
+
+ /* bind to the socket */
+ if (bind(tsocket->sd, (struct sockaddr *) &pin, sizeof(pin)) == -1)
+ {
+ g_set_error (error, THRIFT_SERVER_SOCKET_ERROR,
+ THRIFT_SERVER_SOCKET_ERROR_BIND,
+ "failed to bind to port %d - %s",
+ tsocket->port, strerror(errno));
+ return FALSE;
+ }
+
+ if (listen(tsocket->sd, tsocket->backlog) == -1)
+ {
+ g_set_error (error, THRIFT_SERVER_SOCKET_ERROR,
+ THRIFT_SERVER_SOCKET_ERROR_LISTEN,
+ "failed to listen to port %d - %s",
+ tsocket->port, strerror(errno));
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+ThriftTransport *
+thrift_server_socket_accept (ThriftServerTransport *transport, GError **error)
+{
+ int sd = 0;
+ guint addrlen = 0;
+ struct sockaddr_in address;
+ ThriftSocket *socket = NULL;
+
+ ThriftServerSocket *tsocket = THRIFT_SERVER_SOCKET (transport);
+
+ if ((sd = accept(tsocket->sd, (struct sockaddr *) &address, &addrlen)) == -1)
+ {
+ g_set_error (error, THRIFT_SERVER_SOCKET_ERROR,
+ THRIFT_SERVER_SOCKET_ERROR_ACCEPT,
+ "failed to accept connection - %s",
+ strerror(errno));
+ return FALSE;
+ }
+
+ socket = g_object_new (THRIFT_TYPE_SOCKET, NULL);
+ socket->sd = sd;
+
+ return THRIFT_TRANSPORT(socket);
+}
+
+gboolean
+thrift_server_socket_close (ThriftServerTransport *transport, GError **error)
+{
+ ThriftServerSocket *tsocket = THRIFT_SERVER_SOCKET (transport);
+
+ if (close (tsocket->sd) == -1)
+ {
+ g_set_error (error, THRIFT_SERVER_SOCKET_ERROR,
+ THRIFT_SERVER_SOCKET_ERROR_CLOSE,
+ "unable to close socket - %s", strerror(errno));
+ return FALSE;
+ }
+ tsocket->sd = 0;
+
+ return TRUE;
+}
+
Added: incubator/thrift/trunk/lib/c_glib/src/transport/thrift_server_socket.h
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/c_glib/src/transport/thrift_server_socket.h?rev=1027933&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/c_glib/src/transport/thrift_server_socket.h (added)
+++ incubator/thrift/trunk/lib/c_glib/src/transport/thrift_server_socket.h Wed Oct 27 12:30:11 2010
@@ -0,0 +1,73 @@
+#ifndef _THRIFT_SERVER_SOCKET_H
+#define _THRIFT_SERVER_SOCKET_H
+
+#include <glib-object.h>
+
+#include "thrift_server_transport.h"
+
+/*! \file thrift_server_socket.h
+ * \brief Socket implementation of a Thrift server transport. Implements the
+ * ThriftServerTransport class.
+ */
+
+/* type macros */
+#define THRIFT_TYPE_SERVER_SOCKET (thrift_server_socket_get_type ())
+#define THRIFT_SERVER_SOCKET(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
+ THRIFT_TYPE_SERVER_SOCKET, \
+ ThriftServerSocket))
+#define THRIFT_IS_SERVER_SOCKET(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+ THRIFT_TYPE_SERVER_SOCKET))
+#define THRIFT_SERVER_SOCKET_CLASS(c) (G_TYPE_CHECK_CLASS_CAST ((c), \
+ THRIFT_TYPE_SERVER_SOCKET, \
+ ThriftServerSocketClass))
+#define THRIFT_IS_SERVER_SOCKET_CLASS(c) (G_TYPE_CHECK_CLASS_TYPE ((c), \
+ THRIFT_TYPE_SERVER_SOCKET))
+#define THRIFT_SERVER_SOCKET_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), \
+ THRIFT_TYPE_SERVER_SOCKET, \
+ ThriftServerSocketClass))
+
+/*!
+ * Thrift ServerSocket instance.
+ */
+struct _ThriftServerSocket
+{
+ ThriftServerTransport parent;
+
+ /* private */
+ gshort port;
+ gshort backlog;
+ int sd;
+ guint8 *buf;
+ guint32 buf_size;
+ guint32 buf_len;
+};
+typedef struct _ThriftServerSocket ThriftServerSocket;
+
+/*!
+ * Thrift ServerSocket class.
+ */
+struct _ThriftServerSocketClass
+{
+ ThriftServerTransportClass parent;
+};
+typedef struct _ThriftServerSocketClass ThriftServerSocketClass;
+
+/* used by THRIFT_TYPE_SERVER_SOCKET */
+GType thrift_server_socket_get_type (void);
+
+/* define error/exception types */
+typedef enum
+{
+ THRIFT_SERVER_SOCKET_ERROR_SOCKET,
+ THRIFT_SERVER_SOCKET_ERROR_SETSOCKOPT,
+ THRIFT_SERVER_SOCKET_ERROR_BIND,
+ THRIFT_SERVER_SOCKET_ERROR_LISTEN,
+ THRIFT_SERVER_SOCKET_ERROR_ACCEPT,
+ THRIFT_SERVER_SOCKET_ERROR_CLOSE
+} ThriftServerSocketError;
+
+/* define a error domain for GError to use */
+GQuark thrift_server_socket_error_quark (void);
+#define THRIFT_SERVER_SOCKET_ERROR (thrift_server_socket_error_quark ())
+
+#endif
Added: incubator/thrift/trunk/lib/c_glib/src/transport/thrift_server_transport.c
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/c_glib/src/transport/thrift_server_transport.c?rev=1027933&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/c_glib/src/transport/thrift_server_transport.c (added)
+++ incubator/thrift/trunk/lib/c_glib/src/transport/thrift_server_transport.c Wed Oct 27 12:30:11 2010
@@ -0,0 +1,70 @@
+#include "thrift.h"
+#include "transport/thrift_transport.h"
+#include "transport/thrift_server_transport.h"
+
+/* forward declarations */
+static void thrift_server_transport_class_init (ThriftServerTransportClass *c);
+
+/* define ThriftTransportClass type */
+GType
+thrift_server_transport_get_type (void)
+{
+ static GType type = 0;
+
+ if (type == 0)
+ {
+ static const GTypeInfo info =
+ {
+ sizeof (ThriftServerTransportClass),
+ NULL, /* base_init */
+ NULL, /* base_finalize */
+ (GClassInitFunc) thrift_server_transport_class_init,
+ NULL, /* class_finalize */
+ NULL, /* class_data */
+ sizeof (ThriftServerTransport),
+ 0, /* n_preallocs */
+ NULL, /* instance_init */
+ NULL, /* value_table */
+ };
+
+ type = g_type_register_static (G_TYPE_OBJECT,
+ "ThriftServerTransport",
+ &info, G_TYPE_FLAG_ABSTRACT);
+ }
+
+ return type;
+}
+
+/* base initializer for the server transport interface */
+static void
+thrift_server_transport_class_init (ThriftServerTransportClass *c)
+{
+ c->listen = thrift_server_transport_listen;
+ c->accept = thrift_server_transport_accept;
+ c->close = thrift_server_transport_close;
+}
+
+gboolean
+thrift_server_transport_listen (ThriftServerTransport *transport,
+ GError **error)
+{
+ return THRIFT_SERVER_TRANSPORT_GET_CLASS (transport)->listen (transport,
+ error);
+}
+
+ThriftTransport *
+thrift_server_transport_accept (ThriftServerTransport *transport,
+ GError **error)
+{
+ return THRIFT_SERVER_TRANSPORT_GET_CLASS (transport)->accept (transport,
+ error);
+}
+
+gboolean
+thrift_server_transport_close (ThriftServerTransport *transport, GError **error)
+{
+ return THRIFT_SERVER_TRANSPORT_GET_CLASS (transport)->close (transport,
+ error);
+}
+
+
Added: incubator/thrift/trunk/lib/c_glib/src/transport/thrift_server_transport.h
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/c_glib/src/transport/thrift_server_transport.h?rev=1027933&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/c_glib/src/transport/thrift_server_transport.h (added)
+++ incubator/thrift/trunk/lib/c_glib/src/transport/thrift_server_transport.h Wed Oct 27 12:30:11 2010
@@ -0,0 +1,72 @@
+#ifndef _THRIFT_SERVER_TRANSPORT_H
+#define _THRIFT_SERVER_TRANSPORT_H
+
+#include <glib-object.h>
+
+#include "thrift_transport.h"
+
+/*! \file thrift_server_transport.h
+ * \brief Abstract class for Thrift server transports.
+ */
+
+/* type macros */
+#define THRIFT_TYPE_SERVER_TRANSPORT (thrift_server_transport_get_type ())
+#define THRIFT_SERVER_TRANSPORT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
+ THRIFT_TYPE_SERVER_TRANSPORT, \
+ ThriftServerTransport))
+#define THRIFT_IS_SERVER_TRANSPORT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+ THRIFT_TYPE_SERVER_TRANSPORT))
+#define THRIFT_SERVER_TRANSPORT_CLASS(c) (G_TYPE_CHECK_CLASS_CAST ((c), \
+ THRIFT_TYPE_SERVER_TRANSPORT, \
+ ThriftServerTransportClass))
+#define THRIFT_IS_SERVER_TRANSPORT_CLASS(c) (G_TYPE_CHECK_CLASS_TYPE ((c), \
+ THRIFT_TYPE_SERVER_TRANSPORT))
+#define THRIFT_SERVER_TRANSPORT_GET_CLASS(obj) \
+ (G_TYPE_INSTANCE_GET_CLASS ((obj), THRIFT_TYPE_SERVER_TRANSPORT, \
+ ThriftServerTransportClass))
+
+struct _ThriftServerTransport
+{
+ GObject parent;
+};
+typedef struct _ThriftServerTransport ThriftServerTransport;
+
+/*!
+ * Thrift Transport class
+ */
+struct _ThriftServerTransportClass
+{
+ GObjectClass parent;
+
+ /* vtable */
+ gboolean (*listen) (ThriftServerTransport *transport, GError **error);
+ ThriftTransport *(*accept) (ThriftServerTransport *transport, GError **error);
+ gboolean (*close) (ThriftServerTransport *transport, GError **error);
+};
+typedef struct _ThriftServerTransportClass ThriftServerTransportClass;
+
+/* used by THRIFT_TYPE_SERVER_TRANSPORT */
+GType thrift_server_transport_get_type (void);
+
+/*!
+ * Listen for new connections.
+ * \public \memberof ThriftServerTransportClass
+ */
+gboolean thrift_server_transport_listen (ThriftServerTransport *transport,
+ GError **error);
+
+/*!
+ * Accept a connection.
+ * \public \memberof ThriftServerTransportClass
+ */
+ThriftTransport *thrift_server_transport_accept
+ (ThriftServerTransport *transport, GError **error);
+
+/*!
+ * Close the transport.
+ * \public \memberof ThriftServerTransportClass
+ */
+gboolean thrift_server_transport_close (ThriftServerTransport *transport,
+ GError **error);
+
+#endif /* _THRIFT_SERVER_TRANSPORT_H */
Added: incubator/thrift/trunk/lib/c_glib/src/transport/thrift_socket.c
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/c_glib/src/transport/thrift_socket.c?rev=1027933&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/c_glib/src/transport/thrift_socket.c (added)
+++ incubator/thrift/trunk/lib/c_glib/src/transport/thrift_socket.c Wed Oct 27 12:30:11 2010
@@ -0,0 +1,334 @@
+#include <errno.h>
+#include <netdb.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "thrift.h"
+#include "transport/thrift_transport.h"
+#include "transport/thrift_socket.h"
+
+/* object properties */
+enum _ThriftSocketProperties
+{
+ PROP_0,
+ PROP_THRIFT_SOCKET_HOSTNAME,
+ PROP_THRIFT_SOCKET_PORT
+};
+
+/* for errors coming from socket() and connect() */
+extern int errno;
+
+/* forward declarations */
+static void thrift_socket_instance_init (ThriftSocket *self);
+static void thrift_socket_class_init (ThriftSocketClass *cls);
+
+gboolean thrift_socket_is_open (ThriftTransport *transport);
+gboolean thrift_socket_open (ThriftTransport *transport, GError **error);
+gboolean thrift_socket_close (ThriftTransport *transport, GError **error);
+gint32 thrift_socket_read (ThriftTransport *transport, gpointer buf,
+ guint32 len, GError **error);
+gboolean thrift_socket_read_end (ThriftTransport *transport, GError **error);
+gboolean thrift_socket_write (ThriftTransport *transport, const gpointer buf,
+ const guint32 len, GError **error);
+gboolean thrift_socket_write_end (ThriftTransport *transport, GError **error);
+gboolean thrift_socket_flush (ThriftTransport *transport, GError **error);
+
+GType
+thrift_socket_get_type (void)
+{
+ static GType type = 0;
+
+ if (type == 0)
+ {
+ static const GTypeInfo info =
+ {
+ sizeof (ThriftSocketClass),
+ NULL, /* base_init */
+ NULL, /* base_finalize */
+ (GClassInitFunc) thrift_socket_class_init,
+ NULL, /* class finalize */
+ NULL, /* class data */
+ sizeof (ThriftSocket),
+ 0, /* n_preallocs */
+ (GInstanceInitFunc) thrift_socket_instance_init,
+ NULL, /* value_table */
+ };
+
+ type = g_type_register_static (THRIFT_TYPE_TRANSPORT,
+ "ThriftSocket", &info, 0);
+ }
+
+ return type;
+}
+
+/* initializes the instance */
+static void
+thrift_socket_instance_init (ThriftSocket *socket)
+{
+ socket->sd = 0;
+}
+
+/* destructor */
+static void
+thrift_socket_finalize (GObject *object)
+{
+ ThriftSocket *socket = THRIFT_SOCKET (object);
+
+ if (socket->hostname != NULL)
+ {
+ g_free (socket->hostname);
+ }
+ socket->hostname = NULL;
+
+ if (socket->sd != 0)
+ {
+ close (socket->sd);
+ }
+ socket->sd = 0;
+}
+
+/* property accessor */
+void
+thrift_socket_get_property (GObject *object, guint property_id,
+ GValue *value, GParamSpec *pspec)
+{
+ THRIFT_UNUSED_VAR (pspec);
+ ThriftSocket *socket = THRIFT_SOCKET (object);
+
+ switch (property_id)
+ {
+ case PROP_THRIFT_SOCKET_HOSTNAME:
+ g_value_set_string (value, socket->hostname);
+ break;
+ case PROP_THRIFT_SOCKET_PORT:
+ g_value_set_uint (value, socket->port);
+ break;
+ }
+}
+
+/* property mutator */
+void
+thrift_socket_set_property (GObject *object, guint property_id,
+ const GValue *value, GParamSpec *pspec)
+{
+ THRIFT_UNUSED_VAR (pspec);
+ ThriftSocket *socket = THRIFT_SOCKET (object);
+
+ switch (property_id)
+ {
+ case PROP_THRIFT_SOCKET_HOSTNAME:
+ socket->hostname = g_strdup (g_value_get_string (value));
+ break;
+ case PROP_THRIFT_SOCKET_PORT:
+ socket->port = g_value_get_uint (value);
+ break;
+ }
+}
+
+/* initializes the class */
+static void
+thrift_socket_class_init (ThriftSocketClass *cls)
+{
+ GObjectClass *gobject_class = G_OBJECT_CLASS (cls);
+ GParamSpec *param_spec = NULL;
+
+ /* setup accessors and mutators */
+ gobject_class->get_property = thrift_socket_get_property;
+ gobject_class->set_property = thrift_socket_set_property;
+
+ param_spec = g_param_spec_string ("hostname",
+ "hostname (construct)",
+ "Set the hostname of the remote host",
+ "localhost", /* default value */
+ G_PARAM_CONSTRUCT_ONLY |
+ G_PARAM_READWRITE);
+ g_object_class_install_property (gobject_class, PROP_THRIFT_SOCKET_HOSTNAME,
+ param_spec);
+
+ param_spec = g_param_spec_uint ("port",
+ "port (construct)",
+ "Set the port of the remote host",
+ 0, /* min */
+ 65534, /* max */
+ 9090, /* default by convention */
+ G_PARAM_CONSTRUCT_ONLY |
+ G_PARAM_READWRITE);
+ g_object_class_install_property (gobject_class, PROP_THRIFT_SOCKET_PORT,
+ param_spec);
+
+ ThriftTransportClass *ttc = THRIFT_TRANSPORT_CLASS (cls);
+
+ gobject_class->finalize = thrift_socket_finalize;
+ ttc->is_open = thrift_socket_is_open;
+ ttc->open = thrift_socket_open;
+ ttc->close = thrift_socket_close;
+ ttc->read = thrift_socket_read;
+ ttc->read_end = thrift_socket_read_end;
+ ttc->write = thrift_socket_write;
+ ttc->write_end = thrift_socket_write_end;
+ ttc->flush = thrift_socket_flush;
+}
+
+/* implements thrift_transport_is_open */
+gboolean
+thrift_socket_is_open (ThriftTransport *transport)
+{
+ ThriftSocket *socket = THRIFT_SOCKET (transport);
+ return socket->sd != 0;
+}
+
+/* implements thrift_transport_open */
+gboolean
+thrift_socket_open (ThriftTransport *transport, GError **error)
+{
+ struct hostent *hp = NULL;
+ struct sockaddr_in pin;
+
+ ThriftSocket *tsocket = THRIFT_SOCKET (transport);
+ g_return_val_if_fail (tsocket->sd == 0, FALSE);
+
+ /* lookup the destination host */
+ if ((hp = gethostbyname (tsocket->hostname)) == NULL)
+ {
+ /* host lookup failed, bail out with an error */
+ g_set_error (error, THRIFT_TRANSPORT_ERROR, THRIFT_TRANSPORT_ERROR_HOST,
+ "host lookup failed for %s:%d - %s",
+ tsocket->hostname, tsocket->port,
+ hstrerror (h_errno));
+ return FALSE;
+ }
+
+ /* create a socket structure */
+ memset (&pin, 0, sizeof(pin));
+ pin.sin_family = AF_INET;
+ pin.sin_addr.s_addr = ((struct in_addr *) (hp->h_addr))->s_addr;
+ pin.sin_port = htons (tsocket->port);
+
+ /* create the socket */
+ if ((tsocket->sd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
+ {
+ g_set_error (error, THRIFT_TRANSPORT_ERROR, THRIFT_TRANSPORT_ERROR_SOCKET,
+ "failed to create socket for host %s:%d - %s",
+ tsocket->hostname, tsocket->port,
+ strerror(errno));
+ return FALSE;
+ }
+
+ /* open a connection */
+ if (connect (tsocket->sd, (struct sockaddr *) &pin, sizeof(pin)) == -1)
+ {
+ g_set_error (error, THRIFT_TRANSPORT_ERROR, THRIFT_TRANSPORT_ERROR_CONNECT,
+ "failed to connect to host %s:%d - %s",
+ tsocket->hostname, tsocket->port, strerror(errno));
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+/* implements thrift_transport_close */
+gboolean
+thrift_socket_close (ThriftTransport *transport, GError **error)
+{
+ ThriftSocket *socket = THRIFT_SOCKET (transport);
+
+ if (close (socket->sd) == -1)
+ {
+ g_set_error (error, THRIFT_TRANSPORT_ERROR, THRIFT_TRANSPORT_ERROR_CLOSE,
+ "unable to close socket - %s",
+ strerror(errno));
+ return FALSE;
+ }
+
+ socket->sd = 0;
+ return TRUE;
+}
+
+/* implements thrift_transport_read */
+gint32
+thrift_socket_read (ThriftTransport *transport, gpointer buf,
+ guint32 len, GError **error)
+{
+ gint ret = 0;
+ guint got = 0;
+
+ ThriftSocket *socket = THRIFT_SOCKET (transport);
+
+ while (got < len)
+ {
+ ret = recv (socket->sd, buf, len, 0);
+ if (ret < 0)
+ {
+ g_set_error (error, THRIFT_TRANSPORT_ERROR,
+ THRIFT_TRANSPORT_ERROR_RECEIVE,
+ "failed to read %d bytes - %s", len, strerror(errno));
+ return -1;
+ }
+ got += ret;
+ }
+
+ return got;
+}
+
+/* implements thrift_transport_read_end
+ * called when write is complete. nothing to do on our end. */
+gboolean
+thrift_socket_read_end (ThriftTransport *transport, GError **error)
+{
+ /* satisfy -Wall */
+ THRIFT_UNUSED_VAR (transport);
+ THRIFT_UNUSED_VAR (error);
+ return TRUE;
+}
+
+/* implements thrift_transport_write */
+gboolean
+thrift_socket_write (ThriftTransport *transport, const gpointer buf,
+ const guint32 len, GError **error)
+{
+ gint ret = 0;
+ guint sent = 0;
+
+ ThriftSocket *socket = THRIFT_SOCKET (transport);
+ g_return_val_if_fail (socket->sd != 0, FALSE);
+
+ while (sent < len)
+ {
+ ret = send (socket->sd, buf + sent, len - sent, 0);
+ if (ret < 0)
+ {
+ g_set_error (error, THRIFT_TRANSPORT_ERROR,
+ THRIFT_TRANSPORT_ERROR_SEND,
+ "failed to send %d bytes - %s", len, strerror(errno));
+ return FALSE;
+ }
+ sent += ret;
+ }
+
+ return TRUE;
+}
+
+/* implements thrift_transport_write_end
+ * called when write is complete. nothing to do on our end. */
+gboolean
+thrift_socket_write_end (ThriftTransport *transport, GError **error)
+{
+ /* satisfy -Wall */
+ THRIFT_UNUSED_VAR (transport);
+ THRIFT_UNUSED_VAR (error);
+ return TRUE;
+}
+
+/* implements thrift_transport_flush
+ * flush pending data. since we are not buffered, this is a no-op */
+gboolean
+thrift_socket_flush (ThriftTransport *transport, GError **error)
+{
+ /* satisfy -Wall */
+ THRIFT_UNUSED_VAR (transport);
+ THRIFT_UNUSED_VAR (error);
+ return TRUE;
+}
+
+
Added: incubator/thrift/trunk/lib/c_glib/src/transport/thrift_socket.h
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/c_glib/src/transport/thrift_socket.h?rev=1027933&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/c_glib/src/transport/thrift_socket.h (added)
+++ incubator/thrift/trunk/lib/c_glib/src/transport/thrift_socket.h Wed Oct 27 12:30:11 2010
@@ -0,0 +1,56 @@
+#ifndef _THRIFT_SOCKET_H
+#define _THRIFT_SOCKET_H
+
+#include <glib-object.h>
+
+#include "transport/thrift_transport.h"
+
+/*! \file thrift_socket.h
+ * \brief Socket implementation of a Thrift transport. Subclasses the
+ * ThriftTransport class.
+ */
+
+/* type macros */
+#define THRIFT_TYPE_SOCKET (thrift_socket_get_type ())
+#define THRIFT_SOCKET(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
+ THRIFT_TYPE_SOCKET, ThriftSocket))
+#define THRIFT_IS_SOCKET(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+ THRIFT_TYPE_SOCKET))
+#define THRIFT_SOCKET_CLASS(c) (G_TYPE_CHECK_CLASS_CAST ((c), \
+ THRIFT_TYPE_SOCKET, ThriftSocketClass))
+#define THRIFT_IS_SOCKET_CLASS(c) (G_TYPE_CHECK_CLASS_TYPE ((c), \
+ THRIFT_TYPE_SOCKET))
+#define THRIFT_SOCKET_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), \
+ THRIFT_TYPE_SOCKET, \
+ ThriftSocketClass))
+
+/*!
+ * Thrift Socket instance.
+ */
+struct _ThriftSocket
+{
+ ThriftTransport parent;
+
+ /* private */
+ gchar *hostname;
+ gshort port;
+ int sd;
+ guint8 *buf;
+ guint32 buf_size;
+ guint32 buf_len;
+};
+typedef struct _ThriftSocket ThriftSocket;
+
+/*!
+ * Thrift Socket class.
+ */
+struct _ThriftSocketClass
+{
+ ThriftTransportClass parent;
+};
+typedef struct _ThriftSocketClass ThriftSocketClass;
+
+/* used by THRIFT_TYPE_SOCKET */
+GType thrift_socket_get_type (void);
+
+#endif
Added: incubator/thrift/trunk/lib/c_glib/src/transport/thrift_transport.c
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/c_glib/src/transport/thrift_transport.c?rev=1027933&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/c_glib/src/transport/thrift_transport.c (added)
+++ incubator/thrift/trunk/lib/c_glib/src/transport/thrift_transport.c Wed Oct 27 12:30:11 2010
@@ -0,0 +1,114 @@
+#include "thrift.h"
+#include "transport/thrift_transport.h"
+
+/* define the GError domain string */
+#define THRIFT_TRANSPORT_ERROR_DOMAIN "thrift-transport-error-quark"
+
+/* forward declarations */
+static void thrift_transport_class_init (ThriftTransportClass *cls);
+
+/* define ThriftTransportInterface's type */
+GType
+thrift_transport_get_type (void)
+{
+ static GType type = 0;
+
+ if (type == 0)
+ {
+ static const GTypeInfo info =
+ {
+ sizeof (ThriftTransportClass),
+ NULL, /* base_init */
+ NULL, /* base_finalize */
+ (GClassInitFunc) thrift_transport_class_init,
+ NULL, /* class_finalize */
+ NULL, /* class_data */
+ sizeof (ThriftTransport),
+ 0, /* n_preallocs */
+ NULL, /* instance_init */
+ NULL, /* value_table */
+ };
+
+ type = g_type_register_static (G_TYPE_OBJECT, "ThriftTransport",
+ &info, G_TYPE_FLAG_ABSTRACT);
+ }
+
+ return type;
+}
+
+/* class initializer for ThriftTransport */
+static void
+thrift_transport_class_init (ThriftTransportClass *cls)
+{
+ /* set these as virtual methods to be implemented by a subclass */
+ cls->is_open = thrift_transport_is_open;
+ cls->open = thrift_transport_open;
+ cls->close = thrift_transport_close;
+ cls->read = thrift_transport_read;
+ cls->read_end = thrift_transport_read_end;
+ cls->write = thrift_transport_write;
+ cls->write_end = thrift_transport_write_end;
+ cls->flush = thrift_transport_flush;
+}
+
+gboolean
+thrift_transport_is_open (ThriftTransport *transport)
+{
+ return THRIFT_TRANSPORT_GET_CLASS (transport)->is_open (transport);
+}
+
+gboolean
+thrift_transport_open (ThriftTransport *transport, GError **error)
+{
+ return THRIFT_TRANSPORT_GET_CLASS (transport)->open (transport, error);
+}
+
+gboolean
+thrift_transport_close (ThriftTransport *transport, GError **error)
+{
+ return THRIFT_TRANSPORT_GET_CLASS (transport)->close (transport, error);
+}
+
+gint32
+thrift_transport_read (ThriftTransport *transport, gpointer buf,
+ guint32 len, GError **error)
+{
+ return THRIFT_TRANSPORT_GET_CLASS (transport)->read (transport, buf,
+ len, error);
+}
+
+gboolean
+thrift_transport_read_end (ThriftTransport *transport, GError **error)
+{
+ return THRIFT_TRANSPORT_GET_CLASS (transport)->read_end (transport,
+ error);
+}
+
+gboolean
+thrift_transport_write (ThriftTransport *transport, const gpointer buf,
+ const guint32 len, GError **error)
+{
+ return THRIFT_TRANSPORT_GET_CLASS (transport)->write (transport, buf,
+ len, error);
+}
+
+gboolean
+thrift_transport_write_end (ThriftTransport *transport, GError **error)
+{
+ return THRIFT_TRANSPORT_GET_CLASS (transport)->write_end (transport,
+ error);
+}
+
+gboolean
+thrift_transport_flush (ThriftTransport *transport, GError **error)
+{
+ return THRIFT_TRANSPORT_GET_CLASS (transport)->flush (transport, error);
+}
+
+/* define the GError domain for Thrift transports */
+GQuark
+thrift_transport_error_quark (void)
+{
+ return g_quark_from_static_string (THRIFT_TRANSPORT_ERROR_DOMAIN);
+}
+
Added: incubator/thrift/trunk/lib/c_glib/src/transport/thrift_transport.h
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/c_glib/src/transport/thrift_transport.h?rev=1027933&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/c_glib/src/transport/thrift_transport.h (added)
+++ incubator/thrift/trunk/lib/c_glib/src/transport/thrift_transport.h Wed Oct 27 12:30:11 2010
@@ -0,0 +1,135 @@
+#ifndef _THRIFT_TRANSPORT_H
+#define _THRIFT_TRANSPORT_H
+
+#include <glib-object.h>
+
+/*! \file thrift_transport.h
+ * \brief Abstract class for Thrift transports.
+ *
+ * An abstract class is used instead of an interface because:
+ * - interfaces can't seem to be used as properties. ThriftProtocol has
+ * a ThriftTransport as an object property.
+ * - if a method needs to be added that all subclasses can use, a class
+ * is necessary.
+ */
+
+/* type macros */
+#define THRIFT_TYPE_TRANSPORT (thrift_transport_get_type ())
+#define THRIFT_TRANSPORT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
+ THRIFT_TYPE_TRANSPORT, ThriftTransport))
+#define THRIFT_IS_TRANSPORT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+ THRIFT_TYPE_TRANSPORT))
+#define THRIFT_TRANSPORT_CLASS(c) (G_TYPE_CHECK_CLASS_CAST ((c), \
+ THRIFT_TYPE_TRANSPORT, \
+ ThriftTransportClass))
+#define THRIFT_IS_TRANSPORT_CLASS(c) (G_TYPE_CHECK_CLASS_TYPE ((c), \
+ THRIFT_TYPE_TRANSPORT))
+#define THRIFT_TRANSPORT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), \
+ THRIFT_TYPE_TRANSPORT, \
+ ThriftTransportClass))
+
+/*!
+ * Thrift Protocol object
+ */
+struct _ThriftTransport
+{
+ GObject parent;
+};
+typedef struct _ThriftTransport ThriftTransport;
+
+/*!
+ * Thrift Transport class
+ */
+struct _ThriftTransportClass
+{
+ GObjectClass parent;
+
+ /* vtable */
+ gboolean (*is_open) (ThriftTransport *transport);
+ gboolean (*open) (ThriftTransport *transport, GError **error);
+ gboolean (*close) (ThriftTransport *transport, GError **error);
+ gint32 (*read) (ThriftTransport *transport, gpointer buf,
+ guint32 len, GError **error);
+ gboolean (*read_end) (ThriftTransport *transport, GError **error);
+ gboolean (*write) (ThriftTransport *transport, const gpointer buf,
+ const guint32 len, GError **error);
+ gboolean (*write_end) (ThriftTransport *transport, GError **error);
+ gboolean (*flush) (ThriftTransport *transport, GError **error);
+};
+typedef struct _ThriftTransportClass ThriftTransportClass;
+
+/* used by THRIFT_TYPE_TRANSPORT */
+GType thrift_transport_get_type (void);
+
+/* virtual public methods */
+
+/*!
+ * Checks if this transport is opened.
+ * \public \memberof ThriftTransportInterface
+ */
+gboolean thrift_transport_is_open (ThriftTransport *transport);
+
+/*!
+ * Open the transport for reading and writing.
+ * \public \memberof ThriftTransportInterface
+ */
+gboolean thrift_transport_open (ThriftTransport *transport, GError **error);
+
+/*!
+ * Close the transport.
+ * \public \memberof ThriftTransportInterface
+ */
+gboolean thrift_transport_close (ThriftTransport *transport, GError **error);
+
+/*!
+ * Read some data into the buffer buf.
+ * \public \memberof ThriftTransportInterface
+ */
+gint32 thrift_transport_read (ThriftTransport *transport, gpointer buf,
+ guint32 len, GError **error);
+
+/*!
+ * Called when read is completed.
+ * \public \memberof ThriftTransportInterface
+ */
+gboolean thrift_transport_read_end (ThriftTransport *transport, GError **error);
+
+/*!
+ * Writes data from a buffer to the transport.
+ * \public \memberof ThriftTransportInterface
+ */
+gboolean thrift_transport_write (ThriftTransport *transport, const gpointer buf,
+ const guint32 len, GError **error);
+
+/*!
+ * Called when write is completed.
+ * \public \memberof ThriftTransportInterface
+ */
+gboolean thrift_transport_write_end (ThriftTransport *transport,
+ GError **error);
+
+/*!
+ * Flushes any pending data to be written. Typically used with buffered
+ * transport mechanisms.
+ * \public \memberof ThriftTransportInterface
+ */
+gboolean thrift_transport_flush (ThriftTransport *transport, GError **error);
+
+/* define error/exception types */
+typedef enum
+{
+ THRIFT_TRANSPORT_ERROR_UNKNOWN,
+ THRIFT_TRANSPORT_ERROR_HOST,
+ THRIFT_TRANSPORT_ERROR_SOCKET,
+ THRIFT_TRANSPORT_ERROR_CONNECT,
+ THRIFT_TRANSPORT_ERROR_SEND,
+ THRIFT_TRANSPORT_ERROR_RECEIVE,
+ THRIFT_TRANSPORT_ERROR_CLOSE
+} ThriftTransportError;
+
+/* define an error domain for GError to use */
+GQuark thrift_transport_error_quark (void);
+#define THRIFT_TRANSPORT_ERROR (thrift_transport_error_quark ())
+
+
+#endif /* _THRIFT_TRANSPORT_H */
Added: incubator/thrift/trunk/lib/c_glib/src/transport/thrift_transport_factory.c
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/c_glib/src/transport/thrift_transport_factory.c?rev=1027933&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/c_glib/src/transport/thrift_transport_factory.c (added)
+++ incubator/thrift/trunk/lib/c_glib/src/transport/thrift_transport_factory.c Wed Oct 27 12:30:11 2010
@@ -0,0 +1,51 @@
+#include "thrift.h"
+#include "transport/thrift_transport_factory.h"
+
+/* forward declaration s*/
+static void thrift_transport_factory_class_init (ThriftTransportFactoryClass *cls);
+ThriftTransport *thrift_transport_factory_get_transport (ThriftTransportFactory *factory, ThriftTransport *transport);
+
+GType
+thrift_transport_factory_get_type (void)
+{
+ static GType type = 0;
+
+ if (type == 0)
+ {
+ static const GTypeInfo info = {
+ sizeof (ThriftTransportFactoryClass),
+ NULL, /* base_init */
+ NULL, /* base_finalize */
+ (GClassInitFunc) thrift_transport_factory_class_init,
+ NULL, /* class_finalize */
+ NULL, /* class_data */
+ sizeof (ThriftTransportFactory),
+ 0, /* n_preallocs */
+ NULL, /* instance_init */
+ NULL, /* value_table */
+ };
+
+ type = g_type_register_static (G_TYPE_OBJECT, "ThriftTransportFactory",
+ &info, 0);
+ }
+
+ return type;
+}
+
+static void
+thrift_transport_factory_class_init (ThriftTransportFactoryClass *cls)
+{
+ cls->get_transport = thrift_transport_factory_get_transport;
+}
+
+/* builds a transport from the base transport. */
+ThriftTransport *
+thrift_transport_factory_get_transport (ThriftTransportFactory *factory,
+ ThriftTransport *transport)
+{
+ THRIFT_UNUSED_VAR (factory);
+ return transport;
+}
+
+
+
Added: incubator/thrift/trunk/lib/c_glib/src/transport/thrift_transport_factory.h
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/c_glib/src/transport/thrift_transport_factory.h?rev=1027933&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/c_glib/src/transport/thrift_transport_factory.h (added)
+++ incubator/thrift/trunk/lib/c_glib/src/transport/thrift_transport_factory.h Wed Oct 27 12:30:11 2010
@@ -0,0 +1,55 @@
+#ifndef _THRIFT_TRANSPORT_FACTORY_H
+#define _THRIFT_TRANSPORT_FACTORY_H
+
+#include <glib-object.h>
+
+#include "thrift_transport.h"
+
+/*! \file thrift_transport_factory.h
+ * \brief Base class for Thrift Transport Factories. Used by Thrift Servers
+ * to obtain a client transport from an existing transport. The default
+ * implementation simply clones the provided transport.
+ */
+
+/* type macros */
+#define THRIFT_TYPE_TRANSPORT_FACTORY (thrift_transport_factory_get_type ())
+#define THRIFT_TRANSPORT_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
+ THRIFT_TYPE_TRANSPORT_FACTORY, \
+ ThriftTransportFactory))
+#define THRIFT_IS_TRANSPORT_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+ THRIFT_TYPE_TRANSPORT_FACTORY))
+#define THRIFT_TRANSPORT_FACTORY_CLASS(c) (G_TYPE_CHECK_CLASS_CAST ((c), \
+ THRIFT_TYPE_TRANSPORT_FACTORY, \
+ ThriftTransportFactoryClass))
+#define THRIFT_IS_TRANSPORT_FACTORY_CLASS(c) (G_TYPE_CHECK_CLASS_TYPE ((c), \
+ THRIFT_TYPE_TRANSPORT_FACTORY))
+#define THRIFT_TRANSPORT_FACTORY_GET_CLASS(obj) \
+ (G_TYPE_INSTANCE_GET_CLASS ((obj), THRIFT_TYPE_TRANSPORT_FACTORY, \
+ ThriftTransportFactoryClass))
+
+/* Thrift Transport Factory instance */
+struct _ThriftTransportFactory
+{
+ GObject parent;
+};
+typedef struct _ThriftTransportFactory ThriftTransportFactory;
+
+/* Thrift Transport Factory class */
+struct _ThriftTransportFactoryClass
+{
+ GObjectClass parent;
+
+ /* vtable */
+ ThriftTransport *(*get_transport) (ThriftTransportFactory *factory,
+ ThriftTransport *transport);
+};
+typedef struct _ThriftTransportFactoryClass ThriftTransportFactoryClass;
+
+/* used by THRIFT_TYPE_TRANSPORT_FACTORY */
+GType thrift_transport_factory_get_type (void);
+
+/* virtual public methods */
+ThriftTransport *thrift_transport_factory_get_transport (ThriftTransportFactory *factory, ThriftTransport *transport);
+
+
+#endif /* _THRIFT_TRANSPORT_FACTORY_H */
Propchange: incubator/thrift/trunk/lib/c_glib/test/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Wed Oct 27 12:30:11 2010
@@ -0,0 +1,22 @@
+*.o
+*.lo
+testwrapper.sh
+testwrapper-test*
+.deps
+.libs
+gen-cpp
+gen-c_glib
+testsimpleserver
+Makefile.in
+testthrifttestclient
+testthrifttest
+Makefile
+testoptionalrequired
+testtransportsocket
+test-wrapper.sh
+teststruct
+testmemorybuffer
+testframedtransport
+testbufferedtransport
+testprotocolbinary
+testdebugproto
Added: incubator/thrift/trunk/lib/c_glib/test/Makefile.am
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/c_glib/test/Makefile.am?rev=1027933&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/c_glib/test/Makefile.am (added)
+++ incubator/thrift/trunk/lib/c_glib/test/Makefile.am Wed Oct 27 12:30:11 2010
@@ -0,0 +1,229 @@
+SUBDIRS =
+
+AM_CPPFLAGS = -g -Wall -I../src $(GLIB_CFLAGS)
+AM_LDFLAGS = $(GLIB_LIBS) $(GOBJECT_LIBS) @GCOV_LDFLAGS@
+
+CFLAGS = @GCOV_CFLAGS@
+CXXFLAGS = -g
+
+check_SCRIPTS = \
+ testwrapper-testtransportsocket \
+ testwrapper-testprotocolbinary \
+ testwrapper-testbufferedtransport \
+ testwrapper-testframedtransport \
+ testwrapper-testmemorybuffer \
+ testwrapper-teststruct \
+ testwrapper-testsimpleserver \
+ testwrapper-testdebugproto \
+ testwrapper-testoptionalrequired \
+ testwrapper-testthrifttest
+
+if WITH_CPP
+ check_SCRIPTS += testwrapper-testthrifttestclient
+endif
+
+check_PROGRAMS = \
+ testtransportsocket \
+ testprotocolbinary \
+ testbufferedtransport \
+ testframedtransport \
+ testmemorybuffer \
+ teststruct \
+ testsimpleserver \
+ testdebugproto \
+ testoptionalrequired \
+ testthrifttest
+
+if WITH_CPP
+ check_PROGRAMS += testthrifttestclient
+endif
+
+testtransportsocket_SOURCES = testtransportsocket.c
+testtransportsocket_LDADD = \
+ ../libthrift_c_glib_la-thrift_transport.o \
+ ../libthrift_c_glib_la-thrift_server_transport.o \
+ ../libthrift_c_glib_la-thrift_server_socket.o
+
+testprotocolbinary_SOURCES = testprotocolbinary.c
+testprotocolbinary_LDADD = \
+ ../libthrift_c_glib_la-thrift_protocol.o \
+ ../libthrift_c_glib_la-thrift_transport.o \
+ ../libthrift_c_glib_la-thrift_socket.o \
+ ../libthrift_c_glib_la-thrift_server_transport.o \
+ ../libthrift_c_glib_la-thrift_server_socket.o
+
+testbufferedtransport_SOURCES = testbufferedtransport.c
+testbufferedtransport_LDADD = \
+ ../libthrift_c_glib_la-thrift_transport.o \
+ ../libthrift_c_glib_la-thrift_socket.o \
+ ../libthrift_c_glib_la-thrift_server_transport.o \
+ ../libthrift_c_glib_la-thrift_server_socket.o
+
+testframedtransport_SOURCES = testframedtransport.c
+testframedtransport_LDADD = \
+ ../libthrift_c_glib_la-thrift_transport.o \
+ ../libthrift_c_glib_la-thrift_socket.o \
+ ../libthrift_c_glib_la-thrift_server_transport.o \
+ ../libthrift_c_glib_la-thrift_server_socket.o
+
+testmemorybuffer_SOURCES = testmemorybuffer.c
+testmemorybuffer_LDADD = \
+ ../libthrift_c_glib_la-thrift_transport.o
+
+teststruct_SOURCES = teststruct.c
+teststruct_LDADD = \
+ ../libthrift_c_glib_la-thrift_protocol.o \
+ ../libthrift_c_glib_la-thrift_transport.o
+
+testsimpleserver_SOURCES = testsimpleserver.c
+testsimpleserver_LDADD = \
+ ../libthrift_c_glib_la-thrift_protocol.o \
+ ../libthrift_c_glib_la-thrift_transport.o \
+ ../libthrift_c_glib_la-thrift_transport_factory.o \
+ ../libthrift_c_glib_la-thrift_processor.o \
+ ../libthrift_c_glib_la-thrift_protocol_factory.o \
+ ../libthrift_c_glib_la-thrift_binary_protocol.o \
+ ../libthrift_c_glib_la-thrift_binary_protocol_factory.o \
+ ../libthrift_c_glib_la-thrift_socket.o \
+ ../libthrift_c_glib_la-thrift_server_transport.o \
+ ../libthrift_c_glib_la-thrift_server_socket.o \
+ ../libthrift_c_glib_la-thrift_server.o
+
+testdebugproto_SOURCES = testdebugproto.c
+testdebugproto_LDADD = libtestgenc.la
+
+testoptionalrequired_SOURCES = testoptionalrequired.c
+testoptionalrequired_LDADD = \
+ ../libthrift_c_glib_la-thrift_protocol.o \
+ ../libthrift_c_glib_la-thrift_transport.o \
+ libtestgenc.la
+
+testthrifttest_SOURCES = testthrifttest.c
+testthrifttest_LDADD = libtestgenc.la
+
+testthrifttestclient_SOURCES = testthrifttestclient.cpp
+testthrifttestclient_CPPFLAGS = -I../../cpp/src $(BOOST_CPPFLAGS) -I./gen-cpp -I../src -I./gen-c_glib $(GLIB_CFLAGS)
+testthrifttestclient_LDADD = ../../cpp/.libs/libthrift.la ../libthrift_c_glib.la libtestgenc.la libtestgencpp.la
+testthrifttestclient_LDFLAGS = -L../.libs -L../../cpp/.libs $(GLIB_LIBS) $(GOBJECT_LIBS)
+
+check_LTLIBRARIES = libtestgenc.la
+
+if WITH_CPP
+ check_LTLIBRARIES += libtestgencpp.la
+endif
+
+libtestgenc_la_SOURCES = \
+ gen-c_glib/t_test_debug_proto_test_types.c \
+ gen-c_glib/t_test_empty_service.c \
+ gen-c_glib/t_test_inherited.c \
+ gen-c_glib/t_test_optional_required_test_types.c \
+ gen-c_glib/t_test_reverse_order_service.c \
+ gen-c_glib/t_test_second_service.c \
+ gen-c_glib/t_test_service_for_exception_with_a_map.c \
+ gen-c_glib/t_test_srv.c \
+ gen-c_glib/t_test_thrift_test.c \
+ gen-c_glib/t_test_thrift_test_types.c \
+ gen-c_glib/t_test_debug_proto_test_types.h \
+ gen-c_glib/t_test_empty_service.h \
+ gen-c_glib/t_test_inherited.h \
+ gen-c_glib/t_test_optional_required_test_types.h \
+ gen-c_glib/t_test_reverse_order_service.h \
+ gen-c_glib/t_test_second_service.h \
+ gen-c_glib/t_test_service_for_exception_with_a_map.h \
+ gen-c_glib/t_test_srv.h \
+ gen-c_glib/t_test_thrift_test.h \
+ gen-c_glib/t_test_thrift_test_types.h
+libtestgenc_la_LIBADD = $(top_builddir)/lib/c_glib/libthrift_c_glib.la
+
+libtestgencpp_la_SOURCES = \
+ gen-cpp/ThriftTest.cpp \
+ gen-cpp/ThriftTest_constants.cpp \
+ gen-cpp/ThriftTest_types.cpp \
+ gen-cpp/ThriftTest.h \
+ gen-cpp/ThriftTest_constants.h \
+ gen-cpp/ThriftTest_types.h
+libtestgencpp_la_CPPFLAGS = -I../../cpp/src $(BOOST_CPPFLAGS) -I./gen-cpp
+
+THRIFT = $(top_builddir)/compiler/cpp/thrift
+
+gen-c_glib/t_test_debug_proto_test_types.c gen-c_glib/t_test_debug_proto_test_types.h gen-c_glib/t_test_empty_service.c gen-c_glib/t_test_empty_service.h gen-c_glib/t_test_inherited.c gen-c_glib/t_test_inherited.h gen-c_glib/t_test_reverse_order_service.c gen-c_glib/t_test_reverse_order_service.h gen-c_glib/t_test_service_for_exception_with_a_map.c gen-c_glib/t_test_service_for_exception_with_a_map.h gen-c_glib/t_test_srv.c gen-c_glib/t_test_srv.h: ../../../test/DebugProtoTest.thrift
+ $(THRIFT) --gen c_glib $<
+
+gen-c_glib/t_test_optional_required_test_types.c gen-c_glib/t_test_optional_required_test_types.h: ../../../test/OptionalRequiredTest.thrift
+ $(THRIFT) --gen c_glib $<
+
+gen-c_glib/t_test_second_service.c gen-c_glib/t_test_thrift_test.c gen-c_glib/t_test_thrift_test_types.c gen-c_glib/t_test_second_service.h gen-c_glib/t_test_thrift_test-.h gen-c_glib/t_test_thrift_test_types.h: ../../../test/ThriftTest.thrift
+ $(THRIFT) --gen c_glib $<
+
+gen-cpp/ThriftTest.cpp gen-cpp/ThriftTest.h: ../../../test/ThriftTest.thrift
+ $(THRIFT) --gen cpp $<
+
+
+TESTS = \
+ $(testwrapper-%) \
+ $(check_PROGRAMS) \
+ $(check_SCRIPTS)
+
+# globally added to all instances of valgrind calls
+# VALGRIND_OPTS = --suppressions=glib.suppress
+VALGRIND_OPTS =
+
+# globally added to all memcheck calls
+VALGRIND_MEM_OPTS = --tool=memcheck \
+ --num-callers=10 \
+ ${myextravalgrindmemopts}
+
+# globally added to all leakcheck calls
+VALGRIND_LEAK_OPTS = --tool=memcheck \
+ --num-callers=10 \
+ --leak-check=full \
+ --leak-resolution=high \
+ ${myextravalgrindleakopts}
+
+memcheck: $(check_PROGRAMS)
+ @for x in $(check_PROGRAMS); \
+ do \
+ $(MAKE) memcheck-$$x; \
+ done
+
+leakcheck: $(check_PROGRAMS)
+ @for x in $(check_PROGRAMS); \
+ do \
+ $(MAKE) leakcheck-$$x; \
+ done
+
+memcheck-%: %
+ @echo "*****************************************"; \
+ echo "MEMCHECK: $<"; \
+ echo "ARGS: ${VALGRIND_OPTS} ${VALGRIND_MEM_OPTS} ${$<_VALGRIND_MEM_OPTS}"; \
+ $(LIBTOOL) --mode=execute \
+ valgrind \
+ ${VALGRIND_OPTS} \
+ ${VALGRIND_MEM_OPTS} \
+ ${$<_VALGRIND_MEM_OPTS} ./$<
+
+leakcheck-%: %
+ @echo "*****************************************"; \
+ echo "LEAKCHECK: $<"; \
+ echo "ARGS: ${VALGRIND_OPTS} ${VALGRIND_LEAK_OPTS} ${$<_VALGRIND_LEAK_OPTS}"; \
+ G_SLICE=always-malloc $(LIBTOOL) --mode=execute \
+ valgrind \
+ ${VALGRIND_OPTS} \
+ ${VALGRIND_LEAK_OPTS} \
+ ${$<_VALGRIND_LEAK_OPTS} ./$<
+
+testwrapper-%: % test-wrapper.sh
+ @ln -sf test-wrapper.sh $@
+
+clean-local:
+ $(RM) -r gen-c_glib gen-cpp
+
+CLEANFILES = \
+ testwrapper-* \
+ *.bb \
+ *.bbg \
+ *.da \
+ *.gcno \
+ *.gcda \
+ *.gcov \
+ test-wrapper.sh
Added: incubator/thrift/trunk/lib/c_glib/test/glib.suppress
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/c_glib/test/glib.suppress?rev=1027933&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/c_glib/test/glib.suppress (added)
+++ incubator/thrift/trunk/lib/c_glib/test/glib.suppress Wed Oct 27 12:30:11 2010
@@ -0,0 +1,64 @@
+{
+ g_type_init_1
+ Memcheck:Leak
+ fun:malloc
+ ...
+ fun:g_type_init_with_debug_flags
+}
+
+{
+ g_type_init_2
+ Memcheck:Leak
+ fun:calloc
+ ...
+ fun:g_type_init_with_debug_flags
+}
+
+{
+ g_type_init_3
+ Memcheck:Leak
+ fun:realloc
+ ...
+ fun:g_type_init_with_debug_flags
+}
+
+{
+ g_type_register_static_1
+ Memcheck:Leak
+ fun:realloc
+ ...
+ fun:g_type_register_static
+}
+
+{
+ g_type_register_statuc_2
+ Memcheck:Leak
+ fun:malloc
+ fun:realloc
+ fun:g_realloc
+ ...
+ fun:g_type_register_static
+}
+
+{
+ type_class_init_Wm1
+ Memcheck:Leak
+ fun:calloc
+ fun:g_malloc0
+ fun:type_class_init_Wm
+ fun:g_type_class_ref
+ ...
+ fun:g_object_newv
+}
+
+{
+ type_class_init_Wm2
+ Memcheck:Leak
+ fun:calloc
+ fun:g_malloc0
+ fun:type_class_init_Wm
+ fun:g_type_class_ref
+ ...
+ fun:type_class_init_Wm
+}
+
Added: incubator/thrift/trunk/lib/c_glib/test/test-wrapper.sh.in
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/c_glib/test/test-wrapper.sh.in?rev=1027933&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/c_glib/test/test-wrapper.sh.in (added)
+++ incubator/thrift/trunk/lib/c_glib/test/test-wrapper.sh.in Wed Oct 27 12:30:11 2010
@@ -0,0 +1,58 @@
+# /bin/sh
+
+command="$0"
+
+stripcommand=`echo "$command" | sed 's/testwrapper-//'`
+
+"$stripcommand" "$@" || exit $?
+
+if test "x@ENABLE_COVERAGE@" = "x1"; then
+ # linux: 97.67% of 86 lines executed in file ../src/test123.h
+ # bsd: 100.00% of 196 source lines executed in file testbimap.c
+
+ extrastripcommand=`echo "$stripcommand" | sed 's/\.\///'`
+ ${GCOV:-gcov} "$extrastripcommand" 2>&1 \
+ | perl -ne 'BEGIN { $file = undef; }
+ next if m!^Creating!;
+ next if m!creating!;
+ next if m!^$!;
+ next if m!not exhausted!;
+ next if m!^Unexpected EOF!;
+ if (m!([\d\.]+)\% of \d+( source)? lines executed in file (.+)!)
+ {
+ do
+ {
+ if ( $3 !~ m#^/# )
+ {
+ $a = $3 =~ m%([\-\w\.]+)$%;
+ print STDERR $_;
+ print "$1.gcov\n";
+ }
+ } if $1 < 110.0;
+ }
+ elsif (m#^File .(.*?).$#)
+ {
+ $file = $1;
+ }
+ elsif (m#Lines executed:([\d\.]+)\% of (\d+)#)
+ {
+ $percent = $1;
+ $lines = $2;
+ do
+ {
+ if ( $file !~ m#^/# )
+ {
+ $a = $file =~ m%([\-\w\.]+)$%;
+ print STDERR "$percent% of $lines executed in file $file\n";
+ print "$1.gcov\n";
+ }
+ } if $percent < 110.0;
+ }
+ else
+ {
+ print
+ }' \
+ | xargs grep -n -A2 -B2 '#####.*\w'
+ exit 0
+fi
+
Added: incubator/thrift/trunk/lib/c_glib/test/testbufferedtransport.c
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/c_glib/test/testbufferedtransport.c?rev=1027933&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/c_glib/test/testbufferedtransport.c (added)
+++ incubator/thrift/trunk/lib/c_glib/test/testbufferedtransport.c Wed Oct 27 12:30:11 2010
@@ -0,0 +1,188 @@
+#include <assert.h>
+#include <netdb.h>
+
+#include "transport/thrift_transport.h"
+#include "transport/thrift_socket.h"
+#include "transport/thrift_server_transport.h"
+#include "transport/thrift_server_socket.h"
+
+#define TEST_DATA { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' }
+
+#include "../src/transport/thrift_buffered_transport.c"
+
+static const char TEST_ADDRESS[] = "localhost";
+static const short TEST_PORT = 64444;
+
+static void thrift_server (const int port);
+
+/* test object creation and destruction */
+static void
+test_create_and_destroy(void)
+{
+ ThriftTransport *transport = NULL;
+ guint r_buf_size = 0;
+ guint w_buf_size = 0;
+
+ GObject *object = NULL;
+ object = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT, NULL);
+ assert (object != NULL);
+ g_object_get (G_OBJECT (object), "transport", &transport,
+ "r_buf_size", &r_buf_size,
+ "w_buf_size", &w_buf_size, NULL);
+ g_object_unref (object);
+}
+
+static void
+test_open_and_close(void)
+{
+ ThriftSocket *tsocket = NULL;
+ ThriftTransport *transport = NULL;
+ GError *err = NULL;
+
+ /* create a ThriftSocket */
+ tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
+ "port", 51188, NULL);
+
+ /* create a BufferedTransport wrapper of the Socket */
+ transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
+ "transport", THRIFT_TRANSPORT (tsocket), NULL);
+
+ /* this shouldn't work */
+ assert (thrift_buffered_transport_open (transport, NULL) == FALSE);
+ assert (thrift_buffered_transport_is_open (transport) == TRUE);
+ assert (thrift_buffered_transport_close (transport, NULL) == TRUE);
+ g_object_unref (transport);
+ g_object_unref (tsocket);
+
+ /* try and underlying socket failure */
+ tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost.broken",
+ NULL);
+
+ /* create a BufferedTransport wrapper of the Socket */
+ transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
+ "transport", THRIFT_TRANSPORT (tsocket), NULL);
+
+ assert (thrift_buffered_transport_open (transport, &err) == FALSE);
+ g_object_unref (transport);
+ g_object_unref (tsocket);
+ g_error_free (err);
+ err = NULL;
+}
+
+static void
+test_read_and_write(void)
+{
+ int status;
+ pid_t pid;
+ ThriftSocket *tsocket = NULL;
+ ThriftTransport *transport = NULL;
+ int port = 51199;
+ guchar buf[10] = TEST_DATA; /* a buffer */
+
+ pid = fork ();
+ assert ( pid >= 0 );
+
+ if ( pid == 0 )
+ {
+ /* child listens */
+ thrift_server (port);
+ exit (0);
+ } else {
+ /* parent connects, wait a bit for the socket to be created */
+ sleep (1);
+
+ tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
+ "port", port, NULL);
+ transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
+ "transport", THRIFT_TRANSPORT (tsocket),
+ "w_buf_size", 4, NULL);
+
+ assert (thrift_buffered_transport_open (transport, NULL) == TRUE);
+ assert (thrift_buffered_transport_is_open (transport));
+
+ /* write 10 bytes */
+ thrift_buffered_transport_write (transport, buf, 10, NULL);
+
+ /* write 1 byte at a time */
+ thrift_buffered_transport_write (transport, buf, 1, NULL);
+ thrift_buffered_transport_write (transport, buf, 1, NULL);
+ thrift_buffered_transport_write (transport, buf, 1, NULL);
+
+ /* overflow the buffer */
+ thrift_buffered_transport_write (transport, buf, 2, NULL);
+ thrift_buffered_transport_write (transport, buf, 1, NULL);
+ thrift_buffered_transport_flush (transport, NULL);
+
+ /* write 1 byte and flush */
+ thrift_buffered_transport_write (transport, buf, 1, NULL);
+ thrift_buffered_transport_flush (transport, NULL);
+
+ /* write and overflow buffer with 2 system calls */
+ thrift_buffered_transport_write (transport, buf, 1, NULL);
+ thrift_buffered_transport_write (transport, buf, 3, NULL);
+
+ /* write 10 bytes */
+ thrift_buffered_transport_write (transport, buf, 10, NULL);
+
+ thrift_buffered_transport_write_end (transport, NULL);
+ thrift_buffered_transport_flush (transport, NULL);
+ thrift_buffered_transport_close (transport, NULL);
+
+ g_object_unref (transport);
+ g_object_unref (tsocket);
+
+ assert ( wait (&status) == pid );
+ assert ( status == 0 );
+ }
+}
+
+static void
+thrift_server (const int port)
+{
+ int bytes = 0;
+ ThriftServerTransport *transport = NULL;
+ ThriftTransport *client = NULL;
+ guchar buf[10]; /* a buffer */
+ guchar match[10] = TEST_DATA;
+
+ ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
+ "port", port, NULL);
+
+ transport = THRIFT_SERVER_TRANSPORT (tsocket);
+ thrift_server_transport_listen (transport, NULL);
+
+ /* wrap the client in a BufferedTransport */
+ client = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT, "transport",
+ thrift_server_transport_accept (transport, NULL),
+ "r_buf_size", 5, NULL);
+ assert (client != NULL);
+
+ /* read 10 bytes */
+ bytes = thrift_buffered_transport_read (client, buf, 10, NULL);
+ assert (bytes == 10); /* make sure we've read 10 bytes */
+ assert ( memcmp (buf, match, 10) == 0 ); /* make sure what we got matches */
+
+ /* read 1 byte */
+ bytes = thrift_buffered_transport_read (client, buf, 1, NULL);
+
+ bytes = thrift_buffered_transport_read (client, buf, 6, NULL);
+ bytes = thrift_buffered_transport_read (client, buf, 2, NULL);
+ bytes = thrift_buffered_transport_read (client, buf, 1, NULL);
+
+ thrift_buffered_transport_read_end (client, NULL);
+ thrift_buffered_transport_close (client, NULL);
+ g_object_unref (client);
+ g_object_unref (tsocket);
+}
+
+int
+main(void)
+{
+ g_type_init();
+ test_create_and_destroy();
+ test_open_and_close();
+ test_read_and_write();
+
+ return 0;
+}
+
Added: incubator/thrift/trunk/lib/c_glib/test/testdebugproto.c
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/c_glib/test/testdebugproto.c?rev=1027933&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/c_glib/test/testdebugproto.c (added)
+++ incubator/thrift/trunk/lib/c_glib/test/testdebugproto.c Wed Oct 27 12:30:11 2010
@@ -0,0 +1,64 @@
+#include <math.h>
+#include <glib-object.h>
+
+#ifndef M_PI
+#define M_PI 3.1415926535897932385
+#endif
+
+#include "gen-c_glib/t_test_debug_proto_test_types.h"
+
+
+int
+main(void)
+{
+ g_type_init ();
+
+ TTestOneOfEach *ooe = NULL;
+ TTestNesting *n = NULL;
+ TTestHolyMoley *hm = NULL;
+
+ ooe = g_object_new (T_TEST_TYPE_ONE_OF_EACH, NULL);
+ ooe->im_true = TRUE;
+ ooe->im_false = FALSE;
+ ooe->a_bite = 0xd6;
+ ooe->integer16 = 27000;
+ ooe->integer32 = 1<<24;
+ ooe->integer64 = (guint64) 6000 * 1000 * 1000;
+ ooe->double_precision = M_PI;
+ ooe->some_characters = "Debug THIS!";
+ ooe->zomg_unicode = "\xd7\n\a\t";
+
+ n = g_object_new (T_TEST_TYPE_NESTING, NULL);
+ n->my_ooe = ooe;
+ n->my_ooe->integer16 = 16;
+ n->my_ooe->integer32 = 32;
+ n->my_ooe->integer64 = 64;
+ n->my_ooe->double_precision = (sqrt(5.0) + 1) / 2;
+ n->my_ooe->some_characters = ":R (me going \"rrrr\")";
+ n->my_ooe->zomg_unicode = "\xd3\x80\xe2\x85\xae\xce\x9d\x20";
+ n->my_bonk->type = 31337;
+ n->my_bonk->message = "I am a bonk... xor!";
+
+ hm = g_object_new (T_TEST_TYPE_HOLY_MOLEY, NULL);
+ g_ptr_array_add (hm->big, ooe);
+ g_ptr_array_add (hm->big, n->my_ooe);
+ ((TTestOneOfEach *) g_ptr_array_index (hm->big, 0))->a_bite = 0x22;
+ ((TTestOneOfEach *) g_ptr_array_index (hm->big, 1))->a_bite = 0x33;
+
+ g_hash_table_insert (hm->contain, "random string", "random string");
+
+ TTestBonk *bonk = NULL;
+ bonk = g_object_new (T_TEST_TYPE_BONK, NULL);
+ GPtrArray *bonks = g_ptr_array_new ();
+ g_ptr_array_add (bonks, bonk);
+ g_hash_table_insert (hm->bonks, "nothing", bonks);
+
+ g_ptr_array_free (bonks, TRUE);
+ g_object_unref (bonk);
+ g_object_unref (ooe);
+ g_object_unref (n);
+ g_object_unref (hm);
+
+ return 0;
+}
+
Added: incubator/thrift/trunk/lib/c_glib/test/testframedtransport.c
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/c_glib/test/testframedtransport.c?rev=1027933&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/c_glib/test/testframedtransport.c (added)
+++ incubator/thrift/trunk/lib/c_glib/test/testframedtransport.c Wed Oct 27 12:30:11 2010
@@ -0,0 +1,176 @@
+#include <assert.h>
+#include <netdb.h>
+
+#include "transport/thrift_transport.h"
+#include "transport/thrift_socket.h"
+#include "transport/thrift_server_transport.h"
+#include "transport/thrift_server_socket.h"
+
+#define TEST_DATA { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' }
+
+#include "../src/transport/thrift_framed_transport.c"
+
+static const char TEST_ADDRESS[] = "localhost";
+static const short TEST_PORT = 64444;
+
+static void thrift_server (const int port);
+
+/* test object creation and destruction */
+static void
+test_create_and_destroy(void)
+{
+ ThriftTransport *transport = NULL;
+ guint r_buf_size = 0;
+ guint w_buf_size = 0;
+
+ GObject *object = NULL;
+ object = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT, NULL);
+ assert (object != NULL);
+ g_object_get (G_OBJECT (object), "transport", &transport,
+ "r_buf_size", &r_buf_size,
+ "w_buf_size", &w_buf_size, NULL);
+ g_object_unref (object);
+}
+
+static void
+test_open_and_close(void)
+{
+ ThriftSocket *tsocket = NULL;
+ ThriftTransport *transport = NULL;
+ GError *err = NULL;
+
+ /* create a ThriftSocket */
+ tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
+ "port", 51188, NULL);
+
+ /* create a BufferedTransport wrapper of the Socket */
+ transport = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT,
+ "transport", THRIFT_TRANSPORT (tsocket), NULL);
+
+ /* this shouldn't work */
+ assert (thrift_framed_transport_open (transport, NULL) == FALSE);
+ assert (thrift_framed_transport_is_open (transport) == TRUE);
+ assert (thrift_framed_transport_close (transport, NULL) == TRUE);
+ g_object_unref (transport);
+ g_object_unref (tsocket);
+
+ /* try and underlying socket failure */
+ tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost.broken",
+ NULL);
+
+ /* create a BufferedTransport wrapper of the Socket */
+ transport = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT,
+ "transport", THRIFT_TRANSPORT (tsocket), NULL);
+
+ assert (thrift_framed_transport_open (transport, &err) == FALSE);
+ g_object_unref (transport);
+ g_object_unref (tsocket);
+ g_error_free (err);
+ err = NULL;
+}
+
+static void
+test_read_and_write(void)
+{
+ int status;
+ pid_t pid;
+ ThriftSocket *tsocket = NULL;
+ ThriftTransport *transport = NULL;
+ int port = 51199;
+ guchar buf[10] = TEST_DATA; /* a buffer */
+
+ pid = fork ();
+ assert ( pid >= 0 );
+
+ if ( pid == 0 )
+ {
+ /* child listens */
+ thrift_server (port);
+ exit (0);
+ } else {
+ /* parent connects, wait a bit for the socket to be created */
+ sleep (1);
+
+ tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
+ "port", port, NULL);
+ transport = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT,
+ "transport", THRIFT_TRANSPORT (tsocket),
+ "w_buf_size", 4, NULL);
+
+ assert (thrift_framed_transport_open (transport, NULL) == TRUE);
+ assert (thrift_framed_transport_is_open (transport));
+
+ /* write 10 bytes */
+ thrift_framed_transport_write (transport, buf, 10, NULL);
+ thrift_framed_transport_flush (transport, NULL);
+
+ thrift_framed_transport_write (transport, buf, 1, NULL);
+ thrift_framed_transport_flush (transport, NULL);
+
+ thrift_framed_transport_write (transport, buf, 10, NULL);
+ thrift_framed_transport_flush (transport, NULL);
+
+ thrift_framed_transport_write (transport, buf, 10, NULL);
+ thrift_framed_transport_flush (transport, NULL);
+
+ thrift_framed_transport_write_end (transport, NULL);
+ thrift_framed_transport_flush (transport, NULL);
+ thrift_framed_transport_close (transport, NULL);
+
+ g_object_unref (transport);
+ g_object_unref (tsocket);
+
+ assert ( wait (&status) == pid );
+ assert ( status == 0 );
+ }
+}
+
+static void
+thrift_server (const int port)
+{
+ int bytes = 0;
+ ThriftServerTransport *transport = NULL;
+ ThriftTransport *client = NULL;
+ guchar buf[10]; /* a buffer */
+ guchar match[10] = TEST_DATA;
+
+ ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
+ "port", port, NULL);
+
+ transport = THRIFT_SERVER_TRANSPORT (tsocket);
+ thrift_server_transport_listen (transport, NULL);
+
+ /* wrap the client in a BufferedTransport */
+ client = g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT, "transport",
+ thrift_server_transport_accept (transport, NULL),
+ "r_buf_size", 5, NULL);
+ assert (client != NULL);
+
+ /* read 10 bytes */
+ bytes = thrift_framed_transport_read (client, buf, 10, NULL);
+ assert (bytes == 10); /* make sure we've read 10 bytes */
+ assert ( memcmp (buf, match, 10) == 0 ); /* make sure what we got matches */
+
+ bytes = thrift_framed_transport_read (client, buf, 6, NULL);
+ bytes = thrift_framed_transport_read (client, buf, 5, NULL);
+ bytes = thrift_framed_transport_read (client, buf, 1, NULL);
+
+ bytes = thrift_framed_transport_read (client, buf, 12, NULL);
+
+ thrift_framed_transport_read_end (client, NULL);
+ thrift_framed_transport_close (client, NULL);
+ g_object_unref (client);
+ g_object_unref (tsocket);
+}
+
+int
+main(void)
+{
+ g_type_init();
+ test_create_and_destroy();
+ test_open_and_close();
+ test_read_and_write();
+
+ return 0;
+}
+
Added: incubator/thrift/trunk/lib/c_glib/test/testmemorybuffer.c
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/c_glib/test/testmemorybuffer.c?rev=1027933&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/c_glib/test/testmemorybuffer.c (added)
+++ incubator/thrift/trunk/lib/c_glib/test/testmemorybuffer.c Wed Oct 27 12:30:11 2010
@@ -0,0 +1,75 @@
+#include <assert.h>
+#include <netdb.h>
+
+#include "transport/thrift_transport.h"
+#include "transport/thrift_socket.h"
+#include "transport/thrift_server_transport.h"
+#include "transport/thrift_server_socket.h"
+
+#define TEST_DATA { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' }
+
+#include "../src/transport/thrift_memory_buffer.c"
+
+/* test object creation and destruction */
+static void
+test_create_and_destroy(void)
+{
+ GObject *object = NULL;
+ object = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, NULL);
+ assert (object != NULL);
+ g_object_unref (object);
+}
+
+static void
+test_open_and_close(void)
+{
+ ThriftMemoryBuffer *tbuffer = NULL;
+
+ /* create a ThriftMemoryBuffer */
+ tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, NULL);
+
+ /* this shouldn't work */
+ assert (thrift_memory_buffer_open (THRIFT_TRANSPORT (tbuffer), NULL) == TRUE);
+ assert (thrift_memory_buffer_is_open (THRIFT_TRANSPORT (tbuffer)) == TRUE);
+ assert (thrift_memory_buffer_close (THRIFT_TRANSPORT (tbuffer), NULL) == TRUE);
+ g_object_unref (tbuffer);
+}
+
+static void
+test_read_and_write(void)
+{
+ ThriftMemoryBuffer *tbuffer = NULL;
+ guchar buf[10] = TEST_DATA;
+ guchar read[10];
+ GError *error = NULL;
+
+ tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, "buf_size", 5, NULL);
+ assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer),
+ (gpointer) buf,
+ 10, &error) == FALSE);
+ assert (error != NULL);
+ g_error_free (error);
+ error = NULL;
+ g_object_unref (tbuffer);
+
+ tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, "buf_size", 15, NULL);
+ assert (thrift_memory_buffer_write (THRIFT_TRANSPORT (tbuffer),
+ (gpointer) buf, 10, &error) == TRUE);
+ assert (error == NULL);
+
+ assert (thrift_memory_buffer_read (THRIFT_TRANSPORT (tbuffer),
+ &read, 10, &error) > 0);
+ assert (error == NULL);
+}
+
+int
+main(void)
+{
+ g_type_init();
+ test_create_and_destroy();
+ test_open_and_close();
+ test_read_and_write();
+
+ return 0;
+}
+
Added: incubator/thrift/trunk/lib/c_glib/test/testoptionalrequired.c
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/c_glib/test/testoptionalrequired.c?rev=1027933&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/c_glib/test/testoptionalrequired.c (added)
+++ incubator/thrift/trunk/lib/c_glib/test/testoptionalrequired.c Wed Oct 27 12:30:11 2010
@@ -0,0 +1,182 @@
+#include <assert.h>
+#include <glib.h>
+
+#include "thrift_struct.h"
+#include "protocol/thrift_protocol.h"
+#include "protocol/thrift_binary_protocol.h"
+#include "transport/thrift_memory_buffer.h"
+#include "gen-c_glib/t_test_optional_required_test_types.h"
+
+#include "gen-c_glib/t_test_optional_required_test_types.c"
+
+static void
+write_to_read (ThriftStruct *w, ThriftStruct *r, GError **write_error,
+ GError **read_error)
+{
+ ThriftMemoryBuffer *tbuffer = NULL;
+ ThriftProtocol *protocol = NULL;
+
+ tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, NULL);
+ protocol = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport",
+ tbuffer, NULL);
+
+ thrift_struct_write (w, protocol, write_error);
+ thrift_struct_read (r, protocol, read_error);
+
+ g_object_unref (protocol);
+ g_object_unref (tbuffer);
+}
+
+static void
+test_old_school1 (void)
+{
+ TTestOldSchool *o = NULL;
+
+ o = g_object_new (T_TEST_TYPE_OLD_SCHOOL, NULL);
+ o->im_int = 10;
+ o->im_str = g_strdup ("test");
+ o->im_big = g_ptr_array_new ();
+ g_ptr_array_free (o->im_big, FALSE);
+ g_free (o->im_str);
+ g_object_unref (o);
+}
+
+/**
+ * Write to read with optional fields
+ */
+static void
+test_simple (void)
+{
+ TTestSimple *s1 = NULL, *s2 = NULL, *s3 = NULL;
+
+ s1 = g_object_new (T_TEST_TYPE_SIMPLE, NULL);
+ s2 = g_object_new (T_TEST_TYPE_SIMPLE, NULL);
+ s3 = g_object_new (T_TEST_TYPE_SIMPLE, NULL);
+
+ // write-to-read with optional fields
+ s1->im_optional = 10;
+ assert (s1->__isset_im_default == FALSE);
+ assert (s1->__isset_im_optional == FALSE);
+ write_to_read (THRIFT_STRUCT (s1), THRIFT_STRUCT (s2), NULL, NULL);
+ assert (s2->__isset_im_default = TRUE);
+ assert (s2->__isset_im_optional == FALSE);
+ assert (s2->im_optional == 0);
+
+ s1->__isset_im_optional = TRUE;
+ write_to_read (THRIFT_STRUCT (s1), THRIFT_STRUCT (s3), NULL, NULL);
+ assert (s3->__isset_im_default == TRUE);
+ assert (s3->__isset_im_optional == TRUE);
+ assert (s3->im_optional == 10);
+
+ g_object_unref (s1);
+ g_object_unref (s2);
+}
+
+/**
+ * Writing between optional and default
+ */
+static void
+test_tricky1 (void)
+{
+ TTestTricky1 *t1 = NULL;
+ TTestTricky2 *t2 = NULL;
+
+ t1 = g_object_new (T_TEST_TYPE_TRICKY1, NULL);
+ t2 = g_object_new (T_TEST_TYPE_TRICKY2, NULL);
+
+ t2->im_optional = 10;
+ write_to_read (THRIFT_STRUCT (t2), THRIFT_STRUCT (t1), NULL, NULL);
+ write_to_read (THRIFT_STRUCT (t1), THRIFT_STRUCT (t2), NULL, NULL);
+
+ assert (t1->__isset_im_default == FALSE);
+ assert (t2->__isset_im_optional == TRUE);
+ assert (t1->im_default == t2->im_optional);
+ assert (t1->im_default == 0);
+
+ g_object_unref (t1);
+ g_object_unref (t2);
+}
+
+/**
+ * Writing between default and required.
+ */
+static void
+test_tricky2 (void)
+{
+ TTestTricky1 *t1 = NULL;
+ TTestTricky3 *t3 = NULL;
+
+ t1 = g_object_new (T_TEST_TYPE_TRICKY1, NULL);
+ t3 = g_object_new (T_TEST_TYPE_TRICKY3, NULL);
+
+ write_to_read (THRIFT_STRUCT (t1), THRIFT_STRUCT (t3), NULL, NULL);
+ write_to_read (THRIFT_STRUCT (t3), THRIFT_STRUCT (t1), NULL, NULL);
+
+ assert (t1->__isset_im_default == TRUE);
+
+ g_object_unref (t1);
+ g_object_unref (t3);
+}
+
+/**
+ * Writing between optional and required.
+ */
+static void
+test_tricky3 (void)
+{
+ TTestTricky2 *t2 = NULL;
+ TTestTricky3 *t3 = NULL;
+
+ t2 = g_object_new (T_TEST_TYPE_TRICKY2, NULL);
+ t3 = g_object_new (T_TEST_TYPE_TRICKY3, NULL);
+
+ t2->__isset_im_optional = TRUE;
+
+ write_to_read (THRIFT_STRUCT (t2), THRIFT_STRUCT (t3), NULL, NULL);
+ write_to_read (THRIFT_STRUCT (t3), THRIFT_STRUCT (t2), NULL, NULL);
+
+ g_object_unref (t2);
+ g_object_unref (t3);
+}
+
+/**
+ * Catch an optional not set exception. To quote the
+ * C++ test, "Mu-hu-ha-ha-ha!"
+ */
+static void
+test_tricky4 (void)
+{
+ TTestTricky2 *t2 = NULL;
+ TTestTricky3 *t3 = NULL;
+ GError *read_error = NULL;
+
+ t2 = g_object_new (T_TEST_TYPE_TRICKY2, NULL);
+ t3 = g_object_new (T_TEST_TYPE_TRICKY3, NULL);
+
+ // throws protocol exception
+ write_to_read (THRIFT_STRUCT (t2), THRIFT_STRUCT (t3), NULL, &read_error);
+ assert (read_error != NULL);
+ g_error_free (read_error);
+
+ write_to_read (THRIFT_STRUCT (t3), THRIFT_STRUCT (t2), NULL, NULL);
+
+ assert (t2->__isset_im_optional);
+
+ g_object_unref (t2);
+ g_object_unref (t3);
+}
+
+int
+main(void)
+{
+ g_type_init ();
+ test_old_school1 ();
+ test_simple ();
+ test_tricky1 ();
+ test_tricky2 ();
+ test_tricky3 ();
+ test_tricky4 ();
+ return 0;
+}
+
+