This is an automated email from the ASF dual-hosted git repository.

ianmcook pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-experiments.git


The following commit(s) were added to refs/heads/main by this push:
     new d333e74  Add simple C GLib client example (#14)
d333e74 is described below

commit d333e744d165f31b482f290fe1734b2ebf633c5a
Author: Sutou Kouhei <[email protected]>
AuthorDate: Mon Mar 11 10:51:32 2024 +0900

    Add simple C GLib client example (#14)
---
 http/get_simple/c_glib/client/.gitignore | 18 +++++++
 http/get_simple/c_glib/client/README.md  | 32 ++++++++++++
 http/get_simple/c_glib/client/client.c   | 88 ++++++++++++++++++++++++++++++++
 3 files changed, 138 insertions(+)

diff --git a/http/get_simple/c_glib/client/.gitignore 
b/http/get_simple/c_glib/client/.gitignore
new file mode 100644
index 0000000..6e08afb
--- /dev/null
+++ b/http/get_simple/c_glib/client/.gitignore
@@ -0,0 +1,18 @@
+# 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.
+
+/client
diff --git a/http/get_simple/c_glib/client/README.md 
b/http/get_simple/c_glib/client/README.md
new file mode 100644
index 0000000..7d28fc1
--- /dev/null
+++ b/http/get_simple/c_glib/client/README.md
@@ -0,0 +1,32 @@
+<!---
+  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.
+-->
+
+# HTTP GET Arrow Data: Simple C GLib Client Example
+
+This directory contains a minimal example of an HTTP client implemented in C 
with GLib. The client:
+1. Sends an HTTP GET request to a server.
+2. Receives an HTTP 200 response from the server, with the response body 
containing an Arrow IPC stream of record batches.
+3. Collects the record batches as they are received.
+
+To run this example, first start one of the server examples in the parent 
directory. Then install the `arrow-glib` and `libsoup` C libraries, compile 
`client.c`, and run the executable. For example, using `clang`:
+
+```sh
+clang client.c $(pkg-config --cflags --libs arrow-glib libsoup-3.0) -o client
+./client
+```
diff --git a/http/get_simple/c_glib/client/client.c 
b/http/get_simple/c_glib/client/client.c
new file mode 100644
index 0000000..ccc135b
--- /dev/null
+++ b/http/get_simple/c_glib/client/client.c
@@ -0,0 +1,88 @@
+/*
+ * 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 <stdlib.h>
+
+#include <arrow-glib/arrow-glib.h>
+#include <libsoup/soup.h>
+
+int
+main(int argc, char **argv)
+{
+  int exit_code = EXIT_FAILURE;
+
+  SoupSession *session = soup_session_new();
+  SoupMessage *message = soup_message_new(SOUP_METHOD_GET,
+                                          "http://localhost:8000";);
+
+  GTimer *timer = g_timer_new();
+
+  GError *error = NULL;
+  GInputStream *input = soup_session_send(session, message, NULL, &error);
+  if (error) {
+    g_printerr("Failed to download: %s\n", error->message);
+    g_error_free(error);
+    goto exit;
+  }
+
+  GArrowGIOInputStream *arrow_input = garrow_gio_input_stream_new(input);
+  GArrowRecordBatchStreamReader *reader =
+    garrow_record_batch_stream_reader_new(GARROW_INPUT_STREAM(arrow_input),
+                                          &error);
+  if (error) {
+    g_printerr("Failed to create reader: %s\n", error->message);
+    g_error_free(error);
+    g_object_unref(arrow_input);
+    goto exit;
+  }
+
+  GArrowTable *table =
+    garrow_record_batch_reader_read_all(GARROW_RECORD_BATCH_READER(reader),
+                                        &error);
+  if (error) {
+    g_printerr("Failed to read record batches: %s\n", error->message);
+    g_error_free(error);
+    g_object_unref(reader);
+    g_object_unref(arrow_input);
+    goto exit;
+  }
+  GArrowChunkedArray *chunked_array = garrow_table_get_column_data(table, 0);
+  guint n_received_record_batches =
+    garrow_chunked_array_get_n_chunks(chunked_array);
+  g_object_unref(chunked_array);
+  g_object_unref(table);
+  g_object_unref(reader);
+  g_object_unref(arrow_input);
+
+  g_timer_stop(timer);
+
+  g_print("%u record batches received\n", n_received_record_batches);
+
+  g_print("%.2f seconds elapsed\n", g_timer_elapsed(timer, NULL));
+
+  exit_code = EXIT_SUCCESS;
+
+exit:
+  g_object_unref(input);
+  g_timer_destroy(timer);
+  g_object_unref(message);
+  g_object_unref(session);
+
+  return exit_code;
+}

Reply via email to