aura  0.1
 All Data Structures Functions Variables Modules Pages
transport.c
1 #include <aura/aura.h>
2 
3 static LIST_HEAD(transports);
4 
5 #define required(_rq) \
6  if (!tr->_rq) { \
7  slog(0, SLOG_WARN, \
8  "Transport %s missing required field aura_transport.%s; Disabled", \
9  tr->name, #_rq \
10  ); \
11  return; \
12  }
13 
14 
15 void aura_transport_register(struct aura_transport *tr)
16 {
17  /* Check if we have all that is required */
18  required(name);
19  required(open);
20  required(close);
21  required(loop);
22 
23  if ((tr->buffer_get) || (tr->buffer_put))
24  {
25  required(buffer_get);
26  required(buffer_put)
27  }
28 
29  /* Warn against erroneous config */
30  if (tr->buffer_overhead < tr->buffer_offset) {
31  slog(0, SLOG_WARN,
32  "Transport has buffer_overhead (%d) < buffer_offset (%d). It will crash. Disabled",
34  return;
35  }
36 
37  /* Add it! */
38  list_add_tail(&tr->registry, &transports);
39 }
40 
48 const struct aura_transport *aura_transport_lookup(const char *name)
49 {
50  struct aura_transport *pos;
51  list_for_each_entry(pos, &transports, registry)
52  if (strcmp(pos->name, name) == 0 ) {
53  pos->usage++;
54  return pos;
55  }
56  return NULL;
57 }
58 
59 void aura_transport_release(const struct aura_transport *tr)
60 {
61  ((struct aura_transport* ) tr)->usage--;
62 }
63 
64 void aura_transport_dump_usage()
65 {
66  struct aura_transport *pos;
67  slog(0, SLOG_INFO, "--- Registered transports ---");
68  list_for_each_entry(pos, &transports, registry)
69  slog(0, SLOG_INFO, "%s (%d instances in use)", pos->name, pos->usage);
70 }
71 
81 int aura_get_pollfds(struct aura_node *node, const struct aura_pollfds **fds)
82 {
83  *fds = node->fds;
84  return node->nextfd;
85 }
86 
95 void aura_add_pollfds(struct aura_node *node, int fd, uint32_t events)
96 {
97  struct aura_pollfds *ap;
98 
99  if (!node->fds) {
100  /* Start with 8 fds. Unlikely more will be needed */
101  node->fds = calloc(8, sizeof(*node->fds));
102  node->numfds = 8;
103  node->nextfd = 0;
104  slog(4, SLOG_DEBUG, "node: %d descriptor slots available", node->numfds);
105  }
106 
107  if (node->nextfd >= node->numfds) {
108  int count = node->numfds * 2;
109  node->fds = realloc(node->fds, count * sizeof(*node->fds));
110  node->numfds = count;
111  slog(4, SLOG_DEBUG, "node: Resized. %d descriptor slots available", node->numfds);
112  }
113 
114  if (!node->fds) {
115  slog(0, SLOG_FATAL, "Memory allocation problem");
116  aura_panic(node);
117  }
118 
119  ap = &node->fds[node->nextfd++];
120  ap->fd = fd;
121  ap->events = events;
122  ap->node = node;
123 
124  if (node->fd_changed_cb)
125  node->fd_changed_cb(ap, AURA_FD_ADDED, node->fd_changed_arg);
126 }
127 
134 void aura_del_pollfds(struct aura_node *node, int fd)
135 {
136  int i;
137  for (i=0; i < node->nextfd; i++) {
138  struct aura_pollfds *fds = &node->fds[i];
139  if (fds->fd == fd)
140  break;
141  }
142  if (i == node->nextfd) {
143  slog(0, SLOG_FATAL, "Attempt to delete invalid descriptor from node");
144  aura_panic(node);
145  }
146 
147  /* Fire the callback */
148  if (node->fd_changed_cb)
149  node->fd_changed_cb(&node->fds[i], AURA_FD_REMOVED,
150  node->fd_changed_arg);
151 
152  memmove(&node->fds[i], &node->fds[i+1],
153  sizeof(struct aura_pollfds) * (node->nextfd - i - 1));
154  node->nextfd--;
155  bzero(&node->fds[node->nextfd], sizeof(struct aura_pollfds));
156 }
157 
void(* loop)(struct aura_node *node, const struct aura_pollfds *fd)
Required.
Definition: aura.h:240
const char * name
Required.
Definition: aura.h:168
int buffer_offset
Optional.
Definition: aura.h:195
struct list_head registry
Private.
Definition: aura.h:313
struct aura_buffer *(* buffer_get)(struct aura_buffer *buf)
Optional.
Definition: aura.h:265
int buffer_overhead
Optional.
Definition: aura.h:185
int usage
Private.
Definition: aura.h:308
void(* close)(struct aura_node *node)
Required.
Definition: aura.h:221
int(* open)(struct aura_node *node, const char *opts)
Required.
Definition: aura.h:210
void(* buffer_put)(struct aura_buffer *dst, struct aura_buffer *buf)
Optional.
Definition: aura.h:253