diff options
author | zeshuai007 <51382517@qq.com> | 2020-11-30 11:16:10 +0800 |
---|---|---|
committer | Jens Geyer <jensg@apache.org> | 2021-04-01 23:34:47 +0200 |
commit | 037753eb6b5c56db0c2b9f4d932377f06452022b (patch) | |
tree | 6e8e1cd5656a149bebd59d349864d21dc65c59c9 /lib/c_glib | |
parent | fa87d0ea424da50930c2843e1117a5c2abe20593 (diff) | |
download | thrift-037753eb6b5c56db0c2b9f4d932377f06452022b.tar.gz |
THRIFT-5265 add the zlib transport to c_glib
Client: c_glib
Patch: Zezeng Wang
This closes #2216
Diffstat (limited to 'lib/c_glib')
-rw-r--r-- | lib/c_glib/CMakeLists.txt | 19 | ||||
-rwxr-xr-x | lib/c_glib/Makefile.am | 8 | ||||
-rw-r--r-- | lib/c_glib/src/thrift/c_glib/transport/thrift_zlib_transport.c | 783 | ||||
-rw-r--r-- | lib/c_glib/src/thrift/c_glib/transport/thrift_zlib_transport.h | 102 | ||||
-rw-r--r-- | lib/c_glib/src/thrift/c_glib/transport/thrift_zlib_transport_factory.c | 55 | ||||
-rw-r--r-- | lib/c_glib/src/thrift/c_glib/transport/thrift_zlib_transport_factory.h | 85 | ||||
-rw-r--r-- | lib/c_glib/test/CMakeLists.txt | 14 | ||||
-rwxr-xr-x | lib/c_glib/test/Makefile.am | 19 | ||||
-rw-r--r-- | lib/c_glib/test/testthrifttestzlibclient.cpp | 645 | ||||
-rw-r--r-- | lib/c_glib/test/testzlibtransport.c | 231 |
10 files changed, 1957 insertions, 4 deletions
diff --git a/lib/c_glib/CMakeLists.txt b/lib/c_glib/CMakeLists.txt index 5f0b6f569..2bee9e42a 100644 --- a/lib/c_glib/CMakeLists.txt +++ b/lib/c_glib/CMakeLists.txt @@ -59,6 +59,17 @@ set(thrift_c_glib_SOURCES src/thrift/c_glib/server/thrift_simple_server.c ) +set(thrift_c_glib_zlib_SOURCES + src/thrift/c_glib/thrift.c + src/thrift/c_glib/thrift_struct.c + src/thrift/c_glib/thrift_application_exception.c + src/thrift/c_glib/thrift_configuration.c + src/thrift/c_glib/transport/thrift_transport.c + src/thrift/c_glib/transport/thrift_transport_factory.c + src/thrift/c_glib/transport/thrift_zlib_transport.c + src/thrift/c_glib/transport/thrift_zlib_transport_factory.c +) + # If OpenSSL is not found just ignore the OpenSSL stuff if(WITH_OPENSSL) list(APPEND thrift_c_glib_SOURCES @@ -75,6 +86,14 @@ include(ThriftMacros) ADD_LIBRARY_THRIFT(thrift_c_glib ${thrift_c_glib_SOURCES}) TARGET_LINK_LIBRARIES_THRIFT(thrift_c_glib PUBLIC ${SYSLIBS}) +# If Zlib is not found just ignore the Zlib stuff +if(WITH_ZLIB) + include_directories(SYSTEM ${ZLIB_INCLUDE_DIRS}) + ADD_LIBRARY_THRIFT(thrift_c_glib_zlib ${thrift_c_glib_zlib_SOURCES}) + TARGET_LINK_LIBRARIES_THRIFT(thrift_c_glib_zlib ${SYSLIBS} ${ZLIB_LIBRARIES}) + TARGET_LINK_LIBRARIES_THRIFT_AGAINST_THRIFT_LIBRARY(thrift_c_glib_zlib thrift_c_glib) +endif() + # Install the headers install(DIRECTORY "src/thrift" DESTINATION "${INCLUDE_INSTALL_DIR}" FILES_MATCHING PATTERN "*.h") diff --git a/lib/c_glib/Makefile.am b/lib/c_glib/Makefile.am index 7619fb4cb..b2061bbc7 100755 --- a/lib/c_glib/Makefile.am +++ b/lib/c_glib/Makefile.am @@ -49,6 +49,7 @@ libthrift_c_glib_la_SOURCES = src/thrift/c_glib/thrift.c \ src/thrift/c_glib/transport/thrift_transport_factory.c \ src/thrift/c_glib/transport/thrift_buffered_transport_factory.c \ src/thrift/c_glib/transport/thrift_framed_transport_factory.c \ + src/thrift/c_glib/transport/thrift_zlib_transport_factory.c \ src/thrift/c_glib/transport/thrift_socket.c \ src/thrift/c_glib/transport/thrift_ssl_socket.c \ src/thrift/c_glib/transport/thrift_server_transport.c \ @@ -56,12 +57,13 @@ libthrift_c_glib_la_SOURCES = src/thrift/c_glib/thrift.c \ src/thrift/c_glib/transport/thrift_buffered_transport.c \ src/thrift/c_glib/transport/thrift_fd_transport.c \ src/thrift/c_glib/transport/thrift_framed_transport.c \ + src/thrift/c_glib/transport/thrift_zlib_transport.c \ src/thrift/c_glib/transport/thrift_memory_buffer.c \ src/thrift/c_glib/server/thrift_server.c \ src/thrift/c_glib/server/thrift_simple_server.c libthrift_c_glib_la_CFLAGS = $(AM_CFLAGS) $(GLIB_CFLAGS) $(GOBJECT_CFLAGS) $(OPENSSL_INCLUDES) -I$(top_builddir)/lib/c_glib/src/thrift -libthrift_c_glib_la_LDFLAGS = $(AM_LDFLAGS) $(GLIB_LIBS) $(GOBJECT_LIBS) $(OPENSSL_LDFLAGS) $(OPENSSL_LIBS) +libthrift_c_glib_la_LDFLAGS = $(AM_LDFLAGS) $(GLIB_LIBS) $(GOBJECT_LIBS) $(OPENSSL_LDFLAGS) $(OPENSSL_LIBS) $(ZLIB_LDFLAGS) $(ZLIB_LIBS) include_thriftdir = $(includedir)/thrift/c_glib include_thrift_HEADERS = \ @@ -87,6 +89,7 @@ include_transportdir = $(include_thriftdir)/transport include_transport_HEADERS = src/thrift/c_glib/transport/thrift_buffered_transport.h \ src/thrift/c_glib/transport/thrift_fd_transport.h \ src/thrift/c_glib/transport/thrift_framed_transport.h \ + src/thrift/c_glib/transport/thrift_zlib_transport.h \ src/thrift/c_glib/transport/thrift_memory_buffer.h \ src/thrift/c_glib/transport/thrift_server_socket.h \ src/thrift/c_glib/transport/thrift_server_transport.h \ @@ -96,7 +99,8 @@ include_transport_HEADERS = src/thrift/c_glib/transport/thrift_buffered_transpor src/thrift/c_glib/transport/thrift_transport.h \ src/thrift/c_glib/transport/thrift_transport_factory.h \ src/thrift/c_glib/transport/thrift_buffered_transport_factory.h \ - src/thrift/c_glib/transport/thrift_framed_transport_factory.h + src/thrift/c_glib/transport/thrift_framed_transport_factory.h \ + src/thrift/c_glib/transport/thrift_zlib_transport_factory.h include_serverdir = $(include_thriftdir)/server include_server_HEADERS = src/thrift/c_glib/server/thrift_server.h \ diff --git a/lib/c_glib/src/thrift/c_glib/transport/thrift_zlib_transport.c b/lib/c_glib/src/thrift/c_glib/transport/thrift_zlib_transport.c new file mode 100644 index 000000000..32f1ba27d --- /dev/null +++ b/lib/c_glib/src/thrift/c_glib/transport/thrift_zlib_transport.c @@ -0,0 +1,783 @@ +/* + * 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 <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <sys/socket.h> +#include <sys/un.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_zlib_transport.h> + +#define DEFAULT_URBUF_SIZE 128 +#define DEFAULT_CRBUF_SIZE 1024 +#define DEFAULT_UWBUF_SIZE 128 +#define DEFAULT_CWBUF_SIZE 1024 +#define MIN_DIRECT_DEFLATE_SIZE 32 + +/* object properties */ +enum _ThriftZlibTransportProperties +{ + PROP_0, + PROP_THRIFT_ZLIB_TRANSPORT_TRANSPORT, + PROP_THRIFT_ZLIB_TRANSPORT_URBUF_SIZE, + PROP_THRIFT_ZLIB_TRANSPORT_CRBUF_SIZE, + PROP_THRIFT_ZLIB_TRANSPORT_UWBUF_SIZE, + PROP_THRIFT_ZLIB_TRANSPORT_CWBUF_SIZE, + PROP_THRIFT_ZLIB_TRANSPORT_COMP_LEVEL, + PROP_THRIFT_ZLIB_TRANSPORT_CONFIGURATION, + PROP_THRIFT_ZLIB_TRANSPORT_REMAINING_MESSAGE_SIZE, + PROP_THRIFT_ZLIB_TRANSPORT_KNOW_MESSAGE_SIZE +}; + +G_DEFINE_TYPE (ThriftZlibTransport, thrift_zlib_transport, THRIFT_TYPE_TRANSPORT) + +/*! READING STRATEGY + * We have two buffers for reading: one containing the compressed data (crbuf) + * and one containing the uncompressed data (urbuf). When read is called, + * we repeat the following steps until we have satisfied the request: + * - Copy data from urbuf into the caller's buffer. + * - If we had enough, return. + * - If urbuf is empty, read some data into it from the underlying transport. + * - Inflate data from crbuf into urbuf. + * + * In standalone object, we set input_end to true when inflate returns + * Z_STREAM_END. This allows to make sure that a checksum was verified. + */ +int +thrift_zlib_transport_read_avail (ThriftTransport *transport) +{ + ThriftZlibTransport *t = THRIFT_ZLIB_TRANSPORT (transport); + return t->urbuf_size - t->rstream->avail_out - t->urpos; +} + +/* overrides thrift_transport_is_open */ +gboolean +thrift_zlib_transport_is_open (ThriftTransport *transport) +{ + ThriftZlibTransport *t = THRIFT_ZLIB_TRANSPORT (transport); + return (thrift_zlib_transport_read_avail (transport) > 0) || \ + (t->rstream->avail_in > 0) || THRIFT_TRANSPORT_GET_CLASS (t->transport)->is_open (t->transport); +} + +/* overrides thrift_transport_peek */ +gboolean +thrift_zlib_transport_peek (ThriftTransport *transport, GError **error) +{ + ThriftZlibTransport *t = THRIFT_ZLIB_TRANSPORT (transport); + return (thrift_zlib_transport_read_avail (transport) > 0) || \ + (t->rstream->avail_in > 0) || THRIFT_TRANSPORT_GET_CLASS (t->transport)->peek (t->transport, error); +} + +/* implements thrift_transport_open */ +gboolean +thrift_zlib_transport_open (ThriftTransport *transport, GError **error) +{ + ThriftZlibTransport *t = THRIFT_ZLIB_TRANSPORT (transport); + return THRIFT_TRANSPORT_GET_CLASS (t->transport)->open (t->transport, error); +} + +/* implements thrift_transport_close */ +gboolean +thrift_zlib_transport_close (ThriftTransport *transport, GError **error) +{ + ThriftZlibTransport *t = THRIFT_ZLIB_TRANSPORT (transport); + return THRIFT_TRANSPORT_GET_CLASS (t->transport)->close (t->transport, error); +} + +gint32 +thrift_zlib_transport_read_from_zlib(ThriftTransport *transport, GError **error) +{ + ThriftZlibTransport *t = THRIFT_ZLIB_TRANSPORT (transport); + gint32 got = 0; + int zlib_rv = Z_OK; + + if (t->input_ended) { + /* If input end return error */ + return -1; + } + + /* If we don't have any more compressed data available, + * read some from the underlying transport. + */ + got = THRIFT_TRANSPORT_GET_CLASS(t->transport)->read (t->transport, t->crbuf, 1, error); + if (got < 0) { + return -1; + } + t->rstream->next_in = t->crbuf; + t->rstream->avail_in = got; + + /* We have some compressed data now. Uncompress it. */ + zlib_rv = inflate (t->rstream, Z_SYNC_FLUSH); + if (zlib_rv == Z_STREAM_END) { + t->input_ended = TRUE; + inflateEnd(t->rstream); + return 0; + } else { + if (zlib_rv != Z_OK) { + g_set_error (error, THRIFT_TRANSPORT_ERROR, THRIFT_TRANSPORT_ERROR_RECEIVE, + "zlib error: %d (status = %s)", zlib_rv, t->rstream->msg); + /* It must to return error */ + return -1; + } else { + return 1; + } + } + + /* return 1 to continue to read */ + return 1; +} + +/* implements thrift_transport_read */ +gint32 +thrift_zlib_transport_read_slow (ThriftTransport *transport, gpointer buf, + GError **error) +{ + ThriftZlibTransport *t = THRIFT_ZLIB_TRANSPORT (transport); + gint *buf_tmp = buf; + gint32 need = 1; + gint give; + gint32 ret = 0; + + while(TRUE) + { + if ((guint32)thrift_zlib_transport_read_avail (transport) < 1) { + give = thrift_zlib_transport_read_avail (transport); + } else { + give = need; + } + memcpy (buf_tmp, t->urbuf+t->urpos, give); + if (give > need) { + need = 0; + } else { + need -= give; + } + buf_tmp += give; + t->urpos += give; + + /* If they were satisfied, we are done. */ + if (need == 0) { + return 1; + } + + /* If we will need to read from the underlying transport to get more data, + * but we already have some data available, return it now. Reading from + * the underlying transport may block, and read() is only allowed to block + * when no data is available. + */ + if (need < 1 && t->rstream->avail_in == 0) { + return give; + } + + /* If we get to this point, we need to get some more data. */ + + /* If zlib has reported the end of a stream, we can't really do any more. */ + if (t->input_ended) { + return 1; + } + + /* The uncompressed read buffer is empty, so reset the stream fields. */ + t->rstream->next_out = t->urbuf; + t->rstream->avail_out = t->urbuf_size; + t->urpos = 0; + + /* Call inflate() to uncompress some more data. */ + if ((ret = thrift_zlib_transport_read_from_zlib(transport, error)) == 0) { + /* no data available from underlying transport */ + return 1; + } else { + if (ret < 0) { + return -1; + } + } + } + /* Okay. The read buffer should have whatever we can give it now. */ + /* Loop back to the start and try to give some more. */ +} + +gint32 +thrift_zlib_transport_read (ThriftTransport *transport, gpointer buf, + guint32 len, GError **error) +{ + ThriftZlibTransport *t = THRIFT_ZLIB_TRANSPORT (transport); + ThriftTransportClass *ttc = THRIFT_TRANSPORT_GET_CLASS (transport); + guint32 i; + gint32 ret; + + if (!ttc->checkReadBytesAvailable (transport, len, error)){ + return -1; + } + + for (i=0; i < len; i=i+ret) { + if ((ret = thrift_zlib_transport_read_slow (transport, ((char*)buf)+i, error)) < 0) { + return ret; + } + if (t->input_ended) + break; + } + + return len; +} + +/* implements thrift_transport_read_end + * called when read is complete. nothing to do on our end. */ +gboolean +thrift_zlib_transport_read_end (ThriftTransport *transport, GError **error) +{ + /* satisfy -Wall */ + THRIFT_UNUSED_VAR (error); + THRIFT_UNUSED_VAR (transport); + + return TRUE; +} + +gboolean +thrift_zlib_transport_flush_to_zlib (ThriftTransport *transport, const gint8* buf, + gint len, gint flush, GError **error) +{ + ThriftZlibTransport *t = THRIFT_ZLIB_TRANSPORT (transport); + t->wstream->next_in = (guchar*)buf; + t->wstream->avail_in = len; + + while (TRUE) { + if ((flush == Z_NO_FLUSH || flush == Z_BLOCK) && t->wstream->avail_in == 0) { + break; + } + + /* If our output buffer is full, flush to the underlying transport. */ + if (t->wstream->avail_out == 0) { + THRIFT_TRANSPORT_GET_CLASS (t->transport)->write (t->transport, + t->cwbuf, t->cwbuf_size, error); + t->wstream->next_out = t->cwbuf; + t->wstream->avail_out = t->cwbuf_size; + break; + } + + int zlib_rv = deflate(t->wstream, flush); + + if (flush == Z_FINISH && zlib_rv == Z_STREAM_END) { + if (t->wstream->avail_in != 0) { + g_set_error (error, THRIFT_TRANSPORT_ERROR, THRIFT_TRANSPORT_ERROR_SEND, + "wstream->avail_in != 0"); + return FALSE; + } + deflateEnd(t->wstream); + t->output_finished = TRUE; + break; + } + + if (zlib_rv != Z_OK) { + g_set_error (error, THRIFT_TRANSPORT_ERROR, THRIFT_TRANSPORT_ERROR_SEND, + "zlib error: %d (status = %s)", zlib_rv, t->wstream->msg); + return FALSE; + } + + if ((flush == Z_SYNC_FLUSH || flush == Z_FULL_FLUSH) && t->wstream->avail_in ==0 + && t->wstream->avail_out != 0) { + break; + } + } + return TRUE; +} + +/* implements thrift_transport_write + * WRITING STRATEGY + * We buffer up small writes before sending them to zlib, so our logic is: + * - Is the write big? + * - Send the buffer to zlib. + * - Send this data to zlib. + * - Is the write small? + * - Is there insufficient space in the buffer for it? + * - Send the buffer to zlib. + * - Copy the data to the buffer. + * + * We have two buffers for writing also: the uncompressed buffer (mentioned + * above) and the compressed buffer. When sending data to zlib we loop over + * the following until the source (uncompressed buffer or big write) is empty: + * - Is there no more space in the compressed buffer? + * - Write the compressed buffer to the underkying transport. + * - Deflate from the source into the compressed buffer. */ +gboolean +thrift_zlib_transport_write (ThriftTransport *transport, + const gpointer buf, + const guint32 len, GError **error) +{ + ThriftZlibTransport *t = THRIFT_ZLIB_TRANSPORT (transport); + + if (t->output_finished) { + g_set_error (error, THRIFT_TRANSPORT_ERROR, THRIFT_TRANSPORT_ERROR_SEND, + "write() called after write_end(): %s", + strerror(errno)); + return FALSE; + } + + /* zlib's "deflate" function has enough logic in it that I think + * we're better off (performance-wise) buffering up small writes. */ + if (len > MIN_DIRECT_DEFLATE_SIZE) { + if (!thrift_zlib_transport_flush_to_zlib (transport, (gint8*)t->uwbuf, t->uwpos, Z_NO_FLUSH, error)) { + return FALSE; + } + t->uwpos = 0; + if (!thrift_zlib_transport_flush_to_zlib (transport, buf, len, Z_NO_FLUSH, error)) { + return FALSE; + } + return TRUE; + } else if (len > 0) { + if ((guint32)(t->uwbuf_size - t->uwpos) < len) { + if (!thrift_zlib_transport_flush_to_zlib (transport, (gint8*)t->uwbuf, t->uwpos, Z_NO_FLUSH, error)) { + return FALSE; + } + t->uwpos = 0; + } + memcpy (t->uwbuf + t->uwpos, buf, len); + t->uwpos += len; + return TRUE; + } + return FALSE; +} + +gboolean +thrift_zlib_transport_flush_to_transport (ThriftTransport *transport, gint flush, GError **error) +{ + ThriftZlibTransport *t = THRIFT_ZLIB_TRANSPORT (transport); + + /* write pending data in uwbuf to zlib */ + if (!thrift_zlib_transport_flush_to_zlib (transport, (gint8*)t->uwbuf, t->uwpos, flush, error)) { + return FALSE; + } + t->uwpos = 0; + + /* write all available data from zlib to the transport */ + if (!THRIFT_TRANSPORT_GET_CLASS (t->transport)->write (t->transport, + t->cwbuf, (t->cwbuf_size - t->wstream->avail_out), + error)) { + return FALSE; + } + + t->wstream->next_out = t->cwbuf; + t->wstream->avail_out = t->cwbuf_size; + + /* flush the transport */ + if (!THRIFT_TRANSPORT_GET_CLASS (t->transport)->flush(t->transport, error)) { + return FALSE; + } + return TRUE; +} + +/* implements thrift_transport_write_end + * called when write is complete. nothing to do on our end. */ +gboolean +thrift_zlib_transport_write_end (ThriftTransport *transport, GError **error) +{ + THRIFT_UNUSED_VAR (error); + THRIFT_UNUSED_VAR (transport); + + return TRUE; +} + +/* implements thrift_transport_flush */ +gboolean +thrift_zlib_transport_flush (ThriftTransport *transport, GError **error) +{ + ThriftZlibTransport *t = THRIFT_ZLIB_TRANSPORT (transport); + ThriftTransportClass *ttc = THRIFT_TRANSPORT_GET_CLASS (transport); + + if (t->output_finished) { + return FALSE; + } + + thrift_zlib_transport_flush_to_zlib (transport, (gint8*)t->uwbuf, t->uwpos, Z_NO_FLUSH, error); + t->uwpos = 0; + + if (t->wstream->avail_out < 6) { + if (!THRIFT_TRANSPORT_GET_CLASS (t->transport)->write(t->transport, + t->cwbuf, t->cwbuf_size - t->wstream->avail_out, + error)) { + return FALSE; + } + t->wstream->next_out = t->cwbuf; + t->wstream->avail_out = t->cwbuf_size; + } + + if (!thrift_zlib_transport_flush_to_transport (transport, Z_FULL_FLUSH, error)) { + return FALSE; + } + + if (!ttc->resetConsumedMessageSize (transport, -1, error)) { + return FALSE; + } + return TRUE; +} + +gboolean +thrift_zlib_transport_verify_checksum(ThriftTransport *transport, GError **error) +{ + ThriftZlibTransport *t = THRIFT_ZLIB_TRANSPORT (transport); + + /* If zlib has already reported the end of the stream, + * it has verified the checksum. */ + if (t->input_ended) { + return TRUE; + } + + /* This should only be called when reading is complete. + * If the caller still has unread data, throw an exception. */ + if (thrift_zlib_transport_read_avail (transport) > 0) { + g_set_error (error, THRIFT_TRANSPORT_ERROR, THRIFT_TRANSPORT_ERROR_RECEIVE, + "thrift_zlib_transport_verify_checksum() called bufore end of zlib stream."); + return FALSE; + } + + /* Reset the rsteam fields, in case avail_out is 0. + * (Since thrift_zlib_transport_read_avail() is 0, we know there is no unread data in urbuf) */ + t->rstream->next_out = t->urbuf; + t->rstream->avail_out = t->urbuf_size; + t->urpos = 0; + + /* Call inflate() + * This will set the error if the checksum is bad. */ + gboolean performed_inflate = thrift_zlib_transport_read_from_zlib (transport, error); + if (!performed_inflate) { + g_set_error (error, THRIFT_TRANSPORT_ERROR, THRIFT_TRANSPORT_ERROR_RECEIVE, + "checksum not available yet in thrift_zlib_transport_verify_checksum ()"); + return FALSE; + } + + /* If input_ended is TRUE now, the checksum has been verified */ + if (t->input_ended) { + return TRUE; + } + + /* The caller invoked us before the actual end of the data stream */ + if (t->rstream->avail_out < (guint)t->urbuf_size) { + g_set_error (error, THRIFT_TRANSPORT_ERROR, THRIFT_TRANSPORT_ERROR_RECEIVE, + "rstream->avail_out >= urbuf_size"); + return FALSE; + } + + g_set_error (error, THRIFT_TRANSPORT_ERROR, THRIFT_TRANSPORT_ERROR_RECEIVE, + "thrift_zlib_transport_verify_checksum() called bufore end of zlib stream."); + return FALSE; +} + +gboolean +thrift_zlib_transport_finish(ThriftTransport *transport, GError **error) +{ + ThriftZlibTransport *t = THRIFT_ZLIB_TRANSPORT (transport); + + if (t->output_finished) { + g_set_error (error, THRIFT_TRANSPORT_ERROR, THRIFT_TRANSPORT_ERROR_SEND, + "finish() called more than once"); + return FALSE; + } + + if (!thrift_zlib_transport_flush_to_transport (transport, Z_FINISH, error)) { + return FALSE; + } + + return TRUE; +} + +/* initializes the instance */ +static void +thrift_zlib_transport_init (ThriftZlibTransport *transport) +{ + transport->transport = NULL; + transport->urpos = 0; + transport->uwpos = 0; + transport->input_ended = FALSE; + transport->output_finished = FALSE; + + transport->rstream = g_new0 (struct z_stream_s, 1); + transport->wstream = g_new0 (struct z_stream_s, 1); + + transport->rstream->zalloc = Z_NULL; + transport->wstream->zalloc = Z_NULL; + transport->rstream->zfree = Z_NULL; + transport->wstream->zfree = Z_NULL; + transport->rstream->opaque = Z_NULL; + transport->wstream->opaque = Z_NULL; + + transport->rstream->avail_in = 0; + transport->wstream->avail_in = 0; +} + +/* destructor */ +static void +thrift_zlib_transport_finalize (GObject *object) +{ + ThriftZlibTransport *t = THRIFT_ZLIB_TRANSPORT (object); + inflateEnd (t->rstream); + deflateEnd (t->wstream); + + if (t->urbuf != NULL) { + g_free (t->urbuf); + } + if (t->crbuf != NULL) { + g_free (t->crbuf); + } + if (t->uwbuf != NULL) { + g_free (t->uwbuf); + } + if (t->cwbuf != NULL) { + g_free (t->cwbuf); + } + if (t->rstream != NULL) { + g_free (t->rstream); + } + if (t->wstream != NULL) { + g_free (t->wstream); + } +} + +/* property accessor */ +void +thrift_zlib_transport_get_property (GObject *object, guint property_id, + GValue *value, GParamSpec *pspec) +{ + ThriftZlibTransport *transport = NULL; + ThriftTransport *tt = NULL; + + THRIFT_UNUSED_VAR (pspec); + + transport = THRIFT_ZLIB_TRANSPORT (object); + tt = THRIFT_TRANSPORT (object); + + switch (property_id) { + case PROP_THRIFT_ZLIB_TRANSPORT_TRANSPORT: + g_value_set_object (value, transport->transport); + break; + case PROP_THRIFT_ZLIB_TRANSPORT_URBUF_SIZE: + g_value_set_int (value, transport->urbuf_size); + break; + case PROP_THRIFT_ZLIB_TRANSPORT_CRBUF_SIZE: + g_value_set_int (value, transport->crbuf_size); + break; + case PROP_THRIFT_ZLIB_TRANSPORT_UWBUF_SIZE: + g_value_set_int (value, transport->uwbuf_size); + break; + case PROP_THRIFT_ZLIB_TRANSPORT_CWBUF_SIZE: + g_value_set_int (value, transport->cwbuf_size); + break; + case PROP_THRIFT_ZLIB_TRANSPORT_COMP_LEVEL: + g_value_set_int (value, transport->comp_level); + break; + case PROP_THRIFT_ZLIB_TRANSPORT_CONFIGURATION: + g_value_set_object (value, tt->configuration); + break; + case PROP_THRIFT_ZLIB_TRANSPORT_REMAINING_MESSAGE_SIZE: + g_value_set_long (value, tt->remainingMessageSize_); + break; + case PROP_THRIFT_ZLIB_TRANSPORT_KNOW_MESSAGE_SIZE: + g_value_set_long (value, tt->knowMessageSize_); + break; + default: + break; + } +} + +/* property mutator */ +void +thrift_zlib_transport_set_property (GObject *object, guint property_id, + const GValue *value, GParamSpec *pspec) +{ + ThriftZlibTransport *transport = NULL; + ThriftTransport *tt = NULL; + + THRIFT_UNUSED_VAR (pspec); + + transport = THRIFT_ZLIB_TRANSPORT (object); + tt = THRIFT_TRANSPORT (object); + + switch (property_id) { + case PROP_THRIFT_ZLIB_TRANSPORT_TRANSPORT: + transport->transport = g_value_get_object (value); + break; + case PROP_THRIFT_ZLIB_TRANSPORT_URBUF_SIZE: + transport->urbuf_size = g_value_get_int (value); + transport->urbuf = g_new0 (guint8, transport->urbuf_size); + transport->rstream->next_out = transport->urbuf; + transport->rstream->avail_out = transport->urbuf_size; + break; + case PROP_THRIFT_ZLIB_TRANSPORT_CRBUF_SIZE: + transport->crbuf_size = g_value_get_int (value); + transport->crbuf = g_new0 (guint8, transport->crbuf_size); + transport->rstream->next_in = transport->crbuf; + break; + case PROP_THRIFT_ZLIB_TRANSPORT_UWBUF_SIZE: + transport->uwbuf_size = g_value_get_int (value); + transport->uwbuf = g_new0 (guint8, transport->uwbuf_size); + transport->wstream->next_in = transport->uwbuf; + break; + case PROP_THRIFT_ZLIB_TRANSPORT_CWBUF_SIZE: + transport->cwbuf_size = g_value_get_int (value); + transport->cwbuf = g_new0 (guint8, transport->cwbuf_size); + transport->wstream->next_out = transport->cwbuf; + transport->wstream->avail_out = transport->cwbuf_size; + break; + case PROP_THRIFT_ZLIB_TRANSPORT_COMP_LEVEL: + transport->comp_level = g_value_get_int (value); + if(inflateInit(transport->rstream) != Z_OK) { + printf("inflate_init fail \n"); + return; + } + if(deflateInit (transport->wstream, transport->comp_level) != Z_OK) { + printf("deflate init fail\n"); + return; + } + break; + case PROP_THRIFT_ZLIB_TRANSPORT_CONFIGURATION: + tt->configuration = g_value_dup_object (value); + break; + case PROP_THRIFT_ZLIB_TRANSPORT_REMAINING_MESSAGE_SIZE: + tt->remainingMessageSize_ = g_value_get_long (value); + break; + case PROP_THRIFT_ZLIB_TRANSPORT_KNOW_MESSAGE_SIZE: + tt->knowMessageSize_ = g_value_get_long (value); + break; + default: + break; + } +} + +/* initialize the class */ +static void +thrift_zlib_transport_class_init (ThriftZlibTransportClass *cls) +{ + ThriftTransportClass *ttc; + GObjectClass *gobject_class; + GParamSpec *param_spec; + + ttc = THRIFT_TRANSPORT_CLASS (cls); + gobject_class = G_OBJECT_CLASS (cls); + param_spec = NULL; + + /* setup accessors and mutators */ + gobject_class->get_property = thrift_zlib_transport_get_property; + gobject_class->set_property = thrift_zlib_transport_set_property; + + param_spec = g_param_spec_object ("transport", "transport (construct)", + "Thrift transport", + THRIFT_TYPE_TRANSPORT, + G_PARAM_CONSTRUCT_ONLY | + G_PARAM_READWRITE); + g_object_class_install_property (gobject_class, + PROP_THRIFT_ZLIB_TRANSPORT_TRANSPORT, + param_spec); + + param_spec = g_param_spec_int ("urbuf_size", "urbuf_size (construct)", + "Uncompressed buffer size for reading", + 0, /* min */ + G_MAXINT, /* max */ + DEFAULT_URBUF_SIZE, /* default value */ + G_PARAM_CONSTRUCT_ONLY | + G_PARAM_READWRITE); + g_object_class_install_property (gobject_class, + PROP_THRIFT_ZLIB_TRANSPORT_URBUF_SIZE, + param_spec); + + param_spec = g_param_spec_int ("crbuf_size", "crbuf_size (construct)", + "Compressed buffer size for reading", + 0, /* min */ + G_MAXINT, /* max */ + DEFAULT_CRBUF_SIZE, /* default value */ + G_PARAM_CONSTRUCT_ONLY | + G_PARAM_READWRITE); + g_object_class_install_property (gobject_class, + PROP_THRIFT_ZLIB_TRANSPORT_CRBUF_SIZE, + param_spec); + + param_spec = g_param_spec_int ("uwbuf_size", "uwbuf_size (construct)", + "Uncompressed buffer size for writing", + MIN_DIRECT_DEFLATE_SIZE, /* min */ + G_MAXINT, /* max */ + DEFAULT_UWBUF_SIZE, /* default value */ + G_PARAM_CONSTRUCT_ONLY | + G_PARAM_READWRITE); + g_object_class_install_property (gobject_class, + PROP_THRIFT_ZLIB_TRANSPORT_UWBUF_SIZE, + param_spec); + + param_spec = g_param_spec_int ("cwbuf_size", "cwbuf_size (construct)", + "Compressed buffer size of writing", + 0, /* min */ + G_MAXINT, /* max */ + DEFAULT_CWBUF_SIZE, /* default value */ + G_PARAM_CONSTRUCT_ONLY | + G_PARAM_READWRITE); + g_object_class_install_property (gobject_class, + PROP_THRIFT_ZLIB_TRANSPORT_CWBUF_SIZE, + param_spec); + + param_spec = g_param_spec_int ("comp_level", "comp_level (construct)", + "Compression level (0=none[fast], 6=default, 9=max[slow])", + Z_DEFAULT_COMPRESSION, /* min */ + Z_BEST_COMPRESSION, /* max */ + Z_DEFAULT_COMPRESSION, /* default value */ + G_PARAM_CONSTRUCT_ONLY | + G_PARAM_READWRITE); + g_object_class_install_property (gobject_class, + PROP_THRIFT_ZLIB_TRANSPORT_COMP_LEVEL, + param_spec); + + param_spec = g_param_spec_object ("configuration", "configuration (construct)", + "Thrift Configuration", + THRIFT_TYPE_CONFIGURATION, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY); + g_object_class_install_property (gobject_class, + PROP_THRIFT_ZLIB_TRANSPORT_CONFIGURATION, + param_spec); + + param_spec = g_param_spec_long ("remainingmessagesize", "remainingmessagesize (construct)", + "Set the size of the remaining message", + 0, /* min */ + G_MAXINT32, /* max */ + DEFAULT_MAX_MESSAGE_SIZE, /* default by construct */ + G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY); + g_object_class_install_property (gobject_class, + PROP_THRIFT_ZLIB_TRANSPORT_REMAINING_MESSAGE_SIZE, + param_spec); + + param_spec = g_param_spec_long ("knowmessagesize", "knowmessagesize (construct)", + "Set the size of the know message", + G_MININT, /* min */ + G_MAXINT32, /* max */ + DEFAULT_MAX_MESSAGE_SIZE, /* default by construct */ + G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY); + g_object_class_install_property (gobject_class, + PROP_THRIFT_ZLIB_TRANSPORT_KNOW_MESSAGE_SIZE, + param_spec); + + gobject_class->finalize = thrift_zlib_transport_finalize; + ttc->is_open = thrift_zlib_transport_is_open; + ttc->peek = thrift_zlib_transport_peek; + ttc->open = thrift_zlib_transport_open; + ttc->close = thrift_zlib_transport_close; + ttc->read = thrift_zlib_transport_read; + ttc->read_end = thrift_zlib_transport_read_end; + ttc->write = thrift_zlib_transport_write; + ttc->write_end = thrift_zlib_transport_write_end; + ttc->flush = thrift_zlib_transport_flush; +} diff --git a/lib/c_glib/src/thrift/c_glib/transport/thrift_zlib_transport.h b/lib/c_glib/src/thrift/c_glib/transport/thrift_zlib_transport.h new file mode 100644 index 000000000..8d79ba573 --- /dev/null +++ b/lib/c_glib/src/thrift/c_glib/transport/thrift_zlib_transport.h @@ -0,0 +1,102 @@ +/* + * 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_ZLIB_TRANSPORT_H +#define _THRIFT_ZLIB_TRANSPORT_H + +#include <glib.h> +#include <glib-object.h> +#include <zlib.h> + +#include "thrift_transport.h" + +G_BEGIN_DECLS + +/*! \file thrift_zlib_transport.h + * \brief Class for Thrift file descriptor transports. + */ + +/* type macros */ +#define THRIFT_TYPE_ZLIB_TRANSPORT (thrift_zlib_transport_get_type ()) +#define THRIFT_ZLIB_TRANSPORT(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST ((obj), THRIFT_TYPE_ZLIB_TRANSPORT, \ + ThriftZlibTransport)) +#define THRIFT_IS_ZLIB_TRANSPORT(obj) \ + (G_TYPE_CHECK_CLASS_TYPE ((obj), THRIFT_TYPE_ZLIB_TRANSPORT)) +#define THRIFT_ZLIB_TRANSPORT_CLASS(c) \ + (G_TYPE_CHECK_CLASS_CAST ((c), THRIFT_TYPE_ZLIB_TRANSPORT, \ + ThriftZlibTransportClass)) +#define THRIFT_IS_ZLIB_TRANSPORT_CLASS(c) \ + (G_TYPE_CHECK_CLASS_TYPE ((c), THRIFT_TYPE_ZLIB_TRANSPORT)) +#define THRIFT_ZLIB_TRANSPORT_GET_CLASS(obj) \ + (G_TYPE_INSTANCE_GET_CLASS ((obj), THRIFT_TYPE_ZLIB_TRANSPORT, \ + ThriftZlibTransportClass)) + +typedef struct _ThriftZlibTransport ThriftZlibTransport;\ + +struct _ThriftZlibTransport +{ + ThriftTransport parent; + + /* protected */ + ThriftTransport *transport; + gint urbuf_size; + gint crbuf_size; + gint uwbuf_size; + gint cwbuf_size; + gint comp_level; + ThriftConfiguration *configuration; + glong remainingMessageSize_; + glong knowMessageSize_; + + /* private */ + gint urpos; + gint uwpos; + gboolean input_ended; /* TRUE iff zlib has reached the end of the input stream*/ + gboolean output_finished; /* TRUE iff we have finished the ouput stream*/ + guint8* urbuf; + guint8* crbuf; + guint8* uwbuf; + guint8* cwbuf; + struct z_stream_s* rstream; + struct z_stream_s* wstream; +}; + +typedef struct _ThriftZlibTransportClass ThriftZlibTransportClass; + +/*! + * Thrift Transport class + */ +struct _ThriftZlibTransportClass +{ + ThriftTransportClass parent; +}; + +/* used by THRIFT_TYPE_ZLIB_TRANSPORT */ +GType thrift_zlib_transport_get_type (void); + +gboolean +thrift_zlib_transport_verify_checksum(ThriftTransport *transport, GError **error); + +gboolean +thrift_zlib_transport_finish(ThriftTransport *transport, GError **error); + +G_END_DECLS + +#endif /* _THRIFT_ZLIB_TRANSPORT_H */ + diff --git a/lib/c_glib/src/thrift/c_glib/transport/thrift_zlib_transport_factory.c b/lib/c_glib/src/thrift/c_glib/transport/thrift_zlib_transport_factory.c new file mode 100644 index 000000000..7888c62af --- /dev/null +++ b/lib/c_glib/src/thrift/c_glib/transport/thrift_zlib_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_zlib_transport.h> +#include <thrift/c_glib/transport/thrift_zlib_transport_factory.h> + +G_DEFINE_TYPE (ThriftZlibTransportFactory, + thrift_zlib_transport_factory, + THRIFT_TYPE_TRANSPORT_FACTORY) + +/* Wraps a transport with a ThriftZlibTransport. */ +ThriftTransport * +thrift_zlib_transport_factory_get_transport (ThriftTransportFactory *factory, + ThriftTransport *transport) +{ + THRIFT_UNUSED_VAR (factory); + + return THRIFT_TRANSPORT (g_object_new (THRIFT_TYPE_ZLIB_TRANSPORT, + "transport", transport, + NULL)); +} + +static void +thrift_zlib_transport_factory_init (ThriftZlibTransportFactory *self) +{ + THRIFT_UNUSED_VAR (self); +} + +static void +thrift_zlib_transport_factory_class_init (ThriftZlibTransportFactoryClass *klass) +{ + ThriftTransportFactoryClass *base_class = + THRIFT_TRANSPORT_FACTORY_CLASS (klass); + + base_class->get_transport = + klass->get_transport = + thrift_zlib_transport_factory_get_transport; +} diff --git a/lib/c_glib/src/thrift/c_glib/transport/thrift_zlib_transport_factory.h b/lib/c_glib/src/thrift/c_glib/transport/thrift_zlib_transport_factory.h new file mode 100644 index 000000000..5fa9e6b34 --- /dev/null +++ b/lib/c_glib/src/thrift/c_glib/transport/thrift_zlib_transport_factory.h @@ -0,0 +1,85 @@ +/* + * 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_ZLIB_TRANSPORT_FACTORY_H +#define _THRIFT_ZLIB_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_zlib_transport_factory.h + * \brief Wraps a transport with a ThriffZlibTransport. + */ + +/* type macros */ +#define THRIFT_TYPE_ZLIB_TRANSPORT_FACTORY (thrift_zlib_transport_factory_get_type()) +#define THRIFT_ZLIB_TRANSPORT_FACTORY(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST ((obj), \ + THRIFT_TYPE_ZLIB_TRANSPORT_FACTORY, \ + ThriftZlibTransportFactory)) +#define THRIFT_IS_ZLIB_TRANSPORT_FACTORY(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \ + THRIFT_TYPE_ZLIB_TRANSPORT_FACTORY)) +#define THRIFT_ZLIB_TRANSPORT_FACTORY_CLASS(c) \ + (G_TYPE_CHECK_CLASS_CAST ((c), \ + THRIFT_TYPE_ZLIB_TRANSPORT_FACTORY, \ + ThriftZlibTransportFactoryClass)) +#define THRIFT_IS_ZLIB_TRANSPORT_FACTORY_CLASS(c) \ + (G_TYPE_CHECK_CLASS_TYPE ((c), \ + THRIFT_TYPE_ZLIB_TRANSPORT_FACTORY) +#define THRIFT_ZLIB_TRANSPORT_FACTORY_GET_CLASS(obj) \ + (G_TYPE_INSTANCE_GET_CLASS ((obj), \ + THRIFT_TYPE_ZLIB_TRANSPORT_FACTORY, \ + ThriftZlibTransportFactoryClass)) + +typedef struct _ThriftZlibTransportFactory ThriftZlibTransportFactory; + +/* Thrift Zlib-Transport Factory instance */ +struct _ThriftZlibTransportFactory +{ + ThriftTransportFactory parent; +}; + +typedef struct _ThriftZlibTransportFactoryClass ThriftZlibTransportFactoryClass; + +/* Thrift Zlib-Transport Factory class */ +struct _ThriftZlibTransportFactoryClass +{ + ThriftTransportFactoryClass parent; + + /* vtable */ + ThriftTransport *(*get_transport) (ThriftTransportFactory *factory, + ThriftTransport *transport); +}; + +/* USED BY THRIFT_TYPE_ZLIB_TRANSPORT_FACTORY */ +GType thrift_zlib_transport_factory_get_type (void); + +/* virtual public methods */ +ThriftTransport * +thrift_zlib_transport_factory_get_transport (ThriftTransportFactory *factory, + ThriftTransport *transport); + +G_END_DECLS + +#endif /* _THRIFT_ZLIB_TANSPORT_FACTORY_H */ diff --git a/lib/c_glib/test/CMakeLists.txt b/lib/c_glib/test/CMakeLists.txt index cae8e512e..94c87e2d7 100644 --- a/lib/c_glib/test/CMakeLists.txt +++ b/lib/c_glib/test/CMakeLists.txt @@ -130,6 +130,14 @@ add_executable(testthriftmemorybufferreadcheck testthriftmemorybufferreadcheck.c target_link_libraries(testthriftmemorybufferreadcheck testgenc) add_test(NAME testthriftmemorybufferreadcheck COMMAND testthriftmemorybufferreadcheck) +if(WITH_ZLIB) + include_directories(SYSTEM "${ZLIB_INCLUDE_DIRS}") + add_executable(testzlibtransport testzlibtransport.c) + target_link_libraries(testzlibtransport testgenc ${ZLIB_LIBRARIES}) + LINK_AGAINST_THRIFT_LIBRARY(testzlibtransport thrift_c_glib_zlib) + add_test(NAME testzlibtransport COMMAND testzlibtransport) +endif(WITH_ZLIB) + include_directories("${PROJECT_SOURCE_DIR}/test/c_glib/src" "${CMAKE_CURRENT_BINARY_DIR}/gen-c_glib") add_executable(testthrifttest testthrifttest.c @@ -167,6 +175,12 @@ if(BUILD_CPP) target_link_libraries(testthrifttestclient testgenc testgenc_cpp ${ZLIB_LIBRARIES}) add_test(NAME testthrifttestclient COMMAND testthrifttestclient) + if(WITH_ZLIB) + add_executable(testthrifttestzlibclient testthrifttestzlibclient.cpp) + target_link_libraries(testthrifttestzlibclient testgenc testgenc_cpp thriftz thrift_c_glib_zlib ${ZLIB_LIBRARIES}) + add_test(NAME testthrifttestzlibclient COMMAND testthrifttestzlibclient) +endif(WITH_ZLIB) + endif(BUILD_CPP) # diff --git a/lib/c_glib/test/Makefile.am b/lib/c_glib/test/Makefile.am index 3ff48a316..023165adb 100755 --- a/lib/c_glib/test/Makefile.am +++ b/lib/c_glib/test/Makefile.am @@ -40,7 +40,7 @@ AM_CPPFLAGS = -I../src -I./gen-c_glib -I$(top_builddir)/lib/c_glib/src/thrift AM_CFLAGS = -g -Wall -Wextra -pedantic $(GLIB_CFLAGS) $(GOBJECT_CFLAGS) $(OPENSSL_INCLUDES) \ @GCOV_CFLAGS@ AM_CXXFLAGS = $(AM_CFLAGS) -AM_LDFLAGS = $(GLIB_LIBS) $(GOBJECT_LIBS) $(OPENSSL_LIBS) @GCOV_LDFLAGS@ +AM_LDFLAGS = $(GLIB_LIBS) $(GOBJECT_LIBS) $(OPENSSL_LIBS) $(ZLIB_LIBS) @GCOV_LDFLAGS@ check_PROGRAMS = \ testserialization \ @@ -52,6 +52,7 @@ check_PROGRAMS = \ testcompactprotocol \ testbufferedtransport \ testframedtransport \ + testzlibtransport \ testfdtransport \ testmemorybuffer \ teststruct \ @@ -68,7 +69,8 @@ check_PROGRAMS = \ if WITH_CPP BUILT_SOURCES += gen-cpp/ThriftTest_types.cpp - check_PROGRAMS += testthrifttestclient + check_PROGRAMS += testthrifttestclient \ + testthrifttestzlibclient endif testserialization_SOURCES = testserialization.c @@ -158,6 +160,14 @@ testframedtransport_LDADD = \ $(top_builddir)/lib/c_glib/src/thrift/c_glib/transport/libthrift_c_glib_la-thrift_server_socket.o \ $(top_builddir)/lib/c_glib/src/thrift/c_glib/libthrift_c_glib_la-thrift_configuration.o +testzlibtransport_SOURCES = testzlibtransport.c +testzlibtransport_LDADD = \ + $(top_builddir)/lib/c_glib/src/thrift/c_glib/transport/libthrift_c_glib_la-thrift_transport.o \ + $(top_builddir)/lib/c_glib/src/thrift/c_glib/transport/libthrift_c_glib_la-thrift_socket.o \ + $(top_builddir)/lib/c_glib/src/thrift/c_glib/transport/libthrift_c_glib_la-thrift_server_transport.o \ + $(top_builddir)/lib/c_glib/src/thrift/c_glib/transport/libthrift_c_glib_la-thrift_server_socket.o \ + $(top_builddir)/lib/c_glib/src/thrift/c_glib/libthrift_c_glib_la-thrift_configuration.o + testfdtransport_SOURCES = testfdtransport.c testfdtransport_LDADD = \ $(top_builddir)/lib/c_glib/src/thrift/c_glib/transport/libthrift_c_glib_la-thrift_transport.o \ @@ -258,6 +268,11 @@ testthrifttestclient_CPPFLAGS = -I../../cpp/src $(BOOST_CPPFLAGS) -I./gen-cpp -I 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) +testthrifttestzlibclient_SOURCES = testthrifttestzlibclient.cpp +testthrifttestzlibclient_CPPFLAGS = -I../../cpp/src $(BOOST_CPPFLAGS) -I./gen-cpp -I../src -I./gen-c_glib $(GLIB_CFLAGS) +testthrifttestzlibclient_LDADD = ../../cpp/.libs/libthrift.la ../../cpp/.libs/libthriftz.la ../libthrift_c_glib.la libtestgenc.la libtestgencpp.la +testthrifttestzlibclient_LDFLAGS = -L../.libs -L../../cpp/.libs $(GLIB_LIBS) $(GOBJECT_LIBS) + check_LTLIBRARIES = libtestgenc.la if WITH_CPP diff --git a/lib/c_glib/test/testthrifttestzlibclient.cpp b/lib/c_glib/test/testthrifttestzlibclient.cpp new file mode 100644 index 000000000..5c4b93157 --- /dev/null +++ b/lib/c_glib/test/testthrifttestzlibclient.cpp @@ -0,0 +1,645 @@ +/* + * 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. + */ + +/* test a C client with a C++ server (that makes sense...) */ + +#include <signal.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <thrift/protocol/TBinaryProtocol.h> +#include <thrift/protocol/TDebugProtocol.h> +#include <thrift/server/TSimpleServer.h> +#include <memory> +#include <thrift/transport/TServerSocket.h> +#include <thrift/transport/TZlibTransport.h> +#include "ThriftTest.h" +#include "ThriftTest_types.h" + +#include <iostream> +#include <map> +#include <set> +#include <string> +#include <vector> + +using namespace apache::thrift; +using namespace apache::thrift::concurrency; +using namespace apache::thrift::protocol; +using namespace apache::thrift::server; +using namespace apache::thrift::transport; + +using namespace thrift::test; + +using std::cout; +using std::endl; +using std::fixed; +using std::make_pair; +using std::map; +using std::set; +using std::string; +using std::vector; + +#define TEST_PORT 9980 + +// Extra functions required for ThriftTest_types to work +namespace thrift { namespace test { + +bool Insanity::operator<(thrift::test::Insanity const& other) const { + using apache::thrift::ThriftDebugString; + return ThriftDebugString(*this) < ThriftDebugString(other); +} + +}} + +class TestHandler : public ThriftTestIf { + public: + TestHandler() = default; + + void testVoid() override { + cout << "[C -> C++] testVoid()" << endl; + } + + void testString(string& out, const string &thing) override { + cout << "[C -> C++] testString(\"" << thing << "\")" << endl; + out = thing; + } + + bool testBool(const bool thing) override { + cout << "[C -> C++] testBool(" << (thing ? "true" : "false") << ")" << endl; + return thing; + } + int8_t testByte(const int8_t thing) override { + cout << "[C -> C++] testByte(" << (int)thing << ")" << endl; + return thing; + } + int32_t testI32(const int32_t thing) override { + cout << "[C -> C++] testI32(" << thing << ")" << endl; + return thing; + } + + int64_t testI64(const int64_t thing) override { + cout << "[C -> C++] testI64(" << thing << ")" << endl; + return thing; + } + + double testDouble(const double thing) override { + cout.precision(6); + cout << "[C -> C++] testDouble(" << fixed << thing << ")" << endl; + return thing; + } + + void testBinary(string& out, const string &thing) override { + cout << "[C -> C++] testBinary(\"" << thing << "\")" << endl; + out = thing; + } + + void testStruct(Xtruct& out, const Xtruct &thing) override { + cout << "[C -> C++] testStruct({\"" << thing.string_thing << "\", " << (int)thing.byte_thing << ", " << thing.i32_thing << ", " << thing.i64_thing << "})" << endl; + out = thing; + } + + void testNest(Xtruct2& out, const Xtruct2& nest) override { + const Xtruct &thing = nest.struct_thing; + cout << "[C -> C++] testNest({" << (int)nest.byte_thing << ", {\"" << thing.string_thing << "\", " << (int)thing.byte_thing << ", " << thing.i32_thing << ", " << thing.i64_thing << "}, " << nest.i32_thing << "})" << endl; + out = nest; + } + + void testMap(map<int32_t, int32_t> &out, const map<int32_t, int32_t> &thing) override { + cout << "[C -> C++] testMap({"; + map<int32_t, int32_t>::const_iterator m_iter; + bool first = true; + for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) { + if (first) { + first = false; + } else { + cout << ", "; + } + cout << m_iter->first << " => " << m_iter->second; + } + cout << "})" << endl; + out = thing; + } + + void testStringMap(map<std::string, std::string> &out, const map<std::string, std::string> &thing) override { + cout << "[C -> C++] testStringMap({"; + map<std::string, std::string>::const_iterator m_iter; + bool first = true; + for (m_iter = thing.begin(); m_iter != thing.end(); ++m_iter) { + if (first) { + first = false; + } else { + cout << ", "; + } + cout << "\"" << m_iter->first << "\" => \"" << m_iter->second << "\""; + } + cout << "})" << endl; + out = thing; + } + + + void testSet(set<int32_t> &out, const set<int32_t> &thing) override { + cout << "[C -> C++] testSet({"; + set<int32_t>::const_iterator s_iter; + bool first = true; + for (s_iter = thing.begin(); s_iter != thing.end(); ++s_iter) { + if (first) { + first = false; + } else { + cout << ", "; + } + cout << *s_iter; + } + cout << "})" << endl; + out = thing; + } + + void testList(vector<int32_t> &out, const vector<int32_t> &thing) override { + cout << "[C -> C++] testList({"; + vector<int32_t>::const_iterator l_iter; + bool first = true; + for (l_iter = thing.begin(); l_iter != thing.end(); ++l_iter) { + if (first) { + first = false; + } else { + cout << ", "; + } + cout << *l_iter; + } + cout << "})" << endl; + out = thing; + } + + Numberz::type testEnum(const Numberz::type thing) override { + cout << "[C -> C++] testEnum(" << thing << ")" << endl; + return thing; + } + + UserId testTypedef(const UserId thing) override { + cout << "[C -> C++] testTypedef(" << thing << ")" << endl; + return thing; } + + void testMapMap(map<int32_t, map<int32_t,int32_t> > &mapmap, const int32_t hello) override { + cout << "[C -> C++] testMapMap(" << hello << ")" << endl; + + map<int32_t,int32_t> pos; + map<int32_t,int32_t> neg; + for (int i = 1; i < 5; i++) { + pos.insert(make_pair(i,i)); + neg.insert(make_pair(-i,-i)); + } + + mapmap.insert(make_pair(4, pos)); + mapmap.insert(make_pair(-4, neg)); + + } + + void testInsanity(map<UserId, map<Numberz::type,Insanity> > &insane, const Insanity &argument) override { + THRIFT_UNUSED_VARIABLE (argument); + + cout << "[C -> C++] testInsanity()" << endl; + + Xtruct hello; + hello.string_thing = "Hello2"; + hello.byte_thing = 2; + hello.i32_thing = 2; + hello.i64_thing = 2; + + Xtruct goodbye; + goodbye.string_thing = "Goodbye4"; + goodbye.byte_thing = 4; + goodbye.i32_thing = 4; + goodbye.i64_thing = 4; + + Insanity crazy; + crazy.userMap.insert(make_pair(Numberz::EIGHT, 8)); + crazy.xtructs.push_back(goodbye); + + Insanity looney; + crazy.userMap.insert(make_pair(Numberz::FIVE, 5)); + crazy.xtructs.push_back(hello); + + map<Numberz::type, Insanity> first_map; + map<Numberz::type, Insanity> second_map; + + first_map.insert(make_pair(Numberz::TWO, crazy)); + first_map.insert(make_pair(Numberz::THREE, crazy)); + + second_map.insert(make_pair(Numberz::SIX, looney)); + + insane.insert(make_pair(1, first_map)); + insane.insert(make_pair(2, second_map)); + + cout << "return = {"; + map<UserId, map<Numberz::type,Insanity> >::const_iterator i_iter; + for (i_iter = insane.begin(); i_iter != insane.end(); ++i_iter) { + cout << i_iter->first << " => {"; + map<Numberz::type,Insanity>::const_iterator i2_iter; + for (i2_iter = i_iter->second.begin(); + i2_iter != i_iter->second.end(); + ++i2_iter) { + cout << i2_iter->first << " => {"; + map<Numberz::type, UserId> userMap = i2_iter->second.userMap; + map<Numberz::type, UserId>::const_iterator um; + cout << "{"; + for (um = userMap.begin(); um != userMap.end(); ++um) { + cout << um->first << " => " << um->second << ", "; + } + cout << "}, "; + + vector<Xtruct> xtructs = i2_iter->second.xtructs; + vector<Xtruct>::const_iterator x; + cout << "{"; + for (x = xtructs.begin(); x != xtructs.end(); ++x) { + cout << "{\"" << x->string_thing << "\", " << (int)x->byte_thing << ", " << x->i32_thing << ", " << x->i64_thing << "}, "; + } + cout << "}"; + + cout << "}, "; + } + cout << "}, "; + } + cout << "}" << endl; + + + } + + void testMulti(Xtruct &hello, const int8_t arg0, const int32_t arg1, const int64_t arg2, const std::map<int16_t, std::string> &arg3, const Numberz::type arg4, const UserId arg5) override { + THRIFT_UNUSED_VARIABLE (arg3); + THRIFT_UNUSED_VARIABLE (arg4); + THRIFT_UNUSED_VARIABLE (arg5); + + cout << "[C -> C++] testMulti()" << endl; + + hello.string_thing = "Hello2"; + hello.byte_thing = arg0; + hello.i32_thing = arg1; + hello.i64_thing = (int64_t)arg2; + } + + void testException(const std::string &arg) + throw(Xception, apache::thrift::TException) override + { + cout << "[C -> C++] testException(" << arg << ")" << endl; + if (arg.compare("Xception") == 0) { + Xception e; + e.errorCode = 1001; + e.message = arg; + throw e; + } else if (arg.compare("ApplicationException") == 0) { + apache::thrift::TException e; + throw e; + } else { + Xtruct result; + result.string_thing = arg; + return; + } + } + + void testMultiException(Xtruct &result, const std::string &arg0, const std::string &arg1) throw(Xception, Xception2) override { + + cout << "[C -> C++] testMultiException(" << arg0 << ", " << arg1 << ")" << endl; + + if (arg0.compare("Xception") == 0) { + Xception e; + e.errorCode = 1001; + e.message = "This is an Xception"; + throw e; + } else if (arg0.compare("Xception2") == 0) { + Xception2 e; + e.errorCode = 2002; + e.struct_thing.string_thing = "This is an Xception2"; + throw e; + } else { + result.string_thing = arg1; + return; + } + } + + void testOneway(int sleepFor) override { + cout << "testOneway(" << sleepFor << "): Sleeping..." << endl; + sleep(sleepFor); + cout << "testOneway(" << sleepFor << "): done sleeping!" << endl; + } +}; + +// C CLIENT +extern "C" { + +#undef THRIFT_SOCKET /* from lib/cpp */ + +#include "t_test_thrift_test.h" +#include "t_test_thrift_test_types.h" +#include <thrift/c_glib/transport/thrift_socket.h> +#include <thrift/c_glib/transport/thrift_zlib_transport.h> +#include <thrift/c_glib/protocol/thrift_protocol.h> +#include <thrift/c_glib/protocol/thrift_binary_protocol.h> + +static void +test_thrift_client (void) +{ + ThriftSocket *tsocket = nullptr; + ThriftBinaryProtocol *protocol = nullptr; + ThriftZlibTransport *transport = nullptr; + TTestThriftTestClient *client = nullptr; + TTestThriftTestIf *iface = nullptr; + GError *error = nullptr; + gchar *string = nullptr; + gint8 byte = 0; + gint16 i16 = 0; + gint32 i32 = 0, another_i32 = 56789; + gint64 i64 = 0; + double dbl = 0.0; + TTestXtruct *xtruct_in, *xtruct_out; + TTestXtruct2 *xtruct2_in, *xtruct2_out; + GHashTable *map_in = nullptr, *map_out = nullptr; + GHashTable *set_in = nullptr, *set_out = nullptr; + GArray *list_in = nullptr, *list_out = nullptr; + TTestNumberz enum_in, enum_out; + TTestUserId user_id_in, user_id_out; + GHashTable *insanity_in = nullptr; + TTestXtruct *xtruct1, *xtruct2; + TTestInsanity *insanity_out = nullptr; + TTestXtruct *multi_in = nullptr; + GHashTable *multi_map_out = nullptr; + TTestXception *xception = nullptr; + TTestXception2 *xception2 = nullptr; + +#if (!GLIB_CHECK_VERSION (2, 36, 0)) + // initialize gobject + g_type_init (); +#endif + + // create a C client + tsocket = (ThriftSocket *) g_object_new (THRIFT_TYPE_SOCKET, + "hostname", "localhost", + "port", TEST_PORT, nullptr); + transport = (ThriftZlibTransport *) g_object_new (THRIFT_TYPE_ZLIB_TRANSPORT, + "transport", tsocket, nullptr); + protocol = (ThriftBinaryProtocol *) g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, + "transport", + transport, nullptr); + client = (TTestThriftTestClient *) g_object_new (T_TEST_TYPE_THRIFT_TEST_CLIENT, "input_protocol", protocol, "output_protocol", protocol, nullptr); + iface = T_TEST_THRIFT_TEST_IF (client); + + // open and send + thrift_transport_open (THRIFT_TRANSPORT(transport), nullptr); + + assert (t_test_thrift_test_client_test_void (iface, &error) == TRUE); + assert (error == nullptr); + + assert (t_test_thrift_test_client_test_string (iface, &string, "test123", &error) == TRUE); + assert (strcmp (string, "test123") == 0); + g_free (string); + assert (error == nullptr); + + assert (t_test_thrift_test_client_test_byte (iface, &byte, (gint8) 5, &error) == TRUE); + assert (byte == 5); + assert (error == nullptr); + + assert (t_test_thrift_test_client_test_i32 (iface, &i32, 123, &error) == TRUE); + assert (i32 == 123); + assert (error == nullptr); + + assert (t_test_thrift_test_client_test_i64 (iface, &i64, 12345, &error) == TRUE); + assert (i64 == 12345); + assert (error == nullptr); + + assert (t_test_thrift_test_client_test_double (iface, &dbl, 5.6, &error) == TRUE); + assert (dbl == 5.6); + assert (error == nullptr); + + xtruct_out = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, nullptr); + xtruct_out->byte_thing = 1; + xtruct_out->__isset_byte_thing = TRUE; + xtruct_out->i32_thing = 15; + xtruct_out->__isset_i32_thing = TRUE; + xtruct_out->i64_thing = 151; + xtruct_out->__isset_i64_thing = TRUE; + xtruct_out->string_thing = g_strdup ("abc123"); + xtruct_out->__isset_string_thing = TRUE; + xtruct_in = (TTestXtruct *) g_object_new(T_TEST_TYPE_XTRUCT, nullptr); + assert (t_test_thrift_test_client_test_struct (iface, &xtruct_in, xtruct_out, &error) == TRUE); + assert (error == nullptr); + + xtruct2_out = (TTestXtruct2 *) g_object_new (T_TEST_TYPE_XTRUCT2, nullptr); + xtruct2_out->byte_thing = 1; + xtruct2_out->__isset_byte_thing = TRUE; + if (xtruct2_out->struct_thing != nullptr) + g_object_unref(xtruct2_out->struct_thing); + xtruct2_out->struct_thing = xtruct_out; + xtruct2_out->__isset_struct_thing = TRUE; + xtruct2_out->i32_thing = 123; + xtruct2_out->__isset_i32_thing = TRUE; + xtruct2_in = (TTestXtruct2 *) g_object_new (T_TEST_TYPE_XTRUCT2, nullptr); + assert (t_test_thrift_test_client_test_nest (iface, &xtruct2_in, xtruct2_out, &error) == TRUE); + assert (error == nullptr); + + g_object_unref (xtruct2_out); + g_object_unref (xtruct2_in); + g_object_unref (xtruct_in); + + map_out = g_hash_table_new (nullptr, nullptr); + map_in = g_hash_table_new (nullptr, nullptr); g_hash_table_insert (map_out, &i32, &i32); + assert (t_test_thrift_test_client_test_map (iface, &map_in, map_out, &error) == TRUE); + assert (error == nullptr); + g_hash_table_destroy (map_out); + g_hash_table_destroy (map_in); + + map_out = g_hash_table_new (nullptr, nullptr); + map_in = g_hash_table_new (nullptr, nullptr); + g_hash_table_insert (map_out, g_strdup ("a"), g_strdup ("123")); + g_hash_table_insert (map_out, g_strdup ("a b"), g_strdup ("with spaces ")); + g_hash_table_insert (map_out, g_strdup ("same"), g_strdup ("same")); + g_hash_table_insert (map_out, g_strdup ("0"), g_strdup ("numeric key")); + assert (t_test_thrift_test_client_test_string_map (iface, &map_in, map_out, &error) == TRUE); + assert (error == nullptr); + g_hash_table_destroy (map_out); + g_hash_table_destroy (map_in); + + set_out = g_hash_table_new (nullptr, nullptr); + set_in = g_hash_table_new (nullptr, nullptr); + g_hash_table_insert (set_out, &i32, &i32); + assert (t_test_thrift_test_client_test_set (iface, &set_in, set_out, &error) == TRUE); + assert (error == nullptr); + g_hash_table_destroy (set_out); + g_hash_table_destroy (set_in); + + list_out = g_array_new(TRUE, TRUE, sizeof(gint32)); + list_in = g_array_new(TRUE, TRUE, sizeof(gint32)); + another_i32 = 456; + g_array_append_val (list_out, i32); + g_array_append_val (list_out, another_i32); + assert (t_test_thrift_test_client_test_list (iface, &list_in, list_out, &error) == TRUE); + assert (error == nullptr); + g_array_free (list_out, TRUE); + g_array_free (list_in, TRUE); + + enum_out = T_TEST_NUMBERZ_ONE; + assert (t_test_thrift_test_client_test_enum (iface, &enum_in, enum_out, &error) == TRUE); + assert (enum_in == enum_out); + assert (error == nullptr); + + user_id_out = 12345; + assert (t_test_thrift_test_client_test_typedef (iface, &user_id_in, user_id_out, &error) == TRUE); + assert (user_id_in == user_id_out); + assert (error == nullptr); + + map_in = g_hash_table_new (nullptr, nullptr); + assert (t_test_thrift_test_client_test_map_map (iface, &map_in, i32, &error) == TRUE); + assert (error == nullptr); + g_hash_table_destroy (map_in); + + // insanity + insanity_out = (TTestInsanity *) g_object_new (T_TEST_TYPE_INSANITY, nullptr); + insanity_out->userMap = g_hash_table_new (nullptr, nullptr); + g_hash_table_insert (insanity_out->userMap, GINT_TO_POINTER (enum_out), &user_id_out); + + xtruct1 = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, nullptr); + xtruct1->byte_thing = 1; + xtruct1->__isset_byte_thing = TRUE; + xtruct1->i32_thing = 15; + xtruct1->__isset_i32_thing = TRUE; + xtruct1->i64_thing = 151; + xtruct1->__isset_i64_thing = TRUE; + xtruct1->string_thing = g_strdup ("abc123"); + xtruct1->__isset_string_thing = TRUE; + xtruct2 = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, nullptr); + xtruct2->byte_thing = 1; + xtruct2->__isset_byte_thing = TRUE; + xtruct2->i32_thing = 15; + xtruct2->__isset_i32_thing = TRUE; + xtruct2->i64_thing = 151; + xtruct2->__isset_i64_thing = TRUE; + xtruct2->string_thing = g_strdup ("abc123"); + xtruct2->__isset_string_thing = TRUE; + + insanity_in = g_hash_table_new (nullptr, nullptr); + g_ptr_array_add (insanity_out->xtructs, xtruct1); + g_ptr_array_add (insanity_out->xtructs, xtruct2); + assert (t_test_thrift_test_client_test_insanity (iface, &insanity_in, insanity_out, &error) == TRUE); + + g_hash_table_unref (insanity_in); + g_ptr_array_free (insanity_out->xtructs, TRUE); + + multi_map_out = g_hash_table_new (nullptr, nullptr); + string = g_strdup ("abc123"); + g_hash_table_insert (multi_map_out, &i16, string); + multi_in = (TTestXtruct *) g_object_new (T_TEST_TYPE_XTRUCT, nullptr); + assert (t_test_thrift_test_client_test_multi (iface, &multi_in, byte, i32, i64, multi_map_out, enum_out, user_id_out, &error) == TRUE); + assert (multi_in->i32_thing == i32); + assert (multi_in->i64_thing == i64); + g_object_unref (multi_in); + g_hash_table_unref (multi_map_out); + g_free (string); + + assert (t_test_thrift_test_client_test_exception (iface, "Xception", &xception, &error) == FALSE); + assert (xception->errorCode == 1001); + g_error_free (error); + error = nullptr; + g_object_unref (xception); + xception = nullptr; + + assert (t_test_thrift_test_client_test_exception (iface, "ApplicationException", &xception, &error) == FALSE); + g_error_free (error); + error = nullptr; + assert (xception == nullptr); + + assert (t_test_thrift_test_client_test_exception (iface, "Test", &xception, &error) == TRUE); + assert (error == nullptr); + + multi_in = (TTestXtruct*) g_object_new (T_TEST_TYPE_XTRUCT, nullptr); + assert (t_test_thrift_test_client_test_multi_exception (iface, &multi_in, "Xception", nullptr, &xception, &xception2, &error) == FALSE); + assert (xception->errorCode == 1001); + assert (xception2 == nullptr); + g_error_free (error); + error = nullptr; + g_object_unref (xception); + g_object_unref (multi_in); + xception = nullptr; + multi_in = nullptr; + + multi_in = (TTestXtruct*) g_object_new (T_TEST_TYPE_XTRUCT, nullptr); + assert (t_test_thrift_test_client_test_multi_exception (iface, &multi_in, "Xception2", nullptr, &xception, &xception2, &error) == FALSE); + assert (xception2->errorCode == 2002); + assert (xception == nullptr); + g_error_free (error); + error = nullptr; + g_object_unref (xception2); + g_object_unref (multi_in); + xception2 = nullptr; + multi_in = nullptr; + + multi_in = (TTestXtruct*) g_object_new (T_TEST_TYPE_XTRUCT, nullptr); + assert (t_test_thrift_test_client_test_multi_exception (iface, &multi_in, nullptr , nullptr, &xception, &xception2, &error) == TRUE); + assert (error == nullptr); + g_object_unref(multi_in); + multi_in = nullptr; + + assert (t_test_thrift_test_client_test_oneway (iface, 1, &error) == TRUE); + assert (error == nullptr); + + /* sleep to let the oneway call go through */ + sleep (5); + + thrift_transport_close (THRIFT_TRANSPORT(tsocket), nullptr); + g_object_unref (client); + g_object_unref (protocol); + g_object_unref (tsocket); +} + + +} /* extern "C" */ + + +static void +bailout (int signum) +{ + THRIFT_UNUSED_VARIABLE (signum); + + exit (1); +} + +int +main (void) +{ + int status; + int pid = fork (); + assert (pid >= 0); + + if (pid == 0) /* child */ + { + std::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory()); + std::shared_ptr<TestHandler> testHandler(new TestHandler()); + std::shared_ptr<ThriftTestProcessor> testProcessor(new ThriftTestProcessor(testHandler)); + std::shared_ptr<TServerSocket> serverSocket(new TServerSocket(TEST_PORT)); + std::shared_ptr<TZlibTransportFactory> transportFactory(new TZlibTransportFactory()); + //std::shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory()); + TSimpleServer simpleServer(testProcessor, serverSocket, transportFactory, protocolFactory); + signal (SIGALRM, bailout); + alarm (60); + simpleServer.serve(); + } else { + sleep (1); + test_thrift_client (); + kill (pid, SIGINT); + assert (wait (&status) == pid); + } + + return 0; +} + diff --git a/lib/c_glib/test/testzlibtransport.c b/lib/c_glib/test/testzlibtransport.c new file mode 100644 index 000000000..04e368ffa --- /dev/null +++ b/lib/c_glib/test/testzlibtransport.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 <netdb.h> +#include <signal.h> +#include <sys/wait.h> + +#include <thrift/c_glib/transport/thrift_transport.h> +#include <thrift/c_glib/transport/thrift_socket.h> +#include <thrift/c_glib/transport/thrift_server_transport.h> +#include <thrift/c_glib/transport/thrift_server_socket.h> + +#define TEST_DATA \ + { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', \ + 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', \ + 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', \ + '5', '6', '7', '8', '9', '0' } + +#include "../src/thrift/c_glib/transport/thrift_zlib_transport.c" + +static void thrift_server (const int port); +static void thrift_socket_server_open (const int port, int times); + +/* test object creation and destruction */ +static void +test_create_and_destroy(void) +{ + ThriftTransport *transport = NULL; + gint urbuf_size = 0; + gint crbuf_size = 0; + gint uwbuf_size = 0; + gint cwbuf_size = 0; + gint comp_level = 0; + + GObject *object = NULL; + object = g_object_new (THRIFT_TYPE_ZLIB_TRANSPORT, NULL); + g_assert (object != NULL); + g_object_get (G_OBJECT (object), "transport", &transport, + "urbuf_size", &urbuf_size, + "crbuf_size", &crbuf_size, + "uwbuf_size", &uwbuf_size, + "cwbuf_size", &cwbuf_size, + "comp_level", &comp_level, NULL); + g_object_unref (object); +} + +static void +test_open_and_close(void) +{ + ThriftSocket *tsocket = NULL; + ThriftTransport *transport = NULL; + GError *err = NULL; + pid_t pid; + int port = 51199; + int status; + + pid = fork (); + g_assert ( pid >= 0 ); + + if ( pid == 0 ) + { + /* child listens */ + thrift_socket_server_open (port,1); + exit (0); + } else { + /* parent connects, wait a bit for the socket to be created */ + sleep (1); + /* create a ThriftSocket */ + tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost", + "port", port, NULL); + + /* create a ZlibTransport wrapper of the Socket */ + transport = g_object_new (THRIFT_TYPE_ZLIB_TRANSPORT, + "transport", THRIFT_TRANSPORT (tsocket), NULL); + + /* this shouldn't work */ + g_assert (thrift_zlib_transport_open (transport, NULL) == TRUE); + g_assert (thrift_zlib_transport_is_open (transport) == TRUE); + g_assert (thrift_zlib_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 ZlibTransport wrapper of the Socket */ + transport = g_object_new (THRIFT_TYPE_ZLIB_TRANSPORT, + "transport", THRIFT_TRANSPORT (tsocket), NULL); + + g_assert (thrift_zlib_transport_open (transport, &err) == FALSE); + g_object_unref (transport); + g_object_unref (tsocket); + g_error_free (err); + err = NULL; + + g_assert ( wait (&status) == pid ); + g_assert ( status == 0 ); + } +} + +static void +test_read_and_write(void) +{ + int status; + pid_t pid; + ThriftSocket *tsocket = NULL; + ThriftTransport *transport = NULL; + int port = 51199; + gchar buf[36] = TEST_DATA; + + pid = fork (); + g_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_ZLIB_TRANSPORT, + "transport", THRIFT_TRANSPORT (tsocket), NULL); + + g_assert (thrift_zlib_transport_open (transport, NULL) == TRUE); + g_assert (thrift_zlib_transport_is_open (transport)); + + thrift_zlib_transport_write (transport, buf, 36, NULL); + thrift_zlib_transport_flush (transport, NULL); + thrift_zlib_transport_write_end (transport, NULL); + + g_object_unref (transport); + g_object_unref (tsocket); + + g_assert ( wait (&status) == pid ); + g_assert ( status == 0 ); + } +} + +static void +thrift_socket_server_open (const int port, int times) +{ + ThriftServerTransport *transport = NULL; + ThriftTransport *client = NULL; + int i; + + ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET, + "port", port, NULL); + + transport = THRIFT_SERVER_TRANSPORT (tsocket); + thrift_server_transport_listen (transport, NULL); + for(i=0;i<times;i++){ + client = thrift_server_transport_accept (transport, NULL); + g_assert (client != NULL); + thrift_socket_close (client, NULL); + g_object_unref (client); + } + g_object_unref (tsocket); +} + +static void +thrift_server (const int port) +{ + int bytes = 0; + gboolean check_sum = FALSE; + ThriftServerTransport *transport = NULL; + ThriftTransport *client = NULL; + gchar buf[36]; /* a buffer */ + gchar match[36] = 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 ZlibTransport */ + client = g_object_new (THRIFT_TYPE_ZLIB_TRANSPORT, "transport", + thrift_server_transport_accept (transport, NULL), + NULL); + g_assert (client != NULL); + + /* read 36 bytes */ + thrift_zlib_transport_read (client, buf, 36, NULL); + g_assert (memcmp(buf, match, 36) == 0 ); + + thrift_zlib_transport_read_end (client, NULL); + + check_sum = thrift_zlib_transport_verify_checksum (client, NULL); + g_assert (!check_sum); + + thrift_zlib_transport_close (client, NULL); + g_object_unref (client); + g_object_unref (tsocket); +} + +int +main(int argc, char *argv[]) +{ +#if (!GLIB_CHECK_VERSION (2, 36, 0)) + g_type_init(); +#endif + + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/testzlibtransport/CreateAndDestroy", test_create_and_destroy); + g_test_add_func ("/testzlibtransport/OpenAndClose", test_open_and_close); + g_test_add_func ("/testzlibtransport/ReadAndWrite", test_read_and_write); + + return g_test_run (); +} |