fix portal connection
Some checks failed
Build and Deploy / build-macos (push) Failing after 4s
Build and Deploy / build-linux (push) Failing after 1m34s
Build and Deploy / build-windows (CLANG64) (push) Has been cancelled
Build and Deploy / package-dist (push) Has been cancelled
Build and Deploy / deploy-itch (push) Has been cancelled
Build and Deploy / deploy-gitea (push) Has been cancelled
Some checks failed
Build and Deploy / build-macos (push) Failing after 4s
Build and Deploy / build-linux (push) Failing after 1m34s
Build and Deploy / build-windows (CLANG64) (push) Has been cancelled
Build and Deploy / package-dist (push) Has been cancelled
Build and Deploy / deploy-itch (push) Has been cancelled
Build and Deploy / deploy-gitea (push) Has been cancelled
This commit is contained in:
@@ -620,35 +620,8 @@ var dying = false
|
||||
|
||||
var HEADER = Symbol()
|
||||
|
||||
var $actor = {
|
||||
toString: print_actor
|
||||
}
|
||||
|
||||
function print_actor() {
|
||||
return json.encode(this.__ACTORDATA__, 1)
|
||||
}
|
||||
|
||||
function create_actor(data = {}) {
|
||||
var newactor = Object.create($actor)
|
||||
|
||||
// Store actual address/port values on the data object
|
||||
data._address = data._address || local_address
|
||||
data._port = data._port || local_port
|
||||
|
||||
Object.defineProperty(data, 'address', {
|
||||
get: function() { return this._address || local_address },
|
||||
set: function(x) { this._address = x },
|
||||
enumerable: true
|
||||
})
|
||||
|
||||
Object.defineProperty(data, 'port', {
|
||||
get: function() { return this._port || local_port },
|
||||
set: function(x) { this._port = x },
|
||||
enumerable: true
|
||||
})
|
||||
|
||||
newactor.__ACTORDATA__ = data
|
||||
return newactor
|
||||
function create_actor(__ACTORDATA__ = {id:util.guid()}) {
|
||||
return { __ACTORDATA__ }
|
||||
}
|
||||
|
||||
var $_ = create_actor()
|
||||
@@ -707,15 +680,13 @@ $_.connection = function(callback, actor, config) {
|
||||
}
|
||||
throw new Error(`Could not get connection information for ${actor}`)
|
||||
}
|
||||
$_.connection[prosperon.DOC] = "takes a callback function, an actor object, and a configuration record..."
|
||||
$_.connection[prosperon.DOC] = "The connection function 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. This can include latency, bandwidth, activity, congestion, cost, partitions. The callback is given a record containing the requested information."
|
||||
|
||||
var peers = {}
|
||||
var id_address = {}
|
||||
var peer_queue = new WeakMap()
|
||||
var portal = undefined
|
||||
var portal_fn = undefined
|
||||
var local_address = undefined
|
||||
var local_port = undefined
|
||||
|
||||
var service_delay = 0.01
|
||||
|
||||
@@ -724,12 +695,9 @@ $_.portal = function(fn, port) {
|
||||
if (!port) throw new Error("Requires a valid port.")
|
||||
console.log(`starting a portal on port ${port}`)
|
||||
portal = enet.create_host({address: "any", port})
|
||||
local_address = 'localhost'
|
||||
local_port = port
|
||||
portal_fn = fn
|
||||
console.log(`I am now ${$_}`)
|
||||
}
|
||||
$_.portal[prosperon.DOC] = "starts a public address that performs introduction services..."
|
||||
$_.portal[prosperon.DOC] = "A portal is a special actor with a public 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. The portal function returns null."
|
||||
|
||||
function handle_host(e) {
|
||||
switch (e.type) {
|
||||
@@ -750,14 +718,10 @@ function handle_host(e) {
|
||||
break
|
||||
case "receive":
|
||||
var data = nota.decode(e.data)
|
||||
if (data.replycc && !data.replycc.__ACTORDATA__.address) {
|
||||
console.log(`got message ${json.encode(data)} over the wire`)
|
||||
if (data.replycc && !data.replycc.address) {
|
||||
data.replycc.__ACTORDATA__.address = e.peer.address
|
||||
data.replycc.__ACTORDATA__.port = e.peer.port
|
||||
// Store in global lookup for future routing
|
||||
id_address[data.replycc.__ACTORDATA__.id] = {
|
||||
address: e.peer.address,
|
||||
port: e.peer.port
|
||||
}
|
||||
}
|
||||
// Also populate address/port for any actor objects in the message data
|
||||
function populate_actor_addresses(obj) {
|
||||
@@ -765,11 +729,6 @@ function handle_host(e) {
|
||||
if (obj.__ACTORDATA__ && !obj.__ACTORDATA__.address) {
|
||||
obj.__ACTORDATA__.address = e.peer.address
|
||||
obj.__ACTORDATA__.port = e.peer.port
|
||||
// Store in global lookup for future routing
|
||||
id_address[obj.__ACTORDATA__.id] = {
|
||||
address: e.peer.address,
|
||||
port: e.peer.port
|
||||
}
|
||||
}
|
||||
for (var key in obj) {
|
||||
if (obj.hasOwnProperty(key)) {
|
||||
@@ -778,6 +737,7 @@ function handle_host(e) {
|
||||
}
|
||||
}
|
||||
if (data.data) populate_actor_addresses(data.data)
|
||||
console.log(`turned it into ${json.encode(data)} over the wire`)
|
||||
handle_message(data)
|
||||
break
|
||||
}
|
||||
@@ -785,15 +745,11 @@ function handle_host(e) {
|
||||
|
||||
var contactor = undefined
|
||||
$_.contact = function(callback, record) {
|
||||
$_.send({
|
||||
__ACTORDATA__: {
|
||||
address: record.address,
|
||||
port: record.port
|
||||
}
|
||||
}, record, callback)
|
||||
$_.send(create_actor(record), record, callback)
|
||||
}
|
||||
|
||||
$_.contact[prosperon.DOC] = "The contact function sends a message to a portal..."
|
||||
$_.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
|
||||
@@ -863,12 +819,6 @@ function actor_send(actor, message) {
|
||||
os.mailbox_push(actor.__ACTORDATA__.id, message)
|
||||
return
|
||||
}
|
||||
|
||||
// Use fallback address lookup if actor doesn't have address info
|
||||
if (!actor.__ACTORDATA__.address && id_address[actor.__ACTORDATA__.id]) {
|
||||
Object.assign(actor.__ACTORDATA__, id_address[actor.__ACTORDATA__.id])
|
||||
}
|
||||
|
||||
if (actor.__ACTORDATA__.address) {
|
||||
if (actor.__ACTORDATA__.id) message.target = actor.__ACTORDATA__.id
|
||||
else message.type = "contact"
|
||||
@@ -946,6 +896,8 @@ cmd.process(prosperon.argv.slice())
|
||||
if (!prosperon.args.id) prosperon.id = util.guid()
|
||||
else prosperon.id = prosperon.args.id
|
||||
|
||||
$_.__ACTORDATA__.id = prosperon.id
|
||||
|
||||
os.register_actor(prosperon.id, function(msg) {
|
||||
try {
|
||||
handle_message(msg)
|
||||
@@ -956,9 +908,8 @@ os.register_actor(prosperon.id, function(msg) {
|
||||
}
|
||||
}, prosperon.args.main)
|
||||
|
||||
$_.__ACTORDATA__.id = prosperon.id
|
||||
if (prosperon.args.overling) overling = create_actor({id:prosperon.args.overling})
|
||||
|
||||
if (prosperon.args.overling) overling = {__ACTORDATA__: {id: prosperon.args.overling}}
|
||||
if (prosperon.args.root) root = json.decode(prosperon.args.root)
|
||||
else root = $_
|
||||
|
||||
@@ -970,19 +921,14 @@ if (!prosperon.args.program)
|
||||
if (typeof prosperon.args.program !== 'string')
|
||||
prosperon.args.program = 'main.js';
|
||||
|
||||
console.log(`running ${prosperon.args.program}`)
|
||||
|
||||
actor.spawn(prosperon.args.program)
|
||||
|
||||
function destroyself() {
|
||||
console.log(`Got the message to destroy self.`)
|
||||
dying = true
|
||||
for (var i of underlings) {
|
||||
var act = {
|
||||
__ACTORDATA__: {id: i}
|
||||
}
|
||||
$_.stop(act);
|
||||
}
|
||||
for (var i of underlings)
|
||||
$_.stop(create_actor({id:i}))
|
||||
|
||||
os.destroy()
|
||||
}
|
||||
|
||||
@@ -1039,9 +985,6 @@ function handle_message(msg) {
|
||||
var greeter = greeters[msg.id]
|
||||
if (greeter) greeter({type: "actor_started", actor: create_actor(msg)})
|
||||
break;
|
||||
case "ping":
|
||||
// Keep-alive ping, no action needed
|
||||
break;
|
||||
default:
|
||||
if (receive_fn) receive_fn(msg)
|
||||
break;
|
||||
@@ -1052,19 +995,8 @@ function enet_check()
|
||||
{
|
||||
if (portal) portal.service(handle_host)
|
||||
if (contactor) contactor.service(handle_host)
|
||||
send_messages();
|
||||
|
||||
// Send keep-alive ping to all active peers to prevent timeout
|
||||
for (var peerKey in peers) {
|
||||
var peer = peers[peerKey]
|
||||
if (peer && peer.state === 1) { // ENET_PEER_STATE_CONNECTED
|
||||
try {
|
||||
peer.send(nota.encode({type: 'ping'}))
|
||||
} catch (e) {
|
||||
console.debug(`Failed to send ping to ${peerKey}:`, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
send_messages();
|
||||
|
||||
$_.delay(enet_check, service_delay);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user