Jd/Server

From J Wiki
Jump to navigation Jump to search
Jd | Overview | General | Docs | Ops | Admin | Server | Replicate | Guide | Technical | Release | License | Support

UNDER CONSTRUCTION

jd server (jds)

The jds facilities were introduced in release 4.39 and will eventually replace deprecated older server facilities.

quick start

  jdrt'jds'
  jdrt'jds_client'
  jdrt'node'

overview

Steps to run a jds server:

  jconsole
  load 'jd'
  load JDP,'server/jds_server.ijs'
  <set globals for PORT, logging, and DBs>
  init '' NB. loop: get http request / run jd op / send result

A jds server runs jd ops for http clients. Clients can be local/remote and can be J tasks or non-J tasks (wget/curl/etc.) or browser apps. A jds server only serves http requests. See https for info on node task to provide https support.

jds_server.ijs

This script defines the socket routines. Verb init uses global info on PORT/LOGS/DBs to initialize the server and executes run to start the listen loop. Verb run listens on PORT for http requests. When it has a complete request it calls verb jds_jd_ and sends the result to the client.

Socket ops are done with jcs (~addons/net/jcs/jcs.ijs) which uses zmq (zeromq.org) and provides robust and balanced service.

jds_jd_

Verb jds_jd_ is a wrapper on verb jd.

  jds_jd_ 'FIN FOUT DAN U/P ; string_arg [LF encoded_array]'
FIN is jbin (3!:1) or json (array) for input format.
FOUT is jbin or json (dictionary) for output format.
DAN is data access name (implicit parameter to jd set by jdaccess).
U/P is user/pswd (implicit parameter to jd set by jdaccess).
  'new'jdadmin'test'
  jdaccess'test u/p intask'
  jd'info summary'
...
  jds_jd_ 'json json test u/p ; info summary'
{
"table":"rows",
"":[]
}
  jds_jd_ 'json json test u/p ; createtable f'
{
"Jd OK":0
}
  jds_jd_ 'json json test u/p ; createcol f a int'
...
  jds_jd_ 'json json test u/p ; insert f ',LF,'["a",[1,2,3]]'
...
  jds_jd_ 'json json test u/p ; read from f'
{
"a":[1,2,3]
}

The input format for arrays (e.g. insert pairs) can be jbin (3!:1) or json array. The output format (e.g., read) can be jbin or json dictionary.

jds client

A J task (with or without Jd loaded) can access a jds server with routines that makes http requests.

The jds client code is simple and can be easily ported to any programming environment that allows http post requests. In these cases support of jbin may not be straightforward and json probably suffices.

The simplest non-J client, available on any platform, is a shell script with wget or curl.

https and node

A jds server can only support http clients. This is perhaps OK for casual use on a lan but is probably not appropriate for general web access.

A more robust server with https support and features more easily provided in a web facing server built with standard web server tools can be provided with a node (nodesjs.org) front end.

  jdrt'node'

older server facilities

Remote

Admin can configure a Jd task to service ops that come in as http requests from other tasks. Security is provided by an SSH tunnel that connects client localhost port to the server localhost port.

JHS jjd can service jd ops (reads/delete/insert/update/etc.). The http request processed by jjd can take request data in several formats and return the result in several formats. Supported formats include: JBIN (3!:1/2), Java JSON, html, text. Supported formats are easily extended.

   load'~addons/data/jd/api/jjd.ijs' NB. Jd service to http clients

A client posts an http request to URL localhost:port/jjd with request data that includes dan, user/pswd, and the op data.

Admin can also configure a Jd task to service custom browser apps.

   load'~addons/data/jd/demo/jhs/jdapp1.ijs'

Browse to URL localhost:65001/jdapp1 to use the jdapp1 service.

Jd task

A Jd task makes a db available to admin and users. A Jd task is a J task with the Jd code loaded.

start J
   load'jd'
... welcome message ...
   jdadmin 'northwind' NB. make northwind db available

J client

A J task can run just the Jd client code. It does not need to run Jd or provide any db access itself.

   load'~addons/data/jd/api/client.ijs' NB. just client code
   jdaccess 'foo me/my localhost:65002'
   jd'read from j'                      NB. read from server db

APL client

The Jd client code is simple and can easily be provided in APL. All that is required is to pack the data for the request in a suitable format and do an http post request. For APL it is probably worthwhile to do the work to allow the APL client to use JBIN format as this is the most efficient and the conversion between JBIN and APL arrays would be easy to implement.

c# etc client

The Jd client code is simple and can be easily ported to any programming environment that allows http post requests. In these cases support of JBIN may not be straightforward and JSON, XML, HTML, etc. are quite satisfactory.

The simplest non-J client, available on any platform, is a shell script with wget or curl.

Browser client

A Jd server can be configured to serve pages to a browser and to handle ajax requests that are jd ops. This makes it very easy to provide browser/javascript access to Jd. At some point growth in this kind of use would justify the extra work to provide Apache/nginx between the browsers and the Jd task.

Server

start jconsole:

   load'jd'
   initserver'default' NB. load ~addons/data/jd/config/server_default.ijs

The above starts a JHS server on port 65011. The server loads Jd and makes the northwind db available. This server serves J IDE pages such as jijx. It also serves page jjd which provides services to Jd http clients. It also serves page jdapp1 which provides a demo Jd service to a browser. Keep in mind that a JHS server serves different kinds of requests (pages).

jijx etc pages provide the J IDE and combined with Jd serves Jd admin and user.

jjd page serves Jd users. A client sends an http post to the jjd URL with request parameters and gets back a formatted Jd result. The result can be formatted as text, html, json, or internal J (3!:1). It is easy to add support for custom formats.

jdapp1 page provides a demo service to a browser. The browser client sends an http request for page localhost:65011/jdapp1 with parameters and gets back an html formatted Jd result.

Browse to localhost:65011/jijx to have full IDE access to J and Jd(user/pswd test/test).

A Jd client sends a Jd request (as an http post request) to a Jd server and gets back a response. A J task can access a Jd server by loading the Jd client.ijs code.

start jconsole:

  load'~addons/data/jd/api/client.ijs' NB. just client code
  jdaccess'norhwind u/p localhost:65011'
  jd'reads from from Suppliers'

Any program can send an http request (for example shell command wget) can access the Jd server. A browser can access pages that the Jd server serves (e.g., jdapp1).

See ~addons/data/jd/config/server_default.ijs for details on the default server. In a production system you would not include jdapp1 and you would not support the test and demo databases. Instead you would have something like:

   OKURL_jhs_=: 'jjd' NB. serve requests to URL jjd
   jdaccess'northwind u/p localhost:65011'

A Jd server can serve one or more databases. A machine can host 1 or more Jd servers. This is flexible and scalable. In one scenario a single server on a single machine could serve all your databases. In another scenario each database could be served by its own dedicated server running on its own dedicated hardware.

Server security

If all your servers and clients are in a secure intranet this may not be much of an issue and you can have servers on different local network ip addresses and ports.

But this isn't OK if your server is on a cloud machine and your clients are everywhere. In this case Jd uses the proven security provided by SSH.

Each client who wants to use the server needs an shh account on the server. This means account admin and security are handled by normal host/user mechanisms. A Jd client who wants to access a Jd server running on a machine must be able to ssh to the machine. This provide a secure encrypted data transport between the client and the server machine. The ssh connection provides a tunnel between a local port on the client machine and a local port on the server. For example,

  • machine XYZ runs Jd server with admin for DB1 on localhost:65010
  • client does ssh to XYZ - ssh config provides tunnel localhost:65002 - localhost:65010
  • client sends http request to localhost:65002 which gets passed to Jd server on XYZ

Northwind remote server

Steps to create a server for the northwind db on a different machine than the client. Client and server can be on the same machine, but things are more interesting if the server is on a different machine. If client/server are on the same machine they communicate securely through localhost ports. If client is on a different machine, then it needs to ssh to the server so ssh can provide login security and encrypted data transport through a tunnel.

Prepare northwind server:
1. select a server to which you have ssh access
2. configure ssh to provide a localhost tunnel for port 65011
3. ssh to the server
4. install J, Jd, and update all packages
5. start jconsole

   load'jd'
   jdtests'' NB. validate install and create northwind db
   exit''

Start northwind server:
1. ssh to server
2. start jconsole

   load'jd'
   initserver'' NB. list of available server configs
   initserver'default'

Use northwind as a remote client:
1. start new J task (another/same machine) (jconsole/JHS)

   load'~addons/data/jd/api/client.ijs' NB. just client, not full Jd
   jdaccess'northwind u/p localhost:65011'
   jd'reads CompanyName from Suppliers'

Access Jd northwind server as dbadmin:
1. start browser
2. browse to localhost:65011:jijx
3. login with user test password test

   jdadmin''