Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

works

  • Loading branch information...
commit 1d15da1be7852cf09d84dfbdd5e3b4eae2442c46 1 parent 586ad00
@txus authored
View
2  .gitignore
@@ -6,4 +6,4 @@ tests/tests.log
*.DSYM
callgrind.out*
cachegrind.out*
-bin/terror
+bin/shitdb
View
9 Makefile
@@ -9,11 +9,14 @@ OBJECTS=$(patsubst %.c,%.o,$(SOURCES))
TEST_SRC=$(wildcard tests/*_tests.c)
TESTS=$(patsubst %.c,%,$(TEST_SRC))
+PROGRAMS_SRC=$(wildcard bin/*.c)
+PROGRAMS=$(patsubst %.c,%,$(PROGRAMS_SRC))
+
TARGET=build/libshitdb.a
SO_TARGET=$(patsubst %.a,%.so,$(TARGET))
# The Target Build
-all: $(TARGET) $(SO_TARGET) tests
+all: $(TARGET) $(SO_TARGET) tests $(PROGRAMS)
dev: CFLAGS=-g -Wall -Isrc -Wall -Werror $(OPTFLAGS)
dev: all
@@ -23,6 +26,8 @@ $(TARGET): build $(OBJECTS)
ar rcs $@ $(OBJECTS)
ranlib $@
+$(PROGRAMS): CFLAGS += $(TARGET)
+
$(SO_TARGET): $(TARGET) $(OBJECTS)
$(CC) -shared -o $@ $(OBJECTS)
@@ -41,7 +46,7 @@ valgrind:
# The Cleaner
clean:
- rm -rf build $(OBJECTS) $(TESTS)
+ rm -rf build $(OBJECTS) $(TESTS) $(PROGRAMS)
rm -f tests/tests.log
find . -name "*.gc*" -exec rm {} \;
rm -rf `find . -name "*.dSYM" -print`
View
12 bin/shitdb.c
@@ -0,0 +1,12 @@
+#include <shitdb/db.h>
+#include <shitdb/server.h>
+#define DEFAULT_PORT 4999
+
+int main(int argc, char **argv)
+{
+ DB *db = DB_create();
+
+ Server_start(db, DEFAULT_PORT);
+
+ return 0;
+}
View
10 src/shitdb/object.c
@@ -3,11 +3,12 @@
#include <shitdb/db.h>
#include <shitdb/object.h>
-static inline Object*
+Object*
Object_allocate()
{
Object *obj = calloc(1, sizeof(Object));
check_mem(obj);
+ obj->type = tNil;
return obj;
error:
return NULL;
@@ -68,7 +69,12 @@ String_to_object(bstring string)
int len = blength(string) - 2;
obj = Object_create_string(bmidstr(string, 1, len));
} else {
- obj = Object_create_integer(atoi(bdata(string)));
+ int value = atoi(bdata(string));
+ if (value != 0) {
+ obj = Object_create_integer(atoi(bdata(string)));
+ } else {
+ return NULL;
+ }
}
return obj;
}
View
2  src/shitdb/object.h
@@ -4,6 +4,7 @@
#include <shitdb/bstrlib.h>
typedef enum {
+ tNil,
tInteger,
tString
} Type;
@@ -16,6 +17,7 @@ typedef struct Object {
} value;
} Object;
+Object *Object_allocate();
Object *Object_create_integer(int value);
Object *Object_create_string(bstring value);
Object *Object_destroy(Object *object);
View
68 src/shitdb/server.c
@@ -15,12 +15,14 @@ int Command_arity(bstring cmd)
return 1;
} else if (bstrcmp(cmd, bfromcstr("SET")) == 0) {
return 2;
+ } else if (bstrcmp(cmd, bfromcstr("QUIT")) == 0) {
+ return 0;
} else {
return -1;
}
}
-int Server_execute(DB *db, bstring command)
+int Server_execute(DB *db, bstring command, Object *result)
{
struct bstrList *words = bsplit(command, ' ');
@@ -28,15 +30,24 @@ int Server_execute(DB *db, bstring command)
bstring cmd = *ptr;
- check(words->qty != Command_arity(cmd) + 1, "Wrong number of arguments");
+ int arity = Command_arity(cmd);
- Object *result = NULL;
+ check(words->qty == arity + 1, "Wrong number of arguments, expected %i, got %i", arity, words->qty - 1);
ptr++;
- if(bstrcmp(command, bfromcstr("GET"))) {
- result = DB_get(db, *ptr++);
- } else if (bstrcmp(command, bfromcstr("SET"))) {
- DB_set(db, *ptr++, String_to_object(*ptr++));
+
+ if(bstrcmp(cmd, bfromcstr("GET")) == 0) {
+ Object *ret = DB_get(db, *ptr);
+ memcpy(result, ret, sizeof(Object));
+ } else if (bstrcmp(cmd, bfromcstr("SET")) == 0) {
+ bstring key = *ptr;
+ ptr++;
+ Object *value = String_to_object(*ptr);
+ check(value, "Invalid value to SET.");
+
+ DB_set(db, key, value);
+ } else if (bstrcmp(cmd, bfromcstr("QUIT")) == 0) {
+ return -1;
}
return 0;
@@ -44,6 +55,13 @@ int Server_execute(DB *db, bstring command)
return 1;
}
+static bstring
+chomp(bstring original)
+{
+ struct bstrList *words = bsplit(original, '\n');
+ return *words->entry;
+}
+
/* TODO: Clean up file descriptors when exiting. */
void Server_goodbye()
{
@@ -53,7 +71,7 @@ void Server_goodbye()
void Server_start(DB *db, int port)
{
// Trap SIGINT and call Server_goodbye()
- signal(SIGINT, Server_goodbye);
+ (void) signal(SIGINT, Server_goodbye);
// Declare file descriptors
int listen_fd, accept_fd;
@@ -83,10 +101,13 @@ void Server_start(DB *db, int port)
goto error;
}
+ debug("Server listening at port %i...", port);
+
listen(listen_fd, 5);
int rc = 0;
+ again:
while(1) {
sin_size = sizeof(struct sockaddr_in);
@@ -100,21 +121,28 @@ void Server_start(DB *db, int port)
debug("Incoming connection from %s", inet_ntoa(client.sin_addr));
- // Receive the probe
+ // Receive the message
char buf[256];
- received = recv(accept_fd, buf, 255, 0);
- debug("Message received: %s", buf);
- if (received == 1) {
- rc = Server_execute(db, bfromcstr(buf));
+ while((received = recv(accept_fd, buf, 255, 0))) {
+ Object *result = Object_allocate();
+ rc = Server_execute(db, chomp(bfromcstr(buf)), result);
if(rc == 0) {
- send(accept_fd, "OK", 3, 0);
- } else {
- send(accept_fd, "ERROR", 6, 0);
+ if (result->type != tNil) {
+ bstring ret = Object_to_string(result);
+ send(accept_fd, bdata(ret), blength(ret), 0);
+ send(accept_fd, "\n", 2, 0);
+ } else {
+ send(accept_fd, "OK\n", 4, 0);
+ }
+ } else if (rc == 1) {
+ send(accept_fd, "ERROR\n", 7, 0);
+ } else if (rc == -1) {
+ send(accept_fd, "Bye!\n", 6, 0);
+ // Close the connection
+ close(accept_fd);
+ goto again;
}
- }
-
- // Close the connection
- close(accept_fd);
+ };
}
error:
View
2  src/shitdb/server.h
@@ -7,6 +7,6 @@
int Command_arity(bstring cmd);
void Server_goodbye();
void Server_start(DB *db, int port);
-int Server_execute(DB *db, bstring command);
+int Server_execute(DB *db, bstring command, Object *result);
#endif
Please sign in to comment.
Something went wrong with that request. Please try again.