forked from ssimicro/lib_mysqludf_amqp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
send.c
171 lines (138 loc) · 5.57 KB
/
send.c
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "config.h"
#include <ctype.h>
#include <mysql.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <time.h>
#include <amqp_tcp_socket.h>
#include <amqp.h>
#include <amqp_framing.h>
#include "send.h"
#include "uuid.h"
typedef struct conn_info {
amqp_socket_t *socket;
amqp_connection_state_t conn;
char *message_id;
} conn_info_t;
my_bool
lib_mysqludf_amqp_send_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
int rc;
amqp_rpc_reply_t reply;
conn_info_t *conn_info;
if (args->arg_count != 7 || (args->arg_type[0] != STRING_RESULT) /* host */
|| (args->arg_type[1] != INT_RESULT) /* port */
|| (args->arg_type[2] != STRING_RESULT) /* username */
|| (args->arg_type[3] != STRING_RESULT) /* password */
|| (args->arg_type[4] != STRING_RESULT) /* exchange */
|| (args->arg_type[5] != STRING_RESULT) /* routing key */
|| (args->arg_type[6] != STRING_RESULT)) { /* message */
(void) strncpy(message, "lib_mysqludf_amqp_send: invalid arguments", MYSQL_ERRMSG_SIZE);
return 1;
}
conn_info = (conn_info_t *) malloc(sizeof(conn_info_t));
conn_info->conn = amqp_new_connection();
conn_info->socket = amqp_tcp_socket_new(conn_info->conn);
if (conn_info->socket == NULL) {
(void) strncpy(message, "lib_mysqludf_amqp_send: socket error", MYSQL_ERRMSG_SIZE);
goto init_error_destroy;;
}
rc = amqp_socket_open(conn_info->socket, args->args[0], (int)(*((long long *) args->args[1])));
if (rc < 0) {
(void) strncpy(message, "lib_mysqludf_amqp_send: socket open error", MYSQL_ERRMSG_SIZE);
goto init_error_destroy;
}
reply = amqp_login(conn_info->conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, args->args[2], args->args[3]);
if (reply.reply_type != AMQP_RESPONSE_NORMAL) {
(void) strncpy(message, "lib_mysqludf_amqp_send: login error", MYSQL_ERRMSG_SIZE);
goto init_error_close;
}
amqp_channel_open(conn_info->conn, 1);
reply = amqp_get_rpc_reply(conn_info->conn);
if (reply.reply_type != AMQP_RESPONSE_NORMAL) {
(void) strncpy(message, "lib_mysqludf_amqp_send: channel error", MYSQL_ERRMSG_SIZE);
goto init_error_close;
}
conn_info->message_id = uuidgen();
if (conn_info->message_id == NULL) {
(void) strncpy(message, "lib_mysqludf_amqp_send: uuidgen error", MYSQL_ERRMSG_SIZE);
goto init_error_close;
}
initid->ptr = (char *) conn_info;
initid->maybe_null = 1; /* returns NULL */
initid->const_item = 0; /* may return something different if called again */
return 0;
init_error_close:
(void) amqp_connection_close(conn_info->conn, AMQP_REPLY_SUCCESS);
init_error_destroy:
(void) amqp_destroy_connection(conn_info->conn);
free(initid->ptr);
initid->ptr = NULL;
return 1;
}
char*
lib_mysqludf_amqp_send(UDF_INIT *initid, UDF_ARGS *args, char* result, unsigned long* length, char *is_null, char *error, char *content_type)
{
int rc;
conn_info_t *conn_info = (conn_info_t *) initid->ptr;
amqp_table_entry_t headers[1];
amqp_basic_properties_t props;
props._flags = 0;
/* contentType */
props.content_type = amqp_cstring_bytes(content_type);
props._flags |= AMQP_BASIC_CONTENT_TYPE_FLAG;
/* deliveryMode */
props.delivery_mode = 2;
props._flags |= AMQP_BASIC_DELIVERY_MODE_FLAG;
/* timestamp */
props.timestamp = time(NULL);
props._flags |= AMQP_BASIC_TIMESTAMP_FLAG;
/* headers */
headers[0].key = amqp_cstring_bytes("User-Agent");
headers[0].value.kind = AMQP_FIELD_KIND_UTF8;
headers[0].value.value.bytes = amqp_cstring_bytes(PACKAGE_NAME "/" PACKAGE_VERSION);
props.headers.entries = headers;
props.headers.num_entries = sizeof(headers) / sizeof(headers[0]);
props._flags |= AMQP_BASIC_HEADERS_FLAG;
/* appId */
props.app_id = amqp_cstring_bytes(PACKAGE_NAME);
props._flags |= AMQP_BASIC_APP_ID_FLAG;
/* messageId */
props.message_id = amqp_cstring_bytes(conn_info->message_id);
props._flags |= AMQP_BASIC_MESSAGE_ID_FLAG;
amqp_queue_declare(conn_info->conn, 1, amqp_cstring_bytes(args->args[5]), 0, 1, 0, 0, amqp_empty_table);
rc = amqp_basic_publish(conn_info->conn, 1, amqp_cstring_bytes(args->args[4]), amqp_cstring_bytes(args->args[5]), 0, 0, &props, amqp_cstring_bytes(args->args[6]));
if (rc < 0) {
(void) amqp_channel_close(conn_info->conn, 1, AMQP_REPLY_SUCCESS);
(void) amqp_connection_close(conn_info->conn, AMQP_REPLY_SUCCESS);
(void) amqp_destroy_connection(conn_info->conn);
free(conn_info->message_id);
conn_info->message_id = NULL;
free(initid->ptr);
initid->ptr = NULL;
*is_null = 1;
*error = 1;
return NULL;
}
*is_null = 0;
*error = 0;
*length = (unsigned long) strlen(conn_info->message_id);
return conn_info->message_id;
}
void
lib_mysqludf_amqp_send_deinit(UDF_INIT *initid)
{
if (initid->ptr != NULL) {
conn_info_t *conn_info = (conn_info_t *) initid->ptr;
(void) amqp_channel_close(conn_info->conn, 1, AMQP_REPLY_SUCCESS);
(void) amqp_connection_close(conn_info->conn, AMQP_REPLY_SUCCESS);
(void) amqp_destroy_connection(conn_info->conn);
free(conn_info->message_id);
conn_info->message_id = NULL;
free(initid->ptr);
initid->ptr = NULL;
}
return;
}