cache_test.c
changeset 37 f0188b445c84
parent 36 b4023990811e
--- a/cache_test.c	Sat Aug 09 14:42:59 2008 +0300
+++ b/cache_test.c	Sat Aug 09 20:11:59 2008 +0300
@@ -2,6 +2,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
+#include <assert.h>
 
 #include "cache.h"
 #include "cache_engines.h"
@@ -82,8 +83,10 @@
 void cmd_req (int index, char *key_buf);
 void cmd_status (int index, char *key);
 void cmd_available (int index, char *unused);
+void cmd_read (int index, char *unused);
 void cmd_write (int index, char *unused);
 void cmd_push (int index, char *data);
+void cmd_pull (int index, char *unused);
 void cmd_done (int index, char *unused);
 void cmd_release (int index, char *unused);
 
@@ -98,8 +101,10 @@
     {   "req",      &cmd_req,       "req <req_id> <key>"        },
     {   "status",   &cmd_status,    "status <req_id>"           },
     {   "available",&cmd_available, "available <req_id>"        },
+    {   "read",     &cmd_read,      "read <req_id>"             },
     {   "write",    &cmd_write,     "write <req_id> [<size_hint>]"  },
     {   "push",     &cmd_push,      "push <req_id> <data>"      },
+    {   "pull",     &cmd_pull,      "pull <req_id>"             },
     {   "done",     &cmd_done,      "done <req_id>"             },
     {   "release",  &cmd_release,   "release <req_id>"          },
     {   NULL,       NULL,           NULL                        }
@@ -205,6 +210,24 @@
 
 }
 
+void cmd_read (int index, char *unused) {
+    const struct cache_key *key;
+
+    if (index < 0 || index >= REQ_COUNT)
+        ERROR("index is out of range");
+
+    key = cache_req_key(req_list[index].req);
+
+    INFO("Request %d (%*s): beginning read", 
+        index, (int) key->length, key->buf
+    );
+
+    if (cache_req_begin_read(req_list[index].req))
+        ERROR("cache_req_begin_read failed");
+        
+error:
+    return;
+}
 
 void cmd_write (int index, char *hint_str) {
     size_t hint;
@@ -264,6 +287,42 @@
     return;
 }
 
+void cmd_pull (int index, char *unused) {
+    size_t data_length, length;
+    const struct cache_key *key;
+
+    char buf[LINE_LENGTH];
+    
+    // unknown size
+    length = 0;
+    
+    key = cache_req_key(req_list[index].req);
+
+    if (cache_req_pull(req_list[index].req, req_list[index].pipe_write, &length))
+        ERROR("cache_req_pull failed");
+    
+    assert(length < LINE_LENGTH);
+
+    // read from the pipe
+    if ((data_length = read(req_list[index].pipe_read, buf, length)) == -1)
+        PERROR("read");
+    
+    // terminate
+    buf[data_length] = '\0';
+
+    if (length != data_length)
+        PWARNING("Only %zu/%zu bytes read from pipe!", data_length, length);
+    
+    INFO("Request %d (%*s): pulled %zu/%zu bytes: %s", 
+        index, (int) key->length, key->buf,
+        data_length, length,
+        buf
+    );
+
+error:
+    return;
+}
+
 void cmd_done (int index, char *unused) {
     const struct cache_key *key;