<commit_before>/**
 * SUAS Competition client.
 * Listens for, and makes requests to, the competition server by making
 * http requests using its api.
 *
 * Sends and receives MAVLink messages
 *
 * @file server.js
 *
 * @author juanvallejo
 * @date 11/18/14
 */

// define and import require node packages
var fs 		= require('fs'); 										// filesystem access
var net 	= require('net');										// async network wrapper; socket connection listener
var http 	= require('http'); 										// http package (not compatible with https requests)
var socket 	= require('socket.io');									// WebSocket package. Realtime communication with client
var mavlink = require('./mavlink/javascript/lib/jspack/mavlink');	// import mavlink parser library

// define application constants
var HTTP_PORT 	= 8000; 		// port at which this application will listen for connections
var NET_PORT	= 8080;			// port at which this application will listen for MAVLink connections

// client is server making requests to competition server
var client = http.createServer(function(request, response) {
	// log connection address
	console.log('<http> Receiving connection from ' + request.connection.remoteAddress);

	// read and serve static file to client
	readFile(__dirname + '/index.html', function(err, file) {
		if(err) {
			// log error and exit
			return console.log('An error occurred reading the file -> ' + err);
		}	

		// set headers
		response.writeHead(200, {'Content-Type' : 'text/html'});

		// send response to client
		response.end(file);
	});

});

// start listening for connections
console.log('<http> Listening for connections at port ' + HTTP_PORT);
client.listen(HTTP_PORT);

// start socket connection listener and event handling
var io = socket.listen(client).on('connection', function(socket) {
	// log any time a new connection with a client is established
	console.log('<socket.io> WebSocket client connection received.');
	
	// send 'connection' event to client.
	socket.emit('connection', {
		text : 'Connected to UAS server at port ' + HTTP_PORT
	});

	socket.on('command', function(command) {
		if(command.text == 'connect' || command) {
			// connect request
			socket.emit('message', {
				text : 'Unimplemented command'
			});
		}
	});
});

/**
 * Reads file from url location and returns content as blob
 * when finished parsing.
 *
 * @param fileUrl 	{String} 	url location of the file to read.
 * @param callback 	{Function}	steps to execute after file is parsed.
 */
function readFile(fileUrl, callback) {
	fs.readFile(fileUrl, function(err, blob) {
		// call user function to continue and pass error or file contents
		callback.call(this, err, blob);
	});
}

/**
 * main function. execs on startup
 */
(function main() {
	// create new MAVLink object parser
	var mavlinkParser = new MAVLink(null, 1, 50);

	// create a connection to listen for mavlink messages
	var connection = net.createServer(function(client) {
		// listen for when client disconnects
		client.on('end', function() {
			console.log('<net> Client ' + client.remoteAddress + ' has disconnected');
		});

		// listen for binary data received from client
		client.on('data', function(data) {
			// advertise data was received
			console.log('<net> Binary data stream received from client ' + client.remoteAddress);
			// parse received data using mavlink parser
			mavlinkParser.parseBuffer(data);

			// test message
			var message = new mavlink.messages.request_data_stream(1, 1, mavlink.MAV_DATA_STREAM_ALL, 1, 1);
			console.log(new Buffer(message.pack(mavlinkParser)));
		});

	});

	// listen for errors from server
	connection.on('error', function(err) {
		// advertise errors on console
		console.log('<net> ' + err);
	});

	// listen for connections on port NET_PORT
	connection.listen(NET_PORT, function() {
		// advertise connection on console
		console.log('<net> Listening for MAVLink messages on port ' + NET_PORT);
	});



	/**
	* begin listening for decoded binary mavlink messages using mavlink library
	*/

	// listen for any type of mavlink message
	mavlinkParser.on('message', function(message) {
		console.log('<mavlink> binary data from client successfully decoded');
	});

})();
<commit_msg>add waypoint message test and mavlink sending<commit_after>/**
 * SUAS Competition client.
 * Listens for, and makes requests to, the competition server by making
 * http requests using its api.
 *
 * Sends and receives MAVLink messages
 *
 * @file server.js
 *
 * @author juanvallejo
 * @date 11/18/14
 */

// define and import require node packages
var fs 		= require('fs'); 										// filesystem access
var net 	= require('net');										// async network wrapper; socket connection listener
var http 	= require('http'); 										// http package (not compatible with https requests)
var socket 	= require('socket.io');									// WebSocket package. Realtime communication with client
var mavlink = require('./mavlink/javascript/lib/jspack/mavlink');	// import mavlink parser library

// define application constants
var HTTP_PORT 	= 8000; 		// port at which this application will listen for connections
var NET_PORT	= 8080;			// port at which this application will listen for MAVLink connections

// define connection variables
var net_received_data = false;	// indicates <net> connection received data once. This flag prevents program from logging data multiple times
								// per request


// client is server making requests to competition server
var client = http.createServer(function(request, response) {
	// log connection address
	console.log('<http> Receiving connection from ' + request.connection.remoteAddress);

	// read and serve static file to client
	readFile(__dirname + '/index.html', function(err, file) {
		if(err) {
			// log error and exit
			return console.log('An error occurred reading the file -> ' + err);
		}	

		// set headers
		response.writeHead(200, {'Content-Type' : 'text/html'});

		// send response to client
		response.end(file);
	});

});

// start listening for connections
console.log('<http> Listening for connections at port ' + HTTP_PORT);
client.listen(HTTP_PORT);

// start socket connection listener and event handling
var io = socket.listen(client).on('connection', function(socket) {
	// log any time a new connection with a client is established
	console.log('<socket.io> WebSocket client connection received.');
	
	// send 'connection' event to client.
	socket.emit('connection', {
		text : 'Connected to UAS server at port ' + HTTP_PORT
	});

	socket.on('command', function(command) {
		if(command.text == 'connect' || command) {
			// connect request
			socket.emit('message', {
				text : 'Unimplemented command'
			});
		}
	});
});

/**
 * Reads file from url location and returns content as blob
 * when finished parsing.
 *
 * @param fileUrl 	{String} 	url location of the file to read.
 * @param callback 	{Function}	steps to execute after file is parsed.
 */
function readFile(fileUrl, callback) {
	fs.readFile(fileUrl, function(err, blob) {
		// call user function to continue and pass error or file contents
		callback.call(this, err, blob);
	});
}

/**
 * main function. execs on startup
 */
(function main() {
	// create new MAVLink object parser
	var mavlinkParser = new MAVLink(null, 1, 50);

	// create a connection to listen for mavlink messages
	var connection = net.createServer(function(client) {
		// set connection's ip address to current client's remote address 
		connection.remoteAddress = client.remoteAddress;

		// listen for when client disconnects
		client.on('end', function() {
			// log client disconnect
			console.log('<net> Client ' + connection.remoteAddress + ' has disconnected');
			// reset data received
		});

		// listen for binary data received from client
		client.on('data', function(data) {
			// reset our net_received_data flag so new decoded messages are logged
			net_received_data = false;

			// advertise data was received
			console.log('<net> Binary data stream received from client ' + connection.remoteAddress);
			// parse received data using mavlink parser
			// console.log(mavlinkParser.parseBuffer(data));

			// test message output for a single waypoint
			var message = new mavlink.messages.mission_item(1, 1, 1, 1, 16, 1, 1, 0.149999999999999994, 0, 0, 0, 8.54800000000000004, 47.3759999999999977, 550);
			mavlinkParser.parseBuffer(new Buffer(message.pack(mavlinkParser)));
		});

	});

	// listen for errors from server
	connection.on('error', function(err) {
		// advertise errors on console
		console.log('<net> ' + err);
	});

	// listen for connections on port NET_PORT
	connection.listen(NET_PORT, function() {
		// advertise connection on console
		console.log('<net> Listening for MAVLink messages on port ' + NET_PORT);
	});



	/**
	* begin listening for decoded binary mavlink messages using mavlink library
	*/

	// listen for any type of mavlink message
	mavlinkParser.on('message', function(message) {
		// stop more incoming messages from displaying once one has been received
		if(net_received_data) {
			return;
		}

		console.log('<mavlink> binary data from client ' + connection.remoteAddress + ' successfully decoded:');
		console.log('<mavlink_message>');
		console.log(message);
		console.log('</mavlink_message>');

		// indicate data has been received once already
		net_received_data = true;

	});

})();

{'fraction_non_alphanumeric': 0.1147225489147867, 'fraction_numerical': 0.012081685020848925, 'mean_word_length': 3.707599396074484, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 3, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7601999', 'n_tokens_mistral': 2980, 'n_tokens_neox': 2627, 'n_words': 930}