diff --git a/scripts/core/engine.js b/scripts/core/engine.js index 1f92ea2c..8691cedd 100644 --- a/scripts/core/engine.js +++ b/scripts/core/engine.js @@ -551,10 +551,9 @@ var ar = 5 // seconds before reclamation var $_ = {} $_.random = crypto.random; -$_.clock = function(fn) -{ - return os.now() -} +$_.random[prosperon.DOC] = "returns a number between 0 and 1. There is a 50% chance that the result is less than 0.5." +$_.clock = function(fn) { return os.now() } +$_.clock[prosperon.DOC] = "takes a function input value that will eventually be called with the current time in number form." var underlings = new Set() var overling = undefined @@ -600,6 +599,7 @@ $_.connection = function(callback, actor, config) { state: peer.state }); }; +$_.connection[prosperon.DOC] = "takes a callback function, an actor object, and a configuration record for getting information about the status of a connection to the actor. The configuration record is used to request the sort of information that needs to be communicated." var portal = undefined var pppfn @@ -619,6 +619,7 @@ $_.portal = function(fn, port) pppfn = fn } +$_.portal[prosperon.DOC] = "starts apublic address that performs introduction services. It listens on a specified port for contacts by external actors that need to acquire an actor object. The function will receive the record containing the request. The record can have a reply sent through it. A portal can respond by beginning a new actor, or finding an existing actor, or by forwarding the contact message to another actor. This is how distributed Misty networks are bootstrapped." function portal_fn(e) { @@ -648,11 +649,15 @@ $_.contact = function(callback, record) record }) } +$_.contact[prosperon.DOC] = "The contact function sends a message to a portal on another machine to obtain an actor object. + +The callback is a function with a actor input and a reason input. If successful, actor is bound to an actor object. If not successful, actor is null and reason may contain an explanation." $_.receiver = function(fn) { receive_fn = fn; } +$_.receiver[prosperon.DOC] = "registers a function that will receive all messages sent to the actor except for delay events, reply messages (which are sent to the send callback), the unneeded message, and portal contact messages." $_.start = function(cb, prg, arg) { @@ -673,6 +678,13 @@ $_.start = function(cb, prg, arg) os.createprocess(argv) } +$_.start[prosperon.DOC] = "The start function creates a new actor. The callback function receives messages about the new actor, starting with a message containing the new actor's address object. + +The program text identifies the executable in the program shop that the new actor runs. + +The arguments array contains up to four arguments of type logical, number, text, or actor address object. + +The current actor is the overling of the new actor, and it is notified when the new actor stops. The new actor is an underling of the current actor." $_.stop = function(actor) { @@ -681,6 +693,7 @@ $_.stop = function(actor) actor_send(actor, {type:"stop"}) } +$_.stop[prosperon.DOC] = "The stop function stops an underling." var unneeded_fn = $_.stop var unneeded_time = ar @@ -691,12 +704,14 @@ $_.unneeded = function(fn, seconds = ar) unneeded_fn = fn unneeded_time = seconds } +$_.unneeded[prosperon.DOC] = "registers a function that is called when the actor has not received a message in the recent seconds. The default for seconds is the ar timer. This likely means that the actor is no longer needed. The actor should finish its work and then @.stop()." $_.delay = function(fn, seconds) { var id = os.addtimer(fn, seconds); return function() { os.removetimer(id); } } +$_.delay[prosperon.DOC] = "used to schedule the invocation of a function at a later time. Any value returned from the delayed invocation is ignored" // Set of actor guids var couplings = new Set() @@ -705,14 +720,19 @@ $_.couple = function(actor) { couplings.add(actor.id) } +$_.couple[prosperon.DOC] = "causes this actor to stop when another actor stops." // Shuffles the message to the actor with whatever means available function actor_send(actor, message) { - if (typeof message !== 'object') throw new Error('Must send an object record.') + if (receive_fn && actor.id === prosperon.id) { // handle loopback case + receive_fn(message.data) + return + } + console.log(`Getting peer for ${actor.id}`) var peer = peers[actor.id] if (!peer) throw new Error(`Could not send message to actor.`) @@ -755,6 +775,9 @@ $_.send = function(actor, message, reply) actor_send(actor, send) } +$_.send[prosperon.DOC] = "sends a message to another actor. The left expression must resolve to an actor address object or a message object that is expecting a reply because it was sent with a callback. The right expression is a record containing the outgoing message. The outgoing message record must not contain functions or patterns or cyclic structures. + +If a callback function is included, then the callback function will receive the reply, not the receiver function." var cmd = use('cmd') cmd.process(prosperon.argv) @@ -878,7 +901,7 @@ while (1) { break; case "disconnect": - var id = peer2id(e.peer) + var id = peer2id.get(e.peer) greeters[id]({ type: "actor_stopped" }); diff --git a/tests/contact.js b/tests/contact.js index 027ea286..9bdd6cf9 100644 --- a/tests/contact.js +++ b/tests/contact.js @@ -1,7 +1,10 @@ // Test to connect to a portal $_.contact((actor, reason) => { - + if (actor) + console.log(`Got an actor: ${actor}`) + else + console.log(`Did not get an actor: ${reason}`) }, { address: "localhost", port: 5678,