You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by ro...@apache.org on 2014/07/29 23:59:24 UTC

[2/2] git commit: THRIFT-976 c_glib tutorial

THRIFT-976 c_glib tutorial

Patch: Simon South
Signed-off-by: Roger Meier <ro...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/thrift/repo
Commit: http://git-wip-us.apache.org/repos/asf/thrift/commit/2814c2e7
Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/2814c2e7
Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/2814c2e7

Branch: refs/heads/master
Commit: 2814c2e7265ff7316cf9b2cad42827ec6df2bc47
Parents: 60b7ad6
Author: Roger Meier <ro...@apache.org>
Authored: Tue Jul 29 23:28:46 2014 +0200
Committer: Roger Meier <ro...@apache.org>
Committed: Tue Jul 29 23:28:46 2014 +0200

----------------------------------------------------------------------
 configure.ac                    |   1 +
 tutorial/Makefile.am            |   4 +
 tutorial/c_glib/Makefile.am     |  66 ++++++++++++
 tutorial/c_glib/c_glib_client.c | 190 +++++++++++++++++++++++++++++++++++
 4 files changed, 261 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/thrift/blob/2814c2e7/configure.ac
----------------------------------------------------------------------
diff --git a/configure.ac b/configure.ac
index 5af20de..bccfc69 100755
--- a/configure.ac
+++ b/configure.ac
@@ -663,6 +663,7 @@ AC_CONFIG_FILES([
   test/py.tornado/Makefile
   test/rb/Makefile
   tutorial/Makefile
+  tutorial/c_glib/Makefile
   tutorial/cpp/Makefile
   tutorial/go/Makefile
   tutorial/hs/Makefile

http://git-wip-us.apache.org/repos/asf/thrift/blob/2814c2e7/tutorial/Makefile.am
----------------------------------------------------------------------
diff --git a/tutorial/Makefile.am b/tutorial/Makefile.am
index f3fd1dd..2b9be52 100755
--- a/tutorial/Makefile.am
+++ b/tutorial/Makefile.am
@@ -23,6 +23,10 @@ if MINGW
 # do nothing, just build the compiler
 else
 
+if WITH_C_GLIB
+SUBDIRS += c_glib
+endif
+
 if WITH_CPP
 SUBDIRS += cpp
 endif

http://git-wip-us.apache.org/repos/asf/thrift/blob/2814c2e7/tutorial/c_glib/Makefile.am
----------------------------------------------------------------------
diff --git a/tutorial/c_glib/Makefile.am b/tutorial/c_glib/Makefile.am
new file mode 100755
index 0000000..33d6c93
--- /dev/null
+++ b/tutorial/c_glib/Makefile.am
@@ -0,0 +1,66 @@
+#
+# 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.
+#
+
+AM_CFLAGS = -g -Wall -Wextra $(GLIB_CFLAGS) $(GOBJECT_CFLAGS) @GCOV_CFLAGS@
+AM_CPPFLAGS = -I$(top_srcdir)/lib/c_glib/src -Igen-c_glib
+AM_LDFLAGS = $(GLIB_LIBS) $(GOBJECT_LIBS) @GCOV_LDFLAGS@
+
+THRIFT = $(top_builddir)/compiler/cpp/thrift
+
+
+.NOTPARALLEL:
+noinst_LTLIBRARIES = \
+	libtutorialgencglib.la
+
+nodist_libtutorialgencglib_la_SOURCES = \
+	gen-c_glib/calculator.c \
+	gen-c_glib/calculator.h \
+	gen-c_glib/shared_service.c \
+	gen-c_glib/shared_service.h \
+	gen-c_glib/shared_types.c \
+	gen-c_glib/shared_types.h \
+	gen-c_glib/tutorial_types.c \
+	gen-c_glib/tutorial_types.h
+
+libtutorialgencglib_la_LIBADD = \
+	$(top_builddir)/lib/c_glib/libthrift_c_glib.la
+
+
+noinst_PROGRAMS = \
+	tutorial_client
+
+tutorial_client_SOURCES = \
+	c_glib_client.c
+
+tutorial_client_LDADD = \
+	libtutorialgencglib.la \
+	$(top_builddir)/lib/c_glib/libthrift_c_glib.la
+
+
+$(nodist_libtutorialgencglib_la_SOURCES): $(top_srcdir)/tutorial/tutorial.thrift
+	$(THRIFT) --gen c_glib -r $<
+
+clean-local:
+	$(RM) -r gen-c_glib
+
+tutorialclient: all
+	./tutorial_client
+
+EXTRA_DIST = \
+	c_glib_client.c

http://git-wip-us.apache.org/repos/asf/thrift/blob/2814c2e7/tutorial/c_glib/c_glib_client.c
----------------------------------------------------------------------
diff --git a/tutorial/c_glib/c_glib_client.c b/tutorial/c_glib/c_glib_client.c
new file mode 100644
index 0000000..d3f2a34
--- /dev/null
+++ b/tutorial/c_glib/c_glib_client.c
@@ -0,0 +1,190 @@
+/*
+ * 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 <stdio.h>
+#include <glib-object.h>
+
+#include <thrift/c_glib/protocol/thrift_binary_protocol.h>
+#include <thrift/c_glib/transport/thrift_buffered_transport.h>
+#include <thrift/c_glib/transport/thrift_socket.h>
+
+#include "gen-c_glib/calculator.h"
+
+int main (void) {
+  ThriftSocket *socket;
+  ThriftTransport *transport;
+  ThriftProtocol *protocol;
+  CalculatorClient *client;
+  CalculatorIf *iface;
+
+  GError *error = NULL;
+  InvalidOperation *invalid_operation = NULL;
+
+  Work *work;
+
+  gint32 sum;
+  gint32 diff;
+
+  int exit_status = 0;
+
+  socket = g_object_new (THRIFT_TYPE_SOCKET,
+                         "hostname", "localhost",
+                         "port", 9090,
+                         NULL);
+  transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT,
+                            "transport", socket,
+                            NULL);
+  protocol = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL,
+                           "transport", transport,
+                           NULL);
+
+  thrift_transport_open (transport, &error);
+
+
+  /* In the C (GLib) implementation of Thrift, service methods on the
+     server are accessed via a generated client class that implements
+     the service interface. In this tutorial, we access a Calculator
+     service through an instance of CalculatorClient, which implements
+     CalculatorIf. */
+  client = g_object_new (TYPE_CALCULATOR_CLIENT,
+                         "input_protocol", protocol,
+                         "output_protocol", protocol,
+                         NULL);
+  iface = CALCULATOR_IF (client);
+
+  /* Each of the client methods requires at least two parameters: A
+     pointer to the client-interface implementation (the client
+     object), and a handle to a GError structure to receive
+     information about any error that occurs.
+
+     On success, client methods return TRUE. A return value of FALSE
+     indicates an error occured and the error parameter has been
+     set. */
+  if (!error && calculator_client_ping (iface, &error)) {
+    puts ("ping()");
+  }
+
+  /* Service methods that return a value do so by passing the result
+     back via an output parameter (here, "sum"). */
+  if (!error && calculator_client_add (iface, &sum, 1, 1, &error)) {
+    printf ("1+1=%d\n", sum);
+  }
+
+  /* Thrift structs are implemented as GObjects, with each of the
+     struct's members exposed as an object property. */
+  work = g_object_new (TYPE_WORK, NULL);
+
+  if (!error) {
+    g_object_set (work,
+                  "num1", 1,
+                  "num2", 0,
+                  "op",   OPERATION_DIVIDE,
+                  NULL);
+
+    /* Exceptions are passed back from service methods in a manner
+       similar to return values. */
+    if (calculator_client_calculate (iface,
+                                     NULL,
+                                     1,
+                                     work,
+                                     &invalid_operation,
+                                     &error)) {
+      puts ("Whoa? We can divide by zero!");
+    }
+    else {
+      if (invalid_operation) {
+        gchar *why;
+
+        /* Like structs, exceptions are implemented as objects with
+           properties. */
+        g_object_get (invalid_operation, "why", &why, NULL);
+
+        printf ("InvalidOperation: %s\n", why);
+
+        if (why != NULL)
+          g_free (why);
+        g_object_unref (invalid_operation);
+        invalid_operation = NULL;
+      }
+
+      g_error_free (error);
+      error = NULL;
+    }
+  }
+
+  if (!error) {
+    /* Struct objects can be reused across method invocations. */
+    g_object_set (work,
+                  "num1", 15,
+                  "num2", 10,
+                  "op",   OPERATION_SUBTRACT,
+                  NULL);
+
+    if (calculator_client_calculate (iface,
+                                     &diff,
+                                     1,
+                                     work,
+                                     &invalid_operation,
+                                     &error)) {
+      printf ("15-10=%d\n", diff);
+    }
+  }
+
+  g_object_unref (work);
+
+  if (!error) {
+    SharedStruct *shared_struct;
+    gchar *value;
+
+    shared_struct = g_object_new (TYPE_SHARED_STRUCT, NULL);
+
+    /* As defined in the Thrift file, the Calculator service extends
+       the SharedService service. Correspondingly, in the generated
+       code CalculatorClient inherits from SharedServiceClient, and
+       the parent service's methods are accessible through a simple
+       cast. */
+     if (shared_service_client_get_struct (SHARED_SERVICE_IF (iface),
+                                          &shared_struct,
+                                          1,
+                                          &error)) {
+      g_object_get (shared_struct, "value", &value, NULL);
+      printf ("Check log: %s\n", value);
+      g_free (value);
+    }
+
+    g_object_unref (shared_struct);
+  }
+
+  if (error) {
+    printf ("ERROR: %s\n", error->message);
+    g_error_free (error);
+    error = NULL;
+
+    exit_status = 1;
+  }
+
+  thrift_transport_close (transport, NULL);
+
+  g_object_unref (client);
+  g_object_unref (protocol);
+  g_object_unref (transport);
+  g_object_unref (socket);
+
+  return exit_status;
+}