-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
292 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,4 +20,6 @@ logs | |
tags | ||
build | ||
roach | ||
roach-cli | ||
roach-server | ||
server |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,34 @@ | ||
CC = gcc | ||
CFLAGS = -Wall -Wextra -Werror -Wunused -std=c11 -pedantic -ggdb -fsanitize=address -fsanitize=undefined -fno-omit-frame-pointer -pg -D_DEFAULT_SOURCE=200809L -Iinclude -Isrc | ||
LDFLAGS = -L. -ltimeseries -fsanitize=address -fsanitize=undefined | ||
LDFLAGS_CLI = -fsanitize=address -fsanitize=undefined | ||
|
||
LIB_SOURCES = src/timeseries.c src/partition.c src/wal.c src/disk_io.c src/binary.c src/logging.c src/persistent_index.c src/commit_log.c | ||
LIB_OBJECTS = $(LIB_SOURCES:.c=.o) | ||
LIB_PERSISTENCE = logdata | ||
|
||
SERVER_SOURCES = src/main.c src/parser.c src/protocol.c src/server.c src/ev.h src/ev_tcp.h | ||
SERVER_OBJECTS = $(SERVER_SOURCES:.c=.o) | ||
SERVER_EXECUTABLE = server | ||
SERVER_EXECUTABLE = roach-server | ||
|
||
all: libtimeseries.so $(SERVER_EXECUTABLE) | ||
CLI_SOURCES = src/client.c src/roach_cli.c src/protocol.c src/ev.h src/ev_tcp.h | ||
CLI_OBJECTS = $(CLI_SOURCES:.c=.o) | ||
CLI_EXECUTABLE = roach-cli | ||
|
||
all: libtimeseries.so $(SERVER_EXECUTABLE) $(CLI_EXECUTABLE) | ||
|
||
libtimeseries.so: $(LIB_OBJECTS) | ||
$(CC) -shared -o $@ $(LIB_OBJECTS) | ||
|
||
$(SERVER_EXECUTABLE): $(SERVER_OBJECTS) libtimeseries.so | ||
$(CC) -o $@ $(SERVER_OBJECTS) $(LDFLAGS) | ||
|
||
$(CLI_EXECUTABLE): $(CLI_OBJECTS) | ||
$(CC) -o $@ $(CLI_OBJECTS) $(LDFLAGS_CLI) | ||
|
||
%.o: %.c | ||
$(CC) $(CFLAGS) -fPIC -c $< -o $@ | ||
|
||
clean: | ||
rm -f $(LIB_OBJECTS) $(SERVER_OBJECTS) libtimeseries.so $(SERVER_EXECUTABLE) | ||
rm -rf $(LIB_PERSISTENCE) 2> /dev/null | ||
@rm -f $(LIB_OBJECTS) $(SERVER_OBJECTS) libtimeseries.so $(SERVER_EXECUTABLE) | ||
@rm -rf $(LIB_PERSISTENCE) 2> /dev/null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
#include "client.h" | ||
#include "protocol.h" | ||
#include <ctype.h> | ||
#include <errno.h> | ||
#include <netdb.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <sys/socket.h> | ||
#include <sys/un.h> | ||
#include <unistd.h> | ||
|
||
#define BUFSIZE 2048 | ||
|
||
/* | ||
* Create a non-blocking socket and use it to connect to the specified host and | ||
* port | ||
*/ | ||
static int roach_connect(const struct connect_options *opts) { | ||
|
||
/* socket: create the socket */ | ||
int fd = socket(opts->s_family, SOCK_STREAM, 0); | ||
if (fd < 0) | ||
goto err; | ||
|
||
/* Set socket timeout for read and write if present on options */ | ||
if (opts->timeout > 0) { | ||
struct timeval tv; | ||
tv.tv_sec = opts->timeout; | ||
tv.tv_usec = 0; | ||
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (const char *)&tv, sizeof(tv)); | ||
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (const char *)&tv, sizeof(tv)); | ||
} | ||
|
||
if (opts->s_family == AF_INET) { | ||
struct sockaddr_in addr; | ||
struct hostent *server; | ||
|
||
/* gethostbyname: get the server's DNS entry */ | ||
server = gethostbyname(opts->s_addr); | ||
if (server == NULL) | ||
goto err; | ||
|
||
/* build the server's address */ | ||
addr.sin_family = opts->s_family; | ||
addr.sin_port = htons(opts->s_port); | ||
addr.sin_addr = *((struct in_addr *)server->h_addr); | ||
bzero(&(addr.sin_zero), 8); | ||
|
||
/* connect: create a connection with the server */ | ||
if (connect(fd, (const struct sockaddr *)&addr, sizeof(addr)) == -1) | ||
goto err; | ||
|
||
} else if (opts->s_family == AF_UNIX) { | ||
struct sockaddr_un addr; | ||
memset(&addr, 0, sizeof(addr)); | ||
addr.sun_family = AF_UNIX; | ||
if (*opts->s_addr == '\0') { | ||
*addr.sun_path = '\0'; | ||
strncpy(addr.sun_path + 1, opts->s_addr + 1, | ||
sizeof(addr.sun_path) - 2); | ||
} else { | ||
strncpy(addr.sun_path, opts->s_addr, sizeof(addr.sun_path) - 1); | ||
} | ||
|
||
if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) | ||
goto err; | ||
} | ||
|
||
return fd; | ||
|
||
err: | ||
|
||
if (errno == EINPROGRESS) | ||
return fd; | ||
|
||
perror("socket(2) opening socket failed"); | ||
return CLIENT_FAILURE; | ||
} | ||
|
||
void client_init(Client *c, const struct connect_options *opts) { | ||
c->opts = opts; | ||
} | ||
|
||
int client_connect(Client *c) { | ||
int fd = roach_connect(c->opts); | ||
if (fd < 0) | ||
return CLIENT_FAILURE; | ||
c->fd = fd; | ||
return CLIENT_SUCCESS; | ||
} | ||
|
||
void client_disconnect(Client *c) { close(c->fd); } | ||
|
||
int client_send_command(Client *c, char *buf) { | ||
uint8_t data[BUFSIZE]; | ||
Request rq = {.length = strlen(buf)}; | ||
snprintf(rq.query, sizeof(rq.query), "%s", buf); | ||
ssize_t n = encode_request(&rq, data); | ||
if (n < 0) | ||
return -1; | ||
|
||
return write(c->fd, data, n); | ||
} | ||
|
||
int client_recv_response(Client *c, Response *rs) { | ||
uint8_t data[BUFSIZE]; | ||
ssize_t n = read(c->fd, data, BUFSIZE); | ||
if (n < 0) | ||
return -1; | ||
|
||
n = decode_response(data, rs); | ||
if (n < 0) | ||
return -1; | ||
|
||
return n; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#ifndef CLIENT_H | ||
#define CLIENT_H | ||
|
||
#include <netdb.h> | ||
#include <stdio.h> | ||
|
||
#define CLIENT_SUCCESS 0 | ||
#define CLIENT_FAILURE -1 | ||
#define CLIENT_UNKNOWN_CMD -2 | ||
|
||
typedef struct response Response; | ||
|
||
typedef struct client Client; | ||
|
||
/* | ||
* Connection options, use this structure to specify connection related opts | ||
* like socket family, host port and timeout for communication | ||
*/ | ||
struct connect_options { | ||
int timeout; | ||
int s_family; | ||
int s_port; | ||
char *s_addr; | ||
}; | ||
|
||
/* | ||
* Pretty basic connection wrapper, just a FD with a buffer tracking bytes and | ||
* some options for connection | ||
*/ | ||
struct client { | ||
int fd; | ||
const struct connect_options *opts; | ||
}; | ||
|
||
void client_init(Client *c, const struct connect_options *opts); | ||
|
||
int client_connect(Client *c); | ||
|
||
void client_disconnect(Client *c); | ||
|
||
int client_send_command(Client *c, char *buf); | ||
|
||
int client_recv_response(Client *c, Response *rs); | ||
|
||
#endif // CLIENT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.