MediaProcessors
comm.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, 2018 Rafael Antoniello
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided 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  * 3. Neither the name of copyright holders nor the names of its
14  * contributors may be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS
21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
36 #ifndef MEDIAPROCESSORS_UTILS_SRC_COMM_H_
37 #define MEDIAPROCESSORS_UTILS_SRC_COMM_H_
38 
39 #include <sys/types.h>
40 #include <inttypes.h>
41 #include <pthread.h>
42 #include <stdarg.h>
43 
44 /* **** Definitions **** */
45 
46 /* Forward definitions */
47 typedef struct log_ctx_s log_ctx_t;
48 typedef struct comm_ctx_s comm_ctx_t;
49 
53 typedef enum comm_mode_enum {
54  COMM_MODE_IPUT= 0,
55  COMM_MODE_OPUT,
56  COMM_MODE_MAX
57 } comm_mode_t;
58 
64 typedef struct comm_if_s {
65  char *scheme;
66  comm_ctx_t* (*open)(const char *url, const char *local_url,
67  comm_mode_t comm_mode, log_ctx_t *log_ctx, va_list arg);
68  void (*close)(comm_ctx_t **ref_comm_ctx);
69  int (*send)(comm_ctx_t *comm_ctx, const void *buf, size_t count,
70  struct timeval *timeout);
71  int (*recv)(comm_ctx_t *comm_ctx, void** ref_buf, size_t *ref_count,
72  char **ref_from, struct timeval *timeout);
73  int (*unblock)(comm_ctx_t *comm_ctx);
74 } comm_if_t;
75 
79 typedef struct comm_ctx_s {
87  pthread_mutex_t api_mutex;
99  char* local_url;
103  char* url;
104 } comm_ctx_t;
105 
106 /* **** Prototypes **** */
107 
116 int comm_module_open(log_ctx_t *log_ctx);
117 
122 void comm_module_close();
123 
173 int comm_module_opt(const char *tag, ...);
174 
175 comm_ctx_t* comm_open(const char *url, const char *local_url,
176  comm_mode_t comm_mode, log_ctx_t *log_ctx, ...);
177 
178 void comm_close(comm_ctx_t **ref_comm_ctx);
179 
180 int comm_send(comm_ctx_t *comm_ctx, const void *buf, size_t count,
181  struct timeval *timeout);
182 
183 int comm_recv(comm_ctx_t *comm_ctx, void** ref_buf, size_t *ref_count,
184  char **ref_from, struct timeval* timeout);
185 
186 int comm_unblock(comm_ctx_t* comm_ctx);
187 
188 /* **** Communication module functions to integrate with an "external API" ****
189  *
190  * The set of functions below are provided to be integrated in an external
191  * application with a typical send/receive interface and a control interface
192  * implemented as an API.
193  *
194  * Let us call the 'control functions' to the following set:
195  * - 'comm_open_external()';
196  * - 'comm_close_external()';
197  * - 'comm_reset_external()';
198  * We will call the 'i/o functions' to:
199  * - 'comm_send_external()'
200  * - 'comm_recv_external()'.
201  *
202  * The calling application must be designed in order to make sure the
203  * 'control functions' are never executed concurrently. That is, it should
204  * define a critical section to provide that these functions are never executed
205  * "at the same time", avoiding the potential problems of "opening" while
206  * "closing", etc.
207  *
208  * In the case of 'i/o' functions, these may be executed asynchronously and
209  * concurrently by the calling application, so these are designed to provide
210  * secure means to be able to be executed at the same time as the
211  * 'control functions' (for example, we should be able to read, write at the
212  * same time another thread is requested to close or reset the same COMM
213  * instance).
214  * To make this possible, we use an external specific critical section (given
215  * by the argument 'comm_ctx_mutex_external') to be used to
216  * execute in mutual exclusion the "control functions" and the "i/o" functions.
217  * Finally, it is to note that "unblocking" operation is not performed within
218  * this critical section to avoid deadlocks, as i/o functions may
219  * be defined as blocking operations.
220  */
221 
222 int comm_open_external(pthread_mutex_t *comm_ctx_mutex_external,
223  const char *url, const char *local_url, comm_mode_t comm_mode,
224  log_ctx_t *log_ctx, comm_ctx_t **ref_comm_ctx, ...);
225 
226 void comm_close_external(pthread_mutex_t *comm_ctx_mutex_external,
227  comm_ctx_t **ref_comm_ctx, log_ctx_t *log_ctx);
228 
229 int comm_reset_external(pthread_mutex_t *comm_ctx_mutex_external,
230  const char *new_url, const char *local_url, comm_mode_t comm_mode,
231  log_ctx_t *log_ctx, comm_ctx_t **ref_comm_ctx_curr, ...);
232 
233 int comm_recv_external(pthread_mutex_t *comm_ctx_mutex_external,
234  comm_ctx_t **ref_comm_ctx, void** ref_buf, size_t *ref_count,
235  char **ref_from, struct timeval* timeout, log_ctx_t *log_ctx);
236 
237 #endif /* MEDIAPROCESSORS_UTILS_SRC_COMM_H_ */
comm_mode_t comm_mode
Definition: comm.h:95
pthread_mutex_t api_mutex
Definition: comm.h:87
char * url
Definition: comm.h:103
char * local_url
Definition: comm.h:99
struct comm_if_s comm_if_t
void comm_module_close()
Definition: comm.c:129
comm_mode_enum
Definition: comm.h:53
int comm_module_opt(const char *tag,...)
Definition: comm.c:150
log_ctx_t * log_ctx
Definition: comm.h:91
struct comm_ctx_s comm_ctx_t
Definition: comm.h:48
Definition: log.c:102
enum comm_mode_enum comm_mode_t
int comm_module_open(log_ctx_t *log_ctx)
Definition: comm.c:101
const comm_if_t * comm_if
Definition: comm.h:83
Definition: comm.h:64