/********************************************************************
 ** PARALLEL PORT STUFF
 **
 ** (c) Spak, Darrell Tam, c9107253@ee.newcastle.edu.au (1994)
 ** phone (Australia) 049-829-710
 **                    49-829-710
 ** (c) SST 1994
 **
 ** LITTLE TEST CLIENT FOR THE FILE SERVER (OPENS A REMOTE FILE)
 **
 ** TRIES TO DO A LOCK ON AN OBJECT AND THEN READ THE ENTIRE DIR
 **
 ** DON'T HIT CTRL-C UNLESS ABSOLUTELY NECESSARY - 
 ** can cause subsequent lock ups since the packet is never cancelled
 ** with the net-packet sender - which will still think the net-packet
 ** is active
 **
 ** TABS to 4
 ********************************************************************/

#include "progs:snd/everything.h"
#include <st/st_proto.h>
#include "progs:srv/nfsthings.h"

#define OUR_SERVER_PORT SERV_FILE

long out;

main(int argc, char *argv[])
{
struct MsgPort *port = NULL, *theport = NULL;
char destmach = argv[1][0];
char	*name = argv[2];

	out = Output();

	if(!(port = CreatePort(0,0))) {
		fpf(out, "No ports\n");
		goto nomore;
	}

	if(!(theport = FindPort("ParServer")) ) {
		fpf(out, "Couldn't find par port packet thingy\n");
		goto nomore;
	}


/** ASK FOR A FILE TO BE OPENED */
{
struct PktMsg our_pm;

struct NFSLock 		lock_req;
struct NFSFreeLock  freelock_req;
struct NFSExamine	examine_req;	/* also used for exnext */

struct NFSLockReturn lock_ret;
struct NFSStdReturn	std_ret;
struct FileInfoBlock fib;

unsigned char name_buf[100];

/** BCPL style string */
	name_buf[0] = spf(&name_buf[1], 90, name);

	fpf(out, "Locking [%lc:(%s)]\n", destmach, &name_buf[1]);

	lock_req.request = REQUEST_LOCK;
	lock_req.internallock = NULL;
	lock_req.mode = SHARED_LOCK;
	INIT_PKTMSG_ORIGINAL(&our_pm, port,
						destmach, OUR_SERVER_PORT,
						&lock_req, sizeof(struct NFSLock),	/* request */
						name_buf, name_buf[0]+2,			/* send (name) */
						NULL, 0,								/* result (receive) */
						&lock_ret, sizeof(struct NFSLockReturn));

	PutMsg(theport, &our_pm); Wait((1<<port->mp_SigBit) | SIGBREAKF_CTRL_C);
	/* note the order of checking! */
	if(&our_pm != (struct PktMsg *)GetMsg(port) || our_pm.smsg.result != PAR_OK)
		{ fpf(out, "something went wrong, quitting\n"); goto nomore; }
	if(!lock_ret.remote_il)
		{ fpf(out, "error = %ld\n", lock_ret.remote_res2); goto nomore; }


	fpf(out,"Received: il=%lx, res2=%ld, maj=%ld\n",
		lock_ret.remote_il,
		lock_ret.remote_res2,
		lock_ret.remote_majic);

/** EXAMINE OBJECT FROM LOCK */
	examine_req.request = REQUEST_EXAMINELOCK;
	examine_req.remote_majic = lock_ret.remote_majic;
	examine_req.remote_il = lock_ret.remote_il;
	INIT_PKTMSG_ORIGINAL(&our_pm, port,
						destmach, OUR_SERVER_PORT,
						&examine_req, sizeof(struct NFSExamine),/* request */
						NULL, 0,								/* send */
						&fib, sizeof(fib),						/* result (receive) */
						&std_ret, sizeof(struct NFSStdReturn));	/* "return" */

	PutMsg(theport, &our_pm); Wait((1<<port->mp_SigBit) | SIGBREAKF_CTRL_C);
	if(&our_pm != (struct PktMsg *)GetMsg(port) || our_pm.smsg.result != PAR_OK) {
		fpf(out, "something bad, error code = %ld\n", our_pm.smsg.result);
		goto nomore;
	}

	if(std_ret.remote_res1) {
		fib.fib_FileName[fib.fib_FileName[0]+1] = '\0';
		fpf(out, "DIR: %s\n", &fib.fib_FileName[1]);
	}
	else
		fpf(out, "Couldn't examine\n");



/** GET THE REST OF THE DIRECTORY */
	while(std_ret.remote_res1) {
		examine_req.request = REQUEST_EXAMINENEXT;
		examine_req.remote_majic = lock_ret.remote_majic;
		examine_req.remote_il = lock_ret.remote_il;
		INIT_PKTMSG_ORIGINAL(&our_pm, port,
						destmach, OUR_SERVER_PORT,
						&examine_req, sizeof(struct NFSExamine),/* request */
						&fib, sizeof(fib),						/* send */
						&fib, sizeof(fib),						/* result (receive) */
						&std_ret, sizeof(struct NFSStdReturn));	/* "return" */

		PutMsg(theport, &our_pm); Wait((1<<port->mp_SigBit) | SIGBREAKF_CTRL_C);
		if(&our_pm != (struct PktMsg *)GetMsg(port) || our_pm.smsg.result != PAR_OK) {
			fpf(out, "something bad, error code = %ld\n", our_pm.smsg.result);
			goto nomore;
		}

		if(std_ret.remote_res1) {
			fib.fib_FileName[fib.fib_FileName[0]+1] = '\0';
			fpf(out, "%06ld %s\n", fib.fib_Size, &fib.fib_FileName[1]);
		}
	}

	fpf(out, "no more entries\n");

/** CLOSE THE FILE */
	freelock_req.request = REQUEST_FREELOCK;
	freelock_req.remote_majic = lock_ret.remote_majic;
	freelock_req.remote_il = lock_ret.remote_il;
	PREP_PKTMSG_ORIGINAL(&our_pm, 
						destmach, OUR_SERVER_PORT,
						&freelock_req, sizeof(struct NFSFreeLock),/* request */
						NULL, 0,								/* send */
						NULL, 0,								/* result (receive) */
						&std_ret, sizeof(struct NFSStdReturn));	/* "return" */
	PutMsg(theport, &our_pm); Wait((1<<port->mp_SigBit) | SIGBREAKF_CTRL_C);
	if(&our_pm != (struct PktMsg *)GetMsg(port)) 
		fpf(out, "Wipe out\n");

}

nomore:
	if(port) DeletePort(port);
}

