libamxb  4.8.2
Bus Agnostic C API
amxb_ba_connect.c File Reference
#include <errno.h>
#include <fcntl.h>
#include <uriparser/Uri.h>
#include <amxc/amxc.h>
#include <amxp/amxp.h>
#include <amxd/amxd_dm.h>
#include <amxb/amxb_be_intf.h>
#include <amxb/amxb.h>
#include "amxb_priv.h"

Go to the source code of this file.

Functions

static int amxb_uri_part_to_string (amxc_string_t *buffer, UriTextRangeA *tr)
 
static int amxb_uri_path_to_string (amxc_string_t *buffer, UriPathSegmentA *ps)
 
static int amxb_uri_parse (const char *uri, char **scheme, char **host, char **port, char **path)
 
static int amxb_bus_ctx_init (amxb_bus_ctx_t **ctx, amxb_be_funcs_t *fns)
 
static int amxb_bus_ctx_create (amxb_bus_ctx_t **ctx, amxb_be_funcs_t **fns, const char *uri, char **scheme, char **host, char **port, char **path)
 
static int amxb_bus_ctx_be_init (amxb_bus_ctx_t *ctx, amxb_be_funcs_t *fns, amxb_be_connect_fn_t fn, const char *uri, const char *host, const char *port, const char *path)
 
static void amxb_free_request (amxc_llist_it_t *it)
 
int amxb_connect_intf (amxb_bus_ctx_t **ctx, const char *uri, const char *intf)
 Create a bus connection. More...
 
int amxb_connect (amxb_bus_ctx_t **ctx, const char *uri)
 Create a bus connection. More...
 
int amxb_listen (amxb_bus_ctx_t **ctx, const char *uri)
 Create a listen socket. More...
 
int amxb_accept (amxb_bus_ctx_t *listen_ctx, amxb_bus_ctx_t **accepted_ctx)
 Accepts an incomming connection request. More...
 
int amxb_disconnect (amxb_bus_ctx_t *ctx)
 Disconnects a bus connection. More...
 
void amxb_free (amxb_bus_ctx_t **ctx)
 Frees allocated memory. More...
 
amxb_bus_ctx_tamxb_find_uri (const char *uri)
 Find the bus context for a given uri. More...
 
amxc_array_t * amxb_list_uris (void)
 List all open connections by their uri. More...
 
int amxb_get_fd (const amxb_bus_ctx_t *const ctx)
 Get the connection file descriptor. More...
 
int amxb_read (const amxb_bus_ctx_t *const ctx)
 Reads data from the file descriptor. More...
 
int amxb_read_raw (const amxb_bus_ctx_t *const ctx, void *buf, size_t count)
 Attempts to read up to count bytes from the file descriptor into the buffer starting at buf. More...
 
static CONSTRUCTOR void amxb_initialize_connections (void)
 
static DESTRUCTOR void amxb_cleanup_connections (void)
 

Variables

static amxc_htable_t uri_bus_ctxs
 

Function Documentation

◆ amxb_bus_ctx_be_init()

static int amxb_bus_ctx_be_init ( amxb_bus_ctx_t ctx,
amxb_be_funcs_t fns,
amxb_be_connect_fn_t  fn,
const char *  uri,
const char *  host,
const char *  port,
const char *  path 
)
static

Definition at line 169 of file amxb_ba_connect.c.

175  {
176  int retval = -1;
177 
178  ctx->bus_ctx = fn(host, port, path, &ctx->sigmngr);
179  if(ctx->bus_ctx == NULL) {
180  retval = AMXB_ERROR_BACKEND_FAILED;
181  } else {
182  amxc_llist_append(&fns->connections, &ctx->it);
183  amxc_htable_insert(&uri_bus_ctxs, uri, &ctx->hit);
184  retval = 0;
185  }
186 
187  return retval;
188 }
static amxc_htable_t uri_bus_ctxs
#define AMXB_ERROR_BACKEND_FAILED
Back-end failed.
Definition: amxb_error.h:128
amxc_llist_t connections
amxp_signal_mngr_t sigmngr
Definition: amxb_types.h:120
amxc_htable_it_t hit
Definition: amxb_types.h:117
amxc_llist_it_t it
Definition: amxb_types.h:116
void * bus_ctx
Definition: amxb_types.h:122

◆ amxb_bus_ctx_create()

static int amxb_bus_ctx_create ( amxb_bus_ctx_t **  ctx,
amxb_be_funcs_t **  fns,
const char *  uri,
char **  scheme,
char **  host,
char **  port,
char **  path 
)
static

Definition at line 141 of file amxb_ba_connect.c.

147  {
148  int retval = -1;
149 
150  when_null(ctx, exit);
151  when_not_null(*ctx, exit);
152  if(amxb_uri_parse(uri, scheme, host, port, path) != 0) {
153  retval = AMXB_ERROR_INVALID_URI;
154  goto exit;
155  }
156 
157  *fns = amxb_be_find(*scheme);
158  if(*fns == NULL) {
160  goto exit;
161  }
162 
163  retval = amxb_bus_ctx_init(ctx, *fns);
164 
165 exit:
166  return retval;
167 }
static int amxb_bus_ctx_init(amxb_bus_ctx_t **ctx, amxb_be_funcs_t *fns)
static int amxb_uri_parse(const char *uri, char **scheme, char **host, char **port, char **path)
amxb_be_funcs_t * amxb_be_find(const char *name)
Gets a backend function table using its name.
#define AMXB_ERROR_NOT_SUPPORTED_SCHEME
URI scheme not supported.
Definition: amxb_error.h:104
#define AMXB_ERROR_INVALID_URI
Invalid URI.
Definition: amxb_error.h:98

◆ amxb_bus_ctx_init()

static int amxb_bus_ctx_init ( amxb_bus_ctx_t **  ctx,
amxb_be_funcs_t fns 
)
static

Definition at line 123 of file amxb_ba_connect.c.

123  {
124  int retval = -1;
125 
126  *ctx = (amxb_bus_ctx_t*) calloc(1, sizeof(amxb_bus_ctx_t));
127  when_null(*ctx, exit);
128 
129  (*ctx)->bus_fn = fns;
130  amxp_sigmngr_init(&(*ctx)->sigmngr);
131  amxc_htable_init(&(*ctx)->subscriptions, 5);
132  amxc_llist_init(&(*ctx)->client_subs);
133  (*ctx)->access = AMXB_PROTECTED;
134  (*ctx)->socket_type = AMXB_DATA_SOCK;
135 
136  retval = 0;
137 exit:
138  return retval;
139 }
#define AMXB_PROTECTED
Definition: amxb_types.h:74
#define AMXB_DATA_SOCK
Definition: amxb_types.h:76

◆ amxb_cleanup_connections()

static DESTRUCTOR void amxb_cleanup_connections ( void  )
static

Definition at line 430 of file amxb_ba_connect.c.

430  {
431  amxc_htable_clean(&uri_bus_ctxs, NULL);
432 }

◆ amxb_free_request()

static void amxb_free_request ( amxc_llist_it_t *  it)
static

Definition at line 190 of file amxb_ba_connect.c.

190  {
191  amxb_req_t* req = amxc_container_of(it, amxb_req_t, it);
192  if(req->closed && !req->in_flight) {
193  free(req);
194  }
195 }
Definition: amxb_priv.h:69
bool in_flight
Definition: amxb_priv.h:73
bool closed
Definition: amxb_priv.h:74

◆ amxb_initialize_connections()

static CONSTRUCTOR void amxb_initialize_connections ( void  )
static

Definition at line 426 of file amxb_ba_connect.c.

426  {
427  amxc_htable_init(&uri_bus_ctxs, 10);
428 }

◆ amxb_uri_parse()

static int amxb_uri_parse ( const char *  uri,
char **  scheme,
char **  host,
char **  port,
char **  path 
)
static

Definition at line 92 of file amxb_ba_connect.c.

96  {
97  int retval = -1;
98  UriUriA parsed_uri;
99  amxc_string_t uri_part;
100  amxc_string_init(&uri_part, 0);
101 
102  when_failed(uriParseSingleUriA(&parsed_uri, uri, NULL), exit);
103  amxb_uri_part_to_string(&uri_part, &parsed_uri.scheme);
104  *scheme = amxc_string_take_buffer(&uri_part);
105  when_null(*scheme, exit_clean);
106  amxb_uri_part_to_string(&uri_part, &parsed_uri.hostText);
107  *host = amxc_string_take_buffer(&uri_part);
108  amxb_uri_part_to_string(&uri_part, &parsed_uri.portText);
109  *port = amxc_string_take_buffer(&uri_part);
110  amxb_uri_path_to_string(&uri_part, parsed_uri.pathHead);
111  *path = amxc_string_take_buffer(&uri_part);
112 
113  retval = 0;
114 
115 exit_clean:
116  uriFreeUriMembersA(&parsed_uri);
117 
118 exit:
119  amxc_string_clean(&uri_part);
120  return retval;
121 }
static int amxb_uri_part_to_string(amxc_string_t *buffer, UriTextRangeA *tr)
static int amxb_uri_path_to_string(amxc_string_t *buffer, UriPathSegmentA *ps)

◆ amxb_uri_part_to_string()

static int amxb_uri_part_to_string ( amxc_string_t *  buffer,
UriTextRangeA *  tr 
)
static

Definition at line 72 of file amxb_ba_connect.c.

73  {
74 
75  return amxc_string_append(buffer, tr->first, tr->afterLast - tr->first);
76 }

◆ amxb_uri_path_to_string()

static int amxb_uri_path_to_string ( amxc_string_t *  buffer,
UriPathSegmentA *  ps 
)
static

Definition at line 78 of file amxb_ba_connect.c.

79  {
80 
81  const char* sep = "/";
82  while(ps) {
83  amxc_string_append(buffer, sep, strlen(sep));
84  amxb_uri_part_to_string(buffer, &ps->text);
85  sep = "/";
86  ps = ps->next;
87  }
88 
89  return 0;
90 }

Variable Documentation

◆ uri_bus_ctxs

amxc_htable_t uri_bus_ctxs
static

Definition at line 70 of file amxb_ba_connect.c.