shithub: netgraph

ref: 872b3e0905a1b89852de33a993506b60edc1ba97
dir: /netgraph.c/

View raw version
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <ndb.h>
#include <String.h>
#include "dat.h"
#include "fns.h"

static void
usage(void)
{
	fprint(2, "usage: %s [-s systuples]\n", argv0);
	exits("usage");
}

#define MAXSYSTP 24

char *netdir = nil;

char *systuples[MAXSYSTP];
int nsystuples = 0;

/* A3 format: 842 x 1191 */
int pagewidth = 1191;
int pageheight = 842;

static void
procrequest(char *key, char *val)
{
	if (issysarg(key)) {
		gensys(key, val);
		return;
	}
}

static void
procinput(char *s)
{
	char *c;
	
	c = strchr(s, '=');
	if (!(c && c[0] && c[1]))
		return;
	
	c[0] = 0;
	c++;
	
	procrequest(s, c);
}

void
dumptuples(Tuple *t, void*)
{
	fprint(2, "  %s=%s\n", t->key, t->value);
}

void
dumpsys(Sys *s, void*)
{
	fprint(2, "sys: %d %d\n", s->p.x, s->p.y);
	fortuple(s, dumptuples, nil);
}

static void
calcsyslayout(Sys *s, void *aux)
{
	int n = numsystems();
	int *ip = aux;
	int i = *ip;
	float step = 2*PI / n;
	
	setblocksize(s);
	
	float pagex = pagewidth/2.;
	float pagey = pageheight/2.;
	float spread = 0.5;
	
	s->p.x = cos(step * i) * pagex * spread + pagex - s->width/2;
	s->p.y = sin(step * i) * pagey * spread + pagey - s->height/2;
	
	(*ip)++;
}

static void
calcconn(Tuple *t, void *aux)
{
	Sys *from = aux;
	Sys *to = findsys(t->key, t->value);
	if (!to)
		return;
	if (to == from)
		return;
	addconn(from, to, t->key);
}

void
calcconns(Sys *s, void*)
{
	fortuple(s, calcconn, s);
}

static void
drawconns(Conn *c, void*)
{
	dconn(c->from, c->to, s_to_c(c->types));
}

static void
drawsystems(Sys *s, void*)
{
	dblock(s);
}

void
main(int argc, char **argv)
{
	Biobuf *bin;
	char *stp = "sys,ether,ip,ipnet,dom,auth,authdom";
	char *s;
	int i;
	
	ARGBEGIN{
	case 'h':
		usage();
	case 's':
		stp = EARGF(usage());
		break;
	case 'x':
		netdir = EARGF(usage());
		break;
	}ARGEND;
	
	nsystuples = getfields(stp, systuples, MAXSYSTP, 1, ",");
	
	bin = Bfdopen(0, OREAD);
	if (!bin)
		sysfatal("%r");
	
	while (s = Brdstr(bin, '\n', 1)) {
		procinput(s);
		free(s);
	}
	
	Bterm(bin);
	
	dheader();
	i = 0;
	forsys(calcsyslayout, &i);
	forsys(calcconns, nil);
	forconn(drawconns, nil);
	forsys(drawsystems, nil);
	dfooter();
}