#include "os_base.h"
#include <signal.h>
#ifdef HAVE_GLIB
#include <glib.h>
static GMainLoop *glib_loop;
#endif
#define ONE_MEG 1048576
static int32_t
{
#if 0
if (uid == 0 && gid == 0) {
qb_log(LOG_INFO,
"Authenticated connection");
return 1;
}
qb_log(LOG_NOTICE,
"BAD user!");
return 0;
#else
return 0;
#endif
}
static void
{
qb_log(LOG_INFO,
"Connection created (active:%d, closed:%d)",
srv_stats.active_connections, srv_stats.closed_connections);
}
static void
{
qb_log(LOG_INFO,
"Connection about to be freed");
}
static int32_t
{
"Connection to pid:%d destroyed (active:%d, closed:%d)",
stats.client_pid, srv_stats.active_connections,
srv_stats.closed_connections);
qb_log(LOG_DEBUG,
" Requests %"PRIu64
"", stats.requests);
qb_log(LOG_DEBUG,
" Responses %"PRIu64
"", stats.responses);
qb_log(LOG_DEBUG,
" Events %"PRIu64
"", stats.events);
qb_log(LOG_DEBUG,
" Send retries %"PRIu64
"", stats.send_retries);
qb_log(LOG_DEBUG,
" Recv retries %"PRIu64
"", stats.recv_retries);
qb_log(LOG_DEBUG,
" FC state %d", stats.flow_control_state);
qb_log(LOG_DEBUG,
" FC count %"PRIu64
"", stats.flow_control_count);
return 0;
}
struct my_req {
char message[256];
};
static int32_t
{
struct my_req *req_pt;
ssize_t res;
struct iovec iov[2];
char resp[100];
int32_t sl;
return 0;
}
req_pt = (struct my_req *)data;
qb_log(LOG_DEBUG,
"msg received (id:%d, size:%d, data:%s)",
req_pt->hdr.id, req_pt->hdr.size, req_pt->message);
if (strcmp(req_pt->message, "kill") == 0) {
exit(0);
}
response.id = 13;
response.error = 0;
sl = snprintf(resp, 100, "ACK %zd bytes", size) + 1;
iov[0].iov_len = sizeof(response);
iov[0].iov_base = &response;
iov[1].iov_len = sl;
iov[1].iov_base = resp;
response.size += sl;
send_ten_events = (strcmp(req_pt->message, "events") == 0);
if (use_events && !send_ten_events) {
} else {
}
if (res < 0) {
errno = - res;
}
if (send_ten_events) {
int32_t i;
qb_log(LOG_INFO,
"request to send 10 events");
for (i = 0; i < 10; i++) {
qb_log(LOG_INFO,
"sent event %d res:%d", i, res);
}
}
return 0;
}
static void
sigusr1_handler(int32_t num)
{
qb_log(LOG_DEBUG,
"(%d)", num);
exit(0);
}
static void
show_usage(const char *name)
{
printf("usage: \n");
printf("%s <options>\n", name);
printf("\n");
printf(" options:\n");
printf("\n");
printf(" -h show this help text\n");
printf(" -m use shared memory\n");
printf(" -u use unix sockets\n");
printf(" -g use glib mainloop\n");
printf(" -e use events\n");
printf("\n");
}
#ifdef HAVE_GLIB
struct gio_to_qb_poll {
int32_t is_used;
int32_t events;
int32_t source;
int32_t fd;
void *data;
};
static gboolean
gio_read_socket(GIOChannel * gio, GIOCondition condition, gpointer data)
{
struct gio_to_qb_poll *adaptor = (struct gio_to_qb_poll *)data;
gint fd = g_io_channel_unix_get_fd(gio);
return (adaptor->fn(fd, condition, adaptor->data) == 0);
}
static void
gio_poll_destroy(gpointer data)
{
struct gio_to_qb_poll *adaptor = (struct gio_to_qb_poll *)data;
adaptor->is_used--;
if (adaptor->is_used == 0) {
qb_log(LOG_DEBUG,
"fd %d adaptor destroyed\n", adaptor->fd);
adaptor->fd = 0;
adaptor->source = 0;
}
}
static int32_t
{
struct gio_to_qb_poll *adaptor;
GIOChannel *channel;
int32_t res = 0;
if (res < 0) {
return res;
}
if (adaptor->is_used && adaptor->source) {
if (is_new) {
return -EEXIST;
}
g_source_remove(adaptor->source);
adaptor->source = 0;
}
channel = g_io_channel_unix_new(fd);
if (!channel) {
return -ENOMEM;
}
adaptor->fn = fn;
adaptor->events = evts;
adaptor->data = data;
adaptor->p = p;
adaptor->is_used++;
adaptor->fd = fd;
adaptor->source = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, evts, gio_read_socket, adaptor, gio_poll_destroy);
g_io_channel_unref(channel);
return 0;
}
static int32_t
{
return my_g_dispatch_update(p, fd, evts, data, fn, TRUE);
}
static int32_t
{
return my_g_dispatch_update(p, fd, evts, data, fn, FALSE);
}
static int32_t
my_g_dispatch_del(int32_t fd)
{
struct gio_to_qb_poll *adaptor;
g_source_remove(adaptor->source);
adaptor->source = 0;
}
return 0;
}
#endif
static int32_t
{
}
static int32_t
{
}
static int32_t
{
}
static int32_t
my_dispatch_del(int32_t fd)
{
}
int32_t
main(int32_t argc, char *argv[])
{
const char *options = "mpseugh";
int32_t opt;
int32_t rc;
.connection_created = s1_connection_created_fn,
.msg_process = s1_msg_process_fn,
.connection_destroyed = s1_connection_destroyed_fn,
.connection_closed = s1_connection_closed_fn,
};
.dispatch_add = my_dispatch_add,
.dispatch_mod = my_dispatch_mod,
.dispatch_del = my_dispatch_del,
};
#ifdef HAVE_GLIB
.dispatch_add = my_g_dispatch_add,
.dispatch_mod = my_g_dispatch_mod,
.dispatch_del = my_g_dispatch_del,
};
#endif
while ((opt = getopt(argc, argv, options)) != -1) {
switch (opt) {
case 'm':
break;
case 'u':
break;
case 'g':
break;
case 'e':
break;
case 'h':
default:
show_usage(argv[0]);
exit(0);
break;
}
}
signal(SIGINT, sigusr1_handler);
if (s1 == 0) {
exit(1);
}
if (!use_glib) {
if (rc != 0) {
errno = -rc;
exit(1);
}
} else {
#ifdef HAVE_GLIB
glib_loop = g_main_loop_new(NULL, FALSE);
if (rc != 0) {
errno = -rc;
exit(1);
}
g_main_loop_run(glib_loop);
#else
"You don't seem to have glib-devel installed.\n");
#endif
}
return EXIT_SUCCESS;
}