Skip to content

Commit 985cbc8

Browse files
committed
pull posix socket implementation into it's own file, and set up the framework for adding other platforms
1 parent e663c46 commit 985cbc8

File tree

4 files changed

+131
-106
lines changed

4 files changed

+131
-106
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ CPP_PLATFORM = Gcc
1212
PROJECT_HOME_DIR = .
1313

1414
SRC_DIRS = \
15-
src/tempodb
15+
src/tempodb\
16+
src/tempodb/platform
1617

1718
TEST_SRC_DIRS = \
1819
.\

include/tempodb/tempodb.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,23 @@
1010
#define TEMPODB_POST "POST"
1111

1212
typedef struct tempodb_config tempodb_config;
13+
typedef struct tempodb_platform_config tempodb_platform_config;
1314
typedef struct tempodb_bulk_update tempodb_bulk_update;
1415

1516
enum id_or_key {TEMPODB_ID, TEMPODB_KEY};
1617

17-
struct tempodb_bulk_update{
18+
struct tempodb_bulk_update {
1819
const char *series;
1920
enum id_or_key type;
2021
float value;
2122
};
2223

24+
/* defined by platform-specific files */
25+
tempodb_platform_config * tempodb_platform_create(void);
26+
int tempodb_platform_destroy(tempodb_platform_config *config);
27+
int tempodb_platform_send(tempodb_platform_config *config, const char *command);
28+
int tempodb_platform_read_response(tempodb_platform_config *config, char *buffer, const int buffer_size);
29+
2330
tempodb_config * tempodb_create(const char *key, const char *secret);
2431
void tempodb_destroy(tempodb_config *config);
2532

src/tempodb/platform/posix.c

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#include "tempodb.h"
2+
#include <sys/socket.h>
3+
#include <stdio.h>
4+
#include <netinet/in.h>
5+
#include <netdb.h>
6+
#include <arpa/inet.h>
7+
8+
static int tempodb_create_socket(void);
9+
static struct sockaddr_in * tempodb_addr(void);
10+
static char * tempodb_getip(char *host);
11+
12+
struct tempodb_platform_config {
13+
int sock;
14+
};
15+
16+
tempodb_platform_config * tempodb_platform_create(void) {
17+
tempodb_platform_config *config = (tempodb_platform_config *)malloc(sizeof(tempodb_platform_config));
18+
config->sock = tempodb_create_socket();
19+
return config;
20+
}
21+
22+
int tempodb_platform_destroy(tempodb_platform_config *config)
23+
{
24+
free(config);
25+
return 0;
26+
}
27+
28+
int tempodb_platform_read_response(tempodb_platform_config *config, char *buffer, const int buffer_size) {
29+
size_t bytes_read = 0;
30+
size_t bytes_read_part;
31+
int status = 0;
32+
33+
size_t remaining_buffer_size = buffer_size - 1;
34+
char *remaining_buffer = buffer;
35+
36+
memset(buffer, 0, buffer_size);
37+
38+
while ((bytes_read_part = recv(config->sock, remaining_buffer, remaining_buffer_size, 0)) > 0) {
39+
if (bytes_read_part == -1) {
40+
status = -1;
41+
perror("Can't read from socket");
42+
}
43+
bytes_read += bytes_read_part;
44+
remaining_buffer_size -= bytes_read_part;
45+
remaining_buffer += bytes_read_part;
46+
}
47+
return status;
48+
}
49+
50+
int tempodb_platform_send(tempodb_platform_config *config, const char *query) {
51+
int sent = 0;
52+
int sent_part;
53+
54+
while (sent < strlen(query)) {
55+
sent_part = send(config->sock, query + sent, strlen(query) - sent, 0);
56+
if (sent_part == -1) {
57+
perror("Can't send query");
58+
return -1;
59+
}
60+
sent += sent_part;
61+
}
62+
return 0;
63+
}
64+
65+
static int tempodb_create_socket(void) {
66+
struct sockaddr_in *addr = tempodb_addr();
67+
int sock;
68+
if((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){
69+
perror("Can't create TCP socket");
70+
exit(1);
71+
}
72+
if(connect(sock, (struct sockaddr *)addr, sizeof(struct sockaddr)) < 0){
73+
perror("Could not connect");
74+
exit(1);
75+
}
76+
free(addr);
77+
return sock;
78+
}
79+
80+
static struct sockaddr_in * tempodb_addr(void) {
81+
struct sockaddr_in *remote;
82+
char *ip = tempodb_getip(DOMAIN);
83+
84+
remote = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in));
85+
remote->sin_family = AF_INET;
86+
remote->sin_port = htons(80);
87+
remote->sin_addr.s_addr = inet_addr(ip);
88+
89+
if (remote->sin_addr.s_addr == INADDR_NONE) {
90+
fprintf(stderr, "%s is not a valid IP address\n", ip);
91+
exit(1);
92+
}
93+
94+
free(ip);
95+
return remote;
96+
}
97+
98+
static char * tempodb_getip(char *host)
99+
{
100+
struct hostent *hent;
101+
int iplen = 16;
102+
char *ip = (char *)malloc(iplen+1);
103+
memset(ip, 0, iplen+1);
104+
if((hent = gethostbyname(host)) == NULL)
105+
{
106+
herror("Can't get IP");
107+
exit(1);
108+
}
109+
if(inet_ntop(AF_INET, (void *)hent->h_addr_list[0], ip, iplen) == NULL)
110+
{
111+
perror("Can't resolve host");
112+
exit(1);
113+
}
114+
return ip;
115+
}
116+

src/tempodb/tempodb.c

Lines changed: 5 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,16 @@
22

33
#include <string.h>
44
#include <stdio.h>
5-
#include <sys/socket.h>
6-
#include <netinet/in.h>
7-
#include <netdb.h>
8-
#include <arpa/inet.h>
95
#include <inttypes.h>
106

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-
147
static int tempodb_write(tempodb_config *config, const char *query_buffer, char *response_buffer, const ssize_t response_buffer_size);
158
static int tempodb_write_by_path(tempodb_config *config, const char *path, const float value, char *response_buffer, const ssize_t response_buffer_size);
169
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);
1710

18-
static struct sockaddr_in * tempodb_addr(void);
19-
static int tempodb_create_socket(void);
20-
static char * tempodb_getip(char *host);
21-
2211
struct tempodb_config {
2312
char *access_key;
2413
char *access_secret;
25-
int sock;
14+
tempodb_platform_config *platform_config;
2615
};
2716

2817
tempodb_config * tempodb_create(const char *key, const char *secret)
@@ -33,36 +22,19 @@ tempodb_config * tempodb_create(const char *key, const char *secret)
3322
strncpy(config->access_key, key, ACCESS_KEY_SIZE + 1);
3423
strncpy(config->access_secret, secret, ACCESS_KEY_SIZE + 1);
3524

36-
config->sock = tempodb_create_socket();
25+
config->platform_config = tempodb_platform_create();
3726

3827
return config;
3928
}
4029

4130
void tempodb_destroy(tempodb_config *config)
4231
{
32+
tempodb_platform_destroy(config->platform_config);
4333
free(config->access_key);
4434
free(config->access_secret);
4535
free(config);
4636
}
4737

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-
6638
void tempodb_build_query(tempodb_config *config, char *buffer, const size_t buffer_size, const char *verb, const char *path, const char *payload) {
6739
char access_credentials[ACCESS_KEY_SIZE*2 + 2];
6840
char *encoded_credentials;
@@ -73,77 +45,6 @@ void tempodb_build_query(tempodb_config *config, char *buffer, const size_t buff
7345
free(encoded_credentials);
7446
}
7547

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-
14748
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) {
14849
return tempodb_send_bulk_update(config, updates, update_count, response_buffer, response_buffer_size, "/v1/increment");
14950
}
@@ -239,9 +140,9 @@ int tempodb_write_by_path(tempodb_config *config, const char *path, const float
239140
}
240141

241142
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);
243144
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);
245146
}
246147
return status;
247148
}

0 commit comments

Comments
 (0)