Skip to content

Commit

Permalink
Adding simple persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
abedra committed Jan 5, 2012
1 parent fecdcdc commit 1cda940
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -4,7 +4,7 @@ server:
gcc -o server bungleo-server.c -lzmq -ltofu gcc -o server bungleo-server.c -lzmq -ltofu


client: client:
gcc -o client bungleo-client.c -lzmq -ljson gcc -o client bungleo-client.c -lzmq -ljson -lhiredis


clean: clean:
rm server rm server
Expand Down
18 changes: 16 additions & 2 deletions bungleo-client.c
Expand Up @@ -3,6 +3,7 @@
#include <string.h> #include <string.h>
#include <zmq.h> #include <zmq.h>
#include "json/json.h" #include "json/json.h"
#include "hiredis/hiredis.h"


static char * zrecv(void *socket) { static char * zrecv(void *socket) {
zmq_msg_t message; zmq_msg_t message;
Expand All @@ -18,12 +19,25 @@ static char * zrecv(void *socket) {
return(string); return(string);
} }


static int persist(redisContext *c, const char *key, const char *value) {
redisReply *reply = redisCommand(c, "SET %s %s", key, value);
int response = reply->type;
freeReplyObject(reply);
return(response);
}

int main(void) int main(void)
{ {
void *context = zmq_init(1); void *context = zmq_init(1);
void *receiver = zmq_socket(context, ZMQ_PULL); void *receiver = zmq_socket(context, ZMQ_PULL);
zmq_connect(receiver, "tcp://localhost:5555"); zmq_connect(receiver, "tcp://localhost:5555");


redisContext *c = redisConnect("127.0.0.1", 6379);
if(c->err) {
printf("Error: %s\n", c->errstr);
return 1;
}

const char *id, *message; const char *id, *message;
char *string; char *string;
struct json_object *obj; struct json_object *obj;
Expand All @@ -33,10 +47,10 @@ int main(void)
obj = json_tokener_parse(string); obj = json_tokener_parse(string);
id = json_object_get_string(json_object_object_get(obj, "id")); id = json_object_get_string(json_object_object_get(obj, "id"));
message = json_object_get_string(json_object_object_get(obj, "message")); message = json_object_get_string(json_object_object_get(obj, "message"));

persist(c, id, message);
printf("id is %s and message is %s\n", id, message);
} }


redisFree(c);
zmq_close(receiver); zmq_close(receiver);
zmq_term(context); zmq_term(context);
return 0; return 0;
Expand Down

0 comments on commit 1cda940

Please sign in to comment.