Esempio n. 1
0
var lconfig					= require("./config.json");								// your local lconfig settings.
var gconfig					= require("../../.././config.json");								// your local lconfig settings.


var logging 				= require('../.././logging');


//var mq 							= require('../../../.././kurunt-axon');
var mq 							= require('axon');
var os 							= require('os')
var fs 							= require('fs');


var uidNumberBands 	= require('uid-number-bands');
uidNumberBands.init(gconfig['band'], gconfig['bands'], true);		// start ids at 1 not 0.


var mq_sockets			= [];										// socket connections to workers.
var mps 						= 0;
var n 							= 0;
var msize						= 0;		// the total (cumulative) byte size inputed (for benchmarking).

// Message Que, until downstream node has confirmed commitment.
var que 						= {};										// que messages in object by id for fast lookup.
//var QUE_HWM					= 104857600;						// (bytes) high water mark for que, drop messages above this [104857600 = 100 MB].

// applies a nagle-ish algorithm (http://en.wikipedia.org/wiki/Nagle's_algorithm) for batching messages before sending to 0MQ/Axon.
// lconfig['mq_nodelay'] = false, to apply this algorithm. When applyed speeds messages per seconds by 2 to 3 order of magnatude (~22k mps to ~90k mps).
var TCP_TRAIL_EXPIRES		= 200;				// Miliseconds, Timeout period for sending remaining messages.
var TCP_BUFFER_SIZE 		= 16450;				// Bytes, about TCP tuning see: http://pic.dhe.ibm.com/infocenter/aix/v6r1/index.jsp?topic=%2Fcom.ibm.aix.prftungd%2Fdoc%2Fprftungd%2Ftcp_streaming_workload_tuning.htm
Esempio n. 2
0
function init(xconfig, Connections) {
	
	connections = Connections;
	var self = this;

	config = xconfig;

	uidNumberBands.init(config['band'], config['bands']);	
	
	// zmq connections to make.
	//logging.log('messenger@stores, connections> ', connections);
	var c = 0;
	for ( c = 0; c < connections.length; c++ ) {
		//logging.log('messenger@stores> make ZMQ this connection!!!');
		
		var zmq_pattern = connections[c]['zmq_pattern'];
		// if guaranteed delivery and pattern is push, change to req.
		if ( GUARANTEE_DELIVERY ) {
			if ( zmq_pattern === 'push' ) {
				zmq_pattern = 'req';
			}
			if ( zmq_pattern === 'pull' ) {
				zmq_pattern = 'rep';
			}			
		}
		//logging.log('messenger@stores> zmq_pattern: ' + zmq_pattern );		
		
		socks[c] = mq.socket(zmq_pattern);
		//logging.log('messenger@stores> zmq_address: ' + connections[c]['zmq_address']);
		if ( connections[c]['zmq_socket'] === 'bind' ) {
 			socks[c].bind(connections[c]['zmq_address']);
 		} else {
 			socks[c].connect(connections[c]['zmq_address']);
 		}
		//logging.log('messenger@stores> messenger, ' + connections[c]['zmq_socket'] + ':' + zmq_pattern + '@' + connections[c]['zmq_address']);			

		//socks[c].format('json');

		// SET this from connected axon socket so can control pause() and resume().
		socks[c].on('connect', function(a){
			//logging.log('messenger@stores> CONNECTED --------------');
			var _this = this;
		
			exports.pause = function() {
				//console.log('PAUSE!!!! --------------');
				_this.socks[0].pause();
				return true;
			}
		
			exports.resume = function() {
				//console.log('RESUME!!!! --------------');
				_this.socks[0].resume();
				return true;
			}		
		});

		var buffer = '';		// as string.
		// MESSAGE ------------------------------------------------------------------------------------:
		socks[c].on('message', function(msgs){
			// NOTE: this, msgs will deliver whole message or messages not multipart, as from axon.

			//console.log('msgs> ' + util.inspect(msgs, true, 99, true));


			var _this2 = this;

			if ( toobusy != undefined ) {		
				if (toobusy()) {
					//console.log('*@stores> sock#' + c + ' Im toobusy.');
					_this2.socks[0].pause();
					setTimeout(function() {
						_this2.socks[0].resume();
						//console.log('*@stores> sock#' + c + ' Im not too busy, resume.');
					}, TOOBUSY_PAUSE);
				}
			}	



			var buffer 					= new Buffer(0);

			buffer = Buffer.concat([buffer, msgs], buffer.length + msgs.length);
			//console.log('buffer: ' + buffer);
	
			//var msgs = msgs;
			//var msgs = JSON.parse(msgs);
			//console.log('msgs: ' + msgs);
				
			//var del = msgs.toString().indexOf('\n');
			//console.log('del: ' + del);

			//var str = msgs.toString('ascii');


			var esc = -99;		// reason for setting to -99 so wont trigger when i = 0 if esc here was also 0.
			var i = 0, x = 0;
			for (i = 0; i < buffer.length; i++) {



				//console.log('msgs.readUInt8(i): ' + msgs.readUInt8(i));

				// check if delineate is escaped. ascii 47 = / (escaped).
				if ( buffer.readUInt8(i) == 47 ) {
					esc = i;
				}
				// delineate, MESSAGE_DELINEATE eg = 10 which is ascii for linefeed: \n
				if ( buffer.readUInt8(i) == MESSAGE_DELINEATE && i != (esc + 1) ) {
					//console.log('delineated at: ' + i);
					
					
					var m = buffer.slice(x, i); 
					//console.log('m> ' + util.inspect(m, true, 99, true));
					
					var message = JSON.parse(m);
					//console.log('messenger@stores> message> ' + util.inspect(message, true, 99, true));
				
					var reply = null;
					var x = {};
					self.emit('message', x, message, reply);	
				
					//mps++;
					//tot++;
				
					//var message = msgs.substring(x, i);
					x = i + 1;
					
					//console.log('message: ' + message);
				
				
				}
				
			}


		});
	

	}
}