diff cmu200/session.c @ 0:bd62be88259d

initial import of rfcal code and docs from freecalypso-tools repository
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 20 May 2017 18:49:35 +0000
parents
children b552e8d86474
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cmu200/session.c	Sat May 20 18:49:35 2017 +0000
@@ -0,0 +1,86 @@
+/*
+ * This module contains the code that handles a single local socket
+ * connection session.
+ */
+
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+
+extern int activesock;
+
+#define	MAX_FIELDS	10
+
+char client_cmd[256], *client_cmd_fields[MAX_FIELDS+1];
+int client_cmd_nfields;
+
+parse_cmd_into_fields()
+{
+	char *cp;
+
+	client_cmd_nfields = 0;
+	for (cp = client_cmd; ; ) {
+		while (isspace(*cp))
+			cp++;
+		if (*cp == '\0')
+			break;
+		if (client_cmd_nfields >= MAX_FIELDS) {
+			send_socket_response("-Command has too many fields\n");
+			return(1);
+		}
+		client_cmd_fields[client_cmd_nfields++] = cp;
+		while (*cp && !isspace(*cp))
+			cp++;
+		if (*cp)
+			*cp++ = '\0';
+	}
+	client_cmd_fields[client_cmd_nfields] = 0;
+	return(0);
+}
+
+handle_command()
+{
+	char readbuf[256];
+	int cc, pos, rc;
+
+	for (pos = 0; ; ) {
+		cc = read(activesock, readbuf, sizeof readbuf);
+		if (cc <= 0) {
+			printf("Client program closed connection\n");
+			return(1);
+		}
+		if (pos + cc > sizeof client_cmd) {
+			send_socket_response("-Command too long\n");
+			return(1);
+		}
+		bcopy(readbuf, client_cmd + pos, cc);
+		pos += cc;
+		if (client_cmd[pos-1] == '\n')
+			break;
+	}
+	client_cmd[pos-1] = '\0';
+	printf("Client command: %s\n", client_cmd);
+	rc = parse_cmd_into_fields();
+	if (rc)
+		return(0);
+	if (!client_cmd_nfields) {
+		send_socket_response("+Empty command OK\n");
+		return(0);
+	}
+	return dispatch_client_command();
+}
+
+handle_session()
+{
+	int rc;
+
+	send_socket_response("+CMU200 interface daemon ready\n");
+	for (;;) {
+		rc = handle_command();
+		if (rc)
+			break;
+	}
+	close(activesock);
+}