-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsr_context.h
132 lines (102 loc) · 3.77 KB
/
sr_context.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* vim:set ft=c ts=2 sw=2 sts=2 et cindent: */
/*
* Usage info after license block.
*
* This code is by Peter Silva copyright (c) 2017 part of MetPX.
* copyright is to the Government of Canada. code is GPL.
*
* based on a amqp_sendstring from rabbitmq-c package
* the original license is below:
*/
/*
Minimal c implementation to allow posting of sr_post(7) messages.
call an sr_context_init to set things up.
then sr_post will post files,
then sr_close to tear the connection down.
there is an all in one function: connect_and_post that does all of the above.
*/
#ifndef SR_CONTEXT_H
#define SR_CONTEXT_H 1
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include <stdint.h>
#include <amqp_ssl_socket.h>
#include <amqp_tcp_socket.h>
#include <amqp.h>
#include <amqp_framing.h>
#include "sr_config.h"
struct sr_metrics_s {
int brokerQueuedMessageCount;
int rxGoodCount;
int rxBadCount;
int rejectCount;
int txGoodCount;
char lastTx[SR_TIMESTRLEN];
char lastRx[SR_TIMESTRLEN];
double last_housekeeping;
};
struct sr_context {
char settings[AMQP_MAX_SS];
const char *file;
const char *post_baseUrl;
amqp_socket_t *socket;
int port;
struct sr_config_s *cfg;
struct sr_metrics_s metrics;
};
void sr_amqp_error_print(struct sr_log_context_s *logctx, int x, char const *context);
/* utility functions for handling rabbitmq-c call return values.
for rabbitmq-c routines that return an integer, process the output.
*/
void sr_amqp_reply_print(struct sr_log_context_s *logctx, amqp_rpc_reply_t x, char const *context);
/* utility functions for handling rabbitmq-c call return values.
if return value from a function is an amqp_rpc_reply_t, then feed it to this routine.
context, is a descriptive string.
*/
struct sr_context *sr_context_init_config(struct sr_config_s *sr_cfg, const int avoid_std_fds);
/* context_init sets up a context.
returns connection to a broker based on given configuration.
returns an sr_context ready for use by connect.
This routine opens a socket connection to the broker, which uses an file
descriptor. When called from the shim library, it can be called by code
like this:
close(1)
open("....)
write( 1, ... )
The above assumes and requires fd returned by open will be 1, which is
normally safe, however, if the broker connection is established before the
next file is opened, then the socket will use the first available file
descriptor which could be 0, 1, or 2.
When avoid_std_fds is set, a number of file descriptors are associated with
/dev/null prior to connecting to the broker to ensure that the fd assigned
to that socket will not be one of the standard ones. When called from
well-behaved code such as a single C-program, this is unnecessary.
*/
struct timespec sr_time_of_last_run();
struct sr_context *sr_context_connect(struct sr_context *sr_c);
/*
returns open connection to a broker based on given configuration.
returns an sr_context ready for use by post.
connection establishment is done here.
*/
void sr_context_close(struct sr_context *sr_c);
/* clean up an initialized context.
tears down the connection.
*/
void sr_context_housekeeping(struct sr_context *sr_c);
/* periodic processing that users should call ever *hearbeat* interval.
triggers a print in the log, and cache cleaning, for example.
*/
float sr_context_housekeeping_check(struct sr_context *sr_c);
/*
Check the time. If you need to do to run housekeeping processing, it is done.
Returns: elapsed time since previous call, in seconds.
Note: sr_context_init_config must be called before first call to initialize "previous call" timing.
*/
#endif