maratishe.github.io On the 3-Way Scripting Programming Technique
Author: maratishe@gmail.com -- created 160121
The 3-way scripting technique is something that I have come up with sometime during this year as a new form for writing my own scripts. I have fully committed to this form every since, now most of my software is written in 3-way scripts.
Theory and the Why
Anyone remember CORBA from back in early 2000s? It was originally created so that Java or C/C++ objects would be able to migrate between locations, seamlessly. It was accomplished simply by stringifying objects and parsing them back into objects at the other end. Not much magic here.
Current distributed software platforms can still pass objects between locations, but mostly focus on passing messages (SOAP, etc.) and running tasks . In fact, the latest trend here is the SOA which stands for Service-Oriented Architecture, where execution (and, therefore, optimization) of distributed code focuses on specific tasks. Related concepts are task graphs rather than graphs of physical locations, which are to be optimized.
The biggest problem with that is that software still has to rely on platforms at runtime. This normally means that communications across agents fully depends on whatever the platform decides for them. Today's normal is to use web sockets to send messages. This is good on one hand, but very bad on the other. For example, when one deploys in the cloud, and has multiple containers per VM and multiple VMs per DC, one might mind how many sockets are kept open at a given point of time.
Yet, even with sockets the problem of providing high-throughput connections across software agents is not resolved. If two agents are on the same machine, calling each other via commandline or function call is both faster and can provide much thicker (higher throughput) connections. Out of scope of this post, but there are also emulated e2e circuits when running across the network.
Let us wrap this discussion in form of the figure. The code instance A in modern software needs to have sufficient freedom of choice when it comes to communicating with other instances both within the same machine or over the network.
Specifically, as the figure shows, code today wants the choice also in the area of throughput or/and delay -- the figure shows this is as a thick like connecting A with BigData . This is, in fact, one of the practical needs which stand behind the 3-way scripting concept.
The 3-Way Scripting Concept
So far, I see three ways to call or communicate with another instance of code, which are: (1) calling another code by
instantiating its new instance and calling its function directly, (2)
calling another code via commandline (again, new instance), and, finally
(3) the more traditional form of using a web API of another
instance of the code running in parallel.
The figure puts the same content into the graphical form. Note that the first two (from top) uses are supposed to be run on the same physical (or virtual) machine since it depends on having direct access to the other code, either in form of a class file or commandline executable .
If we just implement this guideline directly, then the first obvious problem is that we have to write the same code in 3 separate forms. This is, of course, bad practice . The 3-way scripting technique is all about having to write the code only once but being able to run it in at least the above three ways.
Implementation
First, let us resort to a well-known trick in PHP (can find the same in other scripting languages) when one can call functions by variables , as is shown in the figure.
We want to call the function callme() of class Name . To do this, we can assign the variable $b the value callme and then use its value to call the intended function. It should be obvious, that this gives us the necessary freedom to handle function calls in an indirect, referenced way. The 3-way scripting can be implemented in other ways as well, but this technique is the easiest there is.
For an actual code, see example.php at this github project
Docker Install
Given the dependencies, both in terms of things that my script requre()s and OS environment, it may be a hassle to deploy your 3-way scripts. So, as easy deployment, I wrote a Dockerfile what created a ready-made container. It does not restrict any of the 3-ways, you can run it from command line, start a server in example.php and use a web API and even have another code inside the container instantiate and use your code. It is up to you. The Dockerfile is short and easy to understand. To insert your scripts into it, replace wget part with a much simpler COPY that would copy files from your local filesystem into the container at build time.
If you do not know how to do that, look up the COPY command in Docker.
That's it.
Written with my own local WYSIWYG editor.