2
2
3
3
#include <string.h>
4
4
#include <stdio.h>
5
- #include <sys/socket.h>
6
- #include <netinet/in.h>
7
- #include <netdb.h>
8
- #include <arpa/inet.h>
9
5
#include <inttypes.h>
10
6
11
- static int tempodb_send (tempodb_config * config , const char * command );
12
- static int tempodb_read_response (tempodb_config * config , char * buffer , const int buffer_size );
13
-
14
7
static int tempodb_write (tempodb_config * config , const char * query_buffer , char * response_buffer , const ssize_t response_buffer_size );
15
8
static int tempodb_write_by_path (tempodb_config * config , const char * path , const float value , char * response_buffer , const ssize_t response_buffer_size );
16
9
static int tempodb_send_bulk_update (tempodb_config * config , const tempodb_bulk_update * updates , ssize_t update_count , char * response_buffer , const ssize_t response_buffer_size , const char * path );
17
10
18
- static struct sockaddr_in * tempodb_addr (void );
19
- static int tempodb_create_socket (void );
20
- static char * tempodb_getip (char * host );
21
-
22
11
struct tempodb_config {
23
12
char * access_key ;
24
13
char * access_secret ;
25
- int sock ;
14
+ tempodb_platform_config * platform_config ;
26
15
};
27
16
28
17
tempodb_config * tempodb_create (const char * key , const char * secret )
@@ -33,36 +22,19 @@ tempodb_config * tempodb_create(const char *key, const char *secret)
33
22
strncpy (config -> access_key , key , ACCESS_KEY_SIZE + 1 );
34
23
strncpy (config -> access_secret , secret , ACCESS_KEY_SIZE + 1 );
35
24
36
- config -> sock = tempodb_create_socket ();
25
+ config -> platform_config = tempodb_platform_create ();
37
26
38
27
return config ;
39
28
}
40
29
41
30
void tempodb_destroy (tempodb_config * config )
42
31
{
32
+ tempodb_platform_destroy (config -> platform_config );
43
33
free (config -> access_key );
44
34
free (config -> access_secret );
45
35
free (config );
46
36
}
47
37
48
- static struct sockaddr_in * tempodb_addr (void ) {
49
- struct sockaddr_in * remote ;
50
- char * ip = tempodb_getip (DOMAIN );
51
-
52
- remote = (struct sockaddr_in * )malloc (sizeof (struct sockaddr_in ));
53
- remote -> sin_family = AF_INET ;
54
- remote -> sin_port = htons (80 );
55
- remote -> sin_addr .s_addr = inet_addr (ip );
56
-
57
- if (remote -> sin_addr .s_addr == INADDR_NONE ) {
58
- fprintf (stderr , "%s is not a valid IP address\n" , ip );
59
- exit (1 );
60
- }
61
-
62
- free (ip );
63
- return remote ;
64
- }
65
-
66
38
void tempodb_build_query (tempodb_config * config , char * buffer , const size_t buffer_size , const char * verb , const char * path , const char * payload ) {
67
39
char access_credentials [ACCESS_KEY_SIZE * 2 + 2 ];
68
40
char * encoded_credentials ;
@@ -73,77 +45,6 @@ void tempodb_build_query(tempodb_config *config, char *buffer, const size_t buff
73
45
free (encoded_credentials );
74
46
}
75
47
76
- static int tempodb_create_socket (void ) {
77
- struct sockaddr_in * addr = tempodb_addr ();
78
- int sock ;
79
- if ((sock = socket (AF_INET , SOCK_STREAM , IPPROTO_TCP )) < 0 ){
80
- perror ("Can't create TCP socket" );
81
- exit (1 );
82
- }
83
- if (connect (sock , (struct sockaddr * )addr , sizeof (struct sockaddr )) < 0 ){
84
- perror ("Could not connect" );
85
- exit (1 );
86
- }
87
- free (addr );
88
- return sock ;
89
- }
90
-
91
- static char * tempodb_getip (char * host )
92
- {
93
- struct hostent * hent ;
94
- int iplen = 16 ;
95
- char * ip = (char * )malloc (iplen + 1 );
96
- memset (ip , 0 , iplen + 1 );
97
- if ((hent = gethostbyname (host )) == NULL )
98
- {
99
- herror ("Can't get IP" );
100
- exit (1 );
101
- }
102
- if (inet_ntop (AF_INET , (void * )hent -> h_addr_list [0 ], ip , iplen ) == NULL )
103
- {
104
- perror ("Can't resolve host" );
105
- exit (1 );
106
- }
107
- return ip ;
108
- }
109
-
110
- static int tempodb_read_response (tempodb_config * config , char * buffer , const int buffer_size ) {
111
- size_t bytes_read = 0 ;
112
- size_t bytes_read_part ;
113
- int status = 0 ;
114
-
115
- size_t remaining_buffer_size = buffer_size - 1 ;
116
- char * remaining_buffer = buffer ;
117
-
118
- memset (buffer , 0 , buffer_size );
119
-
120
- while ((bytes_read_part = recv (config -> sock , remaining_buffer , remaining_buffer_size , 0 )) > 0 ) {
121
- if (bytes_read_part == -1 ) {
122
- status = -1 ;
123
- perror ("Can't read from socket" );
124
- }
125
- bytes_read += bytes_read_part ;
126
- remaining_buffer_size -= bytes_read_part ;
127
- remaining_buffer += bytes_read_part ;
128
- }
129
- return status ;
130
- }
131
-
132
- int tempodb_send (tempodb_config * config , const char * query ) {
133
- int sent = 0 ;
134
- int sent_part ;
135
-
136
- while (sent < strlen (query )) {
137
- sent_part = send (config -> sock , query + sent , strlen (query ) - sent , 0 );
138
- if (sent_part == -1 ) {
139
- perror ("Can't send query" );
140
- return -1 ;
141
- }
142
- sent += sent_part ;
143
- }
144
- return 0 ;
145
- }
146
-
147
48
int tempodb_bulk_increment (tempodb_config * config , const tempodb_bulk_update * updates , ssize_t update_count , char * response_buffer , const ssize_t response_buffer_size ) {
148
49
return tempodb_send_bulk_update (config , updates , update_count , response_buffer , response_buffer_size , "/v1/increment" );
149
50
}
@@ -239,9 +140,9 @@ int tempodb_write_by_path(tempodb_config *config, const char *path, const float
239
140
}
240
141
241
142
int tempodb_write (tempodb_config * config , const char * query_buffer , char * response_buffer , const ssize_t response_buffer_size ) {
242
- int status = tempodb_send (config , query_buffer );
143
+ int status = tempodb_platform_send (config -> platform_config , query_buffer );
243
144
if (status != -1 ) {
244
- status = tempodb_read_response (config , response_buffer , response_buffer_size );
145
+ status = tempodb_platform_read_response (config -> platform_config , response_buffer , response_buffer_size );
245
146
}
246
147
return status ;
247
148
}
0 commit comments