struct timeval clock_diff_barrier;
/** Number of the audio format as announced by para_server. */
int current_audio_format_num;
+ /* The status task btrn is the child of the client task. */
+ struct btr_node *btrn;
};
/** The array of status items sent by para_server. */
{
if (!stat_task->ct)
return;
+ btr_free_node(stat_task->ct->btrn);
client_close(stat_task->ct);
stat_task->ct = NULL;
clear_and_dump_items();
sched_min_delay(s);
}
-/* restart the client task if necessary */
static void status_pre_select(struct sched *s, struct task *t)
+{
+ struct status_task *st = container_of(t, struct status_task, task);
+ int ret;
+
+ ret = btr_node_status(st->btrn, 0, BTR_NT_LEAF);
+ sched_request_barrier(&st->restart_barrier, s);
+}
+
+/* restart the client task if necessary */
+static void status_post_select(struct sched *s, struct task *t)
{
struct status_task *st = container_of(t, struct status_task, task);
if (!st->ct)
goto out;
if (st->ct->task.error >= 0) {
- st->ct->task.error = -E_AUDIOD_OFF;
+ kill_btrn(st->ct->btrn, &st->ct->task, -E_AUDIOD_OFF);
goto out;
}
if (st->ct->task.error != -E_TASK_UNREGISTERED)
goto out;
}
if (st->ct) {
+ char *buf;
+ size_t sz;
int ret;
if (st->ct->task.error < 0) {
if (st->ct->task.error != -E_TASK_UNREGISTERED)
}
if (st->ct->status != CL_RECEIVING)
goto out;
- ret = for_each_stat_item(st->ct->buf, st->ct->loaded,
- update_item);
+ ret = btr_node_status(st->btrn, 0, BTR_NT_LEAF);
+ if (ret <= 0)
+ goto out;
+ sz = btr_next_buffer(st->btrn, &buf);
+ ret = for_each_stat_item(buf, sz, update_item);
if (ret < 0) {
- st->ct->task.error = ret;
+ kill_btrn(st->ct->btrn, &st->ct->task, ret);
goto out;
}
- if (st->ct->loaded != ret) {
+ if (sz != ret)
st->last_status_read = *now;
- st->ct->loaded = ret;
- } else {
+ else {
struct timeval diff;
tv_diff(now, &st->last_status_read, &diff);
if (diff.tv_sec > 61)
- st->ct->task.error = -E_STATUS_TIMEOUT;
+ kill_btrn(st->ct->btrn, &st->ct->task,
+ -E_STATUS_TIMEOUT);
}
+ btr_consume(st->btrn, sz - ret);
goto out;
}
if (tv_diff(now, &st->restart_barrier, NULL) < 0)
int argc = 5;
PARA_INFO_LOG("clock diff count: %d\n", st->clock_diff_count);
st->clock_diff_count--;
- client_open(argc, argv, &st->ct, NULL);
+ client_open(argc, argv, &st->ct, NULL, NULL, st->btrn);
set_stat_task_restart_barrier(2);
sched_min_delay(s);
} else {
char *argv[] = {"audiod", "--", "stat", "-p", NULL};
int argc = 4;
- client_open(argc, argv, &st->ct, NULL);
+ client_open(argc, argv, &st->ct, NULL, NULL, st->btrn);
set_stat_task_restart_barrier(5);
sched_min_delay(s);
}
{
memset(st, 0, sizeof(struct status_task));
st->task.pre_select = status_pre_select;
+ st->task.post_select = status_post_select;
st->sa_time_diff_sign = 1;
st->clock_diff_count = conf.clock_diff_count_arg;
st->current_audio_format_num = -1;
- sprintf(st->task.status, "status task");
+ sprintf(st->task.status, "stat");
+ st->btrn = btr_new_node(&(struct btr_node_description)
+ EMBRACE(.name = "stat"));
}
static void set_initial_status(void)
#include "stdin.h"
#include "stdout.h"
#include "client.h"
+#include "buffer_tree.h"
#include "error.h"
INIT_CLIENT_ERRLISTS;
}
if (ct->status == CL_SENDING) {
stdin_set_defaults(&sit);
- sit.buf = para_malloc(sit.bufsize),
register_task(&sit.task);
- ct->inbuf = sit.buf;
- ct->in_loaded = &sit.loaded;
- ct->in_error = &sit.task.error;
t->error = -E_TASK_STARTED;
return;
}
if (ct->status == CL_RECEIVING) {
stdout_set_defaults(&sot);
- sot.bufp = &ct->buf;
- sot.loaded = &ct->loaded;
- sot.input_error = &ct->task.error;
register_task(&sot.task);
t->error = -E_TASK_STARTED;
return;
.status = "supervisor task"
};
-static int client_loglevel; /* loglevel */
+static int client_loglevel = LL_ERROR; /* loglevel */
INIT_STDERR_LOGGING(client_loglevel);
-
/**
* The client program to connect to para_server.
*
init_random_seed_or_die();
s.default_timeout.tv_sec = 1;
s.default_timeout.tv_usec = 0;
- ret = client_open(argc, argv, &ct, &client_loglevel);
- if (ret < 0) /* can not use PARA_LOG here because ct is NULL */
- exit(EXIT_FAILURE);
+ /*
+ * We add buffer tree nodes for stdin and stdout even though
+ * only one of them will be needed. This simplifies the code
+ * a bit wrt. to the buffer tree setup.
+ */
+ sit.btrn = btr_new_node(&(struct btr_node_description)
+ EMBRACE(.name = "stdin"));
+ ret = client_open(argc, argv, &ct, &client_loglevel, sit.btrn, NULL);
+ if (ret < 0)
+ goto out;
+ sot.btrn = btr_new_node(&(struct btr_node_description)
+ EMBRACE(.name = "stdout", .parent = ct->btrn));
register_task(&svt);
ret = schedule(&s);
- if (ret < 0)
- PARA_ERROR_LOG("%s\n", para_strerror(-ret));
+out:
client_close(ct);
- return ret >= 0? EXIT_SUCCESS: EXIT_FAILURE;
+ btr_free_node(sit.btrn);
+ btr_free_node(sot.btrn);
+ if (ret < 0) {
+ /* can not use PARA_LOG here because ct is NULL */
+ fprintf(stderr, "%s\n", para_strerror(-ret));
+ return EXIT_FAILURE;
+ }
+ return EXIT_SUCCESS;
}
#include "client.cmdline.h"
#include "client.h"
#include "hash.h"
+#include "buffer_tree.h"
+
+/** The size of the receiving buffer. */
+#define CLIENT_BUFSIZE 4000
/**
* Close the connection to para_server and free all resources.
return;
if (ct->rc4c.fd >= 0)
close(ct->rc4c.fd);
- free(ct->buf);
free(ct->user);
free(ct->config_file);
free(ct->key_file);
*/
static void client_pre_select(struct sched *s, struct task *t)
{
+ int ret;
struct client_task *ct = container_of(t, struct client_task, task);
+ struct btr_node *btrn = ct->btrn;
- ct->check_r = 0;
- ct->check_w = 0;
if (ct->rc4c.fd < 0)
return;
switch (ct->status) {
case CL_SENT_CH_RESPONSE:
case CL_SENT_COMMAND:
para_fd_set(ct->rc4c.fd, &s->rfds, &s->max_fileno);
- ct->check_r = 1;
return;
case CL_RECEIVED_WELCOME:
- case CL_RECEIVED_CHALLENGE:
case CL_RECEIVED_PROCEED:
para_fd_set(ct->rc4c.fd, &s->wfds, &s->max_fileno);
- ct->check_w = 1;
return;
case CL_RECEIVING:
- if (ct->loaded < CLIENT_BUFSIZE - 1) {
- para_fd_set(ct->rc4c.fd, &s->rfds, &s->max_fileno);
- ct->check_r = 1;
+ ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
+ if (ret != 0) {
+ if (ret < 0)
+ sched_min_delay(s);
+ else
+ para_fd_set(ct->rc4c.fd, &s->rfds,
+ &s->max_fileno);
}
return;
case CL_SENDING:
- if (!ct->in_loaded) /* stdin task not yet started */
- return;
- if (*ct->in_loaded) {
- PARA_INFO_LOG("loaded: %zd\n", *ct->in_loaded);
- para_fd_set(ct->rc4c.fd, &s->wfds, &s->max_fileno);
- ct->check_w = 1;
- } else {
- if (*ct->in_error) {
- t->error = *ct->in_error;
- s->timeout.tv_sec = 0;
- s->timeout.tv_usec = 1;
- }
+ ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
+ if (ret != 0) {
+ if (ret < 0)
+ sched_min_delay(s);
+ else
+ para_fd_set(ct->rc4c.fd, &s->wfds,
+ &s->max_fileno);
}
return;
}
}
-static ssize_t client_recv_buffer(struct client_task *ct)
+static ssize_t client_recv_buffer(struct client_task *ct, char *buf, size_t len)
{
ssize_t ret;
if (ct->status < CL_SENT_CH_RESPONSE)
- ret = recv_buffer(ct->rc4c.fd, ct->buf + ct->loaded,
- CLIENT_BUFSIZE - ct->loaded);
+ ret = recv_buffer(ct->rc4c.fd, buf, len);
else
- ret = rc4_recv_buffer(&ct->rc4c, ct->buf + ct->loaded,
- CLIENT_BUFSIZE - ct->loaded);
- if (!ret)
+ ret = rc4_recv_buffer(&ct->rc4c, buf, len);
+ if (ret == 0)
return -E_SERVER_EOF;
- if (ret > 0)
- ct->loaded += ret;
return ret;
}
static void client_post_select(struct sched *s, struct task *t)
{
struct client_task *ct = container_of(t, struct client_task, task);
+ struct btr_node *btrn = ct->btrn;
+ int ret = 0;
+ char buf[CLIENT_BUFSIZE];
t->error = 0;
if (ct->rc4c.fd < 0)
return;
- if (!ct->check_r && !ct->check_w)
- return;
- if (ct->check_r && !FD_ISSET(ct->rc4c.fd, &s->rfds))
- return;
- if (ct->check_w && !FD_ISSET(ct->rc4c.fd, &s->wfds))
- return;
switch (ct->status) {
case CL_CONNECTED: /* receive welcome message */
- t->error = client_recv_buffer(ct);
- if (t->error < 0)
+ if (!FD_ISSET(ct->rc4c.fd, &s->rfds))
+ return;
+ ret = client_recv_buffer(ct, buf, sizeof(buf));
+ if (ret < 0)
goto err;
ct->status = CL_RECEIVED_WELCOME;
return;
case CL_RECEIVED_WELCOME: /* send auth command */
- sprintf(ct->buf, AUTH_REQUEST_MSG "%s", ct->user);
- PARA_INFO_LOG("--> %s\n", ct->buf);
- t->error = send_buffer(ct->rc4c.fd, ct->buf);
- if (t->error < 0)
+ sprintf(buf, AUTH_REQUEST_MSG "%s", ct->user);
+ PARA_INFO_LOG("--> %s\n", buf);
+ if (!FD_ISSET(ct->rc4c.fd, &s->wfds))
+ return;
+ ret = send_buffer(ct->rc4c.fd, buf);
+ if (ret < 0)
goto err;
ct->status = CL_SENT_AUTH;
return;
- case CL_SENT_AUTH: /* receive challenge and rc4 keys */
- ct->loaded = 0;
- t->error = client_recv_buffer(ct);
- if (t->error < 0)
- goto err;
- ct->loaded = t->error;
- PARA_INFO_LOG("<-- [challenge] (%zu bytes)\n", ct->loaded);
- ct->status = CL_RECEIVED_CHALLENGE;
- return;
- case CL_RECEIVED_CHALLENGE:
+ case CL_SENT_AUTH:
+ /*
+ * Receive challenge and rc4 keys, decrypt the challenge and
+ * send back the hash of the decrypted challenge.
+ */
{
/* decrypted challenge/rc4 buffer */
unsigned char crypt_buf[1024];
/* the SHA1 of the decrypted challenge */
unsigned char challenge_sha1[HASH_SIZE];
- t->error = para_decrypt_buffer(ct->key_file, crypt_buf,
- (unsigned char *)ct->buf, ct->loaded);
- if (t->error < 0)
+ if (!FD_ISSET(ct->rc4c.fd, &s->rfds))
+ return;
+ ret = client_recv_buffer(ct, buf, sizeof(buf));
+ if (ret < 0)
+ goto err;
+ PARA_INFO_LOG("<-- [challenge] (%zu bytes)\n", ret);
+ ret = para_decrypt_buffer(ct->key_file, crypt_buf,
+ (unsigned char *)buf, ret);
+ if (ret < 0)
goto err;
sha1_hash((char *)crypt_buf, CHALLENGE_SIZE, challenge_sha1);
RC4_set_key(&ct->rc4c.send_key, RC4_KEY_LEN,
crypt_buf + CHALLENGE_SIZE);
RC4_set_key(&ct->rc4c.recv_key, RC4_KEY_LEN,
crypt_buf + CHALLENGE_SIZE + RC4_KEY_LEN);
- hash_to_asc(challenge_sha1, ct->buf);
- PARA_INFO_LOG("--> %s\n", ct->buf);
- t->error = send_bin_buffer(ct->rc4c.fd, (char *)challenge_sha1,
+ hash_to_asc(challenge_sha1, buf);
+ PARA_INFO_LOG("--> %s\n", buf);
+ ret = send_bin_buffer(ct->rc4c.fd, (char *)challenge_sha1,
HASH_SIZE);
- if (t->error < 0)
+ if (ret < 0)
goto err;
ct->status = CL_SENT_CH_RESPONSE;
return;
case CL_SENT_CH_RESPONSE: /* read server response */
{
size_t bytes_received;
- ct->loaded = 0;
- t->error = client_recv_buffer(ct);
- if (t->error < 0)
+ if (!FD_ISSET(ct->rc4c.fd, &s->rfds))
+ return;
+ ret = client_recv_buffer(ct, buf, sizeof(buf));
+ if (ret < 0)
goto err;
- bytes_received = t->error;
+ bytes_received = ret;
/* check if server has sent "Proceed" message */
- t->error = -E_CLIENT_AUTH;
+ ret = -E_CLIENT_AUTH;
if (bytes_received < PROCEED_MSG_LEN)
goto err;
- if (!strstr(ct->buf, PROCEED_MSG))
+ if (!strstr(buf, PROCEED_MSG))
goto err;
ct->status = CL_RECEIVED_PROCEED;
- t->error = 0;
return;
}
case CL_RECEIVED_PROCEED: /* concat args and send command */
{
int i;
char *command = NULL;
+ if (!FD_ISSET(ct->rc4c.fd, &s->wfds))
+ return;
for (i = 0; i < ct->conf.inputs_num; i++) {
char *tmp = command;
command = make_message("%s\n%s", command?
}
command = para_strcat(command, EOC_MSG "\n");
PARA_DEBUG_LOG("--> %s\n", command);
- t->error = rc4_send_buffer(&ct->rc4c, command);
+ ret = rc4_send_buffer(&ct->rc4c, command);
free(command);
- if (t->error < 0)
+ if (ret < 0)
goto err;
ct->status = CL_SENT_COMMAND;
return;
}
case CL_SENT_COMMAND:
- ct->loaded = 0;
- t->error = client_recv_buffer(ct);
- if (t->error < 0)
+ {
+ char *buf2;
+ if (!FD_ISSET(ct->rc4c.fd, &s->rfds))
+ return;
+ /* can not use "buf" here because we need a malloced buffer */
+ buf2 = para_malloc(CLIENT_BUFSIZE);
+ ret = client_recv_buffer(ct, buf2, CLIENT_BUFSIZE);
+ if (ret < 0) {
+ free(buf2);
goto err;
- if (strstr(ct->buf, AWAITING_DATA_MSG))
+ }
+ if (strstr(buf2, AWAITING_DATA_MSG)) {
+ free(buf2);
ct->status = CL_SENDING;
- else
- ct->status = CL_RECEIVING;
+ return;
+ }
+ ct->status = CL_RECEIVING;
+ btr_add_output(buf2, ret, btrn);
return;
+ }
case CL_SENDING:
- PARA_INFO_LOG("loaded: %zd\n", *ct->in_loaded);
- t->error = rc4_send_bin_buffer(&ct->rc4c, ct->inbuf,
- *ct->in_loaded);
- if (t->error < 0)
+ {
+ char *buf2;
+ size_t sz;
+ ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
+ if (ret < 0)
goto err;
- *ct->in_loaded = 0;
+ if (ret == 0)
+ return;
+ if (!FD_ISSET(ct->rc4c.fd, &s->wfds))
+ return;
+ sz = btr_next_buffer(btrn, &buf2);
+ ret = rc4_send_bin_buffer(&ct->rc4c, buf2, sz);
+ if (ret < 0)
+ goto err;
+ btr_consume(btrn, sz);
return;
+ }
case CL_RECEIVING:
- t->error = client_recv_buffer(ct);
- if (t->error < 0)
+ {
+ char *buf2;
+ buf2 = para_malloc(CLIENT_BUFSIZE);
+ ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
+ if (ret < 0)
+ goto err;
+ if (ret == 0)
+ return;
+ if (!FD_ISSET(ct->rc4c.fd, &s->rfds))
+ return;
+ ret = client_recv_buffer(ct, buf2, CLIENT_BUFSIZE);
+ if (ret < 0) {
+ free(buf2);
goto err;
+ }
+ buf2 = para_realloc(buf2, ret);
+ btr_add_output(buf2, ret, btrn);
return;
+ }
}
err:
- if (t->error != -E_SERVER_EOF)
- PARA_ERROR_LOG("%s\n", para_strerror(-t->error));
+ t->error = ret;
+ if (ret < 0) {
+ if (ret != -E_SERVER_EOF && ret != -E_BTR_EOF)
+ PARA_ERROR_LOG("%s\n", para_strerror(-t->error));
+ btr_remove_node(btrn);
+ }
}
/* connect to para_server and register the client task */
* \return Standard.
*/
int client_open(int argc, char *argv[], struct client_task **ct_ptr,
- int *loglevel)
+ int *loglevel, struct btr_node *parent, struct btr_node *child)
{
char *home = para_homedir();
int ret;
struct client_task *ct = para_calloc(sizeof(struct client_task));
- ct->buf = para_malloc(CLIENT_BUFSIZE);
+ ct->btrn = btr_new_node(&(struct btr_node_description)
+ EMBRACE(.name = "client", .parent = parent, .child = child));
*ct_ptr = ct;
ct->rc4c.fd = -1;
ret = -E_CLIENT_SYNTAX;
free(home);
if (ret < 0) {
PARA_ERROR_LOG("%s\n", para_strerror(-ret));
+ btr_free_node(ct->btrn);
client_close(ct);
*ct_ptr = NULL;
}