Ingescape for NodeJS

Ingescape for NodeJS

Ingescape for NodeJS requires the installation of the Ingescape open source library, which is distributed with the Ingescape Circle installer or can be compiled from our Github repository.

To install Ingescape for NodeJS, please refer to our dedicated pages in our Github repository.

If NodeJS and Ingescape are already installed on your system, just type :

npm install ingescape

Cheatsheet

Fully-fledged examples can be found here. And Ingescape Circle code generation helps you bootstrap agents in many languages.

All declared ingescape functions in Node-JS may also be found by inspecting the package in a node console :

var igs = require('ingescape');
console.log(igs);Code language: JavaScript (javascript)

Observe an input with a callback function

igs.observeInput("input", inputCallback, null);Code language: JavaScript (javascript)

The last argument is an optional user data that is passed to the callback every time the input is written.

Handle a written input with a callback function

function inputInputCallback(iopType, name, valueType, value, myData) {
  console.log("Input " + name + " of type " + value_type + " has been written with value " + value + " and user data " + my_data)

  agentJS.setInputI(value);
}Code language: JavaScript (javascript)

The ‘iopType’ callback parameter designates if we are dealing with an input/output/parameter. In our example, it will designate an input.
The call to the agent’s function setInputI is used to affect the incoming value to the attribute of the agent (agentJS in our example).

Get the value of an input

  console.log(igs.inputInt("integerinput_name"))
  console.log(igs.inputDouble("doubleinput_name"))
  console.log(igs.inputBool("booleaninput_name"))
  console.log(igs.inputString("stringinput_name"))
  console.log(igs.inputData("datainput_name"))Code language: JavaScript (javascript)

Write and publish an output

  igs.outputSetInt("integeroutput_name", 1)
  igs.outputSetDouble("doubleoutput_name", 1.1)
  igs.outputSetBool("booleanoutput_name", true)
  igs.outputSetImpulsion("impulsionoutput_name")
  igs.outputSetString("stringoutput_name", "somestring")
  igs.outputSetData("dataoutput_name", (new TextEncoder()).encode('somedata').buffer)Code language: JavaScript (javascript)

Create a service and set its associated callback function

igs.serviceInit("service_name", service_callback, None)Code language: JavaScript (javascript)

The last argument is an optional user data that is passed to the callback every time the input is written.

Handle a service call with its associated callback

function serviceNameCallback(senderAgentName, senderAgentUUID, serviceName, serviceArgs, token, myData) {
  console.log("Service " + serviceName + " was called by " + senderAgentName + " (" + senderAgentUUID + ") with arguments : " + serviceArgs)

  agentJS.serviceName();
}Code language: JavaScript (javascript)

This calls the method serviceName() of the agent when the service is called.

Call a service on another agent

let argsList = [];
argsList = igs.serviceArgsAddDouble(argsList, 2.0);
argsList = igs.serviceArgsAddString(argsList, "anystring");
argsList = igs.serviceArgsAddBool(argsList, true);
argsList = igs.serviceArgsAddInt(argsList, 24);
argsList = igs.serviceArgsAddData(argsList, (new TextEncoder()).encode('somedata').buffer);
igs.serviceCall("other_agent_name", "called_service_name", argsList, "");Code language: JavaScript (javascript)

The last argument is an optional string that is returned if the requested service call triggers a reply service call from the callee, so that each individual service call can be identified precisely.

Detect agent events

igs.observeAgentEvents(onAgentEventCallback, null);Code language: JavaScript (javascript)

The last argument is an optional user data that is passed to the callback every time an agent event is detected.

Handle agent events

function onAgentEventCallback(event, uuid, name, event_data, my_data) {
  console.log("Agent event " + event + " from agent " + name + " with uuid " + uuid + " received : event data " + event_data + ", agent object " + my_data);
  console.log("Possible event types : peer entered " + igs.PEER_ENTERED + " / peer exited " + igs.PEER_EXITED + " / agent entered " + igs.AGENT_ENTERED + " / agent definition update " + igs.AGENT_UPDATED_DEFINITION + " / agent now knows us " + igs.AGENT_KNOWS_US + " / agent exited " + igs.AGENT_EXITED + " / agent mapping updated" + igs.AGENT_UPDATED_MAPPING + " / agent election won " + igs.AGENT_WON_ELECTION + " / agent election lost " + igs.AGENT_LOST_ELECTION);
}Code language: JavaScript (javascript)