Benjamin Conlan avatar Benjamin Conlan committed 3d5e484

updated code to be simpler and cleaner

Comments (0)

Files changed (3)

 }
 
 exports.CHUNK_TYPE = CHUNK_TYPE;
-exports.connect = exports.createConnection = function() {
-  var args = normalizeConnectArgs(arguments);
-  var s = new JVMPin(args[0]);
-  return JVMPin.prototype.connect.apply(s, args);
+exports.Create = function(options) {
+  return new JVMPin(options);
 };
 
 /**
- * A Read stream used by the JVMPin API to get results/state from the nailgun instance
- */
-function IncommingMessage(socket) {
-	Stream.call(this);
-
-	this.client = socket;
-	this.readable = true;
-	this._chunkType = undefined;
-}
-util.inherits(IncommingMessage, stream);
-
-/**
  * The JVMPin instance
+ * 
+ * The options argument is an object containing the following configuration
+ * options: 
+ * {
+ *   args: {},    // arguments passed to ng. Object, defaults to process.argv.
+ *   env: {},     // environment variables.  Object, defaults to process.env.
+ *   cwd: '/tmp'  // current working directory. String, defaults to process.cwd.
+ * }
  */
 function JVMPin(options) {
-	net.Socket.call(this);
+	JVMPin.super_.apply(this, arguments);
+	var options = options || {};
+	this.args = options.args || process.argv.slice(1, process.argv.length);
+	this.env  = options.env  || process.env;
+	this.cwd  = options.cwd  || process.cwd();
 }
 util.inherits(JVMPin, net.Socket);
 
-// To maintain consistent parameter parsing with nodejs's socket api we also provide
-// the option to parse in arguments under the following syntactic rules:
-//    connect(cmd, [port], [host], [cb])
-function normalizeConnectArgs(args) {
-	var options = {
-		port: 2113,
-		host: 'localhost'
-	};
-
-	if (typeof args[0] === 'object') {
-		options = args[0]
-	} else {
-		options.cmd = args[0];
-		if (args.length > 1) {
-			if (typeof args[1] === 'string') { // host or port
-				options.host = args[1]; // host
-			} else {
-				options.port = args[1]; // port
-				if (args.length > 2) {  // host
-					options.host = args[2];
-				}
-			}
-		}  
+/**
+ * Unlike the Socket connection the JVMPin enforces an object to be parsed instead
+ * of an arguments list for explicity specifying hostname, port & cmd. Alternatively
+ * the first parameter can be the cmd string if all defaults will suffice.
+ * 
+ * The following connection options can be provided:
+ * {
+ *   host: 'localhost'  , // hostname.     String, defaults to 'localhost'.
+ *   port: 2113         , // port number.  Number, defaults to 2113.
+ *   cmd: 'package.Nail'  // the java class to call. String, Requried.
+ * }
+ *
+ * The second argument is a callback function which will be invoked when a connection
+ * is made to the nailgun server.
+ */
+JVMPin.prototype.connect_ = function(options, cb) {
+	this.host = 'localhost';
+	this.port = 2113;
+
+	if (typeof options === 'object') {
+		this.host = options.host || this.host;
+		this.port = options.port || this.port;
+		this.cmd  = options.cmd;
+	} else if (typeof options === 'string') {
+		this.cmd = options;
 	}
 
-	var cb = args[args.length - 1];
-	return (typeof cb === 'function') ? [options, cb] : [options];
-}
-
-JVMPin.prototype.connect = function(options, cb) {
-	if (typeof options !== 'object') {
-		return net.Socket.prototype.connect.apply(this, normalizeConnectArgs(arguments));
+	if (this.cmd === undefined) {
+		throw new Error("No cmd argument provided");
 	}
 
-	net.Socket.prototype.connect(options, cb);
+	this.connect(this.port, this.host, cb);
 
+	var self = this;
 	// send the process and environment data
 	this.on('connect', function(socket) {
 		// Send argument chunks
-		/*for (key in process.argv) {
-			JVMPin.prototype.writeChunk(CHUNK_TYPE.ARGUMENT, process.argv[key]);
-		}*/
-		
+		self.args.forEach(function(arg) {
+			JVMPin.prototype._writeChunk(CHUNK_TYPE.ARGUMENT, arg);
+		});
+
 		// Send environment chunks
 		for (key in process.env) {
-			JVMPin.prototype.writeChunk(CHUNK_TYPE.ENVIRONMENT, key + '=' + process.env[key]);
+			JVMPin.prototype._writeChunk(CHUNK_TYPE.ENVIRONMENT, key + '=' + process.env[key]);
 		}
 
-		// Send workind dir chunk
-		JVMPin.prototype.writeChunk(CHUNK_TYPE.WORKING_DIRECTORY, process.execPath);
+		// Send working dir chunk
+		JVMPin.prototype._writeChunk(CHUNK_TYPE.WORKING_DIRECTORY, self.cwd);
 
 		// Finally send the `java` command
-		JVMPin.prototype.writeChunk(CHUNK_TYPE.COMMAND, options.cmd);
+		JVMPin.prototype._writeChunk(CHUNK_TYPE.COMMAND, self.cmd);
 	});
 
 	return this;
 }
 
-JVMPin.prototype.writeChunk = function(chunkType, data, arg1, arg2) {
+/**
+ * _writeChunk(chunkType, data, [encoding], [callback])
+ *
+ * Writes data to the socket using the nailgun chunk protocol data.
+ */
+JVMPin.prototype._writeChunk = function(chunkType, data, arg1, arg2) {
 	var encoding, cb;
 
 	if (arg1) {

test/jvmpin-test.js

  * EPL Licensed
  */
 
-var jvmpin = require('../lib/jvmpin');
 
+var jvmpin = require('../lib/jvmpin');
 
-var client = jvmpin.connect({
-	port: 2113,
-	host: 'localhost',
-	cmd: 'io.foldr.ngtesthost.Stdout'}, function() {
-		console.log('connected');
-	});
+var client = jvmpin.Create();
 
 client.on('data', function(data) {
 	console.log(data.toString());
 	console.log('client disconnected');
 });
 
-client.on('error', function() {
+client.on('error', function(e) {
 	console.log('error');
-})
+	console.log(e);
+});
+
+client.connect_('io.foldr.ngtesthost.Stdout', function() {
+		console.log('connected');
+	});

test/ng-testhost/assembly.xml

 
 	<id>full</id>
 	<formats>
-		<format>jar</format>
+		<format>zip</format>
 	</formats>
 	<includeBaseDirectory>false</includeBaseDirectory>
 	<files>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.