Tuesday 26 November 2013

Data Passing Between Two Programming Languages : Command Line Approach

This blog tries to explain the way to pass the data between two programs written in two different programming languages.

There might be many approach to pass data between two program written in different programming languages. Some of them are

1) Client-Server model
    In this approach, one program up the server on particular port and another program in another  language pass socket on that port.

2) File-store model
    In this approach, one program write data to file and another program read data from that file. So, file act as common sharing center for both program

3) Database model
    In this approach, one program write data to database like MySQL, DB2, Oracle and another program  read data from database. In this both program has to create connection to database.

Above all approach are well and good but each has its own cause and prone. Here We will discuss command line approach to pass data between two programming languages.

I am taking ruby and node as two different languages to pass data.
Lets first see what do we mean by command line argument. We are considering ruby language for the same.

What is command line argument?

Command-line arguments are given after the name of a program in command-line operating systems like DOS or Linux, and are passed in to the program from the operating system. On the command-line, any text following the name of the script(program) is considered a command-line argument.

Example :
Create file test.rb and write following lines into it.
ARGV.each do|a|
  puts "Argument: #{a}"
end

This will catch command line argument and print them.
Run this file as
ruby test.rb test1 test2
Here test1 and test2 are cla. Command pass cla to file and file catch cla in ARGV and process them as array give output
Argument: test1
Argument: test2
Now we are clear with command line argument. 

Pass data between ruby program and node program

Consider we have two files cla.rb and cla.js.
cla.rb represent ruby program and cla.js represent node program. Both files are initially empty and are in same directory.

Ruby Program has data variable holding data to be pass to node program.
data = {foo: "bar"}
Now convert data to json to get string out of it with ruby josn library
Require json library at the top of program with
require "json"
Convert data to json with to_json method
jdata = data.to_json
This will generate json of data and store it to jdata variable as below

"{\"foo\":\"bar\"}"

Now its time to pass it to node program as command line argument.
But before that use Shellwords library's escape method on jdata. The double quotes are removed by the shell before passing it. To prevent shell from doing so we use Shellwords library as follow

require "shellwords"
Shellwords.escape(jdata)

Lets catch cla.js file absolute path as below

node_handler = File.join(File.dirname(__FILE__), "cla.js")

Ruby use  `` to execute shell command. We need this to run node program from ruby program.
Execute shell command to run node program and pass jdata as cla to node program.

`node #{node_handler} #{Shellwords.escape(jdata)}`

So the final program looks like as below.

#cla.rb
# require libraries
require "json"
require "shellwords"

# data to be passed
data = {foo: "bar"}

# data in json formate
jdata = data.to_json

# absolute path for cla.js
node_handler = File.join(File.dirname(__FILE__), "cla.js")

# execute node program with jdata as command line argument
cmd = `node #{node_handler} #{Shellwords.escape(jdata)}`

Here we are done with our ruby side.

Now in node program we will first catch the command line argument

var data = process.argv[2];

Parse the json data

jdata = JSON.parse(data);

We can print the json with

console.error(jdata)

So, final node program look like

// cla.js
// catch the data from command line argument
var data = process.argv[2];

// parsing data
jdata = JSON.parse(data);
console.error(jdata);

Lets have quick review what we exactly did till now

In Ruby,
1) Catch data in variable
2) Convert data to json
3) Apply escape method of Shellword library on json
4) Execute system command to run node program passing data through cla

In Node,
1) Catch the data thought cla using ARGV
2) Parse it through JSON.parse()

So, we have passed the data between two programming languages with command line approach.

No comments:

Post a Comment