librpc
client.c
1 /*
2  * Copyright 2017 Two Pore Guys, Inc.
3  * All rights reserved
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted providing that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
23  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  *
26  */
27 
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <inttypes.h>
45 #include <unistd.h>
46 #include <glib.h>
47 #include <errno.h>
48 #include <string.h>
49 #include <rpc/object.h>
50 #include <rpc/client.h>
51 
52 int
53 main(int argc, const char *argv[])
54 {
55  rpc_client_t client;
56  rpc_connection_t conn;
57  rpc_object_t result;
58  rpc_call_t call;
59  const char *buf;
60  int64_t len;
61  int64_t num;
62  int cnt = 0;
63 
64  if (argc > 1)
65  client = rpc_client_create(argv[1], 0);
66  else
67  client = rpc_client_create("tcp://127.0.0.1:5000", 0);
68  if (client == NULL) {
69  result = rpc_get_last_error();
70  fprintf(stderr, "cannot connect: %s\n",
71  rpc_error_get_message(result));
72  return (1);
73  }
74 
75  conn = rpc_client_get_connection(client);
76  result = rpc_connection_call_simple(conn, "hello", "[s]", "world");
77  printf("result = %s\n", rpc_string_get_string_ptr(result));
78  rpc_release(result);
79 
80  result = rpc_connection_call_simple(conn, "hello", "[s]", "world");
81  printf("result = %s\n", rpc_string_get_string_ptr(result));
82  rpc_release(result);
83 
85  fprintf(stderr, "Remote pid is %d\n",
87  }
88 
89  call = rpc_connection_call(conn, NULL, NULL, "stream", rpc_array_create(), NULL);
90  if (call == NULL) {
91  fprintf(stderr, "Stream call failed\n");
92  rpc_client_close(client);
93  return (1);
94  }
95 
96  rpc_call_set_prefetch(call, 10);
97 
98  for (;;) {
99  rpc_call_wait(call);
100 
101  switch (rpc_call_status(call)) {
103  rpc_call_continue(call, false);
104  break;
105 
107  result = rpc_call_result(call);
108  rpc_object_unpack(result, "[s, i, i]",
109  &buf, &len, &num);
110 
111  cnt++;
112  fprintf(stderr,
113  "frag = %s, len = %" PRId64 ", num = %" PRId64 ","
114  "cnt = %d\n", buf, len, num, cnt);
115 
116  g_assert(len == (int)strlen(buf));
117  rpc_call_continue(call, false);
118  break;
119 
120  case RPC_CALL_DONE:
121  case RPC_CALL_ENDED:
122  fprintf(stderr, "ENDED at %d\n", cnt);
123  goto done;
124 
125  case RPC_CALL_ERROR:
126  fprintf(stderr, "ERRORED out\n");
127  goto done;
128 
129  case RPC_CALL_ABORTED:
130  fprintf(stderr, "ABORTED at %d\n", cnt);
131  goto done;
132 
133  default:
134  break;
135  }
136  }
137 
138 done:
139  fprintf(stderr, "CLOSING client conn %p, cnt = %d\n", conn, cnt);
140 
141  rpc_client_close(client);
142  return (0);
143 }
int rpc_object_unpack(_Nonnull rpc_object_t, const char *_Nonnull fmt,...)
struct rpc_object * rpc_object_t
Definition: object.h:73
int rpc_call_wait(_Nonnull rpc_call_t call)
_Nullable rpc_call_t rpc_connection_call(_Nonnull rpc_connection_t conn, const char *_Nullable path, const char *_Nullable interface, const char *_Nonnull name, _Nullable rpc_object_t args, _Nullable rpc_callback_t callback)
struct rpc_connection * rpc_connection_t
Definition: connection.h:94
_Nullable rpc_object_t rpc_connection_call_simple(_Nonnull rpc_connection_t conn, const char *_Nonnull name, const char *_Nonnull fmt,...)
_Nonnull rpc_object_t rpc_array_create(void)
_Nullable rpc_object_t rpc_call_result(_Nonnull rpc_call_t call)
const char *_Nullable rpc_error_get_message(_Nonnull rpc_object_t error)
struct rpc_call * rpc_call_t
Definition: connection.h:99
_Nullable rpc_client_t rpc_client_create(const char *_Nonnull uri, _Nullable rpc_object_t params)
_Nullable rpc_object_t rpc_get_last_error(void)
bool rpc_connection_has_credentials(_Nonnull rpc_connection_t conn)
int rpc_call_set_prefetch(_Nonnull rpc_call_t call, size_t nitems)
pid_t rpc_connection_get_remote_pid(_Nonnull rpc_connection_t conn)
#define rpc_release(_object)
Definition: object.h:323
_Nonnull rpc_connection_t rpc_client_get_connection(_Nonnull rpc_client_t client)
const char *_Nullable rpc_string_get_string_ptr(_Nonnull rpc_object_t xstring)
rpc_call_status
Definition: connection.h:71
void rpc_client_close(_Nonnull rpc_client_t client)
struct rpc_client * rpc_client_t
Definition: client.h:52
int rpc_call_continue(_Nonnull rpc_call_t call, bool sync)