#include <u.h>
#include <libc.h>
#include <auth.h>
#include <fcall.h>
#include <thread.h>
#include <9p.h>

#define BUCKSIZE 524288
#define MAXQ 512
#define SMBUF 256

typedef struct Hub	Hub;
typedef struct Msgq	Msgq;

struct Hub{
	char name[SMBUF];			/* name */
	char bucket[BUCKSIZE];		/* data buffer */
	char *inbuckp;				/* location to store next message */
	int buckfull;				/* amount of data stored in bucket */
	Req *qreqs[MAXQ];			/* pointers to queued Reqs */
	int qrnum;				/* index of Reqs waiting to be filled */
	int qans;					/* number of Reqs answered */
	int sleeptime;				/* delay time */
	char *msgloc[MAXQ];
	u32int msglen[MAXQ];
	int c;
};

struct Msgq{
	ulong myfid;
	char *msgloc[MAXQ];
	int msglen[MAXQ];
	int c;
};

enum {
	UP = 1,
	DOWN = 1,
};

int verbosity;	
static char Ebad[] = "something bad happened";
static char Enomem[] = "no memory";

void zerohub(Hub *h);
void fsread(Req *r);
void fswrite(Req *r);
void fscreate(Req *r);
void fsopen(Req *r);
void fsdestroyfile(File *f);
void usage(void);

Srv fs = {
	.open=	fsopen,
	.read=	fsread,
	.write=	fswrite,
	.create=	fscreate,
};

void
fsread(Req *r)
{
	Hub *h;
	Msgq *mq;

	h = r->fid->file->aux;
	h->qrnum++;
	if(h->qrnum == MAXQ){
		h->qrnum = 1;
		h->qans = 1;
	}
	h->qreqs[h->qrnum] = r;
	if(verbosity == UP){
		print("\t\tfsread %s queued read %d on fid %ld\n", h->name, h->qrnum, r->fid->fid);
	}

	for(int i = h->qans; i < h->qrnum; i++){
		mq = h->qreqs[i+1]->fid->aux;
		if(mq->c != h->c){
			h->qans++;
			mq->c++;
			if(mq->c == MAXQ){
				mq->c = 0;
			}
			memmove(h->qreqs[h->qans]->ofcall.data, h->msgloc[mq->c], h->msglen[mq->c]);
			h->qreqs[h->qans]->ofcall.count = h->msglen[mq->c];
			if(verbosity == UP){
				print("\t\tfsread %s qans %d mq fid %ld from %d count %d\n", h->name, h->qans, mq->myfid, (int)(h->msgloc[mq->c]), h->msglen[mq-c]);
			}
			respond(h->qreqs[h->qans], nil);
		}
	}
}

void
fswrite(Req *r)
{
	int count;
	Hub *h;
	Msgq *mq;

	h = r->fid->file->aux;
	count = r->ifcall.count;

	if((h->buckfull + count) >= BUCKSIZE){
		if(verbosity == UP){
			print("\n\n %s bucket wrap!! \n\n", h->name);
		}
		h->inbuckp = h->bucket;
		h->buckfull = 0;
	}

	memmove(h->inbuckp, r->ifcall.data, count);
	h->c++;
	if(h->c == MAXQ){
		h->c = 0;
	}
	if(verbosity ==  UP){
		print("\t\tfswrite %s h->c is %d inbuckp at %d count is %d\n", h->name, h->c, (int)h->inbuckp, count);
	}
	h->msgloc[h->c] = h->inbuckp;
	h->msglen[h->c] = count;

	h->inbuckp += count;
	h->buckfull += count;
	r->fid->file->length = h->buckfull;
	r->ofcall.count = count;

	for(int i = h->qans; i < h->qrnum; i++){
		mq = h->qreqs[i+1]->fid->aux;
		if(mq->c != h->c){
			h->qans++;
			mq->c++;
			if(mq->c == MAXQ){
				mq->c = 0;
			}
			memmove(h->qreqs[h->qans]->ofcall.data, h->msgloc[mq->c], h->msglen[mq->c]);
			h->qreqs[h->qans]->ofcall.count = h->msglen[mq->c];
			if(verbosity == UP){
				print("\t\tfswrite %s qans %d mq fid %ld from %d count %d\n", h->name, h->qans, mq->myfid, (int)(h->msgloc[mq->c]), h->msgsize[mq->c]);
			}
			respond(h->qreqs[h->qans], nil);
		}
	}
	respond(r, nil);
}

void
fscreate(Req *r)
{
	Hub *h;
	File *f;

	if(f = createfile(r->fid->file, r->ifcall.name, r->fid->uid, r->ifcall.perm, nil)){
		h = emalloc9p(sizeof(Hub));
		zerohub(h);
		strcat(h->name, r->ifcall.name);
		f->aux = h;
		r->fid->file = f;
		r->ofcall.qid = f->qid;
		respond(r, nil);
		return;
	}
	respond(r, Ebad);
}

void
fsopen(Req *r)
{
	Hub *h;
	Msgq *q;

	h = r->fid->file->aux;
	q = (Msgq*)emalloc9p(sizeof(Msgq));
	memset(q, 0, sizeof(Msgq));
	q->c = 0;
	q->myfid = r->fid->fid;
	r->fid->aux = q;
	if(h && (r->ifcall.mode&OTRUNC)){
		h->inbuckp = h->bucket;
		h->buckfull = 0;
		r->fid->file->length = 0;
	}
	respond(r, nil);
}

void
fsdestroyfile(File *f)
{
	Hub *h;

	h = f->aux;
	if(h){
		free(h);
	}
}

void
zerohub(Hub *h)
{
	memset(h, 0, sizeof(Hub));
	h->sleeptime = 20;
	h->inbuckp = &h->bucket[0];
	h->c = 0;
	h->qrnum = 0;
	h->qans = 0;
	return;
}

void
usage(void)
{
	fprint(2, "usage: hubfs [-D -v] [-s srvname] [-m mtpt]\n");
	exits("usage");
}

void
main(int argc, char **argv)
{
	char *addr = nil;
	char *srvname = nil;
	char *mtpt = nil;
	Qid q;
	fs.tree = alloctree(nil, nil, DMDIR|0777, fsdestroyfile);
	q = fs.tree->root->qid;

	ARGBEGIN{
	case 'D':
		chatty9p++;
		break;
	case 'a':
		addr = EARGF(usage());
		break;
	case 's':
		srvname = EARGF(usage());
		break;
	case 'm':
		mtpt = EARGF(usage());
		break;
	case 'v':
		verbosity = UP;
		print("vebosity UP\n");
		break;
	default:
		usage();
	}ARGEND;

	if(argc)
		usage();
	if(chatty9p)
		fprint(2, "hubsrv.nopipe %d srvname %s mtpt %s\n", fs.nopipe, srvname, mtpt);
	if(addr == nil && srvname == nil && mtpt == nil)
		sysfatal("must specify -a, -s, or -m option");
	if(addr)
		listensrv(&fs, addr);
	if(srvname || mtpt)
		postmountsrv(&fs, srvname, mtpt, MREPL|MCREATE);
	exits(0);
}

/*
	while(h->qrnum > h->qans){

		mq = h->qreqs[h->qans+1]->fid->aux;

		if(mq->c != h->c){
			h->qans++;
			mq->c++;
			if(mq->c == MAXQ){
				mq->c = 0;
			}
			memmove(h->qreqs[h->qans]->ofcall.data, h->msgloc[mq->c], h->msglen[mq->c]);
			h->qreqs[h->qans]->ofcall.count = h->msglen[mq->c];
			if(verbosity == UP){
				print("\t\t%s fswrite responding to qued read %d\n", h->name, h->qans);
			}
			respond(h->qreqs[h->qans], nil);
		}
	}
*/

