Skip to content

Commit 162c8d9

Browse files
committed
Merge pull request #4 from tempodb/posix_sockets
Posix sockets
2 parents e663c46 + 16648e7 commit 162c8d9

File tree

13 files changed

+268
-149
lines changed

13 files changed

+268
-149
lines changed

.gitignore

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
tempodb_tests
2-
tempodb_tests.txt
3-
lib/libtempodb.a
4-
lib/test/libtempodb.a
1+
*tempodb_tests
2+
*tempodb_tests.txt
3+
lib/*.a
4+
lib/test/*.a
55
objs/*
66
a.out
77
examples/example-authed.c
88
CppUTest/CppUTest_tests
9-
CppUTest/lib/libCppUTest.a
10-
CppUTest/lib/libCppUTestExt.a
9+
CppUTest/lib/*.a
1110
CppUTest/objs

CppUTest/build/MakefileWorker.mk

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ OBJ = $(call src_to_o,$(SRC))
192192

193193
STUFF_TO_CLEAN += $(OBJ)
194194

195-
TEST_SRC = $(call get_src_from_dir_list, $(TEST_SRC_DIRS))
195+
TEST_SRC = $(call get_src_from_dir_list, $(TEST_SRC_DIRS)) $(TEST_SRC_FILES)
196196
TEST_OBJS = $(call src_to_o,$(TEST_SRC))
197197
STUFF_TO_CLEAN += $(TEST_OBJS)
198198

@@ -287,10 +287,10 @@ $(TARGET_LIB): $(OBJ)
287287
$(SILENCE)$(AR) $(ARFLAGS) $@ $^
288288
$(SILENCE)ranlib $@
289289

290-
test: $(TEST_TARGET)
290+
test_platform: $(TEST_TARGET)
291291
$(RUN_TEST_TARGET)
292292

293-
vtest: $(TEST_TARGET)
293+
vtest_platform: $(TEST_TARGET)
294294
$(RUN_TEST_TARGET) -v
295295

296296
$(CPPUTEST_OBJS_DIR)/%.o: %.cpp

Makefile

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,74 @@
33
#Set this to @ to keep the makefile quiet
44
SILENCE = @
55

6-
#---- Outputs ----#
7-
COMPONENT_NAME = tempodb
6+
ifdef PLATFORM
7+
COMPONENT_NAME = $(PLATFORM)_tempodb
8+
SRC_FILES = src/tempodb/platform/$(PLATFORM).c
9+
TEST_SRC_FILES = tests/TempoDb/platform/$(PLATFORM)_test.c
10+
MOCKS_SRC_DIRS = mocks/$(PLATFORM)
11+
else
12+
COMPONENT_NAME = tempodb
13+
TEST_SRC_FILES = tests/TempoDb/tempodb_test.c
14+
MOCKS_SRC_DIRS = mocks
15+
endif
816

917
#--- Inputs ----#
1018
CPPUTEST_HOME = CppUTest
1119
CPP_PLATFORM = Gcc
1220
PROJECT_HOME_DIR = .
1321

14-
SRC_DIRS = \
22+
SRC_DIRS += \
1523
src/tempodb
1624

17-
TEST_SRC_DIRS = \
25+
TEST_SRC_DIRS += \
1826
.\
19-
mocks\
20-
tests/TempoDb\
21-
tests\
22-
27+
tests
2328

2429
INCLUDE_DIRS =\
2530
.\
2631
$(CPPUTEST_HOME)/include\
2732
mocks\
2833
include/tempodb
2934

30-
MOCKS_SRC_DIRS = \
31-
mocks\
32-
3335
CPPUTEST_WARNINGFLAGS = -Wall -Wswitch-default -Werror
3436
#CPPUTEST_CFLAGS = -std=c89
3537
CPPUTEST_CFLAGS += -Wall -Wstrict-prototypes -pedantic
3638
LD_LIBRARIES = -lpthread
3739

38-
STUFF_TO_CLEAN += objs/* objs/src/tempodb/* objs/tests/* objs/mocks/* objs/tests/tempodb/* lib/libtempodb.a
40+
STUFF_TO_CLEAN += objs/*.o objs/platform/*.o objs/src/tempodb/* objs/tests/* objs/mocks/* objs/tests/tempodb/* lib/libtempodb*.a *tempodb_tests
3941

4042
CC = cc
4143

42-
all: lib/libtempodb.a
44+
all: posix
4345

44-
lib/libtempodb.a: objs/base64.o objs/tempodb.o
45-
ar rv lib/libtempodb.a objs/base64.o objs/tempodb.o
46+
posix: lib/libtempodb_posix.a
47+
48+
lib/libtempodb_posix.a: objs/base64.o objs/tempodb.o objs/platform/posix.o
49+
ar rv lib/libtempodb_posix.a objs/base64.o objs/tempodb.o objs/platform/posix.o
4650

4751
objs/base64.o:
4852
$(CC) -c -I include/tempodb -o objs/base64.o src/tempodb/base64.c
4953

5054
objs/tempodb.o:
5155
$(CC) -c -I include/tempodb -o objs/tempodb.o src/tempodb/tempodb.c
5256

57+
objs/platform/posix.o:
58+
$(CC) -c -I include/tempodb -o objs/platform/posix.o src/tempodb/platform/posix.c
59+
5360
CppUTest/lib/libCppUTest.a:
5461
cd CppUTest && make lib/libCppUTest.a
5562

63+
vtest_posix:
64+
$(SILENCE)echo ">>> Running Posix Tests"
65+
$(SILENCE)PLATFORM="posix" make vtest_platform
66+
$(SILENCE)echo "<<< Finished Posix Tests"
67+
68+
vtest_common:
69+
$(SILENCE)echo ">>> Running TempoDB Tests"
70+
$(SILENCE)make vtest_platform
71+
$(SILENCE)echo "<<< Finished TempoDB Tests"
72+
73+
74+
vtest: vtest_common vtest_posix
75+
5676
include $(CPPUTEST_HOME)/build/MakefileWorker.mk

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

mocks/platform_mock.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include "platform_mock.h"
2+
3+
static size_t response_buffer_size = 1024;
4+
char *response_buffer;
5+
6+
void test_init(void) {
7+
last_request = malloc(1);
8+
response_buffer = malloc(response_buffer_size + 1);
9+
memset(response_buffer, 0, response_buffer_size + 1);
10+
set_test_response("200 OK");
11+
}
12+
13+
void test_cleanup(void) {
14+
free(last_request);
15+
free(response_buffer);
16+
}
17+
18+
tempodb_platform_config * tempodb_platform_create(void) {
19+
return malloc(1);
20+
}
21+
22+
int tempodb_platform_destroy(tempodb_platform_config *config) {
23+
free(config);
24+
return 0;
25+
}
26+
27+
int tempodb_platform_send(tempodb_platform_config *config, const char *command) {
28+
free(last_request);
29+
last_request = malloc(strlen(command) + 1);
30+
strncpy(last_request, command, strlen(command));
31+
return 0;
32+
}
33+
34+
int tempodb_platform_read_response(tempodb_platform_config *config, char *buffer, const int buffer_size) {
35+
int response_size = response_buffer_size;
36+
if (buffer_size < response_buffer_size) {
37+
response_size = buffer_size;
38+
}
39+
strncpy(buffer, response_buffer, response_size);
40+
return 0;
41+
}
42+
43+
void set_test_response(const char *buffer) {
44+
strncpy(response_buffer, buffer, response_buffer_size);
45+
}

mocks/platform_mock.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef TEMPODB_PLATFORM_MOCK_H
2+
#define TEMPODB_PLATFORM_MOCK_H
3+
4+
#include <string.h>
5+
#include "tempodb.h"
6+
7+
char *last_request;
8+
9+
void test_init(void);
10+
void test_cleanup(void);
11+
12+
void set_test_response(const char *buffer);
13+
14+
#endif

mocks/TCPSocketStub.c renamed to mocks/posix/posix_mock.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "TCPSocketStub.h"
1+
#include "posix_mock.h"
22
#include <stdio.h>
33

44
static struct hostent *hent;
@@ -10,7 +10,6 @@ static size_t response_buffer_bytes_sent;
1010
char *response_buffer;
1111
char *response_buffer_remaining;
1212

13-
1413
static int last_request_size = 1024;
1514

1615
int socket(int domain, int type, int protocol) {

mocks/TCPSocketStub.h renamed to mocks/posix/posix_mock.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef TCPSocketStub_H
2-
#define TCPSocketStub_H
1+
#ifndef TEMPODB_PLATFORM_POSIX_MOCK_H
2+
#define TEMPODB_PLATFORM_POSIX_MOCK_H
33

44
#include <arpa/inet.h>
55
#include <netdb.h>

src/tempodb/base64.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ char *encode_base64(int size, unsigned char *src) {
6060
if(!size)
6161
size= strlen((char *)src);
6262

63-
out= malloc(sizeof(char) * size*4/3+4);
63+
out= malloc(sizeof(char) * size*4/3+4 + 1);
6464

6565
p= out;
6666

@@ -97,6 +97,7 @@ char *encode_base64(int size, unsigned char *src) {
9797
}
9898

9999
}
100+
*p++ = '\0';
100101

101102
return out;
102103

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+

0 commit comments

Comments
 (0)