THRIFT-4329: multiplexed processor, client and server for c_glib
Client: c_glib
This closes #1361
diff --git a/lib/c_glib/test/testbufferedtransport.c b/lib/c_glib/test/testbufferedtransport.c
index 1c15ef2..c6e6b58 100755
--- a/lib/c_glib/test/testbufferedtransport.c
+++ b/lib/c_glib/test/testbufferedtransport.c
@@ -31,6 +31,7 @@
#include "../src/thrift/c_glib/transport/thrift_buffered_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
@@ -44,8 +45,8 @@
object = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT, NULL);
g_assert (object != NULL);
g_object_get (G_OBJECT (object), "transport", &transport,
- "r_buf_size", &r_buf_size,
- "w_buf_size", &w_buf_size, NULL);
+ "r_buf_size", &r_buf_size,
+ "w_buf_size", &w_buf_size, NULL);
g_object_unref (object);
}
@@ -55,35 +56,52 @@
ThriftSocket *tsocket = NULL;
ThriftTransport *transport = NULL;
GError *err = NULL;
+ pid_t pid;
+ int port = 51199;
+ int status;
- /* create a ThriftSocket */
- tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
- "port", 51188, NULL);
+ pid = fork ();
+ g_assert ( pid >= 0 );
- /* create a BufferedTransport wrapper of the Socket */
- transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
- "transport", THRIFT_TRANSPORT (tsocket), NULL);
+ 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);
- /* this shouldn't work */
- g_assert (thrift_buffered_transport_open (transport, NULL) == FALSE);
- g_assert (thrift_buffered_transport_is_open (transport) == TRUE);
- g_assert (thrift_buffered_transport_close (transport, NULL) == TRUE);
- g_object_unref (transport);
- g_object_unref (tsocket);
+ /* create a BufferedTransport wrapper of the Socket */
+ transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
+ "transport", THRIFT_TRANSPORT (tsocket), NULL);
- /* try and underlying socket failure */
- tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost.broken",
- NULL);
+ /* this shouldn't work */
+ g_assert (thrift_buffered_transport_open (transport, NULL) == TRUE);
+ g_assert (thrift_buffered_transport_is_open (transport) == TRUE);
+ g_assert (thrift_buffered_transport_close (transport, NULL) == TRUE);
+ g_object_unref (transport);
+ g_object_unref (tsocket);
- /* create a BufferedTransport wrapper of the Socket */
- transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
- "transport", THRIFT_TRANSPORT (tsocket), NULL);
+ /* try and underlying socket failure */
+ tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost.broken",
+ NULL);
- g_assert (thrift_buffered_transport_open (transport, &err) == FALSE);
- g_object_unref (transport);
- g_object_unref (tsocket);
- g_error_free (err);
- err = NULL;
+ /* create a BufferedTransport wrapper of the Socket */
+ transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
+ "transport", THRIFT_TRANSPORT (tsocket), NULL);
+
+ g_assert (thrift_buffered_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
@@ -100,57 +118,81 @@
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);
+ {
+ /* child listens */
+ thrift_server (port);
+ exit (0);
+ } else {
+ /* parent connects, wait a bit for the socket to be created */
+ sleep (1);
- tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
- "port", port, NULL);
- transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
- "transport", THRIFT_TRANSPORT (tsocket),
- "w_buf_size", 4, NULL);
+ tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
+ "port", port, NULL);
+ transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
+ "transport", THRIFT_TRANSPORT (tsocket),
+ "w_buf_size", 4, NULL);
- g_assert (thrift_buffered_transport_open (transport, NULL) == TRUE);
- g_assert (thrift_buffered_transport_is_open (transport));
+ g_assert (thrift_buffered_transport_open (transport, NULL) == TRUE);
+ g_assert (thrift_buffered_transport_is_open (transport));
- /* write 10 bytes */
- thrift_buffered_transport_write (transport, buf, 10, NULL);
+ /* write 10 bytes */
+ thrift_buffered_transport_write (transport, buf, 10, NULL);
- /* write 1 byte at a time */
- thrift_buffered_transport_write (transport, buf, 1, NULL);
- thrift_buffered_transport_write (transport, buf, 1, NULL);
- thrift_buffered_transport_write (transport, buf, 1, NULL);
+ /* write 1 byte at a time */
+ thrift_buffered_transport_write (transport, buf, 1, NULL);
+ thrift_buffered_transport_write (transport, buf, 1, NULL);
+ thrift_buffered_transport_write (transport, buf, 1, NULL);
- /* overflow the buffer */
- thrift_buffered_transport_write (transport, buf, 2, NULL);
- thrift_buffered_transport_write (transport, buf, 1, NULL);
- thrift_buffered_transport_flush (transport, NULL);
+ /* overflow the buffer */
+ thrift_buffered_transport_write (transport, buf, 2, NULL);
+ thrift_buffered_transport_write (transport, buf, 1, NULL);
+ thrift_buffered_transport_flush (transport, NULL);
- /* write 1 byte and flush */
- thrift_buffered_transport_write (transport, buf, 1, NULL);
- thrift_buffered_transport_flush (transport, NULL);
+ /* write 1 byte and flush */
+ thrift_buffered_transport_write (transport, buf, 1, NULL);
+ thrift_buffered_transport_flush (transport, NULL);
- /* write and overflow buffer with 2 system calls */
- thrift_buffered_transport_write (transport, buf, 1, NULL);
- thrift_buffered_transport_write (transport, buf, 3, NULL);
+ /* write and overflow buffer with 2 system calls */
+ thrift_buffered_transport_write (transport, buf, 1, NULL);
+ thrift_buffered_transport_write (transport, buf, 3, NULL);
- /* write 10 bytes */
- thrift_buffered_transport_write (transport, buf, 10, NULL);
+ /* write 10 bytes */
+ thrift_buffered_transport_write (transport, buf, 10, NULL);
- thrift_buffered_transport_write_end (transport, NULL);
- thrift_buffered_transport_flush (transport, NULL);
- thrift_buffered_transport_close (transport, NULL);
+ thrift_buffered_transport_write_end (transport, NULL);
+ thrift_buffered_transport_flush (transport, NULL);
+ thrift_buffered_transport_close (transport, NULL);
- g_object_unref (transport);
- g_object_unref (tsocket);
+ g_object_unref (transport);
+ g_object_unref (tsocket);
- g_assert ( wait (&status) == pid );
- g_assert ( status == 0 );
+ g_assert ( wait (&status) == pid );
+ g_assert ( status == 0 );
+ }
+}
+
+
+static void
+thrift_socket_server_open (const int port, int times)
+{
+ int bytes = 0;
+ ThriftServerTransport *transport = NULL;
+ ThriftTransport *client = NULL;
+ guchar buf[10]; /* a buffer */
+ guchar match[10] = TEST_DATA;
+ 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
@@ -163,15 +205,15 @@
guchar match[10] = TEST_DATA;
ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
- "port", port, NULL);
+ "port", port, NULL);
transport = THRIFT_SERVER_TRANSPORT (tsocket);
thrift_server_transport_listen (transport, NULL);
/* wrap the client in a BufferedTransport */
client = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT, "transport",
- thrift_server_transport_accept (transport, NULL),
- "r_buf_size", 5, NULL);
+ thrift_server_transport_accept (transport, NULL),
+ "r_buf_size", 5, NULL);
g_assert (client != NULL);
/* read 10 bytes */
@@ -209,62 +251,62 @@
g_assert ( pid >= 0 );
if ( pid == 0 )
- {
- /* child listens */
- ThriftServerTransport *transport = NULL;
- ThriftTransport *client = NULL;
+ {
+ /* child listens */
+ ThriftServerTransport *transport = NULL;
+ ThriftTransport *client = NULL;
- ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
- "port", port, NULL);
+ ThriftServerSocket *tsocket = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
+ "port", port, NULL);
- transport = THRIFT_SERVER_TRANSPORT (tsocket);
- thrift_server_transport_listen (transport, NULL);
+ transport = THRIFT_SERVER_TRANSPORT (tsocket);
+ thrift_server_transport_listen (transport, NULL);
- /* wrap the client in a BufferedTransport */
- client = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT, "transport",
- thrift_server_transport_accept (transport, NULL),
- "r_buf_size", 5, NULL);
- g_assert (client != NULL);
+ /* wrap the client in a BufferedTransport */
+ client = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT, "transport",
+ thrift_server_transport_accept (transport, NULL),
+ "r_buf_size", 5, NULL);
+ g_assert (client != NULL);
- /* just close socket */
- thrift_buffered_transport_close (client, NULL);
- g_object_unref (client);
- g_object_unref (tsocket);
- exit (0);
- } else {
- /* parent connects, wait a bit for the socket to be created */
- sleep (1);
+ /* just close socket */
+ thrift_buffered_transport_close (client, NULL);
+ g_object_unref (client);
+ g_object_unref (tsocket);
+ exit (0);
+ } else {
+ /* parent connects, wait a bit for the socket to be created */
+ sleep (1);
- tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
- "port", port, NULL);
- transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
- "transport", THRIFT_TRANSPORT (tsocket),
- "w_buf_size", 4, NULL);
+ tsocket = g_object_new (THRIFT_TYPE_SOCKET, "hostname", "localhost",
+ "port", port, NULL);
+ transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
+ "transport", THRIFT_TRANSPORT (tsocket),
+ "w_buf_size", 4, NULL);
- g_assert (thrift_buffered_transport_open (transport, NULL) == TRUE);
- g_assert (thrift_buffered_transport_is_open (transport));
+ g_assert (thrift_buffered_transport_open (transport, NULL) == TRUE);
+ g_assert (thrift_buffered_transport_is_open (transport));
- /* recognize disconnection */
- sleep(1);
- g_assert (thrift_buffered_transport_write (transport, buf, 10, NULL) == TRUE);
- g_assert (thrift_buffered_transport_write (transport, buf, 10, NULL) == FALSE);
+ /* recognize disconnection */
+ sleep(1);
+ g_assert (thrift_buffered_transport_write (transport, buf, 10, NULL) == TRUE);
+ g_assert (thrift_buffered_transport_write (transport, buf, 10, NULL) == FALSE);
- /* write and overflow buffer */
- g_assert (thrift_buffered_transport_write (transport, buf, 10, NULL) == FALSE);
+ /* write and overflow buffer */
+ g_assert (thrift_buffered_transport_write (transport, buf, 10, NULL) == FALSE);
- /* write 1 and flush */
- g_assert (thrift_buffered_transport_write (transport, buf, 1, NULL) == TRUE);
- g_assert (thrift_buffered_transport_flush (transport, NULL) == FALSE);
+ /* write 1 and flush */
+ g_assert (thrift_buffered_transport_write (transport, buf, 1, NULL) == TRUE);
+ g_assert (thrift_buffered_transport_flush (transport, NULL) == FALSE);
- thrift_buffered_transport_close (transport, NULL);
+ thrift_buffered_transport_close (transport, NULL);
- g_object_unref (transport);
- g_object_unref (tsocket);
+ g_object_unref (transport);
+ g_object_unref (tsocket);
- g_assert ( wait (&status) == pid );
- g_assert ( status == 0 );
- }
+ g_assert ( wait (&status) == pid );
+ g_assert ( status == 0 );
+ }
}
int