root/src/jlog/jtest.c

Revision 647ef348e1bcdee83786760f7616926b52a6bac1, 4.8 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 9 months ago)

Various code cleanliness fixes.

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2005-2008, Message Systems, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *    * Redistributions of source code must retain the above copyright
10  *      notice, this list of conditions and the following disclaimer.
11  *    * Redistributions in binary form must reproduce the above
12  *      copyright notice, this list of conditions and the following
13  *      disclaimer in the documentation and/or other materials provided
14  *      with the distribution.
15  *    * Neither the name Message Systems, Inc. nor the names
16  *      of its contributors may be used to endorse or promote products
17  *      derived from this software without specific prior written
18  *      permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <stdio.h>
34 #include "jlog.h"
35
36 #define SUBSCRIBER "voyeur"
37 #define LOGNAME    "/tmp/jtest.foo"
38 jlog_ctx *ctx;
39
40 void usage() {
41   fprintf(stderr, "options:\n\tinit\n\tread <count>\n\twrite <len> <count>\n");
42 }
43 void jcreate() {
44   ctx = jlog_new(LOGNAME);
45   jlog_ctx_alter_journal_size(ctx, 1024);
46   if(jlog_ctx_init(ctx) != 0) {
47     fprintf(stderr, "jlog_ctx_init failed: %d %s\n", jlog_ctx_err(ctx), jlog_ctx_err_string(ctx));
48   } else {
49     jlog_ctx_add_subscriber(ctx, SUBSCRIBER, JLOG_BEGIN);
50   }
51   jlog_ctx_close(ctx);
52 }
53
54 void jopenw(char *foo, int count) {
55   int i;
56   ctx = jlog_new(LOGNAME);
57   if(jlog_ctx_open_writer(ctx) != 0) {
58     fprintf(stderr, "jlog_ctx_open_writer failed: %d %s\n", jlog_ctx_err(ctx), jlog_ctx_err_string(ctx));
59     exit(-1);
60   }
61   for(i=0; i<count; i++)
62     if(jlog_ctx_write(ctx, foo, strlen(foo)) != 0)
63       fprintf(stderr, "jlog_ctx_write_message failed: %d %s\n", jlog_ctx_err(ctx), jlog_ctx_err_string(ctx));
64
65   jlog_ctx_close(ctx);
66 }
67 void jopenr(char *s, int expect) {
68   char begins[20], ends[20];
69   jlog_id begin, end;
70   int count;
71   jlog_message message;
72
73   ctx = jlog_new(LOGNAME);
74   if(jlog_ctx_open_reader(ctx, s) != 0) {
75     fprintf(stderr, "jlog_ctx_open_reader failed: %d %s\n", jlog_ctx_err(ctx), jlog_ctx_err_string(ctx));
76     exit(-1);
77   }
78   while(expect > 0) {
79     if((count = jlog_ctx_read_interval(ctx, &begin, &end)) == -1) {
80       fprintf(stderr, "jlog_ctx_read_interval failed: %d %s\n", jlog_ctx_err(ctx), jlog_ctx_err_string(ctx));
81       exit(-1);
82     }
83     jlog_snprint_logid(begins, sizeof(begins), &begin);
84     jlog_snprint_logid(ends, sizeof(ends), &end);
85     /* printf("reader [%s]  (%s, %s] count: %d\n", s, begins, ends, count); */
86     if(count > 0) {
87       int i;
88       count = MIN(count, expect);
89       for(i=0; i<count; i++, JLOG_ID_ADVANCE(&begin)) {
90         end = begin;
91         if(jlog_ctx_read_message(ctx, &begin, &message) != 0) {
92           fprintf(stderr, "read failed: %d\n", jlog_ctx_err(ctx));
93         } else {
94           expect--;
95           jlog_snprint_logid(begins, sizeof(begins), &begin);
96           fprintf(stderr, "[%7d] read: [%s]\n\t'%.*s'\n", expect, begins,
97                   message.mess_len, (char *)message.mess);
98         }
99       }
100       if(jlog_ctx_read_checkpoint(ctx, &end) != 0) {
101         fprintf(stderr, "checkpoint failed: %d %s\n", jlog_ctx_err(ctx), jlog_ctx_err_string(ctx));
102       } else {
103         fprintf(stderr, "\tcheckpointed...\n");
104       }
105     }
106   }
107   jlog_ctx_close(ctx);
108 }
109 int main(int argc, char **argv) {
110   int i;
111 #if _WIN32
112   mem_init();
113 #endif
114   for(i=1; i<argc; i++) {
115     if(!strcmp(argv[i], "init")) {
116       jcreate();
117       exit(0);
118     } else if(!strcmp(argv[i], "write")) {
119       int len;
120       char *message;
121       if(i+2 >= argc) { usage(); exit(-1); }
122       i++;
123       len = atoi(argv[i]);
124       message = malloc(len+1);
125       memset(message, 'X', len-1);
126       message[len-1] = '\n';
127       message[len] = '\0';
128       i++;
129       jopenw(message, atoi(argv[i]));
130       exit(0);
131     } else if(!strcmp(argv[i], "read")) {
132       if(i+1 >= argc) { usage(); exit(-1); }
133       i++;
134       jopenr(SUBSCRIBER, atoi(argv[i]));
135       exit(0);
136     } else {
137       fprintf(stderr, "command '%s' not understood\n", argv[i]);
138       usage();
139     }
140   }
141   return 0;
142 }
Note: See TracBrowser for help on using the browser.