2 * 2008+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <sys/types.h>
18 #include <sys/socket.h>
20 #include <sys/syscall.h>
31 #include <netinet/in.h>
33 #include "elliptics/packet.h"
34 #include "elliptics/interface.h"
39 #define __unused __attribute__ ((unused))
42 static struct dnet_log notify_logger
;
44 static int notify_complete(struct dnet_net_state
*state
,
48 struct dnet_io_notification
*io
;
54 if (is_trans_destroyed(state
, cmd
))
57 if (cmd
->size
!= sizeof(struct dnet_io_notification
))
60 gettimeofday(&tv
, NULL
);
61 localtime_r((time_t *)&tv
.tv_sec
, &tm
);
62 strftime(str
, sizeof(str
), "%F %R:%S", &tm
);
64 fprintf(stream
, "%s.%06lu : ", str
, (unsigned long)tv
.tv_usec
);
66 io
= (struct dnet_io_notification
*)(cmd
+ 1);
68 dnet_convert_io_notification(io
);
70 fprintf(stream
, "%s: client: %s, size: %llu, offset: %llu, flags: %x\n",
71 dnet_dump_id_str(io
->io
.id
), dnet_server_convert_dnet_addr(&io
->addr
.addr
),
72 (unsigned long long)io
->io
.size
,
73 (unsigned long long)io
->io
.offset
, io
->io
.flags
);
79 static void notify_usage(char *p
)
81 fprintf(stderr
, "Usage: %s\n"
82 " -a addr:port:family - creates a node with given network address\n"
83 " -r addr:port:family - adds a route to the given node\n"
84 " -l log - log file. Default: disabled\n"
85 " -L log - notifications log. Default: stdout\n"
86 " -w timeout - wait timeout in seconds used to wait for content sync.\n"
87 " -m level - log level\n"
88 " -g group_id - group ID to connect\n"
89 " -I id - request notifications for given ID\n"
93 int main(int argc
, char *argv
[])
95 int ch
, err
, have_remote
= 0, i
;
96 struct dnet_node
*n
= NULL
;
97 struct dnet_session
*s
= NULL
;
98 struct dnet_config cfg
, rem
;
99 int max_id_idx
= 1000, id_idx
= 0, group_id
= 0;
100 unsigned char id
[max_id_idx
][DNET_ID_SIZE
];
101 char *logfile
= "/dev/stderr", *notify_file
= "/dev/stdout";
102 FILE *log
= NULL
, *notify
;
104 memset(&cfg
, 0, sizeof(struct dnet_config
));
106 cfg
.sock_type
= SOCK_STREAM
;
107 cfg
.proto
= IPPROTO_TCP
;
108 cfg
.wait_timeout
= 60*60;
109 notify_logger
.log_level
= DNET_LOG_INFO
;
111 memcpy(&rem
, &cfg
, sizeof(struct dnet_config
));
113 while ((ch
= getopt(argc
, argv
, "g:m:w:l:I:a:r:h")) != -1) {
116 notify_logger
.log_level
= strtoul(optarg
, NULL
, 0);
119 cfg
.wait_timeout
= atoi(optarg
);
122 notify_file
= optarg
;
128 if (id_idx
< max_id_idx
) {
129 err
= dnet_parse_numeric_id(optarg
, id
[id_idx
]);
136 group_id
= atoi(optarg
);
139 err
= dnet_parse_addr(optarg
, &cfg
);
144 err
= dnet_parse_addr(optarg
, &rem
);
151 notify_usage(argv
[0]);
157 fprintf(stderr
, "No ID specified to watch.\n");
162 fprintf(stderr
, "No remote node specified to route requests.\n");
166 log
= fopen(logfile
, "a");
169 fprintf(stderr
, "Failed to open log file %s: %s.\n", logfile
, strerror(errno
));
173 notify_logger
.log_private
= log
;
174 notify_logger
.log
= dnet_common_log
;
175 cfg
.log
= ¬ify_logger
;
177 notify
= fopen(notify_file
, "a");
180 fprintf(stderr
, "Failed to open notify file %s: %s.\n", notify_file
, strerror(errno
));
184 n
= dnet_node_create(&cfg
);
188 s
= dnet_session_create(n
);
192 err
= dnet_add_state(n
, &rem
);
196 for (i
=0; i
<id_idx
; ++i
) {
198 dnet_setup_id(&raw
, group_id
, id
[i
]);
199 err
= dnet_request_notification(s
, &raw
, notify_complete
, notify
);