/*
 * Copyright 1990 by Baylor College of Medicine ALL RIGHTS RESERVED. 
 *
 * This program is subject to a license agreement between 
 * Baylor College of Medicine and MIT. Any use inconsistent with
 * said license and any use by persons other than the faculty, 
 * students and staff at MIT or any use on a computer not operated 
 * as part of the Athena Computing Environment (ACE) is expressly 
 * prohibited.
 */
/****************************************************************
 * File: server.c
 * Date: 04/11/91
 *
 * Description:
 *   This file contains functions for supporting the vns_comm
 *   library. This allows VNS to act as a "server" for client
 *   applications.
 *
 * Notes:
 *   This code will have to be modifuied as well as the vns_comm
 *   library if VNS is extended to handle multiple database 
 *   connections. The function handle_client_request() will be
 *   called for any request made to the VNS X application from
 *   any client using vns_comm. But, the specific database of
 *   interest is not specified. This will have to be provided
 *   in order for the proper checking so that VNS only opens
 *   pages in databases that it is currently connected to.
 *
 * Revisions:
 ****************************************************************/
#include <stdio.h>
#include <string.h>
#include <X11/Intrinsic.h>
#include <srvlib.h>

#include "VnsP.h"

/****************************************************************
 *                  Local Storage
 ****************************************************************/

static Atom request;
static Atom request_type;
static Atom reply;
static Atom reply_type;

/****************************************************************
 *                  PRIVATE FUNCTIONS
 ****************************************************************/

static
char *
get_tok(s,t)
	char *s;
	char *t;
{
	while(*s == ' ')
	{
		s++;
	}
	while(*s != ' ' && *s)
	{
		*t++ = *s++;
	}
	*t = 0;
	return s;
}

static
int
select_page(vns,nid,pid)
	VnsContext *vns;
	int nid;
	int pid;
{
	static char buf[1024];
	Display *dpy = XtDisplay(vns->toplevel);
	VnsDatabase *db = vns->current_db;
	char *db_name = srv_database(db->srv);
	char *host = srv_host(db->srv);
	int port = srv_port(db->srv);

	sprintf(buf,"Refer to: Notebook:%d Page:%d Server:%s Port:%d Database:%s",
		nid,pid,host,port,db_name);

	vns_link_here_off(vns);

	/* send reply */
	XChangeProperty(dpy,DefaultRootWindow(dpy),
		reply,reply_type,
		8,PropModeReplace,buf,strlen(buf));
}

/****************************************************************
 *                  PUBLIC FUNCTIONS
 ****************************************************************/

PUBLIC
void
init_client_server(dpy)
	Display *dpy;
{
	request = XInternAtom(dpy,"VNS_REQUEST",False);
	request_type = XInternAtom(dpy,"VNS_REQUEST_TYPE",False);
	reply = XInternAtom(dpy,"VNS_REPLY",False);
	reply_type = XInternAtom(dpy,"VNS_REPLY_TYPE",False);
	XSelectInput(dpy,DefaultRootWindow(dpy),PropertyChangeMask);
}

PUBLIC
void
handle_client_request(vns,dpy,event)
	VnsContext *vns;
	Display *dpy;
	XEvent *event;
{
	VnsDatabase *db = vns->current_db;
	int type,format,nitems,left;
	char *req;

	if (event->xproperty.atom != request) return;

	if (XGetWindowProperty(dpy,DefaultRootWindow(dpy),
		request,0,1024,
		TRUE,request_type,&type,&format,&nitems,
		&left,&req) == Success &&
		type == request_type)
	{
		char name[80];
		char *s = get_tok(req,name);
		static char buf[1024];

		if (!strcmp(name,"ping"))
		{
			strcpy(buf," ");
			XChangeProperty(dpy,DefaultRootWindow(dpy),
				reply,reply_type,
				8,PropModeReplace,buf,strlen(buf));
		}
		else if (!strcmp(name,"select_page"))
		{
			/* deselect everything first */
			deselect_all(vns);

			vns->link_callback = select_page;
			vns->link_data = (XtPointer)vns;

			vns_link_here_on(vns);
		}
		else if (!strcmp(name,"display_page"))
		{
			int nid,pid;
			sscanf(s,"%d %d",&nid,&pid);

			if (srv_validate_page(db->srv,nid,pid) == 1)
			{
				VnsNotebook *nl = *(VnsFindNotebook(db,nid));
				VnsAddPage(nl,pid,(struct return_page *)NULL,(char *)NULL,
					(VnsPage *)NULL);
			}

			strcpy(buf," ");
			XChangeProperty(dpy,DefaultRootWindow(dpy),
				reply,reply_type,
				8,PropModeReplace,buf,strlen(buf));
		}
	}

	free(req);
}




