Commit 3f28c014 authored by Jeroen Vreeken's avatar Jeroen Vreeken
Browse files

Merge branch 'ui' into corso2013

parents 417ce60c eff036e1
......@@ -28,6 +28,7 @@
#include <sys/stat.h>
#include <sys/param.h>
#include <fcntl.h>
#include <stdbool.h>
#include "tcp_listen.h"
#include "tcp_connect.h"
......@@ -39,17 +40,65 @@
char *logfile = "controller.log";
int rb;
char buffer[1000];
#define LOG_RING_SIZE 1000
#define LOG_ENTRY_SIZE 1000
#define LOG_ENTRY_LEN (LOG_ENTRY_SIZE-1)
static struct log_entry {
bool used;
char buffer[LOG_ENTRY_SIZE];
size_t buflen;
} log_ring[LOG_RING_SIZE];
static int ring_wr;
static int ring_rd;
static int entries;
static struct log_entry *log_entry_alloc(void)
{
int i;
i = ring_wr;
ring_wr++;
if (ring_wr >= LOG_RING_SIZE)
ring_wr = 0;
if (!log_ring[i].used)
entries++;
log_ring[i].used = true;
return log_ring + i;
}
static struct log_entry *log_entry_pull(void)
{
struct log_entry *entry;
if (log_ring[ring_rd].used) {
entry = log_ring + ring_rd;
entry->used = false;
ring_rd++;
if (ring_rd >= LOG_RING_SIZE)
ring_rd = 0;
} else {
entry = NULL;
}
return entry;
}
int handle_cmd(char *command)
{
strncpy(buffer, command, 999);
buffer[999] = 0;
buffer[strlen(buffer)] = 0;
rb = strlen(buffer);
struct log_entry *entry;
entry = log_entry_alloc();
printf("Handling command rb = %d\n", rb);
strncpy(entry->buffer, command, LOG_ENTRY_LEN);
entry->buffer[LOG_ENTRY_LEN] = 0;
entry->buffer[strlen(entry->buffer)] = 0;
entry->buflen = strlen(entry->buffer);
printf("Handling command buflen = %zd\n", entry->buflen);
return 0;
}
......@@ -91,7 +140,6 @@ int main (int argc, char **argv)
signal(SIGPIPE, SIG_IGN);
ioctl(fd_listen, FIONBIO, &(int){ 1 });
ioctl(fd_listen, FIONBIO, &(int){ 1 });
fd_log = tcp_connect(dt_host_controller(), CTRL_LOG_PORT);
......@@ -102,6 +150,7 @@ int main (int argc, char **argv)
fd_set fd_rd;
struct timeval timeout;
int high = 0;
struct log_entry *entry;
timeout.tv_usec = 0;
timeout.tv_sec = 1;
......@@ -117,43 +166,45 @@ int main (int argc, char **argv)
select(high + 1, &fd_rd, NULL, NULL, &timeout);
rb = -1;
if (fd_log < 0) {
printf("Trying to connect to server.\n");
fd_log = tcp_connect(dt_host_controller(), CTRL_LOG_PORT);
if (fd_log >= 0) {
log_string(buffer, 999, LOG_T_INFO, "Established connection to controller");
buffer[strlen(buffer)+1] = 0;
buffer[strlen(buffer)] = '\n';
printf(buffer);
rb = strlen(buffer);
entry = log_entry_alloc();
log_string(entry->buffer, LOG_ENTRY_LEN, LOG_T_INFO,
"Established connection to controller");
entry->buffer[strlen(entry->buffer)+1] = 0;
entry->buffer[strlen(entry->buffer)] = '\n';
printf(entry->buffer);
entry->buflen = strlen(entry->buffer);
f_log = fdopen(fd_log, "r");
printf("fd_log: %d f_log %p\n", fd_log, f_log);
}
} else if (FD_ISSET(fd_log, &fd_rd)) {
char *s;
entry = log_entry_alloc();
printf("Going to wait for data\n");
s = fgets(buffer, sizeof(buffer), f_log);
if (!s)
rb = -1;
else
rb = strlen(buffer);
printf("rb: %d\n", rb);
s = fgets(entry->buffer, LOG_ENTRY_LEN, f_log);
if (s)
entry->buflen = strlen(entry->buffer);
printf("rb: %zd\n", entry->buflen);
if (rb < 0) {
if (!s) {
fclose(f_log);
fd_log = -1;
log_string(buffer, 999, LOG_T_ERROR, "Lost connection to controller");
buffer[strlen(buffer)+1] = 0;
buffer[strlen(buffer)] = '\n';
printf(buffer);
rb = strlen(buffer);
log_string(entry->buffer, LOG_ENTRY_LEN, LOG_T_ERROR,
"Lost connection to controller");
entry->buffer[strlen(entry->buffer)+1] = 0;
entry->buffer[strlen(entry->buffer)] = '\n';
printf(entry->buffer);
entry->buflen = strlen(entry->buffer);
}
}
command_server_fdset_handle(cmd_srv, &fd_rd);
......@@ -163,20 +214,11 @@ int main (int argc, char **argv)
O_WRONLY | O_APPEND | O_CREAT,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
}
if (fd_logfile >= 0 && rb > 0) {
int ret;
ret = write(fd_logfile, buffer, rb);
if (ret != 1) {
close(fd_logfile);
fd_logfile = -1;
}
}
fdnew = tcp_accept(fd_listen);
if (fdnew >= 0) {
char status[1000];
char status[LOG_ENTRY_SIZE];
int statlen;
nr_clients++;
......@@ -186,13 +228,13 @@ int main (int argc, char **argv)
printf("New client. (now at %d)\n", nr_clients);
if (fd_log >= 0) {
log_string(status, 999, LOG_T_INFO,
log_string(status, LOG_ENTRY_LEN, LOG_T_INFO,
"New connection to proxy, controller conection is established.");
status[strlen(status)+1] = 0;
status[strlen(status)] = '\n';
statlen = strlen(status);
} else {
log_string(status, 999, LOG_T_WARNING,
log_string(status, LOG_ENTRY_LEN, LOG_T_WARNING,
"New connection to proxy, no connection to controller.");
status[strlen(status)+1] = 0;
status[strlen(status)] = '\n';
......@@ -201,13 +243,24 @@ int main (int argc, char **argv)
write(fdnew, status, statlen);
}
if (rb > 0) {
while ((entry = log_entry_pull())) {
int i;
if (fd_logfile >= 0) {
int ret;
ret = write(fd_logfile, entry->buffer, entry->buflen);
if (ret != 1) {
close(fd_logfile);
fd_logfile = -1;
}
}
for (i = 0; i < nr_clients; i++) {
int ret;
ret = write(fd_clients[i], buffer, rb);
if (ret != rb) {
ret = write(fd_clients[i], entry->buffer, entry->buflen);
if (ret != entry->buflen) {
int j;
close(fd_clients[i]);
for (j = i + 1; j < nr_clients; j++) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment