/* 
 * $Id: server.c,v 1.2 91/05/03 03:08:04 qjb Exp Locker: qjb $
 * $Source: /afs/athena.mit.edu/astaff/project/qcs/src/RCS/server.c,v $
 * $Author: qjb $
 *
 * This is a test server for the qcs library.
 */

#if !defined(lint) && !defined(SABER) || defined(RCS_HDRS)
static char *rcsid = "$Id: server.c,v 1.2 91/05/03 03:08:04 qjb Exp Locker: qjb $";
#endif /* !lint && !SABER || RCS_HDRS */

#include <stdio.h>
#include <syslog.h>
#include "qcs.h"

#include "sample.h"

char tmpbuf[BUFSIZ];

void disp(qcs_client client, qcs_request *req)
{
    sprintf(tmpbuf, "Hello.  You have issued the %s command.", req->cmd[0]);
    qcs_client_message(client, tmpbuf);
    if (req->cmd[1]) {
	sprintf(tmpbuf, "I am now going to wait %d seconds before finishing.", 
	       atoi(req->cmd[1]));
	qcs_client_message(client, tmpbuf);
	sleep(atoi(req->cmd[1]));
    }	
}

void flush(qcs_client client, qcs_request *req)
{
    client->done = (qcs_bool) 1;
}

void toggle_debug(qcs_client client, qcs_request *req)
{
    extern int qcs_debug;
    qcs_debug = -1 - qcs_debug;
}

void showfile(qcs_client client, qcs_request *req)
{
    qcs_error_t status;
    qrpc_t qrpc;

    if (status = qcs_create_comm(client, req, &qrpc, sample_opcodes, 
				 SAMPLE_MINV, SAMPLE_MAXV)) {
	return;
    }

    qrpc_destroy(&qrpc);
}

static qcs_cmd_desc cmd0[] = 
{ {"shutdown", qcs_sd_shutdown }, 
  { "status", qcs_sd_status }, { "flush", flush }, { "showfile", showfile },
  {"zeroa", disp}, {"debug", toggle_debug}, {NULL, NULL} };

static qcs_cmd_desc cmd1[] = 
{ {"onea", disp}, {"oneb", disp}, {NULL, NULL} };

static qcs_cmd_desc cmd2[] = 
{ {"twoa", disp}, {"twob", disp}, {NULL, NULL} };

static qcs_cmd_desc cmd3[] = 
{ {"showfile2", showfile}, {NULL, NULL} };

static qcs_cmd_desc *desclist[] = { cmd0, cmd1, cmd2, cmd3, NULL };

main(int argc, char *argv[])
{ 
    qrpc_t qrpc;
    char errmsg[BUFSIZ];
    extern int qcs_debug;
    char *whoami;

    /* Don't strip off the path name for the server */
    whoami = argv[0];
    
    qcs_server_params params;

    qcs_setup_server_params(&params, "afs-cmd", "/usr/local/afs-cmd", 30, 
			    "afs-cmd", 3708);

    if (qcs_server_setup(desclist, &qrpc, &params, errmsg) != QCS_SUCCESS) {
	syslog(LOG_DAEMON | LOG_ERR, errmsg);
	fprintf(stderr, "%s: %s\n", whoami, errmsg);
	exit(1);
    }
    else
	puts("Server has started");

    qcs_server_mainloop();

    puts("Server has shut down");
    exit(0);
}
