netstart/
object $root;

object $sys: $root;

driver method .startup() {
	arg args;

	// called by the driver when the db is up and ready

	dblog("startup called");

	$listener.startup();
};

object $listener: $root;

var $listener leftover = `[];

public method .tell() {
	arg str;

	// send a string to the connected user
	cwrite(str_to_buf(str + "\n"));
};

public method .startup() {
	// called by $sys.startup to initialize the listener

	catch any {
		bind_port(4249);
	} with {
		dblog("Error binding port: " + toliteral(traceback()));
	}
};

driver method .connect() {
	arg remote, local, socket;

	// called by driver on incoming connection

	dblog("Connection from " + tostr(remote));

	.tell("Welcome!  This is NetStart core v0.1.");
	.tell("As-is, the core only supports a single net connection, and doesn't really have any useful commands available.  It's designed to let you, the programmer, edit the textdump to implement commands, shutdown, run bin/coldcc -c and bin/genesis, and try them out.  If you're interested in a more full-featured core, check http://cold.xidus.net for info on BaseCore.");
	.tell("Commands available: (initially, anyway) test, shutdown");
};

driver method .parse() {
	arg buf;
	var lines, line;

	// called by driver on incoming data

	// handles buffer->string conversion, including leftovers from
	// packet frags or whatnot.  buf_to_strings is native
	lines = buf_to_strings((leftover ? leftover : `[]) + buf);
	
	// last item returned is a buffer with leftovers
	leftover = lines[listlen(lines)];
	lines = delete(lines, listlen(lines));
	
	// call parse_line on each
	for line in (lines)
		.parse_line(line);
};

public method .parse_line() {
	arg str;

	// called by driver parse method with a line of text to parse
	// this is where you start coding.  Initially only two commands,
	// 'test', and 'shutdown' are implemented.

	switch (str) {
	case "test":
		.tell("'test' command tested.");
	case "shutdown":
		.tell("Shutting down now, I hope that's what you wanted to do...");
		// native method, save db and shutdown
		shutdown();
	default:
		.tell("Unknown command: '" + str + "' received.");
	}
};