http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_framed_transport.c ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_framed_transport.c b/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_framed_transport.c new file mode 100644 index 0000000..4c8b71f --- /dev/null +++ b/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_framed_transport.c @@ -0,0 +1,384 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include <assert.h> +#include <netdb.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> +#include <sys/socket.h> +#include <netinet/in.h> + +#include <thrift/c_glib/thrift.h> +#include <thrift/c_glib/transport/thrift_transport.h> +#include <thrift/c_glib/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 +}; + +G_DEFINE_TYPE(ThriftFramedTransport, thrift_framed_transport, THRIFT_TYPE_TRANSPORT) + +/* 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); +} + +/* overrides thrift_transport_peek */ +gboolean +thrift_framed_transport_peek (ThriftTransport *transport, GError **error) +{ + ThriftFramedTransport *t = THRIFT_FRAMED_TRANSPORT (transport); + return (t->r_buf->len > 0) || thrift_transport_peek (t->transport, error); +} + +/* 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); +} + +/* 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); + guint32 sz; + gint32 bytes; + gboolean result = FALSE; + + /* read the size */ + if (thrift_transport_read (t->transport, + &sz, + sizeof (sz), + error) == sizeof (sz)) + { + guchar *tmpdata; + + sz = ntohl (sz); + + /* create a buffer to hold the data and read that much data */ + tmpdata = g_alloca (sz); + bytes = thrift_transport_read (t->transport, tmpdata, sz, error); + + if (bytes > 0 && (error == NULL || *error == NULL)) + { + /* add the data to the buffer */ + g_byte_array_append (t->r_buf, tmpdata, bytes); + + result = TRUE; + } + } + + return result; +} + +/* 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; + gint32 result = -1; + + /* 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 */ + if (thrift_framed_transport_read_frame (transport, error) == TRUE) + { + /* 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 ((guint8 *)buf + len - want, t->r_buf->data, give); + t->r_buf = g_byte_array_remove_range (t->r_buf, 0, give); + want -= give; + + result = len - want; + } + + return result; +} + +/* 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; +} + +gboolean +thrift_framed_transport_write_slow (ThriftTransport *transport, gpointer buf, + guint32 len, GError **error) +{ + ThriftFramedTransport *t = THRIFT_FRAMED_TRANSPORT (transport); + + THRIFT_UNUSED_VAR (error); + + /* append the data to the buffer and we're done */ + g_byte_array_append (t->w_buf, buf, len); + + 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; +} + +/* implements thrift_transport_flush */ +gboolean +thrift_framed_transport_flush (ThriftTransport *transport, GError **error) +{ + ThriftFramedTransport *t = THRIFT_FRAMED_TRANSPORT (transport); + gint32 sz_hbo, sz_nbo; + guchar *tmpdata; + + /* 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) t->w_buf->len); + + /* copy the size of the frame and then the frame itself */ + tmpdata = g_alloca (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; +} + +/* initializes the instance */ +static void +thrift_framed_transport_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) +{ + ThriftFramedTransport *transport = THRIFT_FRAMED_TRANSPORT (object); + + THRIFT_UNUSED_VAR (pspec); + + 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) +{ + ThriftFramedTransport *transport = THRIFT_FRAMED_TRANSPORT (object); + + THRIFT_UNUSED_VAR (pspec); + + 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) +{ + ThriftTransportClass *ttc = THRIFT_TRANSPORT_CLASS (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); + + gobject_class->finalize = thrift_framed_transport_finalize; + ttc->is_open = thrift_framed_transport_is_open; + ttc->peek = thrift_framed_transport_peek; + 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; +}
http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_framed_transport.h ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_framed_transport.h b/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_framed_transport.h new file mode 100644 index 0000000..95c0123 --- /dev/null +++ b/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_framed_transport.h @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef _THRIFT_FRAMED_TRANSPORT_H +#define _THRIFT_FRAMED_TRANSPORT_H + +#include <glib.h> +#include <glib-object.h> + +#include <thrift/c_glib/transport/thrift_transport.h> + +G_BEGIN_DECLS + +/*! \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)) + +typedef struct _ThriftFramedTransport ThriftFramedTransport; + +/*! + * 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 _ThriftFramedTransportClass ThriftFramedTransportClass; + +/*! + * ThriftFramedTransport class. + */ +struct _ThriftFramedTransportClass +{ + ThriftTransportClass parent; +}; + +/* used by THRIFT_TYPE_FRAMED_TRANSPORT */ +GType thrift_framed_transport_get_type (void); + +G_END_DECLS + +#endif http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_framed_transport_factory.c ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_framed_transport_factory.c b/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_framed_transport_factory.c new file mode 100644 index 0000000..e68fe0a --- /dev/null +++ b/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_framed_transport_factory.c @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include <thrift/c_glib/thrift.h> +#include <thrift/c_glib/transport/thrift_framed_transport.h> +#include <thrift/c_glib/transport/thrift_framed_transport_factory.h> + +G_DEFINE_TYPE (ThriftFramedTransportFactory, + thrift_framed_transport_factory, + THRIFT_TYPE_TRANSPORT_FACTORY) + +/* Wraps a transport with a ThriftFramedTransport. */ +ThriftTransport * +thrift_framed_transport_factory_get_transport (ThriftTransportFactory *factory, + ThriftTransport *transport) +{ + THRIFT_UNUSED_VAR (factory); + + return THRIFT_TRANSPORT (g_object_new (THRIFT_TYPE_FRAMED_TRANSPORT, + "transport", transport, + NULL)); +} + +static void +thrift_framed_transport_factory_init (ThriftFramedTransportFactory *self) +{ + THRIFT_UNUSED_VAR (self); +} + +static void +thrift_framed_transport_factory_class_init (ThriftFramedTransportFactoryClass *klass) +{ + ThriftTransportFactoryClass *base_class = + THRIFT_TRANSPORT_FACTORY_CLASS (klass); + + base_class->get_transport = + klass->get_transport = + thrift_framed_transport_factory_get_transport; +} http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_framed_transport_factory.h ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_framed_transport_factory.h b/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_framed_transport_factory.h new file mode 100644 index 0000000..c3e9496 --- /dev/null +++ b/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_framed_transport_factory.h @@ -0,0 +1,86 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef _THRIFT_FRAMED_TRANSPORT_FACTORY_H +#define _THRIFT_FRAMED_TRANSPORT_FACTORY_H + +#include <glib-object.h> + +#include <thrift/c_glib/transport/thrift_transport.h> +#include <thrift/c_glib/transport/thrift_transport_factory.h> + +G_BEGIN_DECLS + +/*! \file thrift_framed_transport_factory.h + * \brief Wraps a transport with a ThriftFramedTransport. + */ + +/* type macros */ +#define THRIFT_TYPE_FRAMED_TRANSPORT_FACTORY \ + (thrift_framed_transport_factory_get_type ()) +#define THRIFT_FRAMED_TRANSPORT_FACTORY(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST ((obj), \ + THRIFT_TYPE_FRAMED_TRANSPORT_FACTORY, \ + ThriftFramedTransportFactory)) +#define THRIFT_IS_FRAMED_TRANSPORT_FACTORY(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \ + THRIFT_TYPE_FRAMED_TRANSPORT_FACTORY)) +#define THRIFT_FRAMED_TRANSPORT_FACTORY_CLASS(c) \ + (G_TYPE_CHECK_CLASS_CAST ((c), \ + THRIFT_TYPE_FRAMED_TRANSPORT_FACTORY, \ + ThriftFramedTransportFactoryClass)) +#define THRIFT_IS_FRAMED_TRANSPORT_FACTORY_CLASS(c) \ + (G_TYPE_CHECK_CLASS_TYPE ((c), \ + THRIFT_TYPE_FRAMED_TRANSPORT_FACTORY)) +#define THRIFT_FRAMED_TRANSPORT_FACTORY_GET_CLASS(obj) \ + (G_TYPE_INSTANCE_GET_CLASS ((obj), \ + THRIFT_TYPE_FRAMED_TRANSPORT_FACTORY, \ + ThriftFramedTransportFactoryClass)) + +typedef struct _ThriftFramedTransportFactory ThriftFramedTransportFactory; + +/* Thrift Framed-Transport Factory instance */ +struct _ThriftFramedTransportFactory +{ + ThriftTransportFactory parent; +}; + +typedef struct _ThriftFramedTransportFactoryClass ThriftFramedTransportFactoryClass; + +/* Thrift Framed-Transport Factory class */ +struct _ThriftFramedTransportFactoryClass +{ + ThriftTransportFactoryClass parent; + + /* vtable */ + ThriftTransport *(*get_transport) (ThriftTransportFactory *factory, + ThriftTransport *transport); +}; + +/* used by THRIFT_TYPE_FRAMED_TRANSPORT_FACTORY */ +GType thrift_framed_transport_factory_get_type (void); + +/* virtual public methods */ +ThriftTransport * +thrift_framed_transport_factory_get_transport (ThriftTransportFactory *factory, + ThriftTransport *transport); + +G_END_DECLS + +#endif /* _THRIFT_FRAMED_TRANSPORT_FACTORY_H */ http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_memory_buffer.c ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_memory_buffer.c b/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_memory_buffer.c new file mode 100644 index 0000000..8d66c3d --- /dev/null +++ b/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_memory_buffer.c @@ -0,0 +1,231 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include <assert.h> +#include <netdb.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +#include <thrift/c_glib/thrift.h> +#include <thrift/c_glib/transport/thrift_transport.h> +#include <thrift/c_glib/transport/thrift_memory_buffer.h> + +/* object properties */ +enum _ThriftMemoryBufferProperties +{ + PROP_0, + PROP_THRIFT_MEMORY_BUFFER_BUFFER_SIZE +}; + +G_DEFINE_TYPE(ThriftMemoryBuffer, thrift_memory_buffer, THRIFT_TYPE_TRANSPORT) + +/* 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) +{ + ThriftMemoryBuffer *t = THRIFT_MEMORY_BUFFER (transport); + guint32 give = len; + + THRIFT_UNUSED_VAR (error); + + /* 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) +{ + ThriftMemoryBuffer *t = THRIFT_MEMORY_BUFFER (transport); + + THRIFT_UNUSED_VAR (error); + + /* 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; +} + +/* initializes the instance */ +static void +thrift_memory_buffer_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) +{ + ThriftMemoryBuffer *transport = THRIFT_MEMORY_BUFFER (object); + + THRIFT_UNUSED_VAR (pspec); + + 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) +{ + ThriftMemoryBuffer *transport = THRIFT_MEMORY_BUFFER (object); + + THRIFT_UNUSED_VAR (pspec); + + 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) +{ + ThriftTransportClass *ttc = THRIFT_TRANSPORT_CLASS (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); + + 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; +} http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_memory_buffer.h ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_memory_buffer.h b/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_memory_buffer.h new file mode 100644 index 0000000..4ebe98f --- /dev/null +++ b/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_memory_buffer.h @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef _THRIFT_MEMORY_BUFFER_H +#define _THRIFT_MEMORY_BUFFER_H + +#include <glib.h> +#include <glib-object.h> + +#include <thrift/c_glib/transport/thrift_transport.h> + +G_BEGIN_DECLS + +/*! \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)) + +typedef struct _ThriftMemoryBuffer ThriftMemoryBuffer; + +/*! + * ThriftMemoryBuffer instance. + */ +struct _ThriftMemoryBuffer +{ + ThriftTransport parent; + + /* private */ + GByteArray *buf; + guint32 buf_size; +}; + +typedef struct _ThriftMemoryBufferClass ThriftMemoryBufferClass; + +/*! + * ThriftMemoryBuffer class. + */ +struct _ThriftMemoryBufferClass +{ + ThriftTransportClass parent; +}; + +/* used by THRIFT_TYPE_MEMORY_BUFFER */ +GType thrift_memory_buffer_get_type (void); + +G_END_DECLS + +#endif http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_server_socket.c ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_server_socket.c b/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_server_socket.c new file mode 100644 index 0000000..97a0ec2 --- /dev/null +++ b/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_server_socket.c @@ -0,0 +1,256 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include <errno.h> +#include <netdb.h> +#include <string.h> +#include <stdlib.h> +#include <unistd.h> +#include <sys/socket.h> +#include <netinet/in.h> + +#include <thrift/c_glib/thrift.h> +#include <thrift/c_glib/transport/thrift_socket.h> +#include <thrift/c_glib/transport/thrift_transport.h> +#include <thrift/c_glib/transport/thrift_server_transport.h> +#include <thrift/c_glib/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; + +G_DEFINE_TYPE(ThriftServerSocket, thrift_server_socket, THRIFT_TYPE_SERVER_TRANSPORT) + +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 = THRIFT_INVALID_SOCKET; + 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 = THRIFT_INVALID_SOCKET; + + return TRUE; +} + +/* 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_init (ThriftServerSocket *socket) +{ + socket->sd = THRIFT_INVALID_SOCKET; +} + +/* destructor */ +static void +thrift_server_socket_finalize (GObject *object) +{ + ThriftServerSocket *socket = THRIFT_SERVER_SOCKET (object); + + if (socket->sd != THRIFT_INVALID_SOCKET) + { + close (socket->sd); + } + socket->sd = THRIFT_INVALID_SOCKET; +} + +/* 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) +{ + ThriftServerTransportClass *tstc = THRIFT_SERVER_TRANSPORT_CLASS (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; + + tstc->listen = thrift_server_socket_listen; + tstc->accept = thrift_server_socket_accept; + tstc->close = thrift_server_socket_close; +} + http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_server_socket.h ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_server_socket.h b/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_server_socket.h new file mode 100644 index 0000000..f072cb0 --- /dev/null +++ b/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_server_socket.h @@ -0,0 +1,90 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef _THRIFT_SERVER_SOCKET_H +#define _THRIFT_SERVER_SOCKET_H + +#include <glib-object.h> + +#include "thrift_server_transport.h" + +G_BEGIN_DECLS + +/*! \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)) + +typedef struct _ThriftServerSocket ThriftServerSocket; + +/*! + * Thrift ServerSocket instance. + */ +struct _ThriftServerSocket +{ + ThriftServerTransport parent; + + /* private */ + gshort port; + gshort backlog; + int sd; + guint8 *buf; + guint32 buf_size; + guint32 buf_len; +}; + +typedef struct _ThriftServerSocketClass ThriftServerSocketClass; + +/*! + * Thrift ServerSocket class. + */ +struct _ThriftServerSocketClass +{ + ThriftServerTransportClass parent; +}; + +/* 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 ()) + +G_END_DECLS + +#endif http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_server_transport.c ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_server_transport.c b/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_server_transport.c new file mode 100644 index 0000000..c25d138 --- /dev/null +++ b/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_server_transport.c @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include <thrift/c_glib/thrift.h> +#include <thrift/c_glib/transport/thrift_transport.h> +#include <thrift/c_glib/transport/thrift_server_transport.h> + +G_DEFINE_ABSTRACT_TYPE(ThriftServerTransport, thrift_server_transport, G_TYPE_OBJECT) + +/* 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; +} + +static void +thrift_server_transport_init (ThriftServerTransport *transport) +{ + THRIFT_UNUSED_VAR (transport); +} + +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); +} http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_server_transport.h ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_server_transport.h b/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_server_transport.h new file mode 100644 index 0000000..98a9191 --- /dev/null +++ b/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_server_transport.h @@ -0,0 +1,89 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef _THRIFT_SERVER_TRANSPORT_H +#define _THRIFT_SERVER_TRANSPORT_H + +#include <glib-object.h> + +#include "thrift_transport.h" + +G_BEGIN_DECLS + +/*! \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)) + +typedef struct _ThriftServerTransport ThriftServerTransport; + +struct _ThriftServerTransport +{ + GObject parent; +}; + +typedef struct _ThriftServerTransportClass ThriftServerTransportClass; + +/*! + * 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); +}; + +/* 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); + +G_END_DECLS + +#endif /* _THRIFT_SERVER_TRANSPORT_H */ http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_socket.c ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_socket.c b/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_socket.c new file mode 100644 index 0000000..1c147ec --- /dev/null +++ b/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_socket.c @@ -0,0 +1,371 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include <errno.h> +#include <netdb.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <sys/socket.h> +#include <netinet/in.h> + +#include <thrift/c_glib/thrift.h> +#include <thrift/c_glib/transport/thrift_transport.h> +#include <thrift/c_glib/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; + +G_DEFINE_TYPE(ThriftSocket, thrift_socket, THRIFT_TYPE_TRANSPORT) + +/* implements thrift_transport_is_open */ +gboolean +thrift_socket_is_open (ThriftTransport *transport) +{ + ThriftSocket *socket = THRIFT_SOCKET (transport); + return socket->sd != THRIFT_INVALID_SOCKET; +} + +/* overrides thrift_transport_peek */ +gboolean +thrift_socket_peek (ThriftTransport *transport, GError **error) +{ + gboolean result = FALSE; + guint8 buf; + int r; + int errno_copy; + + ThriftSocket *socket = THRIFT_SOCKET (transport); + + if (thrift_socket_is_open (transport)) + { + r = recv (socket->sd, &buf, 1, MSG_PEEK); + if (r == -1) + { + errno_copy = errno; + + #if defined __FreeBSD__ || defined __MACH__ + /* FreeBSD returns -1 and ECONNRESET if the socket was closed by the other + side */ + if (errno_copy == ECONNRESET) + { + thrift_socket_close (transport, error); + } + else + { + #endif + + g_set_error (error, + THRIFT_TRANSPORT_ERROR, + THRIFT_TRANSPORT_ERROR_SOCKET, + "failed to peek at socket - %s", + strerror (errno_copy)); + + #if defined __FreeBSD__ || defined __MACH__ + } + #endif + } + else if (r > 0) + { + result = TRUE; + } + } + + return result; +} + +/* implements thrift_transport_open */ +gboolean +thrift_socket_open (ThriftTransport *transport, GError **error) +{ + struct hostent *hp = NULL; + struct sockaddr_in pin; + int err; +#if defined(HAVE_GETHOSTBYNAME_R) + struct hostent he; + char buf[1024]; +#endif + + ThriftSocket *tsocket = THRIFT_SOCKET (transport); + g_return_val_if_fail (tsocket->sd == THRIFT_INVALID_SOCKET, FALSE); + + /* lookup the destination host */ +#if defined(HAVE_GETHOSTBYNAME_R) + if (gethostbyname_r (tsocket->hostname, &he, buf, 1024, &hp, &err) != 0 || hp == NULL) +#else + if ((hp = gethostbyname (tsocket->hostname)) == NULL && (err = h_errno)) +#endif + { + /* 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 (err)); + 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 = THRIFT_INVALID_SOCKET; + 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, (guint8 *)buf + got, len-got, 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 != THRIFT_INVALID_SOCKET, FALSE); + + while (sent < len) + { + ret = send (socket->sd, (guint8 *)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; +} + +/* initializes the instance */ +static void +thrift_socket_init (ThriftSocket *socket) +{ + socket->sd = THRIFT_INVALID_SOCKET; +} + +/* 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 != THRIFT_INVALID_SOCKET) + { + close (socket->sd); + } + socket->sd = THRIFT_INVALID_SOCKET; +} + +/* property accessor */ +void +thrift_socket_get_property (GObject *object, guint property_id, + GValue *value, GParamSpec *pspec) +{ + ThriftSocket *socket = THRIFT_SOCKET (object); + + THRIFT_UNUSED_VAR (pspec); + + 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) +{ + ThriftSocket *socket = THRIFT_SOCKET (object); + + THRIFT_UNUSED_VAR (pspec); + + 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) +{ + ThriftTransportClass *ttc = THRIFT_TRANSPORT_CLASS (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); + + gobject_class->finalize = thrift_socket_finalize; + ttc->is_open = thrift_socket_is_open; + ttc->peek = thrift_socket_peek; + 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; +} http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_socket.h ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_socket.h b/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_socket.h new file mode 100644 index 0000000..981577d --- /dev/null +++ b/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_socket.h @@ -0,0 +1,75 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef _THRIFT_SOCKET_H +#define _THRIFT_SOCKET_H + +#include <glib-object.h> + +#include <thrift/c_glib/transport/thrift_transport.h> + +G_BEGIN_DECLS + +/*! \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)) + +typedef struct _ThriftSocket ThriftSocket; + +/*! + * Thrift Socket instance. + */ +struct _ThriftSocket +{ + ThriftTransport parent; + + /* private */ + gchar *hostname; + gshort port; + int sd; + guint8 *buf; + guint32 buf_size; + guint32 buf_len; +}; + +typedef struct _ThriftSocketClass ThriftSocketClass; + +/*! + * Thrift Socket class. + */ +struct _ThriftSocketClass +{ + ThriftTransportClass parent; +}; + +/* used by THRIFT_TYPE_SOCKET */ +GType thrift_socket_get_type (void); + +G_END_DECLS + +#endif http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_transport.c ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_transport.c b/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_transport.c new file mode 100644 index 0000000..5533437 --- /dev/null +++ b/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_transport.c @@ -0,0 +1,126 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include <thrift/c_glib/thrift.h> +#include <thrift/c_glib/transport/thrift_transport.h> + +/* define the GError domain string */ +#define THRIFT_TRANSPORT_ERROR_DOMAIN "thrift-transport-error-quark" + +G_DEFINE_ABSTRACT_TYPE(ThriftTransport, thrift_transport, G_TYPE_OBJECT) + +gboolean +thrift_transport_is_open (ThriftTransport *transport) +{ + return THRIFT_TRANSPORT_GET_CLASS (transport)->is_open (transport); +} + +gboolean +thrift_transport_peek (ThriftTransport *transport, GError **error) +{ + return THRIFT_TRANSPORT_GET_CLASS (transport)->peek (transport, error); +} + +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); +} + +/* by default, peek returns true if and only if the transport is open */ +static gboolean +thrift_transport_real_peek (ThriftTransport *transport, GError **error) +{ + THRIFT_UNUSED_VAR (error); + + return THRIFT_TRANSPORT_GET_CLASS (transport)->is_open (transport); +} + +/* define the GError domain for Thrift transports */ +GQuark +thrift_transport_error_quark (void) +{ + return g_quark_from_static_string (THRIFT_TRANSPORT_ERROR_DOMAIN); +} + +/* 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; + + /* provide a default implementation for the peek method */ + cls->peek = thrift_transport_real_peek; +} + +static void +thrift_transport_init (ThriftTransport *transport) +{ + THRIFT_UNUSED_VAR (transport); +} http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_transport.h ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_transport.h b/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_transport.h new file mode 100644 index 0000000..5555a5e --- /dev/null +++ b/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_transport.h @@ -0,0 +1,167 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef _THRIFT_TRANSPORT_H +#define _THRIFT_TRANSPORT_H + +#include <glib-object.h> + +G_BEGIN_DECLS + +/*! \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)) + +typedef struct _ThriftTransport ThriftTransport; + +/*! + * Thrift Protocol object + */ +struct _ThriftTransport +{ + GObject parent; +}; + +typedef struct _ThriftTransportClass ThriftTransportClass; + +/*! + * Thrift Transport class + */ +struct _ThriftTransportClass +{ + GObjectClass parent; + + /* vtable */ + gboolean (*is_open) (ThriftTransport *transport); + gboolean (*peek) (ThriftTransport *transport, GError **error); + 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); +}; + +/* 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); + +/*! + * Tests whether there is more data to read or if the remote side is still + * open. By default this is true whenever the transport is open, but + * implementations should add logic to test for this condition where possible + * (i.e. on a socket). + * + * This is used by a server to check if it should listen for another request. + * \public \memberof ThriftTransportInterface + */ +gboolean thrift_transport_peek (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 ()) + +/* define macro for invalid socket */ +#define THRIFT_INVALID_SOCKET (-1) + +G_END_DECLS + +#endif /* _THRIFT_TRANSPORT_H */ http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_transport_factory.c ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_transport_factory.c b/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_transport_factory.c new file mode 100644 index 0000000..a2025cf --- /dev/null +++ b/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_transport_factory.c @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include <thrift/c_glib/thrift.h> +#include <thrift/c_glib/transport/thrift_transport_factory.h> + +G_DEFINE_TYPE(ThriftTransportFactory, thrift_transport_factory, G_TYPE_OBJECT) + +/* builds a transport from the base transport. */ +ThriftTransport * +thrift_transport_factory_get_transport (ThriftTransportFactory *factory, + ThriftTransport *transport) +{ + THRIFT_UNUSED_VAR (factory); + return transport; +} + +static void +thrift_transport_factory_class_init (ThriftTransportFactoryClass *cls) +{ + cls->get_transport = thrift_transport_factory_get_transport; +} + +static void +thrift_transport_factory_init (ThriftTransportFactory *factory) +{ + THRIFT_UNUSED_VAR (factory); +} http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/d709f67d/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_transport_factory.h ---------------------------------------------------------------------- diff --git a/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_transport_factory.h b/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_transport_factory.h new file mode 100644 index 0000000..0e3da30 --- /dev/null +++ b/depends/thirdparty/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_transport_factory.h @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef _THRIFT_TRANSPORT_FACTORY_H +#define _THRIFT_TRANSPORT_FACTORY_H + +#include <glib-object.h> + +#include "thrift_transport.h" + +G_BEGIN_DECLS + +/*! \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)) + +typedef struct _ThriftTransportFactory ThriftTransportFactory; + +/* Thrift Transport Factory instance */ +struct _ThriftTransportFactory +{ + GObject parent; +}; + +typedef struct _ThriftTransportFactoryClass ThriftTransportFactoryClass; + +/* Thrift Transport Factory class */ +struct _ThriftTransportFactoryClass +{ + GObjectClass parent; + + /* vtable */ + ThriftTransport *(*get_transport) (ThriftTransportFactory *factory, + ThriftTransport *transport); +}; + +/* 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); + +G_END_DECLS + +#endif /* _THRIFT_TRANSPORT_FACTORY_H */
