Progressive refinement

r1199 | dcl | 2006-10-26 18:38:34 +1000 (Thu, 26 Oct 2006) | 2 lines
This commit is contained in:
Douglas Clowes
2006-10-26 18:38:34 +10:00
parent 7dfa37057b
commit 4623b25ccd
8 changed files with 261 additions and 172 deletions

View File

@@ -1,7 +1,6 @@
#include "sock.h"
#include "utility.h"
#include "display.h"
#include "dio.h"
#include <unistd.h>
#include <stdlib.h>
@@ -17,7 +16,7 @@
#include <netinet/ip.h> /* superset of previous */
#define LINE_LEN 1024
#define MAX_SOCK 50
#define MAX_SOCK 200
/**
* Mode of the socket
@@ -65,6 +64,9 @@ typedef struct terminal_t
char url[LINE_LEN];
/** value from Content-Length header */
int content_length;
/** associated device */
int(*command)(void* device, const char* cmd);
void* device;
} TERMINAL, *pTERMINAL;
/**
@@ -89,20 +91,25 @@ static int sock_l;
*
* \param addr the TCP/IP port number on which to listen
*/
void sock_init(int addr)
void sock_init(void)
{
dprintf(0, "sock_init\n");
int status;
long flags;
int i;
int one = 1;
struct sockaddr_in my_addr;
dbg_printf(0, "sock_init\n");
memset(fdv, 0, sizeof(fdv));
for (i = 0; i < MAX_SOCK; ++i)
{
fdv[i].fd = -1;
}
memset(fds, 0, sizeof(fds));
num_fds = 0;
}
void sock_listen(int addr, int (*command)(void* device, const char* cmd), void* device)
{
int status;
long flags;
int one = 1;
struct sockaddr_in my_addr;
status = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (status < 0)
{
@@ -130,10 +137,12 @@ void sock_init(int addr)
perror("listen");
exit(EXIT_FAILURE);
}
fds[0].fd = sock_l;
fds[0].events = POLLIN | POLLOUT;
fdv[0].fd = sock_l;
fdv[0].input = sock_accept;
fds[num_fds].fd = sock_l;
fds[num_fds].events = POLLIN | POLLOUT;
fdv[num_fds].fd = sock_l;
fdv[num_fds].input = sock_accept;
fdv[num_fds].command = command;
fdv[num_fds].device = device;
++num_fds;
}
@@ -157,7 +166,7 @@ void sock_check(int timeout)
}
if (ready == 0)
return;
dprintf(0, "sock_check, ready=%d\n", ready);
dbg_printf(0, "sock_check, ready=%d\n", ready);
for (i = 0; i < MAX_SOCK; ++i)
{
if (fds[i].revents)
@@ -177,7 +186,7 @@ void sock_check(int timeout)
*/
void sock_close(int n)
{
dprintf(0, "sock_close\n");
dbg_printf(0, "sock_close\n");
shutdown(fdv[n].fd, SHUT_RDWR);
close(fdv[n].fd);
if (n != num_fds)
@@ -198,9 +207,9 @@ void sock_line(int n)
{
char *cp = &fdv[n].line[0];
char *up;
dprintf(0, "%3d: %s", fdv[n].state, fdv[n].line);
dbg_printf(0, "%3d: %s", fdv[n].state, fdv[n].line);
if (fdv[n].line[fdv[n].line_len - 1] != '\n')
dprintf(0, "\n");
dbg_printf(0, "\n");
switch(fdv[n].state)
{
case 0:
@@ -276,16 +285,7 @@ void sock_line(int n)
put_form(n);
else if (strncasecmp(fdv[n].url, "/cmd=", 5) == 0)
{
if (strncasecmp(fdv[n].url, "/cmd=pause", 10) == 0)
cntr_pause(&counter);
else if (strncasecmp(fdv[n].url, "/cmd=continue", 10) == 0)
cntr_resume(&counter);
else if (strncasecmp(fdv[n].url, "/cmd=resume", 10) == 0)
cntr_resume(&counter);
else if (strncasecmp(fdv[n].url, "/cmd=start", 10) == 0)
cntr_start(&counter);
else if (strncasecmp(fdv[n].url, "/cmd=stop", 10) == 0)
cntr_stop(&counter);
fdv[n].command(fdv[n].device, &fdv[n].url[5]);
put_page_refresh(n);
}
else
@@ -311,14 +311,14 @@ void sock_line(int n)
(cp = strchr(&cp[14], ':')))
{
fdv[n].content_length = atoi(&cp[1]);
dprintf(0, "Content Length = %d\n", fdv[n].content_length);
dbg_printf(0, "Content Length = %d\n", fdv[n].content_length);
}
break;
case 4:
/*
* we are scanning the body of a POST
*/
dprintf(0, "Content-Length: %d, Line-Length: %d\n",
dbg_printf(0, "Content-Length: %d, Line-Length: %d\n",
fdv[n].content_length, fdv[n].line_len);
fdv[n].content_length -= fdv[n].line_len;
if (fdv[n].content_length <= 0)
@@ -338,7 +338,7 @@ void sock_line(int n)
*/
if (fdv[n].state == 5)
{
put_form(n);
put_form_refresh(n);
fdv[n].state = 0;
}
}
@@ -350,7 +350,7 @@ void sock_line(int n)
*/
void sock_input(int n)
{
dprintf(0, "sock_input(%d)\n", n);
dbg_printf(0, "sock_input(%d)\n", n);
ssize_t sz;
char buffer[1024];
sz = recv(fdv[n].fd, &buffer, sizeof(buffer), 0);
@@ -362,9 +362,9 @@ void sock_input(int n)
if (sz < 0)
{
if (errno == EAGAIN) /* AKA EWOULDBLOCK */
dprintf(0, "EAGAIN:");
dbg_printf(0, "EAGAIN:");
else if (errno == ESPIPE) /* Illegal seek (on pipe or socket) */
dprintf(0, "ESPIPE:");
dbg_printf(0, "ESPIPE:");
else
perror("recv");
return;
@@ -392,7 +392,7 @@ void sock_input(int n)
*/
void sock_accept(int n)
{
dprintf(0, "sock_accept(%d)\n", n);
dbg_printf(0, "sock_accept(%d)\n", n);
int sock_n;
struct sockaddr_in my_addr;
socklen_t my_len = sizeof(my_addr);
@@ -407,6 +407,8 @@ void sock_accept(int n)
fdv[num_fds].fd = sock_n;
fdv[num_fds].addr = my_addr;
fdv[num_fds].input = sock_input;
fdv[num_fds].command = fdv[n].command;
fdv[num_fds].device = fdv[n].device;
fdv[num_fds].line_len = 0;
fdv[num_fds].state = 0;
fds[num_fds].fd = sock_n;
@@ -502,3 +504,8 @@ void sock_err(int n)
buffer.length = strlen(buffer.body);
sock_send(n, &buffer);
}
void* sock_device(int n)
{
return fdv[n].device;
}